2 * $Id: zoom-c.c,v 1.14 2001-11-30 08:24:06 adam Exp $
4 * ZOOM layer for C, connections, result sets, queries.
7 #include <yaz/xmalloc.h>
8 #include <yaz/otherinfo.h>
10 #include <yaz/pquery.h>
11 #include <yaz/diagbib1.h>
19 static ZOOM_Event ZOOM_Event_create (int kind)
21 ZOOM_Event event = xmalloc (sizeof(*event));
28 static void ZOOM_Event_destroy (ZOOM_Event event)
33 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
35 // put in back of queue
38 c->m_queue_back->prev = event;
39 assert (c->m_queue_front);
43 assert (!c->m_queue_front);
44 c->m_queue_front = event;
46 event->next = c->m_queue_back;
48 c->m_queue_back = event;
51 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
53 // get from front of queue
54 ZOOM_Event event = c->m_queue_front;
57 assert (c->m_queue_back);
58 c->m_queue_front = event->prev;
61 assert (c->m_queue_back);
62 c->m_queue_front->next = 0;
69 static void clear_error (ZOOM_connection c)
71 c->error = ZOOM_ERROR_NONE;
76 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
78 ZOOM_task *taskp = &c->tasks;
80 taskp = &(*taskp)->next;
81 *taskp = xmalloc (sizeof(**taskp));
82 (*taskp)->running = 0;
83 (*taskp)->which = which;
89 void ZOOM_connection_remove_task (ZOOM_connection c)
91 ZOOM_task task = c->tasks;
95 c->tasks = task->next;
98 case ZOOM_TASK_SEARCH:
99 ZOOM_resultset_destroy (task->u.search.resultset);
101 case ZOOM_TASK_RETRIEVE:
102 ZOOM_resultset_destroy (task->u.retrieve.resultset);
104 case ZOOM_TASK_CONNECT:
113 void ZOOM_connection_remove_tasks (ZOOM_connection c)
116 ZOOM_connection_remove_task(c);
119 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
121 const char *elementSetName);
123 ZOOM_connection ZOOM_connection_create (ZOOM_options options)
125 ZOOM_connection c = xmalloc (sizeof(*c));
129 c->state = STATE_IDLE;
130 c->error = ZOOM_ERROR_NONE;
138 c->options = ZOOM_options_create_with_parent(options);
147 c->odr_in = odr_createmem (ODR_DECODE);
148 c->odr_out = odr_createmem (ODR_ENCODE);
151 c->support_named_resultsets = 0;
153 c->m_queue_front = 0;
158 /* set database names. Take local databases (if set); otherwise
159 take databases given in ZURL (if set); otherwise use Default */
160 static char **set_DatabaseNames (ZOOM_connection con, int *num)
162 char **databaseNames;
165 const char *cp = ZOOM_options_get (con->options, "databaseName");
169 cp = strchr (con->host_port, '/');
176 while ((c = strchr(c, '+')))
184 databaseNames = odr_malloc (con->odr_out, no * sizeof(*databaseNames));
188 c = strchr (cp, '+');
196 /* cp ptr to first char of db name, c is char
198 databaseNames[no] = odr_malloc (con->odr_out, 1+c-cp);
199 memcpy (databaseNames[no], cp, c-cp);
200 databaseNames[no++][c-cp] = '\0';
205 databaseNames[no] = NULL;
207 return databaseNames;
210 ZOOM_connection ZOOM_connection_new (const char *host, int portnum)
212 ZOOM_connection c = ZOOM_connection_create (0);
214 ZOOM_connection_connect (c, host, portnum);
218 void ZOOM_connection_connect(ZOOM_connection c,
219 const char *host, int portnum)
224 val = ZOOM_options_get (c->options, "proxy");
226 c->proxy = xstrdup (val);
233 sprintf (hostn, "%.80s:%d", host, portnum);
234 c->host_port = xstrdup(hostn);
237 c->host_port = xstrdup(host);
239 ZOOM_options_set(c->options, "host", c->host_port);
241 c->async = ZOOM_options_get_bool (c->options, "async", 0);
243 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
247 while (ZOOM_event (1, &c))
252 ZOOM_query ZOOM_query_create(void)
254 ZOOM_query s = xmalloc (sizeof(*s));
259 s->odr = odr_createmem (ODR_ENCODE);
264 void ZOOM_query_destroy(ZOOM_query s)
270 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
271 if (s->refcount == 0)
273 odr_destroy (s->odr);
278 int ZOOM_query_prefix(ZOOM_query s, const char *str)
280 s->query = odr_malloc (s->odr, sizeof(*s->query));
281 s->query->which = Z_Query_type_1;
282 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
283 if (!s->query->u.type_1)
288 int ZOOM_query_sortby(ZOOM_query s, const char *criteria)
290 s->sort_spec = yaz_sort_spec (s->odr, criteria);
296 static int do_write(ZOOM_connection c);
298 void ZOOM_connection_destroy(ZOOM_connection c)
305 for (r = c->resultsets; r; r = r->next)
310 odr_destroy (c->odr_in);
311 odr_destroy (c->odr_out);
312 ZOOM_options_destroy (c->options);
313 ZOOM_connection_remove_tasks (c);
314 xfree (c->host_port);
318 void ZOOM_resultset_addref (ZOOM_resultset r)
323 ZOOM_resultset ZOOM_resultset_create ()
325 ZOOM_resultset r = xmalloc (sizeof(*r));
329 r->odr = odr_createmem (ODR_ENCODE);
343 ZOOM_resultset ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
346 ZOOM_query s = ZOOM_query_create();
348 ZOOM_query_prefix (s, q);
350 r = ZOOM_connection_search (c, s);
351 ZOOM_query_destroy (s);
355 ZOOM_resultset ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
357 ZOOM_resultset r = ZOOM_resultset_create ();
361 r->r_sort_spec = q->sort_spec;
362 r->r_query = q->query;
365 r->options = ZOOM_options_create_with_parent(c->options);
367 r->start = ZOOM_options_get_int(r->options, "start", 0);
368 r->count = ZOOM_options_get_int(r->options, "count", 0);
369 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
370 cp = ZOOM_options_get (r->options, "setname");
372 r->setname = xstrdup (cp);
376 r->next = c->resultsets;
379 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
380 task->u.search.resultset = r;
381 ZOOM_resultset_addref (r);
387 while (ZOOM_event (1, &c))
393 void ZOOM_resultset_destroy(ZOOM_resultset r)
398 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
399 if (r->refcount == 0)
403 /* remove ourselves from the resultsets in connection */
404 ZOOM_resultset *rp = &r->connection->resultsets;
407 assert (*rp); /* we must be in this list!! */
409 { /* OK, we're here - take us out of it */
416 ZOOM_query_destroy (r->search);
417 ZOOM_options_destroy (r->options);
418 odr_destroy (r->odr);
424 size_t ZOOM_resultset_size (ZOOM_resultset r)
429 static void do_close (ZOOM_connection c)
435 c->state = STATE_IDLE;
438 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
439 int force_sync, int start, int count)
450 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
451 task->u.retrieve.resultset = r;
452 task->u.retrieve.start = start;
453 task->u.retrieve.count = count;
455 ZOOM_resultset_addref (r);
457 if (!r->connection->async || force_sync)
458 while (r->connection && ZOOM_event (1, &r->connection))
462 void ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
463 size_t start, size_t count)
465 int force_present = 0;
471 ZOOM_resultset_retrieve (r, force_present, start, count);
475 for (i = 0; i< count; i++)
476 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
480 static int do_connect (ZOOM_connection c)
483 const char *effective_host;
486 effective_host = c->proxy;
488 effective_host = c->host_port;
490 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
493 c->cs = cs_create_host (effective_host, 0, &add);
497 int ret = cs_connect (c->cs, add);
498 yaz_log (LOG_DEBUG, "cs_connect returned %d", ret);
501 c->state = STATE_CONNECTING;
502 c->mask = ZOOM_SELECT_READ | ZOOM_SELECT_WRITE |
507 c->state = STATE_IDLE;
508 c->error = ZOOM_ERROR_CONNECT;
512 int z3950_connection_socket(ZOOM_connection c)
515 return cs_fileno(c->cs);
519 int z3950_connection_mask(ZOOM_connection c)
526 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
531 sprintf (str, "send_APDU t=%p type=%d", c, a->which);
534 Z_OtherInformation **oi;
536 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
538 if (!z_APDU(out, &a, 0, 0))
540 FILE *outf = fopen("/tmp/apdu.txt", "w");
543 ODR odr_pr = odr_createmem(ODR_PRINT);
544 fprintf (outf, "a=%p\n", a);
545 odr_setprint(odr_pr, outf);
546 z_APDU(odr_pr, &a, 0, 0);
550 c->error = ZOOM_ERROR_ENCODE;
557 static int send_APDU (ZOOM_connection c, Z_APDU *a)
560 if (encode_APDU(c, a, c->odr_out))
562 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
563 odr_reset(c->odr_out);
568 static int ZOOM_connection_send_init (ZOOM_connection c)
571 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
572 Z_InitRequest *ireq = apdu->u.initRequest;
573 Z_IdAuthentication *auth = odr_malloc(c->odr_out, sizeof(*auth));
574 const char *auth_groupId = ZOOM_options_get (c->options, "group");
575 const char *auth_userId = ZOOM_options_get (c->options, "user");
576 const char *auth_password = ZOOM_options_get (c->options, "pass");
578 ODR_MASK_SET(ireq->options, Z_Options_search);
579 ODR_MASK_SET(ireq->options, Z_Options_present);
580 ODR_MASK_SET(ireq->options, Z_Options_scan);
581 ODR_MASK_SET(ireq->options, Z_Options_sort);
582 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
583 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
585 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
586 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
587 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
589 impname = ZOOM_options_get (c->options, "implementationName");
590 ireq->implementationName =
591 odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
592 strcpy (ireq->implementationName, "");
595 strcat (ireq->implementationName, impname);
596 strcat (ireq->implementationName, "/");
598 strcat (ireq->implementationName, "ZOOM-C/YAZ");
600 *ireq->maximumRecordSize =
601 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
602 *ireq->preferredMessageSize =
603 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
605 if (auth_groupId || auth_password)
607 Z_IdPass *pass = odr_malloc(c->odr_out, sizeof(*pass));
610 if (auth_groupId && *auth_groupId)
612 pass->groupId = odr_malloc(c->odr_out, strlen(auth_groupId)+1);
613 strcpy(pass->groupId, auth_groupId);
617 if (auth_userId && *auth_userId)
619 pass->userId = odr_malloc(c->odr_out, strlen(auth_userId)+1);
620 strcpy(pass->userId, auth_userId);
624 if (auth_password && *auth_password)
626 pass->password = odr_malloc(c->odr_out, strlen(auth_password)+1);
627 strcpy(pass->password, auth_password);
632 auth->which = Z_IdAuthentication_idPass;
633 auth->u.idPass = pass;
634 ireq->idAuthentication = auth;
637 else if (auth_userId)
639 auth->which = Z_IdAuthentication_open;
640 auth->u.open = odr_malloc(c->odr_out, strlen(auth_userId)+1);
641 strcpy(auth->u.open, auth_userId);
642 ireq->idAuthentication = auth;
645 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
646 VAL_PROXY, 1, c->host_port);
653 static int ZOOM_connection_send_search (ZOOM_connection c)
656 int lslb, ssub, mspn;
658 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
659 Z_SearchRequest *search_req = apdu->u.searchRequest;
660 const char *elementSetName;
661 const char *smallSetElementSetName;
662 const char *mediumSetElementSetName;
666 assert (c->tasks->which == ZOOM_TASK_SEARCH);
668 r = c->tasks->u.search.resultset;
671 ZOOM_options_get (r->options, "elementSetName");
672 smallSetElementSetName =
673 ZOOM_options_get (r->options, "smallSetElementSetName");
674 mediumSetElementSetName =
675 ZOOM_options_get (r->options, "mediumSetElementSetName");
677 ZOOM_options_get (r->options, "schema");
679 if (!smallSetElementSetName)
680 smallSetElementSetName = elementSetName;
682 if (!mediumSetElementSetName)
683 mediumSetElementSetName = elementSetName;
688 /* prepare query for the search request */
689 search_req->query = r->r_query;
691 search_req->databaseNames =
692 set_DatabaseNames (c, &search_req->num_databaseNames);
694 /* get syntax (no need to provide unless piggyback is in effect) */
695 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
697 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
698 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
699 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
700 if (lslb != -1 && ssub != -1 && mspn != -1)
702 /* So're a Z39.50 expert? Let's hope you don't do sort */
703 *search_req->largeSetLowerBound = lslb;
704 *search_req->smallSetUpperBound = ssub;
705 *search_req->mediumSetPresentNumber = mspn;
707 else if (r->start == 0 && r->count > 0
708 && r->piggyback && !r->r_sort_spec && !schema)
710 /* Regular piggyback - do it unless we're going to do sort */
711 *search_req->largeSetLowerBound = 2000000000;
712 *search_req->smallSetUpperBound = r->count;
713 *search_req->mediumSetPresentNumber = r->count;
714 smallSetElementSetName = 0; /* no need to provide this */
718 /* non-piggyback. Need not provide elementsets or syntaxes .. */
719 smallSetElementSetName = 0;
720 mediumSetElementSetName = 0;
723 if (smallSetElementSetName && *smallSetElementSetName)
725 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
727 esn->which = Z_ElementSetNames_generic;
728 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
729 search_req->smallSetElementSetNames = esn;
731 if (mediumSetElementSetName && *mediumSetElementSetName)
733 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
735 esn->which = Z_ElementSetNames_generic;
736 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
737 search_req->mediumSetElementSetNames = esn;
740 search_req->preferredRecordSyntax =
741 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
745 if (c->support_named_resultsets)
749 /* find the lowest unused ordinal so that we re-use
750 result sets on the server. */
751 for (ord = 1; ; ord++)
754 sprintf (setname, "%d", ord);
755 for (rp = c->resultsets; rp; rp = rp->next)
756 if (rp->setname && !strcmp (rp->setname, setname))
761 r->setname = xstrdup (setname);
762 yaz_log (LOG_DEBUG, "allocating %s", r->setname);
765 r->setname = xstrdup ("default");
766 ZOOM_options_set (r->options, "setname", r->setname);
768 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
769 /* send search request */
775 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
777 Z_DefaultDiagFormat *r;
782 if (p->which != Z_DiagRec_defaultFormat)
784 c->error = ZOOM_ERROR_DECODE;
787 r = p->u.defaultFormat;
790 case Z_DefaultDiagFormat_v2Addinfo:
791 addinfo = r->u.v2Addinfo;
793 case Z_DefaultDiagFormat_v3Addinfo:
794 addinfo = r->u.v3Addinfo;
798 c->addinfo = xstrdup (addinfo);
799 c->error = *r->condition;
802 ZOOM_record ZOOM_record_clone (ZOOM_record srec)
809 odr_enc = odr_createmem(ODR_ENCODE);
810 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
812 buf = odr_getbuf (odr_enc, &size, 0);
814 nrec = xmalloc (sizeof(*nrec));
815 nrec->odr = odr_createmem(ODR_DECODE);
816 nrec->wrbuf_marc = 0;
817 odr_setbuf (nrec->odr, buf, size, 0);
818 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
820 odr_destroy (odr_enc);
824 ZOOM_record ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
826 return record_cache_lookup (s, pos, 0);
829 ZOOM_record ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
831 ZOOM_resultset_retrieve (r, 1, pos, 1);
832 return ZOOM_resultset_record_immediate (r, pos);
835 void ZOOM_record_destroy (ZOOM_record rec)
840 wrbuf_free (rec->wrbuf_marc, 1);
841 odr_destroy (rec->odr);
845 void *ZOOM_record_get (ZOOM_record rec, const char *type, size_t *len)
847 Z_NamePlusRecord *npr;
853 if (!strcmp (type, "database"))
855 return npr->databaseName;
857 else if (!strcmp (type, "syntax"))
859 if (npr->which == Z_NamePlusRecord_databaseRecord)
861 Z_External *r = (Z_External *) npr->u.databaseRecord;
862 oident *ent = oid_getentbyoid(r->direct_reference);
868 else if (!strcmp (type, "render"))
870 if (npr->which == Z_NamePlusRecord_databaseRecord)
872 Z_External *r = (Z_External *) npr->u.databaseRecord;
873 oident *ent = oid_getentbyoid(r->direct_reference);
875 if (r->which == Z_External_sutrs)
877 *len = r->u.sutrs->len;
878 return r->u.sutrs->buf;
880 else if (r->which == Z_External_octet)
889 case VAL_APPLICATION_XML:
892 if (!rec->wrbuf_marc)
893 rec->wrbuf_marc = wrbuf_alloc();
894 if (marc_display_wrbuf (r->u.octet_aligned->buf,
896 r->u.octet_aligned->len) > 0)
898 *len = wrbuf_len(rec->wrbuf_marc);
899 return wrbuf_buf(rec->wrbuf_marc);
902 *len = r->u.octet_aligned->len;
903 return r->u.octet_aligned->buf;
905 else if (r->which == Z_External_grs1)
913 else if (!strcmp (type, "raw"))
915 if (npr->which == Z_NamePlusRecord_databaseRecord)
918 return (Z_External *) npr->u.databaseRecord;
925 static void record_cache_add (ZOOM_resultset r,
926 Z_NamePlusRecord *npr,
928 const char *elementSetName)
930 ZOOM_record_cache rc;
932 for (rc = r->record_cache; rc; rc = rc->next)
936 if ((!elementSetName && !rc->elementSetName)
937 || (elementSetName && rc->elementSetName &&
938 !strcmp (elementSetName, rc->elementSetName)))
940 /* not destroying rc->npr (it's handled by nmem )*/
942 /* keeping wrbuf_marc too */
948 rc = odr_malloc (r->odr, sizeof(*rc));
951 rc->rec.wrbuf_marc = 0;
953 rc->elementSetName = odr_strdup (r->odr, elementSetName);
955 rc->elementSetName = 0;
957 rc->next = r->record_cache;
958 r->record_cache = rc;
961 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
963 const char *elementSetName)
965 ZOOM_record_cache rc;
967 for (rc = r->record_cache; rc; rc = rc->next)
971 if ((!elementSetName && !rc->elementSetName)
972 || (elementSetName && rc->elementSetName &&
973 !strcmp (elementSetName, rc->elementSetName)))
980 static void handle_records (ZOOM_connection c, Z_Records *sr,
983 ZOOM_resultset resultset;
987 switch (c->tasks->which)
989 case ZOOM_TASK_SEARCH:
990 resultset = c->tasks->u.search.resultset;
992 case ZOOM_TASK_RETRIEVE:
993 resultset = c->tasks->u.retrieve.resultset;
998 if (sr && sr->which == Z_Records_NSD)
1000 Z_DiagRec dr, *dr_p = &dr;
1001 dr.which = Z_DiagRec_defaultFormat;
1002 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1004 response_diag (c, dr_p);
1006 else if (sr && sr->which == Z_Records_multipleNSD)
1008 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1009 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1011 c->error = ZOOM_ERROR_DECODE;
1015 if (resultset->count + resultset->start > resultset->size)
1016 resultset->count = resultset->size - resultset->start;
1017 if (resultset->count < 0)
1018 resultset->count = 0;
1019 if (sr && sr->which == Z_Records_DBOSD)
1022 NMEM nmem = odr_extract_mem (c->odr_in);
1023 Z_NamePlusRecordList *p =
1024 sr->u.databaseOrSurDiagnostics;
1025 for (i = 0; i<p->num_records; i++)
1027 record_cache_add (resultset, p->records[i],
1028 i+ resultset->start, 0);
1030 /* transfer our response to search_nmem .. we need it later */
1031 nmem_transfer (resultset->odr->mem, nmem);
1032 nmem_destroy (nmem);
1033 if (present_phase && p->num_records == 0)
1035 /* present response and we didn't get any records! */
1036 c->error = ZOOM_ERROR_DECODE;
1039 else if (present_phase)
1041 /* present response and we didn't get any records! */
1042 c->error = ZOOM_ERROR_DECODE;
1047 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1049 handle_records (c, pr->records, 1);
1052 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1054 ZOOM_resultset resultset;
1056 yaz_log (LOG_DEBUG, "got search response");
1058 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1061 resultset = c->tasks->u.search.resultset;
1063 resultset->size = *sr->resultCount;
1064 handle_records (c, sr->records, 0);
1067 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1069 if (res->diagnostics && res->num_diagnostics > 0)
1070 response_diag (c, res->diagnostics[0]);
1073 static int send_sort (ZOOM_connection c)
1075 ZOOM_resultset resultset;
1077 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1080 resultset = c->tasks->u.search.resultset;
1084 resultset->r_sort_spec = 0;
1087 if (resultset->r_sort_spec)
1089 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1090 Z_SortRequest *req = apdu->u.sortRequest;
1092 req->num_inputResultSetNames = 1;
1093 req->inputResultSetNames = (Z_InternationalString **)
1094 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1095 req->inputResultSetNames[0] =
1096 odr_strdup (c->odr_out, resultset->setname);
1097 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1098 req->sortSequence = resultset->r_sort_spec;
1099 resultset->r_sort_spec = 0;
1100 send_APDU (c, apdu);
1106 static int send_present (ZOOM_connection c)
1108 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1109 Z_PresentRequest *req = apdu->u.presentRequest;
1111 const char *syntax =
1112 ZOOM_options_get (c->options, "preferredRecordSyntax");
1113 const char *element =
1114 ZOOM_options_get (c->options, "elementSetName");
1115 const char *schema =
1116 ZOOM_options_get (c->options, "schema");
1117 ZOOM_resultset resultset;
1122 switch (c->tasks->which)
1124 case ZOOM_TASK_SEARCH:
1125 resultset = c->tasks->u.search.resultset;
1127 case ZOOM_TASK_RETRIEVE:
1128 resultset = c->tasks->u.retrieve.resultset;
1129 resultset->start = c->tasks->u.retrieve.start;
1130 resultset->count = c->tasks->u.retrieve.count;
1132 if (resultset->start >= resultset->size)
1134 if (resultset->start + resultset->count > resultset->size)
1135 resultset->count = resultset->size - resultset->start;
1141 if (c->error) /* don't continue on error */
1143 if (resultset->start < 0)
1145 for (i = 0; i<resultset->count; i++)
1148 record_cache_lookup (resultset, i + resultset->start, 0);
1152 if (i == resultset->count)
1155 resultset->start += i;
1156 resultset->count -= i;
1157 *req->resultSetStartPoint = resultset->start + 1;
1158 *req->numberOfRecordsRequested = resultset->count;
1159 assert (*req->numberOfRecordsRequested > 0);
1161 if (syntax && *syntax)
1162 req->preferredRecordSyntax =
1163 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1165 if (schema && *schema)
1167 Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1169 req->recordComposition = compo;
1170 compo->which = Z_RecordComp_complex;
1171 compo->u.complex = (Z_CompSpec *)
1172 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1173 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1174 odr_malloc(c->odr_out, sizeof(bool_t));
1175 *compo->u.complex->selectAlternativeSyntax = 0;
1177 compo->u.complex->generic = (Z_Specification *)
1178 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1180 compo->u.complex->generic->schema = (Odr_oid *)
1181 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1183 if (!compo->u.complex->generic->schema)
1185 /* OID wasn't a schema! Try record syntax instead. */
1187 compo->u.complex->generic->schema = (Odr_oid *)
1188 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1190 if (element && *element)
1192 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1193 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1194 compo->u.complex->generic->elementSpec->which =
1195 Z_ElementSpec_elementSetName;
1196 compo->u.complex->generic->elementSpec->u.elementSetName =
1197 odr_strdup (c->odr_out, element);
1200 compo->u.complex->generic->elementSpec = 0;
1201 compo->u.complex->num_dbSpecific = 0;
1202 compo->u.complex->dbSpecific = 0;
1203 compo->u.complex->num_recordSyntax = 0;
1204 compo->u.complex->recordSyntax = 0;
1206 else if (element && *element)
1208 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
1209 Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1211 esn->which = Z_ElementSetNames_generic;
1212 esn->u.generic = odr_strdup (c->odr_out, element);
1213 compo->which = Z_RecordComp_simple;
1214 compo->u.simple = esn;
1215 req->recordComposition = compo;
1217 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1218 send_APDU (c, apdu);
1222 static int ZOOM_connection_exec_task (ZOOM_connection c)
1224 ZOOM_task task = c->tasks;
1226 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1229 if (c->error != ZOOM_ERROR_NONE ||
1230 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1232 ZOOM_connection_remove_tasks (c);
1235 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d", task->which);
1239 switch (task->which)
1241 case ZOOM_TASK_SEARCH:
1242 /* see if search hasn't been sent yet. */
1243 if (ZOOM_connection_send_search (c))
1246 case ZOOM_TASK_RETRIEVE:
1247 if (send_present (c))
1250 case ZOOM_TASK_CONNECT:
1254 ZOOM_connection_remove_task (c);
1258 static int send_sort_present (ZOOM_connection c)
1260 int r = send_sort (c);
1262 r = send_present (c);
1266 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1268 Z_InitResponse *initrs;
1270 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1274 case Z_APDU_initResponse:
1275 initrs = apdu->u.initResponse;
1276 if (!*initrs->result)
1278 c->error = ZOOM_ERROR_INIT;
1283 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1285 xfree (c->cookie_in);
1288 c->cookie_in = xstrdup(cookie);
1289 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets))
1290 c->support_named_resultsets = 1;
1293 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1294 ZOOM_connection_remove_task (c);
1296 ZOOM_connection_exec_task (c);
1299 case Z_APDU_searchResponse:
1300 handle_search_response (c, apdu->u.searchResponse);
1301 if (!send_sort_present (c))
1302 ZOOM_connection_remove_task (c);
1304 case Z_APDU_presentResponse:
1305 handle_present_response (c, apdu->u.presentResponse);
1306 if (!send_present (c))
1307 ZOOM_connection_remove_task (c);
1309 case Z_APDU_sortResponse:
1310 sort_response (c, apdu->u.sortResponse);
1311 if (!send_present (c))
1312 ZOOM_connection_remove_task (c);
1316 static int do_read (ZOOM_connection c)
1321 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1326 c->error= ZOOM_ERROR_CONNECTION_LOST;
1331 odr_reset (c->odr_in);
1332 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1333 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1335 c->error = ZOOM_ERROR_DECODE;
1340 handle_apdu (c, apdu);
1346 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
1350 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1352 if (c->state == STATE_CONNECTING)
1353 c->error = ZOOM_ERROR_CONNECT;
1355 c->error = ZOOM_ERROR_CONNECTION_LOST;
1361 c->state = STATE_ESTABLISHED;
1362 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_WRITE|ZOOM_SELECT_EXCEPT;
1366 c->state = STATE_ESTABLISHED;
1367 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
1372 static int do_write(ZOOM_connection c)
1374 return do_write_ex (c, c->buf_out, c->len_out);
1378 const char *ZOOM_connection_option_get (ZOOM_connection c, const char *key)
1380 return ZOOM_options_get (c->options, key);
1383 void ZOOM_connection_option_set (ZOOM_connection c, const char *key,
1386 ZOOM_options_set (c->options, key, val);
1389 const char *ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
1391 return ZOOM_options_get (r->options, key);
1394 void ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
1397 ZOOM_options_set (r->options, key, val);
1401 int ZOOM_connection_errcode (ZOOM_connection c)
1403 return ZOOM_connection_error (c, 0, 0);
1406 const char *ZOOM_connection_errmsg (ZOOM_connection c)
1409 ZOOM_connection_error (c, &msg, 0);
1413 const char *ZOOM_connection_addinfo (ZOOM_connection c)
1415 const char *addinfo;
1416 ZOOM_connection_error (c, 0, &addinfo);
1420 int ZOOM_connection_error (ZOOM_connection c, const char **cp,
1421 const char **addinfo)
1423 int error = c->error;
1428 case ZOOM_ERROR_NONE:
1429 *cp = "No error"; break;
1430 case ZOOM_ERROR_CONNECT:
1431 *cp = "Connect failed"; break;
1432 case ZOOM_ERROR_MEMORY:
1433 *cp = "Out of memory"; break;
1434 case ZOOM_ERROR_ENCODE:
1435 *cp = "Encoding failed"; break;
1436 case ZOOM_ERROR_DECODE:
1437 *cp = "Decoding failed"; break;
1438 case ZOOM_ERROR_CONNECTION_LOST:
1439 *cp = "Connection lost"; break;
1440 case ZOOM_ERROR_INIT:
1441 *cp = "Init rejected"; break;
1442 case ZOOM_ERROR_INTERNAL:
1443 *cp = "Internal failure"; break;
1444 case ZOOM_ERROR_TIMEOUT:
1445 *cp = "Timeout"; break;
1447 *cp = diagbib1_str (error);
1453 *addinfo = c->addinfo;
1460 int ZOOM_connection_do_io(ZOOM_connection c, int mask)
1464 int r = cs_look(c->cs);
1465 yaz_log (LOG_LOG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
1470 c->error = ZOOM_ERROR_CONNECT;
1473 else if (r == CS_CONNECT)
1475 yaz_log (LOG_LOG, "calling rcvconnect");
1476 if (cs_rcvconnect (c->cs) < 0)
1478 c->error = ZOOM_ERROR_CONNECT;
1482 ZOOM_connection_send_init (c);
1486 if (mask & ZOOM_SELECT_READ)
1488 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1492 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d", c, mask);
1493 if (c->state == STATE_CONNECTING)
1495 if (mask & ZOOM_SELECT_WRITE)
1496 ZOOM_connection_send_init (c);
1499 c->error = ZOOM_ERROR_CONNECT;
1503 else if (c->state == STATE_ESTABLISHED)
1505 if (mask & ZOOM_SELECT_READ)
1507 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1512 c->error = ZOOM_ERROR_INTERNAL;
1516 event = ZOOM_Event_create (1);
1517 ZOOM_connection_put_event (c, event);
1522 int ZOOM_event (int no, ZOOM_connection *cs)
1525 struct pollfd pollfds[1024];
1526 ZOOM_connection poll_cs[1024];
1529 fd_set input, output, except;
1534 for (i = 0; i<no; i++)
1536 ZOOM_connection c = cs[i];
1538 if (c && (event = ZOOM_connection_get_event(c)))
1540 ZOOM_Event_destroy (event);
1544 for (i = 0; i<no; i++)
1546 ZOOM_connection c = cs[i];
1547 if (c && ZOOM_connection_exec_task (c))
1561 for (i = 0; i<no; i++)
1563 ZOOM_connection c = cs[i];
1568 fd = z3950_connection_socket(c);
1569 mask = z3950_connection_mask(c);
1579 short poll_events = 0;
1581 if (mask & ZOOM_SELECT_READ)
1582 poll_events += POLLIN;
1583 if (mask & ZOOM_SELECT_WRITE)
1584 poll_events += POLLOUT;
1585 if (mask & ZOOM_SELECT_EXCEPT)
1586 poll_events += POLLERR;
1587 pollfds[nfds].fd = fd;
1588 pollfds[nfds].events = poll_events;
1589 pollfds[nfds].revents = 0;
1594 if (mask & ZOOM_SELECT_READ)
1596 FD_SET (fd, &input);
1599 if (mask & ZOOM_SELECT_WRITE)
1601 FD_SET (fd, &output);
1604 if (mask & ZOOM_SELECT_EXCEPT)
1606 FD_SET (fd, &except);
1614 yaz_log (LOG_DEBUG, "poll start");
1615 r = poll (pollfds, nfds, 15000);
1616 yaz_log (LOG_DEBUG, "poll stop, returned r=%d", r);
1617 for (i = 0; i<nfds; i++)
1619 ZOOM_connection c = poll_cs[i];
1623 if (pollfds[i].revents & POLLIN)
1624 mask += ZOOM_SELECT_READ;
1625 if (pollfds[i].revents & POLLOUT)
1626 mask += ZOOM_SELECT_WRITE;
1627 if (pollfds[i].revents & POLLERR)
1628 mask += ZOOM_SELECT_EXCEPT;
1630 ZOOM_connection_do_io(c, mask);
1632 else if (r == 0 && c->mask)
1634 ZOOM_Event event = ZOOM_Event_create(0);
1635 /* timeout and this connection was waiting */
1636 c->error = ZOOM_ERROR_TIMEOUT;
1638 ZOOM_connection_put_event(c, event);
1642 yaz_log (LOG_DEBUG, "select start");
1643 r = select (max_fd+1, &input, &output, &except, &tv);
1644 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
1645 for (i = 0; i<no; i++)
1647 ZOOM_connection c = cs[i];
1652 fd = z3950_connection_socket(c);
1656 /* no timeout and real socket */
1657 if (FD_ISSET(fd, &input))
1658 mask += ZOOM_SELECT_READ;
1659 if (FD_ISSET(fd, &output))
1660 mask += ZOOM_SELECT_WRITE;
1661 if (FD_ISSET(fd, &except))
1662 mask += ZOOM_SELECT_EXCEPT;
1664 ZOOM_connection_do_io(c, mask);
1666 if (r == 0 && c->mask)
1668 ZOOM_Event event = ZOOM_Event_create(0);
1669 /* timeout and this connection was waiting */
1670 c->error = ZOOM_ERROR_TIMEOUT;
1672 yaz_log (LOG_DEBUG, "timeout");
1673 ZOOM_connection_put_event(c, event);
1677 for (i = 0; i<no; i++)
1679 ZOOM_connection c = cs[i];
1681 if (c && (event = ZOOM_connection_get_event(c)))
1683 ZOOM_Event_destroy (event);