2 * $Id: zoom-c.c,v 1.19 2002-01-09 11:03:41 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;
66 c->last_event = event->kind;
70 static void clear_error (ZOOM_connection c)
72 c->error = ZOOM_ERROR_NONE;
77 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
79 ZOOM_task *taskp = &c->tasks;
81 taskp = &(*taskp)->next;
82 *taskp = xmalloc (sizeof(**taskp));
83 (*taskp)->running = 0;
84 (*taskp)->which = which;
90 void ZOOM_connection_remove_task (ZOOM_connection c)
92 ZOOM_task task = c->tasks;
96 c->tasks = task->next;
99 case ZOOM_TASK_SEARCH:
100 ZOOM_resultset_destroy (task->u.search.resultset);
102 case ZOOM_TASK_RETRIEVE:
103 ZOOM_resultset_destroy (task->u.retrieve.resultset);
105 case ZOOM_TASK_CONNECT:
108 ZOOM_scanset_destroy (task->u.scan.scan);
117 void ZOOM_connection_remove_tasks (ZOOM_connection c)
120 ZOOM_connection_remove_task(c);
123 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
125 const char *elementSetName);
127 ZOOM_connection ZOOM_connection_create (ZOOM_options options)
129 ZOOM_connection c = xmalloc (sizeof(*c));
133 c->state = STATE_IDLE;
134 c->error = ZOOM_ERROR_NONE;
142 c->options = ZOOM_options_create_with_parent(options);
151 c->odr_in = odr_createmem (ODR_DECODE);
152 c->odr_out = odr_createmem (ODR_ENCODE);
155 c->support_named_resultsets = 0;
156 c->last_event = ZOOM_EVENT_NONE;
158 c->m_queue_front = 0;
163 /* set database names. Take local databases (if set); otherwise
164 take databases given in ZURL (if set); otherwise use Default */
165 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
168 char **databaseNames;
171 const char *cp = ZOOM_options_get (options, "databaseName");
175 cp = strchr (con->host_port, '/');
182 while ((c = strchr(c, '+')))
190 databaseNames = odr_malloc (con->odr_out, no * sizeof(*databaseNames));
194 c = strchr (cp, '+');
202 /* cp ptr to first char of db name, c is char
204 databaseNames[no] = odr_malloc (con->odr_out, 1+c-cp);
205 memcpy (databaseNames[no], cp, c-cp);
206 databaseNames[no++][c-cp] = '\0';
211 databaseNames[no] = NULL;
213 return databaseNames;
216 ZOOM_connection ZOOM_connection_new (const char *host, int portnum)
218 ZOOM_connection c = ZOOM_connection_create (0);
220 ZOOM_connection_connect (c, host, portnum);
224 void ZOOM_connection_connect(ZOOM_connection c,
225 const char *host, int portnum)
230 val = ZOOM_options_get (c->options, "proxy");
232 c->proxy = xstrdup (val);
239 sprintf (hostn, "%.80s:%d", host, portnum);
240 c->host_port = xstrdup(hostn);
243 c->host_port = xstrdup(host);
245 ZOOM_options_set(c->options, "host", c->host_port);
247 c->async = ZOOM_options_get_bool (c->options, "async", 0);
249 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
253 while (ZOOM_event (1, &c))
258 ZOOM_query ZOOM_query_create(void)
260 ZOOM_query s = xmalloc (sizeof(*s));
265 s->odr = odr_createmem (ODR_ENCODE);
270 void ZOOM_query_destroy(ZOOM_query s)
276 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
277 if (s->refcount == 0)
279 odr_destroy (s->odr);
284 int ZOOM_query_prefix(ZOOM_query s, const char *str)
286 s->query = odr_malloc (s->odr, sizeof(*s->query));
287 s->query->which = Z_Query_type_1;
288 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
289 if (!s->query->u.type_1)
294 int ZOOM_query_sortby(ZOOM_query s, const char *criteria)
296 s->sort_spec = yaz_sort_spec (s->odr, criteria);
302 static int do_write(ZOOM_connection c);
304 void ZOOM_connection_destroy(ZOOM_connection c)
311 for (r = c->resultsets; r; r = r->next)
316 odr_destroy (c->odr_in);
317 odr_destroy (c->odr_out);
318 ZOOM_options_destroy (c->options);
319 ZOOM_connection_remove_tasks (c);
320 xfree (c->host_port);
324 void ZOOM_resultset_addref (ZOOM_resultset r)
329 ZOOM_resultset ZOOM_resultset_create ()
331 ZOOM_resultset r = xmalloc (sizeof(*r));
335 r->odr = odr_createmem (ODR_ENCODE);
349 ZOOM_resultset ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
352 ZOOM_query s = ZOOM_query_create();
354 ZOOM_query_prefix (s, q);
356 r = ZOOM_connection_search (c, s);
357 ZOOM_query_destroy (s);
361 ZOOM_resultset ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
363 ZOOM_resultset r = ZOOM_resultset_create ();
367 r->r_sort_spec = q->sort_spec;
368 r->r_query = q->query;
371 r->options = ZOOM_options_create_with_parent(c->options);
373 r->start = ZOOM_options_get_int(r->options, "start", 0);
374 r->count = ZOOM_options_get_int(r->options, "count", 0);
375 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
376 cp = ZOOM_options_get (r->options, "setname");
378 r->setname = xstrdup (cp);
382 r->next = c->resultsets;
385 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
386 task->u.search.resultset = r;
387 ZOOM_resultset_addref (r);
393 while (ZOOM_event (1, &c))
399 void ZOOM_resultset_destroy(ZOOM_resultset r)
404 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
405 if (r->refcount == 0)
409 /* remove ourselves from the resultsets in connection */
410 ZOOM_resultset *rp = &r->connection->resultsets;
413 assert (*rp); /* we must be in this list!! */
415 { /* OK, we're here - take us out of it */
422 ZOOM_query_destroy (r->search);
423 ZOOM_options_destroy (r->options);
424 odr_destroy (r->odr);
430 size_t ZOOM_resultset_size (ZOOM_resultset r)
435 static void do_close (ZOOM_connection c)
441 c->state = STATE_IDLE;
444 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
445 int force_sync, int start, int count)
455 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
456 task->u.retrieve.resultset = r;
457 task->u.retrieve.start = start;
458 task->u.retrieve.count = count;
460 ZOOM_resultset_addref (r);
462 if (!r->connection->async || force_sync)
463 while (r->connection && ZOOM_event (1, &r->connection))
467 void ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
468 size_t start, size_t count)
470 int force_present = 0;
476 ZOOM_resultset_retrieve (r, force_present, start, count);
480 for (i = 0; i< count; i++)
481 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
485 static int do_connect (ZOOM_connection c)
488 const char *effective_host;
491 effective_host = c->proxy;
493 effective_host = c->host_port;
495 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
498 c->cs = cs_create_host (effective_host, 0, &add);
502 int ret = cs_connect (c->cs, add);
503 yaz_log (LOG_DEBUG, "cs_connect returned %d", ret);
506 c->state = STATE_CONNECTING;
507 c->mask = ZOOM_SELECT_READ | ZOOM_SELECT_WRITE |
512 c->state = STATE_IDLE;
513 c->error = ZOOM_ERROR_CONNECT;
517 int z3950_connection_socket(ZOOM_connection c)
520 return cs_fileno(c->cs);
524 int z3950_connection_mask(ZOOM_connection c)
531 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
536 sprintf (str, "send_APDU t=%p type=%d", c, a->which);
539 Z_OtherInformation **oi;
541 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
543 if (!z_APDU(out, &a, 0, 0))
545 FILE *outf = fopen("/tmp/apdu.txt", "w");
548 ODR odr_pr = odr_createmem(ODR_PRINT);
549 fprintf (outf, "a=%p\n", a);
550 odr_setprint(odr_pr, outf);
551 z_APDU(odr_pr, &a, 0, 0);
555 c->error = ZOOM_ERROR_ENCODE;
563 static int send_APDU (ZOOM_connection c, Z_APDU *a)
567 if (encode_APDU(c, a, c->odr_out))
569 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
570 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
571 ZOOM_connection_put_event (c, event);
572 odr_reset(c->odr_out);
577 static int ZOOM_connection_send_init (ZOOM_connection c)
580 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
581 Z_InitRequest *ireq = apdu->u.initRequest;
582 Z_IdAuthentication *auth = odr_malloc(c->odr_out, sizeof(*auth));
583 const char *auth_groupId = ZOOM_options_get (c->options, "group");
584 const char *auth_userId = ZOOM_options_get (c->options, "user");
585 const char *auth_password = ZOOM_options_get (c->options, "pass");
587 ODR_MASK_SET(ireq->options, Z_Options_search);
588 ODR_MASK_SET(ireq->options, Z_Options_present);
589 ODR_MASK_SET(ireq->options, Z_Options_scan);
590 ODR_MASK_SET(ireq->options, Z_Options_sort);
591 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
592 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
594 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
595 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
596 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
598 impname = ZOOM_options_get (c->options, "implementationName");
599 ireq->implementationName =
600 odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
601 strcpy (ireq->implementationName, "");
604 strcat (ireq->implementationName, impname);
605 strcat (ireq->implementationName, "/");
607 strcat (ireq->implementationName, "ZOOM-C/YAZ");
609 *ireq->maximumRecordSize =
610 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
611 *ireq->preferredMessageSize =
612 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
614 if (auth_groupId || auth_password)
616 Z_IdPass *pass = odr_malloc(c->odr_out, sizeof(*pass));
619 if (auth_groupId && *auth_groupId)
621 pass->groupId = odr_malloc(c->odr_out, strlen(auth_groupId)+1);
622 strcpy(pass->groupId, auth_groupId);
626 if (auth_userId && *auth_userId)
628 pass->userId = odr_malloc(c->odr_out, strlen(auth_userId)+1);
629 strcpy(pass->userId, auth_userId);
633 if (auth_password && *auth_password)
635 pass->password = odr_malloc(c->odr_out, strlen(auth_password)+1);
636 strcpy(pass->password, auth_password);
641 auth->which = Z_IdAuthentication_idPass;
642 auth->u.idPass = pass;
643 ireq->idAuthentication = auth;
646 else if (auth_userId)
648 auth->which = Z_IdAuthentication_open;
649 auth->u.open = odr_malloc(c->odr_out, strlen(auth_userId)+1);
650 strcpy(auth->u.open, auth_userId);
651 ireq->idAuthentication = auth;
654 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
655 VAL_PROXY, 1, c->host_port);
662 static int ZOOM_connection_send_search (ZOOM_connection c)
665 int lslb, ssub, mspn;
667 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
668 Z_SearchRequest *search_req = apdu->u.searchRequest;
669 const char *elementSetName;
670 const char *smallSetElementSetName;
671 const char *mediumSetElementSetName;
675 assert (c->tasks->which == ZOOM_TASK_SEARCH);
677 r = c->tasks->u.search.resultset;
680 ZOOM_options_get (r->options, "elementSetName");
681 smallSetElementSetName =
682 ZOOM_options_get (r->options, "smallSetElementSetName");
683 mediumSetElementSetName =
684 ZOOM_options_get (r->options, "mediumSetElementSetName");
686 ZOOM_options_get (r->options, "schema");
688 if (!smallSetElementSetName)
689 smallSetElementSetName = elementSetName;
691 if (!mediumSetElementSetName)
692 mediumSetElementSetName = elementSetName;
697 /* prepare query for the search request */
698 search_req->query = r->r_query;
700 search_req->databaseNames =
701 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
703 /* get syntax (no need to provide unless piggyback is in effect) */
704 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
706 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
707 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
708 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
709 if (lslb != -1 && ssub != -1 && mspn != -1)
711 /* So're a Z39.50 expert? Let's hope you don't do sort */
712 *search_req->largeSetLowerBound = lslb;
713 *search_req->smallSetUpperBound = ssub;
714 *search_req->mediumSetPresentNumber = mspn;
716 else if (r->start == 0 && r->count > 0
717 && r->piggyback && !r->r_sort_spec && !schema)
719 /* Regular piggyback - do it unless we're going to do sort */
720 *search_req->largeSetLowerBound = 2000000000;
721 *search_req->smallSetUpperBound = r->count;
722 *search_req->mediumSetPresentNumber = r->count;
723 smallSetElementSetName = 0; /* no need to provide this */
727 /* non-piggyback. Need not provide elementsets or syntaxes .. */
728 smallSetElementSetName = 0;
729 mediumSetElementSetName = 0;
732 if (smallSetElementSetName && *smallSetElementSetName)
734 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
736 esn->which = Z_ElementSetNames_generic;
737 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
738 search_req->smallSetElementSetNames = esn;
740 if (mediumSetElementSetName && *mediumSetElementSetName)
742 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
744 esn->which = Z_ElementSetNames_generic;
745 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
746 search_req->mediumSetElementSetNames = esn;
749 search_req->preferredRecordSyntax =
750 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
754 if (c->support_named_resultsets)
758 /* find the lowest unused ordinal so that we re-use
759 result sets on the server. */
760 for (ord = 1; ; ord++)
763 sprintf (setname, "%d", ord);
764 for (rp = c->resultsets; rp; rp = rp->next)
765 if (rp->setname && !strcmp (rp->setname, setname))
770 r->setname = xstrdup (setname);
771 yaz_log (LOG_DEBUG, "allocating %s", r->setname);
774 r->setname = xstrdup ("default");
775 ZOOM_options_set (r->options, "setname", r->setname);
777 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
778 /* send search request */
784 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
786 Z_DefaultDiagFormat *r;
791 if (p->which != Z_DiagRec_defaultFormat)
793 c->error = ZOOM_ERROR_DECODE;
796 r = p->u.defaultFormat;
799 case Z_DefaultDiagFormat_v2Addinfo:
800 addinfo = r->u.v2Addinfo;
802 case Z_DefaultDiagFormat_v3Addinfo:
803 addinfo = r->u.v3Addinfo;
807 c->addinfo = xstrdup (addinfo);
808 c->error = *r->condition;
811 ZOOM_record ZOOM_record_clone (ZOOM_record srec)
818 odr_enc = odr_createmem(ODR_ENCODE);
819 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
821 buf = odr_getbuf (odr_enc, &size, 0);
823 nrec = xmalloc (sizeof(*nrec));
824 nrec->odr = odr_createmem(ODR_DECODE);
825 nrec->wrbuf_marc = 0;
826 odr_setbuf (nrec->odr, buf, size, 0);
827 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
829 odr_destroy (odr_enc);
833 ZOOM_record ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
835 return record_cache_lookup (s, pos, 0);
838 ZOOM_record ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
840 ZOOM_resultset_retrieve (r, 1, pos, 1);
841 return ZOOM_resultset_record_immediate (r, pos);
844 void ZOOM_record_destroy (ZOOM_record rec)
849 wrbuf_free (rec->wrbuf_marc, 1);
850 odr_destroy (rec->odr);
854 void *ZOOM_record_get (ZOOM_record rec, const char *type, size_t *len)
856 Z_NamePlusRecord *npr;
862 if (!strcmp (type, "database"))
864 return npr->databaseName;
866 else if (!strcmp (type, "syntax"))
868 if (npr->which == Z_NamePlusRecord_databaseRecord)
870 Z_External *r = (Z_External *) npr->u.databaseRecord;
871 oident *ent = oid_getentbyoid(r->direct_reference);
877 else if (!strcmp (type, "render"))
879 if (npr->which == Z_NamePlusRecord_databaseRecord)
881 Z_External *r = (Z_External *) npr->u.databaseRecord;
882 oident *ent = oid_getentbyoid(r->direct_reference);
884 if (r->which == Z_External_sutrs)
886 *len = r->u.sutrs->len;
887 return r->u.sutrs->buf;
889 else if (r->which == Z_External_octet)
898 case VAL_APPLICATION_XML:
901 if (!rec->wrbuf_marc)
902 rec->wrbuf_marc = wrbuf_alloc();
903 if (marc_display_wrbuf (r->u.octet_aligned->buf,
905 r->u.octet_aligned->len) > 0)
907 *len = wrbuf_len(rec->wrbuf_marc);
908 return wrbuf_buf(rec->wrbuf_marc);
911 *len = r->u.octet_aligned->len;
912 return r->u.octet_aligned->buf;
914 else if (r->which == Z_External_grs1)
922 else if (!strcmp (type, "raw"))
924 if (npr->which == Z_NamePlusRecord_databaseRecord)
926 Z_External *r = (Z_External *) npr->u.databaseRecord;
928 if (r->which == Z_External_sutrs)
930 *len = r->u.sutrs->len;
931 return r->u.sutrs->buf;
933 else if (r->which == Z_External_octet)
935 *len = r->u.octet_aligned->len;
936 return r->u.octet_aligned->buf;
938 else /* grs-1, explain, ... */
941 return (Z_External *) npr->u.databaseRecord;
949 static void record_cache_add (ZOOM_resultset r,
950 Z_NamePlusRecord *npr,
952 const char *elementSetName)
954 ZOOM_record_cache rc;
956 for (rc = r->record_cache; rc; rc = rc->next)
960 if ((!elementSetName && !rc->elementSetName)
961 || (elementSetName && rc->elementSetName &&
962 !strcmp (elementSetName, rc->elementSetName)))
964 /* not destroying rc->npr (it's handled by nmem )*/
966 /* keeping wrbuf_marc too */
972 rc = odr_malloc (r->odr, sizeof(*rc));
975 rc->rec.wrbuf_marc = 0;
977 rc->elementSetName = odr_strdup (r->odr, elementSetName);
979 rc->elementSetName = 0;
981 rc->next = r->record_cache;
982 r->record_cache = rc;
985 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
987 const char *elementSetName)
989 ZOOM_record_cache rc;
991 for (rc = r->record_cache; rc; rc = rc->next)
995 if ((!elementSetName && !rc->elementSetName)
996 || (elementSetName && rc->elementSetName &&
997 !strcmp (elementSetName, rc->elementSetName)))
1004 static void handle_records (ZOOM_connection c, Z_Records *sr,
1007 ZOOM_resultset resultset;
1011 switch (c->tasks->which)
1013 case ZOOM_TASK_SEARCH:
1014 resultset = c->tasks->u.search.resultset;
1016 case ZOOM_TASK_RETRIEVE:
1017 resultset = c->tasks->u.retrieve.resultset;
1022 if (sr && sr->which == Z_Records_NSD)
1024 Z_DiagRec dr, *dr_p = &dr;
1025 dr.which = Z_DiagRec_defaultFormat;
1026 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1028 response_diag (c, dr_p);
1030 else if (sr && sr->which == Z_Records_multipleNSD)
1032 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1033 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1035 c->error = ZOOM_ERROR_DECODE;
1039 if (resultset->count + resultset->start > resultset->size)
1040 resultset->count = resultset->size - resultset->start;
1041 if (resultset->count < 0)
1042 resultset->count = 0;
1043 if (sr && sr->which == Z_Records_DBOSD)
1046 NMEM nmem = odr_extract_mem (c->odr_in);
1047 Z_NamePlusRecordList *p =
1048 sr->u.databaseOrSurDiagnostics;
1049 for (i = 0; i<p->num_records; i++)
1051 record_cache_add (resultset, p->records[i],
1052 i+ resultset->start, 0);
1054 /* transfer our response to search_nmem .. we need it later */
1055 nmem_transfer (resultset->odr->mem, nmem);
1056 nmem_destroy (nmem);
1057 if (present_phase && p->num_records == 0)
1059 /* present response and we didn't get any records! */
1060 c->error = ZOOM_ERROR_DECODE;
1063 else if (present_phase)
1065 /* present response and we didn't get any records! */
1066 c->error = ZOOM_ERROR_DECODE;
1071 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1073 handle_records (c, pr->records, 1);
1076 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1078 ZOOM_resultset resultset;
1080 yaz_log (LOG_DEBUG, "got search response");
1082 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1085 resultset = c->tasks->u.search.resultset;
1087 resultset->size = *sr->resultCount;
1088 handle_records (c, sr->records, 0);
1091 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1093 if (res->diagnostics && res->num_diagnostics > 0)
1094 response_diag (c, res->diagnostics[0]);
1097 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1099 NMEM nmem = odr_extract_mem (c->odr_in);
1102 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1104 scan = c->tasks->u.scan.scan;
1106 if (res->entries && res->entries->nonsurrogateDiagnostics)
1107 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1108 scan->scan_response = res;
1109 nmem_transfer (scan->odr->mem, nmem);
1111 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1112 if (res->positionOfTerm)
1113 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1114 if (res->scanStatus)
1115 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1116 if (res->numberOfEntriesReturned)
1117 ZOOM_options_set_int (scan->options, "number",
1118 *res->numberOfEntriesReturned);
1119 nmem_destroy (nmem);
1123 static int send_sort (ZOOM_connection c)
1125 ZOOM_resultset resultset;
1127 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1130 resultset = c->tasks->u.search.resultset;
1134 resultset->r_sort_spec = 0;
1137 if (resultset->r_sort_spec)
1139 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1140 Z_SortRequest *req = apdu->u.sortRequest;
1142 req->num_inputResultSetNames = 1;
1143 req->inputResultSetNames = (Z_InternationalString **)
1144 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1145 req->inputResultSetNames[0] =
1146 odr_strdup (c->odr_out, resultset->setname);
1147 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1148 req->sortSequence = resultset->r_sort_spec;
1149 resultset->r_sort_spec = 0;
1150 send_APDU (c, apdu);
1156 static int send_present (ZOOM_connection c)
1158 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1159 Z_PresentRequest *req = apdu->u.presentRequest;
1161 const char *syntax =
1162 ZOOM_options_get (c->options, "preferredRecordSyntax");
1163 const char *element =
1164 ZOOM_options_get (c->options, "elementSetName");
1165 const char *schema =
1166 ZOOM_options_get (c->options, "schema");
1167 ZOOM_resultset resultset;
1172 switch (c->tasks->which)
1174 case ZOOM_TASK_SEARCH:
1175 resultset = c->tasks->u.search.resultset;
1177 case ZOOM_TASK_RETRIEVE:
1178 resultset = c->tasks->u.retrieve.resultset;
1179 resultset->start = c->tasks->u.retrieve.start;
1180 resultset->count = c->tasks->u.retrieve.count;
1182 if (resultset->start >= resultset->size)
1184 if (resultset->start + resultset->count > resultset->size)
1185 resultset->count = resultset->size - resultset->start;
1191 if (c->error) /* don't continue on error */
1193 if (resultset->start < 0)
1195 for (i = 0; i<resultset->count; i++)
1198 record_cache_lookup (resultset, i + resultset->start, 0);
1202 if (i == resultset->count)
1205 resultset->start += i;
1206 resultset->count -= i;
1207 *req->resultSetStartPoint = resultset->start + 1;
1208 *req->numberOfRecordsRequested = resultset->count;
1209 assert (*req->numberOfRecordsRequested > 0);
1211 if (syntax && *syntax)
1212 req->preferredRecordSyntax =
1213 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1215 if (schema && *schema)
1217 Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1219 req->recordComposition = compo;
1220 compo->which = Z_RecordComp_complex;
1221 compo->u.complex = (Z_CompSpec *)
1222 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1223 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1224 odr_malloc(c->odr_out, sizeof(bool_t));
1225 *compo->u.complex->selectAlternativeSyntax = 0;
1227 compo->u.complex->generic = (Z_Specification *)
1228 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1230 compo->u.complex->generic->schema = (Odr_oid *)
1231 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1233 if (!compo->u.complex->generic->schema)
1235 /* OID wasn't a schema! Try record syntax instead. */
1237 compo->u.complex->generic->schema = (Odr_oid *)
1238 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1240 if (element && *element)
1242 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1243 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1244 compo->u.complex->generic->elementSpec->which =
1245 Z_ElementSpec_elementSetName;
1246 compo->u.complex->generic->elementSpec->u.elementSetName =
1247 odr_strdup (c->odr_out, element);
1250 compo->u.complex->generic->elementSpec = 0;
1251 compo->u.complex->num_dbSpecific = 0;
1252 compo->u.complex->dbSpecific = 0;
1253 compo->u.complex->num_recordSyntax = 0;
1254 compo->u.complex->recordSyntax = 0;
1256 else if (element && *element)
1258 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
1259 Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1261 esn->which = Z_ElementSetNames_generic;
1262 esn->u.generic = odr_strdup (c->odr_out, element);
1263 compo->which = Z_RecordComp_simple;
1264 compo->u.simple = esn;
1265 req->recordComposition = compo;
1267 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1268 send_APDU (c, apdu);
1272 ZOOM_scanset ZOOM_connection_scan (ZOOM_connection c, const char *start)
1274 ZOOM_scanset scan = xmalloc (sizeof(*scan));
1276 scan->connection = c;
1277 scan->odr = odr_createmem (ODR_DECODE);
1278 scan->options = ZOOM_options_create_with_parent (c->options);
1280 scan->scan_response = 0;
1282 if ((scan->termListAndStartPoint =
1283 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1286 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1287 task->u.scan.scan = scan;
1292 while (ZOOM_event (1, &c))
1299 void ZOOM_scanset_destroy (ZOOM_scanset scan)
1304 if (scan->refcount == 0)
1306 odr_destroy (scan->odr);
1308 ZOOM_options_destroy (scan->options);
1313 int send_scan (ZOOM_connection c)
1316 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1317 Z_ScanRequest *req = apdu->u.scanRequest;
1320 assert (c->tasks->which == ZOOM_TASK_SCAN);
1321 scan = c->tasks->u.scan.scan;
1323 req->termListAndStartPoint = scan->termListAndStartPoint;
1324 req->attributeSet = scan->attributeSet;
1326 *req->numberOfTermsRequested =
1327 ZOOM_options_get_int(scan->options, "number", 10);
1329 req->preferredPositionInResponse =
1330 odr_intdup (c->odr_out,
1331 ZOOM_options_get_int(scan->options, "position", 1));
1334 odr_intdup (c->odr_out,
1335 ZOOM_options_get_int(scan->options, "stepSize", 0));
1337 req->databaseNames = set_DatabaseNames (c, scan->options,
1338 &req->num_databaseNames);
1340 send_APDU (c, apdu);
1345 size_t ZOOM_scanset_size (ZOOM_scanset scan)
1347 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1349 return scan->scan_response->entries->num_entries;
1352 const char *ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1353 int *occ, size_t *len)
1355 const char *term = 0;
1356 size_t noent = ZOOM_scanset_size (scan);
1357 Z_ScanResponse *res = scan->scan_response;
1363 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1365 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1367 if (t->term->which == Z_Term_general)
1369 term = t->term->u.general->buf;
1370 *len = t->term->u.general->len;
1372 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1377 const char *ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1379 return ZOOM_options_get (scan->options, key);
1382 void ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1385 ZOOM_options_set (scan->options, key, val);
1388 static int ZOOM_connection_exec_task (ZOOM_connection c)
1390 ZOOM_task task = c->tasks;
1392 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1395 if (c->error != ZOOM_ERROR_NONE ||
1396 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1398 ZOOM_connection_remove_tasks (c);
1401 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d", task->which);
1405 switch (task->which)
1407 case ZOOM_TASK_SEARCH:
1408 /* see if search hasn't been sent yet. */
1409 if (ZOOM_connection_send_search (c))
1412 case ZOOM_TASK_RETRIEVE:
1413 if (send_present (c))
1416 case ZOOM_TASK_CONNECT:
1420 case ZOOM_TASK_SCAN:
1424 ZOOM_connection_remove_task (c);
1428 static int send_sort_present (ZOOM_connection c)
1430 int r = send_sort (c);
1432 r = send_present (c);
1436 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1438 Z_InitResponse *initrs;
1440 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1444 case Z_APDU_initResponse:
1445 initrs = apdu->u.initResponse;
1446 if (!*initrs->result)
1448 c->error = ZOOM_ERROR_INIT;
1453 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1455 xfree (c->cookie_in);
1458 c->cookie_in = xstrdup(cookie);
1459 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1460 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1461 c->support_named_resultsets = 1;
1464 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1465 ZOOM_connection_remove_task (c);
1467 ZOOM_connection_exec_task (c);
1470 case Z_APDU_searchResponse:
1471 handle_search_response (c, apdu->u.searchResponse);
1472 if (!send_sort_present (c))
1473 ZOOM_connection_remove_task (c);
1475 case Z_APDU_presentResponse:
1476 handle_present_response (c, apdu->u.presentResponse);
1477 if (!send_present (c))
1478 ZOOM_connection_remove_task (c);
1480 case Z_APDU_sortResponse:
1481 sort_response (c, apdu->u.sortResponse);
1482 if (!send_present (c))
1483 ZOOM_connection_remove_task (c);
1485 case Z_APDU_scanResponse:
1486 scan_response (c, apdu->u.scanResponse);
1487 ZOOM_connection_remove_task (c);
1491 static int do_read (ZOOM_connection c)
1497 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
1498 ZOOM_connection_put_event (c, event);
1500 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1505 c->error= ZOOM_ERROR_CONNECTION_LOST;
1511 odr_reset (c->odr_in);
1512 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1513 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
1514 ZOOM_connection_put_event (c, event);
1515 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1517 c->error = ZOOM_ERROR_DECODE;
1522 handle_apdu (c, apdu);
1528 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
1533 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1534 ZOOM_connection_put_event (c, event);
1536 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1538 if (c->state == STATE_CONNECTING)
1539 c->error = ZOOM_ERROR_CONNECT;
1541 c->error = ZOOM_ERROR_CONNECTION_LOST;
1547 c->state = STATE_ESTABLISHED;
1548 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_WRITE|ZOOM_SELECT_EXCEPT;
1552 c->state = STATE_ESTABLISHED;
1553 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
1558 static int do_write(ZOOM_connection c)
1560 return do_write_ex (c, c->buf_out, c->len_out);
1564 const char *ZOOM_connection_option_get (ZOOM_connection c, const char *key)
1566 return ZOOM_options_get (c->options, key);
1569 void ZOOM_connection_option_set (ZOOM_connection c, const char *key,
1572 ZOOM_options_set (c->options, key, val);
1575 const char *ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
1577 return ZOOM_options_get (r->options, key);
1580 void ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
1583 ZOOM_options_set (r->options, key, val);
1587 int ZOOM_connection_errcode (ZOOM_connection c)
1589 return ZOOM_connection_error (c, 0, 0);
1592 const char *ZOOM_connection_errmsg (ZOOM_connection c)
1595 ZOOM_connection_error (c, &msg, 0);
1599 const char *ZOOM_connection_addinfo (ZOOM_connection c)
1601 const char *addinfo;
1602 ZOOM_connection_error (c, 0, &addinfo);
1606 int ZOOM_connection_error (ZOOM_connection c, const char **cp,
1607 const char **addinfo)
1609 int error = c->error;
1614 case ZOOM_ERROR_NONE:
1615 *cp = "No error"; break;
1616 case ZOOM_ERROR_CONNECT:
1617 *cp = "Connect failed"; break;
1618 case ZOOM_ERROR_MEMORY:
1619 *cp = "Out of memory"; break;
1620 case ZOOM_ERROR_ENCODE:
1621 *cp = "Encoding failed"; break;
1622 case ZOOM_ERROR_DECODE:
1623 *cp = "Decoding failed"; break;
1624 case ZOOM_ERROR_CONNECTION_LOST:
1625 *cp = "Connection lost"; break;
1626 case ZOOM_ERROR_INIT:
1627 *cp = "Init rejected"; break;
1628 case ZOOM_ERROR_INTERNAL:
1629 *cp = "Internal failure"; break;
1630 case ZOOM_ERROR_TIMEOUT:
1631 *cp = "Timeout"; break;
1633 *cp = diagbib1_str (error);
1639 *addinfo = c->addinfo;
1646 int ZOOM_connection_do_io(ZOOM_connection c, int mask)
1648 ZOOM_Event event = 0;
1650 int r = cs_look(c->cs);
1651 yaz_log (LOG_LOG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
1656 event = ZOOM_Event_create (ZOOM_EVENT_IO_CONNECT);
1657 c->error = ZOOM_ERROR_CONNECT;
1659 ZOOM_connection_put_event (c, event);
1661 else if (r == CS_CONNECT)
1663 event = ZOOM_Event_create (ZOOM_EVENT_IO_CONNECT);
1664 yaz_log (LOG_LOG, "calling rcvconnect");
1665 if (cs_rcvconnect (c->cs) < 0)
1667 c->error = ZOOM_ERROR_CONNECT;
1669 ZOOM_connection_put_event (c, event);
1673 ZOOM_connection_put_event (c, event);
1674 ZOOM_connection_send_init (c);
1679 if (mask & ZOOM_SELECT_READ)
1681 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1685 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d", c, mask);
1686 if (c->state == STATE_CONNECTING)
1688 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
1689 if (mask & ZOOM_SELECT_WRITE)
1691 ZOOM_connection_put_event (c, event);
1692 ZOOM_connection_send_init (c);
1696 c->error = ZOOM_ERROR_CONNECT;
1698 ZOOM_connection_put_event (c, event);
1701 else if (c->state == STATE_ESTABLISHED)
1703 if (mask & ZOOM_SELECT_READ)
1705 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1710 event = ZOOM_Event_create (ZOOM_EVENT_UNKNOWN);
1711 ZOOM_connection_put_event (c, event);
1712 c->error = ZOOM_ERROR_INTERNAL;
1719 int ZOOM_connection_last_event(ZOOM_connection cs)
1722 return ZOOM_EVENT_NONE;
1723 return cs->last_event;
1726 int ZOOM_event (int no, ZOOM_connection *cs)
1729 struct pollfd pollfds[1024];
1730 ZOOM_connection poll_cs[1024];
1733 fd_set input, output, except;
1738 for (i = 0; i<no; i++)
1740 ZOOM_connection c = cs[i];
1742 if (c && (event = ZOOM_connection_get_event(c)))
1744 ZOOM_Event_destroy (event);
1748 for (i = 0; i<no; i++)
1750 ZOOM_connection c = cs[i];
1752 if (c && ZOOM_connection_exec_task (c))
1754 if ((event = ZOOM_connection_get_event(c)))
1756 ZOOM_Event_destroy (event);
1772 for (i = 0; i<no; i++)
1774 ZOOM_connection c = cs[i];
1779 fd = z3950_connection_socket(c);
1780 mask = z3950_connection_mask(c);
1790 short poll_events = 0;
1792 if (mask & ZOOM_SELECT_READ)
1793 poll_events += POLLIN;
1794 if (mask & ZOOM_SELECT_WRITE)
1795 poll_events += POLLOUT;
1796 if (mask & ZOOM_SELECT_EXCEPT)
1797 poll_events += POLLERR;
1798 pollfds[nfds].fd = fd;
1799 pollfds[nfds].events = poll_events;
1800 pollfds[nfds].revents = 0;
1805 if (mask & ZOOM_SELECT_READ)
1807 FD_SET (fd, &input);
1810 if (mask & ZOOM_SELECT_WRITE)
1812 FD_SET (fd, &output);
1815 if (mask & ZOOM_SELECT_EXCEPT)
1817 FD_SET (fd, &except);
1825 yaz_log (LOG_DEBUG, "poll start");
1826 r = poll (pollfds, nfds, 15000);
1827 yaz_log (LOG_DEBUG, "poll stop, returned r=%d", r);
1828 for (i = 0; i<nfds; i++)
1830 ZOOM_connection c = poll_cs[i];
1834 if (pollfds[i].revents & POLLIN)
1835 mask += ZOOM_SELECT_READ;
1836 if (pollfds[i].revents & POLLOUT)
1837 mask += ZOOM_SELECT_WRITE;
1838 if (pollfds[i].revents & POLLERR)
1839 mask += ZOOM_SELECT_EXCEPT;
1841 ZOOM_connection_do_io(c, mask);
1843 else if (r == 0 && c->mask)
1845 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
1846 /* timeout and this connection was waiting */
1847 c->error = ZOOM_ERROR_TIMEOUT;
1849 ZOOM_connection_put_event(c, event);
1853 yaz_log (LOG_DEBUG, "select start");
1854 r = select (max_fd+1, &input, &output, &except, &tv);
1855 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
1856 for (i = 0; i<no; i++)
1858 ZOOM_connection c = cs[i];
1863 fd = z3950_connection_socket(c);
1867 /* no timeout and real socket */
1868 if (FD_ISSET(fd, &input))
1869 mask += ZOOM_SELECT_READ;
1870 if (FD_ISSET(fd, &output))
1871 mask += ZOOM_SELECT_WRITE;
1872 if (FD_ISSET(fd, &except))
1873 mask += ZOOM_SELECT_EXCEPT;
1875 ZOOM_connection_do_io(c, mask);
1877 if (r == 0 && c->mask)
1879 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
1880 /* timeout and this connection was waiting */
1881 c->error = ZOOM_ERROR_TIMEOUT;
1883 yaz_log (LOG_DEBUG, "timeout");
1884 ZOOM_connection_put_event(c, event);
1888 for (i = 0; i<no; i++)
1890 ZOOM_connection c = cs[i];
1892 if (c && (event = ZOOM_connection_get_event(c)))
1894 ZOOM_Event_destroy (event);