2 * $Id: zoom-c.c,v 1.26 2002-05-07 11:00:58 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 int ZOOM_connection_send_init (ZOOM_connection c);
21 static ZOOM_Event ZOOM_Event_create (int kind)
23 ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event));
30 static void ZOOM_Event_destroy (ZOOM_Event event)
35 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
39 c->m_queue_back->prev = event;
40 assert (c->m_queue_front);
44 assert (!c->m_queue_front);
45 c->m_queue_front = event;
47 event->next = c->m_queue_back;
49 c->m_queue_back = event;
52 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
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)
75 case ZOOM_ERROR_CONNECT:
76 case ZOOM_ERROR_MEMORY:
77 case ZOOM_ERROR_DECODE:
78 case ZOOM_ERROR_CONNECTION_LOST:
80 case ZOOM_ERROR_INTERNAL:
83 c->error = ZOOM_ERROR_NONE;
89 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
91 ZOOM_task *taskp = &c->tasks;
93 taskp = &(*taskp)->next;
94 *taskp = (ZOOM_task) xmalloc (sizeof(**taskp));
95 (*taskp)->running = 0;
96 (*taskp)->which = which;
102 void ZOOM_connection_remove_task (ZOOM_connection c)
104 ZOOM_task task = c->tasks;
108 c->tasks = task->next;
111 case ZOOM_TASK_SEARCH:
112 ZOOM_resultset_destroy (task->u.search.resultset);
114 case ZOOM_TASK_RETRIEVE:
115 ZOOM_resultset_destroy (task->u.retrieve.resultset);
117 case ZOOM_TASK_CONNECT:
120 ZOOM_scanset_destroy (task->u.scan.scan);
129 void ZOOM_connection_remove_tasks (ZOOM_connection c)
132 ZOOM_connection_remove_task(c);
135 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
137 const char *elementSetName);
139 ZOOM_connection ZOOM_connection_create (ZOOM_options options)
141 ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
145 c->state = STATE_IDLE;
146 c->error = ZOOM_ERROR_NONE;
154 c->options = ZOOM_options_create_with_parent(options);
163 c->odr_in = odr_createmem (ODR_DECODE);
164 c->odr_out = odr_createmem (ODR_ENCODE);
167 c->support_named_resultsets = 0;
168 c->last_event = ZOOM_EVENT_NONE;
170 c->m_queue_front = 0;
175 /* set database names. Take local databases (if set); otherwise
176 take databases given in ZURL (if set); otherwise use Default */
177 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
180 char **databaseNames;
183 const char *cp = ZOOM_options_get (options, "databaseName");
187 cp = strchr (con->host_port, '/');
194 while ((c = strchr(c, '+')))
202 databaseNames = (char**)
203 odr_malloc (con->odr_out, no * sizeof(*databaseNames));
207 c = strchr (cp, '+');
215 /* cp ptr to first char of db name, c is char
217 databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
218 memcpy (databaseNames[no], cp, c-cp);
219 databaseNames[no++][c-cp] = '\0';
224 databaseNames[no] = NULL;
226 return databaseNames;
229 ZOOM_connection ZOOM_connection_new (const char *host, int portnum)
231 ZOOM_connection c = ZOOM_connection_create (0);
233 ZOOM_connection_connect (c, host, portnum);
237 void ZOOM_connection_connect(ZOOM_connection c,
238 const char *host, int portnum)
243 val = ZOOM_options_get (c->options, "proxy");
245 c->proxy = xstrdup (val);
252 sprintf (hostn, "%.80s:%d", host, portnum);
253 c->host_port = xstrdup(hostn);
256 c->host_port = xstrdup(host);
258 ZOOM_options_set(c->options, "host", c->host_port);
260 c->async = ZOOM_options_get_bool (c->options, "async", 0);
262 c->error = ZOOM_ERROR_NONE;
264 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
268 while (ZOOM_event (1, &c))
273 ZOOM_query ZOOM_query_create(void)
275 ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
280 s->odr = odr_createmem (ODR_ENCODE);
285 void ZOOM_query_destroy(ZOOM_query s)
291 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
292 if (s->refcount == 0)
294 odr_destroy (s->odr);
299 int ZOOM_query_prefix(ZOOM_query s, const char *str)
301 s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query));
302 s->query->which = Z_Query_type_1;
303 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
304 if (!s->query->u.type_1)
309 int ZOOM_query_sortby(ZOOM_query s, const char *criteria)
311 s->sort_spec = yaz_sort_spec (s->odr, criteria);
317 static int do_write(ZOOM_connection c);
319 void ZOOM_connection_destroy(ZOOM_connection c)
326 for (r = c->resultsets; r; r = r->next)
331 odr_destroy (c->odr_in);
332 odr_destroy (c->odr_out);
333 ZOOM_options_destroy (c->options);
334 ZOOM_connection_remove_tasks (c);
335 xfree (c->host_port);
337 xfree (c->cookie_out);
338 xfree (c->cookie_in);
342 void ZOOM_resultset_addref (ZOOM_resultset r)
347 ZOOM_resultset ZOOM_resultset_create ()
349 ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
353 r->odr = odr_createmem (ODR_ENCODE);
367 ZOOM_resultset ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
370 ZOOM_query s = ZOOM_query_create();
372 ZOOM_query_prefix (s, q);
374 r = ZOOM_connection_search (c, s);
375 ZOOM_query_destroy (s);
379 ZOOM_resultset ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
381 ZOOM_resultset r = ZOOM_resultset_create ();
385 r->r_sort_spec = q->sort_spec;
386 r->r_query = q->query;
389 r->options = ZOOM_options_create_with_parent(c->options);
391 r->start = ZOOM_options_get_int(r->options, "start", 0);
392 r->count = ZOOM_options_get_int(r->options, "count", 0);
393 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
394 cp = ZOOM_options_get (r->options, "setname");
396 r->setname = xstrdup (cp);
400 r->next = c->resultsets;
403 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
404 task->u.search.resultset = r;
405 ZOOM_resultset_addref (r);
411 while (ZOOM_event (1, &c))
417 void ZOOM_resultset_destroy(ZOOM_resultset r)
422 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
423 if (r->refcount == 0)
425 ZOOM_record_cache rc;
427 for (rc = r->record_cache; rc; rc = rc->next)
428 if (rc->rec.wrbuf_marc)
429 wrbuf_free (rc->rec.wrbuf_marc, 1);
432 /* remove ourselves from the resultsets in connection */
433 ZOOM_resultset *rp = &r->connection->resultsets;
436 assert (*rp); /* we must be in this list!! */
438 { /* OK, we're here - take us out of it */
445 ZOOM_query_destroy (r->search);
446 ZOOM_options_destroy (r->options);
447 odr_destroy (r->odr);
453 size_t ZOOM_resultset_size (ZOOM_resultset r)
458 static void do_close (ZOOM_connection c)
464 c->state = STATE_IDLE;
467 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
468 int force_sync, int start, int count)
478 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
479 task->u.retrieve.resultset = r;
480 task->u.retrieve.start = start;
481 task->u.retrieve.count = count;
483 ZOOM_resultset_addref (r);
485 if (!r->connection->async || force_sync)
486 while (r->connection && ZOOM_event (1, &r->connection))
490 void ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
491 size_t start, size_t count)
493 int force_present = 0;
499 ZOOM_resultset_retrieve (r, force_present, start, count);
503 for (i = 0; i< count; i++)
504 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
508 static int do_connect (ZOOM_connection c)
511 const char *effective_host;
514 effective_host = c->proxy;
516 effective_host = c->host_port;
518 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
521 c->cs = cs_create_host (effective_host, 0, &add);
525 int ret = cs_connect (c->cs, add);
526 yaz_log (LOG_DEBUG, "cs_connect returned %d", ret);
529 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
530 ZOOM_connection_put_event(c, event);
531 ZOOM_connection_send_init(c);
532 c->state = STATE_ESTABLISHED;
537 c->state = STATE_CONNECTING;
538 c->mask = ZOOM_SELECT_EXCEPT;
539 if (c->cs->io_pending & CS_WANT_WRITE)
540 c->mask += ZOOM_SELECT_WRITE;
541 if (c->cs->io_pending & CS_WANT_READ)
542 c->mask += ZOOM_SELECT_READ;
546 c->state = STATE_IDLE;
547 c->error = ZOOM_ERROR_CONNECT;
551 int z3950_connection_socket(ZOOM_connection c)
554 return cs_fileno(c->cs);
558 int z3950_connection_mask(ZOOM_connection c)
565 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
570 sprintf (str, "send_APDU t=%p type=%d", c, a->which);
573 Z_OtherInformation **oi;
575 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
577 if (!z_APDU(out, &a, 0, 0))
579 FILE *outf = fopen("/tmp/apdu.txt", "w");
582 ODR odr_pr = odr_createmem(ODR_PRINT);
583 fprintf (outf, "a=%p\n", a);
584 odr_setprint(odr_pr, outf);
585 z_APDU(odr_pr, &a, 0, 0);
589 c->error = ZOOM_ERROR_ENCODE;
597 static int send_APDU (ZOOM_connection c, Z_APDU *a)
601 if (encode_APDU(c, a, c->odr_out))
603 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
604 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
605 ZOOM_connection_put_event (c, event);
606 odr_reset(c->odr_out);
611 static int ZOOM_connection_send_init (ZOOM_connection c)
614 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
615 Z_InitRequest *ireq = apdu->u.initRequest;
616 Z_IdAuthentication *auth = (Z_IdAuthentication *)
617 odr_malloc(c->odr_out, sizeof(*auth));
618 const char *auth_groupId = ZOOM_options_get (c->options, "group");
619 const char *auth_userId = ZOOM_options_get (c->options, "user");
620 const char *auth_password = ZOOM_options_get (c->options, "pass");
622 ODR_MASK_SET(ireq->options, Z_Options_search);
623 ODR_MASK_SET(ireq->options, Z_Options_present);
624 ODR_MASK_SET(ireq->options, Z_Options_scan);
625 ODR_MASK_SET(ireq->options, Z_Options_sort);
626 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
627 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
629 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
630 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
631 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
633 impname = ZOOM_options_get (c->options, "implementationName");
634 ireq->implementationName =
635 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
636 strcpy (ireq->implementationName, "");
639 strcat (ireq->implementationName, impname);
640 strcat (ireq->implementationName, "/");
642 strcat (ireq->implementationName, "ZOOM-C/YAZ");
644 *ireq->maximumRecordSize =
645 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
646 *ireq->preferredMessageSize =
647 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
649 if (auth_groupId || auth_password)
651 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
654 if (auth_groupId && *auth_groupId)
656 pass->groupId = (char *)
657 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
658 strcpy(pass->groupId, auth_groupId);
662 if (auth_userId && *auth_userId)
664 pass->userId = (char *)
665 odr_malloc(c->odr_out, strlen(auth_userId)+1);
666 strcpy(pass->userId, auth_userId);
670 if (auth_password && *auth_password)
672 pass->password = (char *)
673 odr_malloc(c->odr_out, strlen(auth_password)+1);
674 strcpy(pass->password, auth_password);
679 auth->which = Z_IdAuthentication_idPass;
680 auth->u.idPass = pass;
681 ireq->idAuthentication = auth;
684 else if (auth_userId)
686 auth->which = Z_IdAuthentication_open;
687 auth->u.open = (char *)
688 odr_malloc(c->odr_out, strlen(auth_userId)+1);
689 strcpy(auth->u.open, auth_userId);
690 ireq->idAuthentication = auth;
693 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
694 VAL_PROXY, 1, c->host_port);
701 static int ZOOM_connection_send_search (ZOOM_connection c)
704 int lslb, ssub, mspn;
706 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
707 Z_SearchRequest *search_req = apdu->u.searchRequest;
708 const char *elementSetName;
709 const char *smallSetElementSetName;
710 const char *mediumSetElementSetName;
714 assert (c->tasks->which == ZOOM_TASK_SEARCH);
716 r = c->tasks->u.search.resultset;
719 ZOOM_options_get (r->options, "elementSetName");
720 smallSetElementSetName =
721 ZOOM_options_get (r->options, "smallSetElementSetName");
722 mediumSetElementSetName =
723 ZOOM_options_get (r->options, "mediumSetElementSetName");
725 ZOOM_options_get (r->options, "schema");
727 if (!smallSetElementSetName)
728 smallSetElementSetName = elementSetName;
730 if (!mediumSetElementSetName)
731 mediumSetElementSetName = elementSetName;
736 /* prepare query for the search request */
737 search_req->query = r->r_query;
739 search_req->databaseNames =
740 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
742 /* get syntax (no need to provide unless piggyback is in effect) */
743 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
745 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
746 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
747 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
748 if (lslb != -1 && ssub != -1 && mspn != -1)
750 /* So're a Z39.50 expert? Let's hope you don't do sort */
751 *search_req->largeSetLowerBound = lslb;
752 *search_req->smallSetUpperBound = ssub;
753 *search_req->mediumSetPresentNumber = mspn;
755 else if (r->start == 0 && r->count > 0
756 && r->piggyback && !r->r_sort_spec && !schema)
758 /* Regular piggyback - do it unless we're going to do sort */
759 *search_req->largeSetLowerBound = 2000000000;
760 *search_req->smallSetUpperBound = r->count;
761 *search_req->mediumSetPresentNumber = r->count;
762 smallSetElementSetName = 0; /* no need to provide this */
766 /* non-piggyback. Need not provide elementsets or syntaxes .. */
767 smallSetElementSetName = 0;
768 mediumSetElementSetName = 0;
771 if (smallSetElementSetName && *smallSetElementSetName)
773 Z_ElementSetNames *esn = (Z_ElementSetNames *)
774 odr_malloc (c->odr_out, sizeof(*esn));
776 esn->which = Z_ElementSetNames_generic;
777 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
778 search_req->smallSetElementSetNames = esn;
780 if (mediumSetElementSetName && *mediumSetElementSetName)
782 Z_ElementSetNames *esn = (Z_ElementSetNames *)
783 odr_malloc (c->odr_out, sizeof(*esn));
785 esn->which = Z_ElementSetNames_generic;
786 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
787 search_req->mediumSetElementSetNames = esn;
790 search_req->preferredRecordSyntax =
791 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
795 if (c->support_named_resultsets)
799 /* find the lowest unused ordinal so that we re-use
800 result sets on the server. */
801 for (ord = 1; ; ord++)
804 sprintf (setname, "%d", ord);
805 for (rp = c->resultsets; rp; rp = rp->next)
806 if (rp->setname && !strcmp (rp->setname, setname))
811 r->setname = xstrdup (setname);
812 yaz_log (LOG_DEBUG, "allocating %s", r->setname);
815 r->setname = xstrdup ("default");
816 ZOOM_options_set (r->options, "setname", r->setname);
818 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
819 /* send search request */
825 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
827 Z_DefaultDiagFormat *r;
832 if (p->which != Z_DiagRec_defaultFormat)
834 c->error = ZOOM_ERROR_DECODE;
837 r = p->u.defaultFormat;
840 case Z_DefaultDiagFormat_v2Addinfo:
841 addinfo = r->u.v2Addinfo;
843 case Z_DefaultDiagFormat_v3Addinfo:
844 addinfo = r->u.v3Addinfo;
848 c->addinfo = xstrdup (addinfo);
849 c->error = *r->condition;
852 ZOOM_record ZOOM_record_clone (ZOOM_record srec)
859 odr_enc = odr_createmem(ODR_ENCODE);
860 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
862 buf = odr_getbuf (odr_enc, &size, 0);
864 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
865 nrec->odr = odr_createmem(ODR_DECODE);
866 nrec->wrbuf_marc = 0;
867 odr_setbuf (nrec->odr, buf, size, 0);
868 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
870 odr_destroy (odr_enc);
874 ZOOM_record ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
876 return record_cache_lookup (s, pos, 0);
879 ZOOM_record ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
881 ZOOM_resultset_retrieve (r, 1, pos, 1);
882 return ZOOM_resultset_record_immediate (r, pos);
885 void ZOOM_record_destroy (ZOOM_record rec)
890 wrbuf_free (rec->wrbuf_marc, 1);
891 odr_destroy (rec->odr);
895 const char *ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
897 Z_NamePlusRecord *npr;
903 if (!strcmp (type, "database"))
905 return npr->databaseName;
907 else if (!strcmp (type, "syntax"))
909 if (npr->which == Z_NamePlusRecord_databaseRecord)
911 Z_External *r = (Z_External *) npr->u.databaseRecord;
912 oident *ent = oid_getentbyoid(r->direct_reference);
918 else if (!strcmp (type, "render") &&
919 npr->which == Z_NamePlusRecord_databaseRecord)
921 Z_External *r = (Z_External *) npr->u.databaseRecord;
922 oident *ent = oid_getentbyoid(r->direct_reference);
924 if (r->which == Z_External_sutrs)
926 *len = r->u.sutrs->len;
927 return (const char *) r->u.sutrs->buf;
929 else if (r->which == Z_External_octet)
938 case VAL_APPLICATION_XML:
941 if (!rec->wrbuf_marc)
942 rec->wrbuf_marc = wrbuf_alloc();
943 wrbuf_rewind (rec->wrbuf_marc);
944 if (yaz_marc_decode ((const char *)
945 r->u.octet_aligned->buf,
947 r->u.octet_aligned->len,
950 *len = wrbuf_len(rec->wrbuf_marc);
951 return wrbuf_buf(rec->wrbuf_marc);
954 *len = r->u.octet_aligned->len;
955 return (const char *) r->u.octet_aligned->buf;
957 else if (r->which == Z_External_grs1)
964 else if (!strcmp (type, "xml") &&
965 npr->which == Z_NamePlusRecord_databaseRecord)
967 Z_External *r = (Z_External *) npr->u.databaseRecord;
968 oident *ent = oid_getentbyoid(r->direct_reference);
970 if (r->which == Z_External_sutrs)
972 *len = r->u.sutrs->len;
973 return (const char *) r->u.sutrs->buf;
975 else if (r->which == Z_External_octet)
984 case VAL_APPLICATION_XML:
987 if (!rec->wrbuf_marc)
988 rec->wrbuf_marc = wrbuf_alloc();
989 wrbuf_rewind (rec->wrbuf_marc);
990 if (yaz_marc_decode ((const char *)
991 r->u.octet_aligned->buf,
993 r->u.octet_aligned->len,
996 *len = wrbuf_len(rec->wrbuf_marc);
997 return wrbuf_buf(rec->wrbuf_marc);
1000 *len = r->u.octet_aligned->len;
1001 return (const char *) r->u.octet_aligned->buf;
1003 else if (r->which == Z_External_grs1)
1010 else if (!strcmp (type, "raw"))
1012 if (npr->which == Z_NamePlusRecord_databaseRecord)
1014 Z_External *r = (Z_External *) npr->u.databaseRecord;
1016 if (r->which == Z_External_sutrs)
1018 *len = r->u.sutrs->len;
1019 return (const char *) r->u.sutrs->buf;
1021 else if (r->which == Z_External_octet)
1023 *len = r->u.octet_aligned->len;
1024 return (const char *) r->u.octet_aligned->buf;
1026 else /* grs-1, explain, ... */
1029 return (const char *) npr->u.databaseRecord;
1037 static void record_cache_add (ZOOM_resultset r,
1038 Z_NamePlusRecord *npr,
1040 const char *elementSetName)
1042 ZOOM_record_cache rc;
1044 for (rc = r->record_cache; rc; rc = rc->next)
1048 if ((!elementSetName && !rc->elementSetName)
1049 || (elementSetName && rc->elementSetName &&
1050 !strcmp (elementSetName, rc->elementSetName)))
1052 /* not destroying rc->npr (it's handled by nmem )*/
1054 /* keeping wrbuf_marc too */
1059 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1062 rc->rec.wrbuf_marc = 0;
1064 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1066 rc->elementSetName = 0;
1068 rc->next = r->record_cache;
1069 r->record_cache = rc;
1072 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
1074 const char *elementSetName)
1076 ZOOM_record_cache rc;
1078 for (rc = r->record_cache; rc; rc = rc->next)
1082 if ((!elementSetName && !rc->elementSetName)
1083 || (elementSetName && rc->elementSetName &&
1084 !strcmp (elementSetName, rc->elementSetName)))
1091 static void handle_records (ZOOM_connection c, Z_Records *sr,
1094 ZOOM_resultset resultset;
1098 switch (c->tasks->which)
1100 case ZOOM_TASK_SEARCH:
1101 resultset = c->tasks->u.search.resultset;
1103 case ZOOM_TASK_RETRIEVE:
1104 resultset = c->tasks->u.retrieve.resultset;
1109 if (sr && sr->which == Z_Records_NSD)
1111 Z_DiagRec dr, *dr_p = &dr;
1112 dr.which = Z_DiagRec_defaultFormat;
1113 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1115 response_diag (c, dr_p);
1117 else if (sr && sr->which == Z_Records_multipleNSD)
1119 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1120 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1122 c->error = ZOOM_ERROR_DECODE;
1126 if (resultset->count + resultset->start > resultset->size)
1127 resultset->count = resultset->size - resultset->start;
1128 if (resultset->count < 0)
1129 resultset->count = 0;
1130 if (sr && sr->which == Z_Records_DBOSD)
1133 NMEM nmem = odr_extract_mem (c->odr_in);
1134 Z_NamePlusRecordList *p =
1135 sr->u.databaseOrSurDiagnostics;
1136 for (i = 0; i<p->num_records; i++)
1138 record_cache_add (resultset, p->records[i],
1139 i+ resultset->start, 0);
1141 /* transfer our response to search_nmem .. we need it later */
1142 nmem_transfer (resultset->odr->mem, nmem);
1143 nmem_destroy (nmem);
1144 if (present_phase && p->num_records == 0)
1146 /* present response and we didn't get any records! */
1147 c->error = ZOOM_ERROR_DECODE;
1150 else if (present_phase)
1152 /* present response and we didn't get any records! */
1153 c->error = ZOOM_ERROR_DECODE;
1158 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1160 handle_records (c, pr->records, 1);
1163 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1165 ZOOM_resultset resultset;
1167 yaz_log (LOG_DEBUG, "got search response");
1169 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1172 resultset = c->tasks->u.search.resultset;
1174 resultset->size = *sr->resultCount;
1175 handle_records (c, sr->records, 0);
1178 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1180 if (res->diagnostics && res->num_diagnostics > 0)
1181 response_diag (c, res->diagnostics[0]);
1184 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1186 NMEM nmem = odr_extract_mem (c->odr_in);
1189 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1191 scan = c->tasks->u.scan.scan;
1193 if (res->entries && res->entries->nonsurrogateDiagnostics)
1194 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1195 scan->scan_response = res;
1196 nmem_transfer (scan->odr->mem, nmem);
1198 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1199 if (res->positionOfTerm)
1200 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1201 if (res->scanStatus)
1202 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1203 if (res->numberOfEntriesReturned)
1204 ZOOM_options_set_int (scan->options, "number",
1205 *res->numberOfEntriesReturned);
1206 nmem_destroy (nmem);
1210 static int send_sort (ZOOM_connection c)
1212 ZOOM_resultset resultset;
1214 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1217 resultset = c->tasks->u.search.resultset;
1221 resultset->r_sort_spec = 0;
1224 if (resultset->r_sort_spec)
1226 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1227 Z_SortRequest *req = apdu->u.sortRequest;
1229 req->num_inputResultSetNames = 1;
1230 req->inputResultSetNames = (Z_InternationalString **)
1231 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1232 req->inputResultSetNames[0] =
1233 odr_strdup (c->odr_out, resultset->setname);
1234 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1235 req->sortSequence = resultset->r_sort_spec;
1236 resultset->r_sort_spec = 0;
1237 send_APDU (c, apdu);
1243 static int send_present (ZOOM_connection c)
1245 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1246 Z_PresentRequest *req = apdu->u.presentRequest;
1248 const char *syntax =
1249 ZOOM_options_get (c->options, "preferredRecordSyntax");
1250 const char *element =
1251 ZOOM_options_get (c->options, "elementSetName");
1252 const char *schema =
1253 ZOOM_options_get (c->options, "schema");
1254 ZOOM_resultset resultset;
1259 switch (c->tasks->which)
1261 case ZOOM_TASK_SEARCH:
1262 resultset = c->tasks->u.search.resultset;
1264 case ZOOM_TASK_RETRIEVE:
1265 resultset = c->tasks->u.retrieve.resultset;
1266 resultset->start = c->tasks->u.retrieve.start;
1267 resultset->count = c->tasks->u.retrieve.count;
1269 if (resultset->start >= resultset->size)
1271 if (resultset->start + resultset->count > resultset->size)
1272 resultset->count = resultset->size - resultset->start;
1278 if (c->error) /* don't continue on error */
1280 if (resultset->start < 0)
1282 for (i = 0; i<resultset->count; i++)
1285 record_cache_lookup (resultset, i + resultset->start, 0);
1289 if (i == resultset->count)
1292 resultset->start += i;
1293 resultset->count -= i;
1294 *req->resultSetStartPoint = resultset->start + 1;
1295 *req->numberOfRecordsRequested = resultset->count;
1296 assert (*req->numberOfRecordsRequested > 0);
1298 if (syntax && *syntax)
1299 req->preferredRecordSyntax =
1300 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1302 if (schema && *schema)
1304 Z_RecordComposition *compo = (Z_RecordComposition *)
1305 odr_malloc (c->odr_out, sizeof(*compo));
1307 req->recordComposition = compo;
1308 compo->which = Z_RecordComp_complex;
1309 compo->u.complex = (Z_CompSpec *)
1310 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1311 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1312 odr_malloc(c->odr_out, sizeof(bool_t));
1313 *compo->u.complex->selectAlternativeSyntax = 0;
1315 compo->u.complex->generic = (Z_Specification *)
1316 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1318 compo->u.complex->generic->schema = (Odr_oid *)
1319 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1321 if (!compo->u.complex->generic->schema)
1323 /* OID wasn't a schema! Try record syntax instead. */
1325 compo->u.complex->generic->schema = (Odr_oid *)
1326 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1328 if (element && *element)
1330 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1331 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1332 compo->u.complex->generic->elementSpec->which =
1333 Z_ElementSpec_elementSetName;
1334 compo->u.complex->generic->elementSpec->u.elementSetName =
1335 odr_strdup (c->odr_out, element);
1338 compo->u.complex->generic->elementSpec = 0;
1339 compo->u.complex->num_dbSpecific = 0;
1340 compo->u.complex->dbSpecific = 0;
1341 compo->u.complex->num_recordSyntax = 0;
1342 compo->u.complex->recordSyntax = 0;
1344 else if (element && *element)
1346 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1347 odr_malloc (c->odr_out, sizeof(*esn));
1348 Z_RecordComposition *compo = (Z_RecordComposition *)
1349 odr_malloc (c->odr_out, sizeof(*compo));
1351 esn->which = Z_ElementSetNames_generic;
1352 esn->u.generic = odr_strdup (c->odr_out, element);
1353 compo->which = Z_RecordComp_simple;
1354 compo->u.simple = esn;
1355 req->recordComposition = compo;
1357 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1358 send_APDU (c, apdu);
1362 ZOOM_scanset ZOOM_connection_scan (ZOOM_connection c, const char *start)
1364 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1366 scan->connection = c;
1367 scan->odr = odr_createmem (ODR_DECODE);
1368 scan->options = ZOOM_options_create_with_parent (c->options);
1370 scan->scan_response = 0;
1372 if ((scan->termListAndStartPoint =
1373 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1376 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1377 task->u.scan.scan = scan;
1382 while (ZOOM_event (1, &c))
1389 void ZOOM_scanset_destroy (ZOOM_scanset scan)
1394 if (scan->refcount == 0)
1396 odr_destroy (scan->odr);
1398 ZOOM_options_destroy (scan->options);
1403 int send_scan (ZOOM_connection c)
1406 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1407 Z_ScanRequest *req = apdu->u.scanRequest;
1410 assert (c->tasks->which == ZOOM_TASK_SCAN);
1411 scan = c->tasks->u.scan.scan;
1413 req->termListAndStartPoint = scan->termListAndStartPoint;
1414 req->attributeSet = scan->attributeSet;
1416 *req->numberOfTermsRequested =
1417 ZOOM_options_get_int(scan->options, "number", 10);
1419 req->preferredPositionInResponse =
1420 odr_intdup (c->odr_out,
1421 ZOOM_options_get_int(scan->options, "position", 1));
1424 odr_intdup (c->odr_out,
1425 ZOOM_options_get_int(scan->options, "stepSize", 0));
1427 req->databaseNames = set_DatabaseNames (c, scan->options,
1428 &req->num_databaseNames);
1430 send_APDU (c, apdu);
1435 size_t ZOOM_scanset_size (ZOOM_scanset scan)
1437 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1439 return scan->scan_response->entries->num_entries;
1442 const char *ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1445 const char *term = 0;
1446 size_t noent = ZOOM_scanset_size (scan);
1447 Z_ScanResponse *res = scan->scan_response;
1453 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1455 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1457 if (t->term->which == Z_Term_general)
1459 term = (const char *) t->term->u.general->buf;
1460 *len = t->term->u.general->len;
1462 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1467 const char *ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1469 return ZOOM_options_get (scan->options, key);
1472 void ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1475 ZOOM_options_set (scan->options, key, val);
1478 static int ZOOM_connection_exec_task (ZOOM_connection c)
1480 ZOOM_task task = c->tasks;
1482 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1485 if (c->error != ZOOM_ERROR_NONE ||
1486 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1488 ZOOM_connection_remove_tasks (c);
1491 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d", task->which);
1495 switch (task->which)
1497 case ZOOM_TASK_SEARCH:
1498 /* see if search hasn't been sent yet. */
1499 if (ZOOM_connection_send_search (c))
1502 case ZOOM_TASK_RETRIEVE:
1503 if (send_present (c))
1506 case ZOOM_TASK_CONNECT:
1510 case ZOOM_TASK_SCAN:
1514 ZOOM_connection_remove_task (c);
1518 static int send_sort_present (ZOOM_connection c)
1520 int r = send_sort (c);
1522 r = send_present (c);
1526 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1528 Z_InitResponse *initrs;
1530 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1534 case Z_APDU_initResponse:
1535 initrs = apdu->u.initResponse;
1536 if (!*initrs->result)
1538 c->error = ZOOM_ERROR_INIT;
1543 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1545 xfree (c->cookie_in);
1548 c->cookie_in = xstrdup(cookie);
1549 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1550 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1551 c->support_named_resultsets = 1;
1554 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1555 ZOOM_connection_remove_task (c);
1557 ZOOM_connection_exec_task (c);
1560 case Z_APDU_searchResponse:
1561 handle_search_response (c, apdu->u.searchResponse);
1562 if (!send_sort_present (c))
1563 ZOOM_connection_remove_task (c);
1565 case Z_APDU_presentResponse:
1566 handle_present_response (c, apdu->u.presentResponse);
1567 if (!send_present (c))
1568 ZOOM_connection_remove_task (c);
1570 case Z_APDU_sortResponse:
1571 sort_response (c, apdu->u.sortResponse);
1572 if (!send_present (c))
1573 ZOOM_connection_remove_task (c);
1575 case Z_APDU_scanResponse:
1576 scan_response (c, apdu->u.scanResponse);
1577 ZOOM_connection_remove_task (c);
1581 static int do_read (ZOOM_connection c)
1587 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
1588 ZOOM_connection_put_event (c, event);
1590 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1595 c->error= ZOOM_ERROR_CONNECTION_LOST;
1601 odr_reset (c->odr_in);
1602 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1603 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
1604 ZOOM_connection_put_event (c, event);
1605 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1607 c->error = ZOOM_ERROR_DECODE;
1612 handle_apdu (c, apdu);
1618 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
1623 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1624 ZOOM_connection_put_event (c, event);
1626 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1628 if (c->state == STATE_CONNECTING)
1629 c->error = ZOOM_ERROR_CONNECT;
1631 c->error = ZOOM_ERROR_CONNECTION_LOST;
1637 c->mask = ZOOM_SELECT_EXCEPT;
1638 if (c->cs->io_pending & CS_WANT_WRITE)
1639 c->mask += ZOOM_SELECT_WRITE;
1640 if (c->cs->io_pending & CS_WANT_READ)
1641 c->mask += ZOOM_SELECT_READ;
1642 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
1646 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
1647 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
1652 static int do_write(ZOOM_connection c)
1654 return do_write_ex (c, c->buf_out, c->len_out);
1658 const char *ZOOM_connection_option_get (ZOOM_connection c, const char *key)
1660 return ZOOM_options_get (c->options, key);
1663 void ZOOM_connection_option_set (ZOOM_connection c, const char *key,
1666 ZOOM_options_set (c->options, key, val);
1669 const char *ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
1671 return ZOOM_options_get (r->options, key);
1674 void ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
1677 ZOOM_options_set (r->options, key, val);
1681 int ZOOM_connection_errcode (ZOOM_connection c)
1683 return ZOOM_connection_error (c, 0, 0);
1686 const char *ZOOM_connection_errmsg (ZOOM_connection c)
1689 ZOOM_connection_error (c, &msg, 0);
1693 const char *ZOOM_connection_addinfo (ZOOM_connection c)
1695 const char *addinfo;
1696 ZOOM_connection_error (c, 0, &addinfo);
1700 int ZOOM_connection_error (ZOOM_connection c, const char **cp,
1701 const char **addinfo)
1703 int error = c->error;
1708 case ZOOM_ERROR_NONE:
1709 *cp = "No error"; break;
1710 case ZOOM_ERROR_CONNECT:
1711 *cp = "Connect failed"; break;
1712 case ZOOM_ERROR_MEMORY:
1713 *cp = "Out of memory"; break;
1714 case ZOOM_ERROR_ENCODE:
1715 *cp = "Encoding failed"; break;
1716 case ZOOM_ERROR_DECODE:
1717 *cp = "Decoding failed"; break;
1718 case ZOOM_ERROR_CONNECTION_LOST:
1719 *cp = "Connection lost"; break;
1720 case ZOOM_ERROR_INIT:
1721 *cp = "Init rejected"; break;
1722 case ZOOM_ERROR_INTERNAL:
1723 *cp = "Internal failure"; break;
1724 case ZOOM_ERROR_TIMEOUT:
1725 *cp = "Timeout"; break;
1727 *cp = diagbib1_str (error);
1733 *addinfo = c->addinfo;
1740 int ZOOM_connection_do_io(ZOOM_connection c, int mask)
1742 ZOOM_Event event = 0;
1743 int r = cs_look(c->cs);
1744 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
1749 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
1750 c->error = ZOOM_ERROR_CONNECT;
1752 ZOOM_connection_put_event (c, event);
1754 else if (r == CS_CONNECT)
1757 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
1759 ret = cs_rcvconnect (c->cs);
1760 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
1763 c->mask = ZOOM_SELECT_EXCEPT;
1764 if (c->cs->io_pending & CS_WANT_WRITE)
1765 c->mask += ZOOM_SELECT_WRITE;
1766 if (c->cs->io_pending & CS_WANT_READ)
1767 c->mask += ZOOM_SELECT_READ;
1768 ZOOM_connection_put_event (c, event);
1772 ZOOM_connection_put_event (c, event);
1773 ZOOM_connection_send_init (c);
1774 c->state = STATE_ESTABLISHED;
1778 c->error = ZOOM_ERROR_CONNECT;
1780 ZOOM_connection_put_event (c, event);
1785 if (mask & ZOOM_SELECT_READ)
1787 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1793 int ZOOM_connection_last_event(ZOOM_connection cs)
1796 return ZOOM_EVENT_NONE;
1797 return cs->last_event;
1800 int ZOOM_event (int no, ZOOM_connection *cs)
1803 struct pollfd pollfds[1024];
1804 ZOOM_connection poll_cs[1024];
1807 fd_set input, output, except;
1812 for (i = 0; i<no; i++)
1814 ZOOM_connection c = cs[i];
1816 if (c && (event = ZOOM_connection_get_event(c)))
1818 ZOOM_Event_destroy (event);
1822 for (i = 0; i<no; i++)
1824 ZOOM_connection c = cs[i];
1826 if (c && ZOOM_connection_exec_task (c))
1828 if ((event = ZOOM_connection_get_event(c)))
1830 ZOOM_Event_destroy (event);
1846 for (i = 0; i<no; i++)
1848 ZOOM_connection c = cs[i];
1853 fd = z3950_connection_socket(c);
1854 mask = z3950_connection_mask(c);
1864 short poll_events = 0;
1866 if (mask & ZOOM_SELECT_READ)
1867 poll_events += POLLIN;
1868 if (mask & ZOOM_SELECT_WRITE)
1869 poll_events += POLLOUT;
1870 if (mask & ZOOM_SELECT_EXCEPT)
1871 poll_events += POLLERR;
1872 pollfds[nfds].fd = fd;
1873 pollfds[nfds].events = poll_events;
1874 pollfds[nfds].revents = 0;
1879 if (mask & ZOOM_SELECT_READ)
1881 FD_SET (fd, &input);
1884 if (mask & ZOOM_SELECT_WRITE)
1886 FD_SET (fd, &output);
1889 if (mask & ZOOM_SELECT_EXCEPT)
1891 FD_SET (fd, &except);
1899 r = poll (pollfds, nfds, 15000);
1900 for (i = 0; i<nfds; i++)
1902 ZOOM_connection c = poll_cs[i];
1906 if (pollfds[i].revents & POLLIN)
1907 mask += ZOOM_SELECT_READ;
1908 if (pollfds[i].revents & POLLOUT)
1909 mask += ZOOM_SELECT_WRITE;
1910 if (pollfds[i].revents & POLLERR)
1911 mask += ZOOM_SELECT_EXCEPT;
1913 ZOOM_connection_do_io(c, mask);
1915 else if (r == 0 && c->mask)
1917 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
1918 /* timeout and this connection was waiting */
1919 c->error = ZOOM_ERROR_TIMEOUT;
1921 ZOOM_connection_put_event(c, event);
1925 yaz_log (LOG_DEBUG, "select start");
1926 r = select (max_fd+1, &input, &output, &except, &tv);
1927 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
1928 for (i = 0; i<no; i++)
1930 ZOOM_connection c = cs[i];
1935 fd = z3950_connection_socket(c);
1939 /* no timeout and real socket */
1940 if (FD_ISSET(fd, &input))
1941 mask += ZOOM_SELECT_READ;
1942 if (FD_ISSET(fd, &output))
1943 mask += ZOOM_SELECT_WRITE;
1944 if (FD_ISSET(fd, &except))
1945 mask += ZOOM_SELECT_EXCEPT;
1947 ZOOM_connection_do_io(c, mask);
1949 if (r == 0 && c->mask)
1951 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
1952 /* timeout and this connection was waiting */
1953 c->error = ZOOM_ERROR_TIMEOUT;
1955 yaz_log (LOG_DEBUG, "timeout");
1956 ZOOM_connection_put_event(c, event);
1960 for (i = 0; i<no; i++)
1962 ZOOM_connection c = cs[i];
1964 if (c && (event = ZOOM_connection_get_event(c)))
1966 ZOOM_Event_destroy (event);