2 * $Id: zoom-c.c,v 1.23 2002-01-28 09:27:48 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 = (ZOOM_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)
37 c->m_queue_back->prev = event;
38 assert (c->m_queue_front);
42 assert (!c->m_queue_front);
43 c->m_queue_front = event;
45 event->next = c->m_queue_back;
47 c->m_queue_back = event;
50 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
52 ZOOM_Event event = c->m_queue_front;
55 assert (c->m_queue_back);
56 c->m_queue_front = event->prev;
59 assert (c->m_queue_back);
60 c->m_queue_front->next = 0;
64 c->last_event = event->kind;
68 static void clear_error (ZOOM_connection c)
73 case ZOOM_ERROR_CONNECT:
74 case ZOOM_ERROR_MEMORY:
75 case ZOOM_ERROR_DECODE:
76 case ZOOM_ERROR_CONNECTION_LOST:
78 case ZOOM_ERROR_INTERNAL:
81 c->error = ZOOM_ERROR_NONE;
87 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
89 ZOOM_task *taskp = &c->tasks;
91 taskp = &(*taskp)->next;
92 *taskp = (ZOOM_task) xmalloc (sizeof(**taskp));
93 (*taskp)->running = 0;
94 (*taskp)->which = which;
100 void ZOOM_connection_remove_task (ZOOM_connection c)
102 ZOOM_task task = c->tasks;
106 c->tasks = task->next;
109 case ZOOM_TASK_SEARCH:
110 ZOOM_resultset_destroy (task->u.search.resultset);
112 case ZOOM_TASK_RETRIEVE:
113 ZOOM_resultset_destroy (task->u.retrieve.resultset);
115 case ZOOM_TASK_CONNECT:
118 ZOOM_scanset_destroy (task->u.scan.scan);
127 void ZOOM_connection_remove_tasks (ZOOM_connection c)
130 ZOOM_connection_remove_task(c);
133 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
135 const char *elementSetName);
137 ZOOM_connection ZOOM_connection_create (ZOOM_options options)
139 ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
143 c->state = STATE_IDLE;
144 c->error = ZOOM_ERROR_NONE;
152 c->options = ZOOM_options_create_with_parent(options);
161 c->odr_in = odr_createmem (ODR_DECODE);
162 c->odr_out = odr_createmem (ODR_ENCODE);
165 c->support_named_resultsets = 0;
166 c->last_event = ZOOM_EVENT_NONE;
168 c->m_queue_front = 0;
173 /* set database names. Take local databases (if set); otherwise
174 take databases given in ZURL (if set); otherwise use Default */
175 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
178 char **databaseNames;
181 const char *cp = ZOOM_options_get (options, "databaseName");
185 cp = strchr (con->host_port, '/');
192 while ((c = strchr(c, '+')))
200 databaseNames = (char**)
201 odr_malloc (con->odr_out, no * sizeof(*databaseNames));
205 c = strchr (cp, '+');
213 /* cp ptr to first char of db name, c is char
215 databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
216 memcpy (databaseNames[no], cp, c-cp);
217 databaseNames[no++][c-cp] = '\0';
222 databaseNames[no] = NULL;
224 return databaseNames;
227 ZOOM_connection ZOOM_connection_new (const char *host, int portnum)
229 ZOOM_connection c = ZOOM_connection_create (0);
231 ZOOM_connection_connect (c, host, portnum);
235 void ZOOM_connection_connect(ZOOM_connection c,
236 const char *host, int portnum)
241 val = ZOOM_options_get (c->options, "proxy");
243 c->proxy = xstrdup (val);
250 sprintf (hostn, "%.80s:%d", host, portnum);
251 c->host_port = xstrdup(hostn);
254 c->host_port = xstrdup(host);
256 ZOOM_options_set(c->options, "host", c->host_port);
258 c->async = ZOOM_options_get_bool (c->options, "async", 0);
260 c->error = ZOOM_ERROR_NONE;
262 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
266 while (ZOOM_event (1, &c))
271 ZOOM_query ZOOM_query_create(void)
273 ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
278 s->odr = odr_createmem (ODR_ENCODE);
283 void ZOOM_query_destroy(ZOOM_query s)
289 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
290 if (s->refcount == 0)
292 odr_destroy (s->odr);
297 int ZOOM_query_prefix(ZOOM_query s, const char *str)
299 s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query));
300 s->query->which = Z_Query_type_1;
301 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
302 if (!s->query->u.type_1)
307 int ZOOM_query_sortby(ZOOM_query s, const char *criteria)
309 s->sort_spec = yaz_sort_spec (s->odr, criteria);
315 static int do_write(ZOOM_connection c);
317 void ZOOM_connection_destroy(ZOOM_connection c)
324 for (r = c->resultsets; r; r = r->next)
329 odr_destroy (c->odr_in);
330 odr_destroy (c->odr_out);
331 ZOOM_options_destroy (c->options);
332 ZOOM_connection_remove_tasks (c);
333 xfree (c->host_port);
337 void ZOOM_resultset_addref (ZOOM_resultset r)
342 ZOOM_resultset ZOOM_resultset_create ()
344 ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
348 r->odr = odr_createmem (ODR_ENCODE);
362 ZOOM_resultset ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
365 ZOOM_query s = ZOOM_query_create();
367 ZOOM_query_prefix (s, q);
369 r = ZOOM_connection_search (c, s);
370 ZOOM_query_destroy (s);
374 ZOOM_resultset ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
376 ZOOM_resultset r = ZOOM_resultset_create ();
380 r->r_sort_spec = q->sort_spec;
381 r->r_query = q->query;
384 r->options = ZOOM_options_create_with_parent(c->options);
386 r->start = ZOOM_options_get_int(r->options, "start", 0);
387 r->count = ZOOM_options_get_int(r->options, "count", 0);
388 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
389 cp = ZOOM_options_get (r->options, "setname");
391 r->setname = xstrdup (cp);
395 r->next = c->resultsets;
398 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
399 task->u.search.resultset = r;
400 ZOOM_resultset_addref (r);
406 while (ZOOM_event (1, &c))
412 void ZOOM_resultset_destroy(ZOOM_resultset r)
417 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
418 if (r->refcount == 0)
420 ZOOM_record_cache rc;
422 for (rc = r->record_cache; rc; rc = rc->next)
423 if (rc->rec.wrbuf_marc)
424 wrbuf_free (rc->rec.wrbuf_marc, 1);
427 /* remove ourselves from the resultsets in connection */
428 ZOOM_resultset *rp = &r->connection->resultsets;
431 assert (*rp); /* we must be in this list!! */
433 { /* OK, we're here - take us out of it */
440 ZOOM_query_destroy (r->search);
441 ZOOM_options_destroy (r->options);
442 odr_destroy (r->odr);
448 size_t ZOOM_resultset_size (ZOOM_resultset r)
453 static void do_close (ZOOM_connection c)
459 c->state = STATE_IDLE;
462 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
463 int force_sync, int start, int count)
473 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
474 task->u.retrieve.resultset = r;
475 task->u.retrieve.start = start;
476 task->u.retrieve.count = count;
478 ZOOM_resultset_addref (r);
480 if (!r->connection->async || force_sync)
481 while (r->connection && ZOOM_event (1, &r->connection))
485 void ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
486 size_t start, size_t count)
488 int force_present = 0;
494 ZOOM_resultset_retrieve (r, force_present, start, count);
498 for (i = 0; i< count; i++)
499 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
503 static int do_connect (ZOOM_connection c)
506 const char *effective_host;
509 effective_host = c->proxy;
511 effective_host = c->host_port;
513 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
516 c->cs = cs_create_host (effective_host, 0, &add);
520 int ret = cs_connect (c->cs, add);
521 yaz_log (LOG_DEBUG, "cs_connect returned %d", ret);
524 c->state = STATE_CONNECTING;
525 c->mask = ZOOM_SELECT_EXCEPT;
526 if (c->cs->io_pending & CS_WANT_WRITE)
527 c->mask += ZOOM_SELECT_WRITE;
528 if (c->cs->io_pending & CS_WANT_READ)
529 c->mask += ZOOM_SELECT_READ;
533 c->state = STATE_IDLE;
534 c->error = ZOOM_ERROR_CONNECT;
538 int z3950_connection_socket(ZOOM_connection c)
541 return cs_fileno(c->cs);
545 int z3950_connection_mask(ZOOM_connection c)
552 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
557 sprintf (str, "send_APDU t=%p type=%d", c, a->which);
560 Z_OtherInformation **oi;
562 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
564 if (!z_APDU(out, &a, 0, 0))
566 FILE *outf = fopen("/tmp/apdu.txt", "w");
569 ODR odr_pr = odr_createmem(ODR_PRINT);
570 fprintf (outf, "a=%p\n", a);
571 odr_setprint(odr_pr, outf);
572 z_APDU(odr_pr, &a, 0, 0);
576 c->error = ZOOM_ERROR_ENCODE;
584 static int send_APDU (ZOOM_connection c, Z_APDU *a)
588 if (encode_APDU(c, a, c->odr_out))
590 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
591 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
592 ZOOM_connection_put_event (c, event);
593 odr_reset(c->odr_out);
598 static int ZOOM_connection_send_init (ZOOM_connection c)
601 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
602 Z_InitRequest *ireq = apdu->u.initRequest;
603 Z_IdAuthentication *auth = (Z_IdAuthentication *)
604 odr_malloc(c->odr_out, sizeof(*auth));
605 const char *auth_groupId = ZOOM_options_get (c->options, "group");
606 const char *auth_userId = ZOOM_options_get (c->options, "user");
607 const char *auth_password = ZOOM_options_get (c->options, "pass");
609 ODR_MASK_SET(ireq->options, Z_Options_search);
610 ODR_MASK_SET(ireq->options, Z_Options_present);
611 ODR_MASK_SET(ireq->options, Z_Options_scan);
612 ODR_MASK_SET(ireq->options, Z_Options_sort);
613 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
614 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
616 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
617 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
618 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
620 impname = ZOOM_options_get (c->options, "implementationName");
621 ireq->implementationName =
622 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
623 strcpy (ireq->implementationName, "");
626 strcat (ireq->implementationName, impname);
627 strcat (ireq->implementationName, "/");
629 strcat (ireq->implementationName, "ZOOM-C/YAZ");
631 *ireq->maximumRecordSize =
632 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
633 *ireq->preferredMessageSize =
634 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
636 if (auth_groupId || auth_password)
638 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
641 if (auth_groupId && *auth_groupId)
643 pass->groupId = (char *)
644 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
645 strcpy(pass->groupId, auth_groupId);
649 if (auth_userId && *auth_userId)
651 pass->userId = (char *)
652 odr_malloc(c->odr_out, strlen(auth_userId)+1);
653 strcpy(pass->userId, auth_userId);
657 if (auth_password && *auth_password)
659 pass->password = (char *)
660 odr_malloc(c->odr_out, strlen(auth_password)+1);
661 strcpy(pass->password, auth_password);
666 auth->which = Z_IdAuthentication_idPass;
667 auth->u.idPass = pass;
668 ireq->idAuthentication = auth;
671 else if (auth_userId)
673 auth->which = Z_IdAuthentication_open;
674 auth->u.open = (char *)
675 odr_malloc(c->odr_out, strlen(auth_userId)+1);
676 strcpy(auth->u.open, auth_userId);
677 ireq->idAuthentication = auth;
680 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
681 VAL_PROXY, 1, c->host_port);
688 static int ZOOM_connection_send_search (ZOOM_connection c)
691 int lslb, ssub, mspn;
693 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
694 Z_SearchRequest *search_req = apdu->u.searchRequest;
695 const char *elementSetName;
696 const char *smallSetElementSetName;
697 const char *mediumSetElementSetName;
701 assert (c->tasks->which == ZOOM_TASK_SEARCH);
703 r = c->tasks->u.search.resultset;
706 ZOOM_options_get (r->options, "elementSetName");
707 smallSetElementSetName =
708 ZOOM_options_get (r->options, "smallSetElementSetName");
709 mediumSetElementSetName =
710 ZOOM_options_get (r->options, "mediumSetElementSetName");
712 ZOOM_options_get (r->options, "schema");
714 if (!smallSetElementSetName)
715 smallSetElementSetName = elementSetName;
717 if (!mediumSetElementSetName)
718 mediumSetElementSetName = elementSetName;
723 /* prepare query for the search request */
724 search_req->query = r->r_query;
726 search_req->databaseNames =
727 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
729 /* get syntax (no need to provide unless piggyback is in effect) */
730 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
732 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
733 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
734 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
735 if (lslb != -1 && ssub != -1 && mspn != -1)
737 /* So're a Z39.50 expert? Let's hope you don't do sort */
738 *search_req->largeSetLowerBound = lslb;
739 *search_req->smallSetUpperBound = ssub;
740 *search_req->mediumSetPresentNumber = mspn;
742 else if (r->start == 0 && r->count > 0
743 && r->piggyback && !r->r_sort_spec && !schema)
745 /* Regular piggyback - do it unless we're going to do sort */
746 *search_req->largeSetLowerBound = 2000000000;
747 *search_req->smallSetUpperBound = r->count;
748 *search_req->mediumSetPresentNumber = r->count;
749 smallSetElementSetName = 0; /* no need to provide this */
753 /* non-piggyback. Need not provide elementsets or syntaxes .. */
754 smallSetElementSetName = 0;
755 mediumSetElementSetName = 0;
758 if (smallSetElementSetName && *smallSetElementSetName)
760 Z_ElementSetNames *esn = (Z_ElementSetNames *)
761 odr_malloc (c->odr_out, sizeof(*esn));
763 esn->which = Z_ElementSetNames_generic;
764 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
765 search_req->smallSetElementSetNames = esn;
767 if (mediumSetElementSetName && *mediumSetElementSetName)
769 Z_ElementSetNames *esn = (Z_ElementSetNames *)
770 odr_malloc (c->odr_out, sizeof(*esn));
772 esn->which = Z_ElementSetNames_generic;
773 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
774 search_req->mediumSetElementSetNames = esn;
777 search_req->preferredRecordSyntax =
778 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
782 if (c->support_named_resultsets)
786 /* find the lowest unused ordinal so that we re-use
787 result sets on the server. */
788 for (ord = 1; ; ord++)
791 sprintf (setname, "%d", ord);
792 for (rp = c->resultsets; rp; rp = rp->next)
793 if (rp->setname && !strcmp (rp->setname, setname))
798 r->setname = xstrdup (setname);
799 yaz_log (LOG_DEBUG, "allocating %s", r->setname);
802 r->setname = xstrdup ("default");
803 ZOOM_options_set (r->options, "setname", r->setname);
805 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
806 /* send search request */
812 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
814 Z_DefaultDiagFormat *r;
819 if (p->which != Z_DiagRec_defaultFormat)
821 c->error = ZOOM_ERROR_DECODE;
824 r = p->u.defaultFormat;
827 case Z_DefaultDiagFormat_v2Addinfo:
828 addinfo = r->u.v2Addinfo;
830 case Z_DefaultDiagFormat_v3Addinfo:
831 addinfo = r->u.v3Addinfo;
835 c->addinfo = xstrdup (addinfo);
836 c->error = *r->condition;
839 ZOOM_record ZOOM_record_clone (ZOOM_record srec)
846 odr_enc = odr_createmem(ODR_ENCODE);
847 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
849 buf = odr_getbuf (odr_enc, &size, 0);
851 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
852 nrec->odr = odr_createmem(ODR_DECODE);
853 nrec->wrbuf_marc = 0;
854 odr_setbuf (nrec->odr, buf, size, 0);
855 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
857 odr_destroy (odr_enc);
861 ZOOM_record ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
863 return record_cache_lookup (s, pos, 0);
866 ZOOM_record ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
868 ZOOM_resultset_retrieve (r, 1, pos, 1);
869 return ZOOM_resultset_record_immediate (r, pos);
872 void ZOOM_record_destroy (ZOOM_record rec)
877 wrbuf_free (rec->wrbuf_marc, 1);
878 odr_destroy (rec->odr);
882 const char *ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
884 Z_NamePlusRecord *npr;
890 if (!strcmp (type, "database"))
892 return npr->databaseName;
894 else if (!strcmp (type, "syntax"))
896 if (npr->which == Z_NamePlusRecord_databaseRecord)
898 Z_External *r = (Z_External *) npr->u.databaseRecord;
899 oident *ent = oid_getentbyoid(r->direct_reference);
905 else if (!strcmp (type, "render"))
907 if (npr->which == Z_NamePlusRecord_databaseRecord)
909 Z_External *r = (Z_External *) npr->u.databaseRecord;
910 oident *ent = oid_getentbyoid(r->direct_reference);
912 if (r->which == Z_External_sutrs)
914 *len = r->u.sutrs->len;
915 return (const char *) r->u.sutrs->buf;
917 else if (r->which == Z_External_octet)
926 case VAL_APPLICATION_XML:
929 if (!rec->wrbuf_marc)
930 rec->wrbuf_marc = wrbuf_alloc();
931 wrbuf_rewind (rec->wrbuf_marc);
932 if (marc_display_wrbuf ((const char *)
933 r->u.octet_aligned->buf,
935 r->u.octet_aligned->len) > 0)
937 *len = wrbuf_len(rec->wrbuf_marc);
938 return wrbuf_buf(rec->wrbuf_marc);
941 *len = r->u.octet_aligned->len;
942 return (const char *) r->u.octet_aligned->buf;
944 else if (r->which == Z_External_grs1)
952 else if (!strcmp (type, "raw"))
954 if (npr->which == Z_NamePlusRecord_databaseRecord)
956 Z_External *r = (Z_External *) npr->u.databaseRecord;
958 if (r->which == Z_External_sutrs)
960 *len = r->u.sutrs->len;
961 return (const char *) r->u.sutrs->buf;
963 else if (r->which == Z_External_octet)
965 *len = r->u.octet_aligned->len;
966 return (const char *) r->u.octet_aligned->buf;
968 else /* grs-1, explain, ... */
971 return (const char *) npr->u.databaseRecord;
979 static void record_cache_add (ZOOM_resultset r,
980 Z_NamePlusRecord *npr,
982 const char *elementSetName)
984 ZOOM_record_cache rc;
986 for (rc = r->record_cache; rc; rc = rc->next)
990 if ((!elementSetName && !rc->elementSetName)
991 || (elementSetName && rc->elementSetName &&
992 !strcmp (elementSetName, rc->elementSetName)))
994 /* not destroying rc->npr (it's handled by nmem )*/
996 /* keeping wrbuf_marc too */
1001 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1004 rc->rec.wrbuf_marc = 0;
1006 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1008 rc->elementSetName = 0;
1010 rc->next = r->record_cache;
1011 r->record_cache = rc;
1014 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
1016 const char *elementSetName)
1018 ZOOM_record_cache rc;
1020 for (rc = r->record_cache; rc; rc = rc->next)
1024 if ((!elementSetName && !rc->elementSetName)
1025 || (elementSetName && rc->elementSetName &&
1026 !strcmp (elementSetName, rc->elementSetName)))
1033 static void handle_records (ZOOM_connection c, Z_Records *sr,
1036 ZOOM_resultset resultset;
1040 switch (c->tasks->which)
1042 case ZOOM_TASK_SEARCH:
1043 resultset = c->tasks->u.search.resultset;
1045 case ZOOM_TASK_RETRIEVE:
1046 resultset = c->tasks->u.retrieve.resultset;
1051 if (sr && sr->which == Z_Records_NSD)
1053 Z_DiagRec dr, *dr_p = &dr;
1054 dr.which = Z_DiagRec_defaultFormat;
1055 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1057 response_diag (c, dr_p);
1059 else if (sr && sr->which == Z_Records_multipleNSD)
1061 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1062 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1064 c->error = ZOOM_ERROR_DECODE;
1068 if (resultset->count + resultset->start > resultset->size)
1069 resultset->count = resultset->size - resultset->start;
1070 if (resultset->count < 0)
1071 resultset->count = 0;
1072 if (sr && sr->which == Z_Records_DBOSD)
1075 NMEM nmem = odr_extract_mem (c->odr_in);
1076 Z_NamePlusRecordList *p =
1077 sr->u.databaseOrSurDiagnostics;
1078 for (i = 0; i<p->num_records; i++)
1080 record_cache_add (resultset, p->records[i],
1081 i+ resultset->start, 0);
1083 /* transfer our response to search_nmem .. we need it later */
1084 nmem_transfer (resultset->odr->mem, nmem);
1085 nmem_destroy (nmem);
1086 if (present_phase && p->num_records == 0)
1088 /* present response and we didn't get any records! */
1089 c->error = ZOOM_ERROR_DECODE;
1092 else if (present_phase)
1094 /* present response and we didn't get any records! */
1095 c->error = ZOOM_ERROR_DECODE;
1100 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1102 handle_records (c, pr->records, 1);
1105 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1107 ZOOM_resultset resultset;
1109 yaz_log (LOG_DEBUG, "got search response");
1111 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1114 resultset = c->tasks->u.search.resultset;
1116 resultset->size = *sr->resultCount;
1117 handle_records (c, sr->records, 0);
1120 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1122 if (res->diagnostics && res->num_diagnostics > 0)
1123 response_diag (c, res->diagnostics[0]);
1126 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1128 NMEM nmem = odr_extract_mem (c->odr_in);
1131 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1133 scan = c->tasks->u.scan.scan;
1135 if (res->entries && res->entries->nonsurrogateDiagnostics)
1136 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1137 scan->scan_response = res;
1138 nmem_transfer (scan->odr->mem, nmem);
1140 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1141 if (res->positionOfTerm)
1142 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1143 if (res->scanStatus)
1144 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1145 if (res->numberOfEntriesReturned)
1146 ZOOM_options_set_int (scan->options, "number",
1147 *res->numberOfEntriesReturned);
1148 nmem_destroy (nmem);
1152 static int send_sort (ZOOM_connection c)
1154 ZOOM_resultset resultset;
1156 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1159 resultset = c->tasks->u.search.resultset;
1163 resultset->r_sort_spec = 0;
1166 if (resultset->r_sort_spec)
1168 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1169 Z_SortRequest *req = apdu->u.sortRequest;
1171 req->num_inputResultSetNames = 1;
1172 req->inputResultSetNames = (Z_InternationalString **)
1173 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1174 req->inputResultSetNames[0] =
1175 odr_strdup (c->odr_out, resultset->setname);
1176 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1177 req->sortSequence = resultset->r_sort_spec;
1178 resultset->r_sort_spec = 0;
1179 send_APDU (c, apdu);
1185 static int send_present (ZOOM_connection c)
1187 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1188 Z_PresentRequest *req = apdu->u.presentRequest;
1190 const char *syntax =
1191 ZOOM_options_get (c->options, "preferredRecordSyntax");
1192 const char *element =
1193 ZOOM_options_get (c->options, "elementSetName");
1194 const char *schema =
1195 ZOOM_options_get (c->options, "schema");
1196 ZOOM_resultset resultset;
1201 switch (c->tasks->which)
1203 case ZOOM_TASK_SEARCH:
1204 resultset = c->tasks->u.search.resultset;
1206 case ZOOM_TASK_RETRIEVE:
1207 resultset = c->tasks->u.retrieve.resultset;
1208 resultset->start = c->tasks->u.retrieve.start;
1209 resultset->count = c->tasks->u.retrieve.count;
1211 if (resultset->start >= resultset->size)
1213 if (resultset->start + resultset->count > resultset->size)
1214 resultset->count = resultset->size - resultset->start;
1220 if (c->error) /* don't continue on error */
1222 if (resultset->start < 0)
1224 for (i = 0; i<resultset->count; i++)
1227 record_cache_lookup (resultset, i + resultset->start, 0);
1231 if (i == resultset->count)
1234 resultset->start += i;
1235 resultset->count -= i;
1236 *req->resultSetStartPoint = resultset->start + 1;
1237 *req->numberOfRecordsRequested = resultset->count;
1238 assert (*req->numberOfRecordsRequested > 0);
1240 if (syntax && *syntax)
1241 req->preferredRecordSyntax =
1242 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1244 if (schema && *schema)
1246 Z_RecordComposition *compo = (Z_RecordComposition *)
1247 odr_malloc (c->odr_out, sizeof(*compo));
1249 req->recordComposition = compo;
1250 compo->which = Z_RecordComp_complex;
1251 compo->u.complex = (Z_CompSpec *)
1252 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1253 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1254 odr_malloc(c->odr_out, sizeof(bool_t));
1255 *compo->u.complex->selectAlternativeSyntax = 0;
1257 compo->u.complex->generic = (Z_Specification *)
1258 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1260 compo->u.complex->generic->schema = (Odr_oid *)
1261 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1263 if (!compo->u.complex->generic->schema)
1265 /* OID wasn't a schema! Try record syntax instead. */
1267 compo->u.complex->generic->schema = (Odr_oid *)
1268 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1270 if (element && *element)
1272 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1273 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1274 compo->u.complex->generic->elementSpec->which =
1275 Z_ElementSpec_elementSetName;
1276 compo->u.complex->generic->elementSpec->u.elementSetName =
1277 odr_strdup (c->odr_out, element);
1280 compo->u.complex->generic->elementSpec = 0;
1281 compo->u.complex->num_dbSpecific = 0;
1282 compo->u.complex->dbSpecific = 0;
1283 compo->u.complex->num_recordSyntax = 0;
1284 compo->u.complex->recordSyntax = 0;
1286 else if (element && *element)
1288 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1289 odr_malloc (c->odr_out, sizeof(*esn));
1290 Z_RecordComposition *compo = (Z_RecordComposition *)
1291 odr_malloc (c->odr_out, sizeof(*compo));
1293 esn->which = Z_ElementSetNames_generic;
1294 esn->u.generic = odr_strdup (c->odr_out, element);
1295 compo->which = Z_RecordComp_simple;
1296 compo->u.simple = esn;
1297 req->recordComposition = compo;
1299 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1300 send_APDU (c, apdu);
1304 ZOOM_scanset ZOOM_connection_scan (ZOOM_connection c, const char *start)
1306 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1308 scan->connection = c;
1309 scan->odr = odr_createmem (ODR_DECODE);
1310 scan->options = ZOOM_options_create_with_parent (c->options);
1312 scan->scan_response = 0;
1314 if ((scan->termListAndStartPoint =
1315 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1318 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1319 task->u.scan.scan = scan;
1324 while (ZOOM_event (1, &c))
1331 void ZOOM_scanset_destroy (ZOOM_scanset scan)
1336 if (scan->refcount == 0)
1338 odr_destroy (scan->odr);
1340 ZOOM_options_destroy (scan->options);
1345 int send_scan (ZOOM_connection c)
1348 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1349 Z_ScanRequest *req = apdu->u.scanRequest;
1352 assert (c->tasks->which == ZOOM_TASK_SCAN);
1353 scan = c->tasks->u.scan.scan;
1355 req->termListAndStartPoint = scan->termListAndStartPoint;
1356 req->attributeSet = scan->attributeSet;
1358 *req->numberOfTermsRequested =
1359 ZOOM_options_get_int(scan->options, "number", 10);
1361 req->preferredPositionInResponse =
1362 odr_intdup (c->odr_out,
1363 ZOOM_options_get_int(scan->options, "position", 1));
1366 odr_intdup (c->odr_out,
1367 ZOOM_options_get_int(scan->options, "stepSize", 0));
1369 req->databaseNames = set_DatabaseNames (c, scan->options,
1370 &req->num_databaseNames);
1372 send_APDU (c, apdu);
1377 size_t ZOOM_scanset_size (ZOOM_scanset scan)
1379 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1381 return scan->scan_response->entries->num_entries;
1384 const char *ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1387 const char *term = 0;
1388 size_t noent = ZOOM_scanset_size (scan);
1389 Z_ScanResponse *res = scan->scan_response;
1395 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1397 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1399 if (t->term->which == Z_Term_general)
1401 term = (const char *) t->term->u.general->buf;
1402 *len = t->term->u.general->len;
1404 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1409 const char *ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1411 return ZOOM_options_get (scan->options, key);
1414 void ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1417 ZOOM_options_set (scan->options, key, val);
1420 static int ZOOM_connection_exec_task (ZOOM_connection c)
1422 ZOOM_task task = c->tasks;
1424 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1427 if (c->error != ZOOM_ERROR_NONE ||
1428 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1430 ZOOM_connection_remove_tasks (c);
1433 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d", task->which);
1437 switch (task->which)
1439 case ZOOM_TASK_SEARCH:
1440 /* see if search hasn't been sent yet. */
1441 if (ZOOM_connection_send_search (c))
1444 case ZOOM_TASK_RETRIEVE:
1445 if (send_present (c))
1448 case ZOOM_TASK_CONNECT:
1452 case ZOOM_TASK_SCAN:
1456 ZOOM_connection_remove_task (c);
1460 static int send_sort_present (ZOOM_connection c)
1462 int r = send_sort (c);
1464 r = send_present (c);
1468 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1470 Z_InitResponse *initrs;
1472 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1476 case Z_APDU_initResponse:
1477 initrs = apdu->u.initResponse;
1478 if (!*initrs->result)
1480 c->error = ZOOM_ERROR_INIT;
1485 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1487 xfree (c->cookie_in);
1490 c->cookie_in = xstrdup(cookie);
1491 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1492 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1493 c->support_named_resultsets = 1;
1496 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1497 ZOOM_connection_remove_task (c);
1499 ZOOM_connection_exec_task (c);
1502 case Z_APDU_searchResponse:
1503 handle_search_response (c, apdu->u.searchResponse);
1504 if (!send_sort_present (c))
1505 ZOOM_connection_remove_task (c);
1507 case Z_APDU_presentResponse:
1508 handle_present_response (c, apdu->u.presentResponse);
1509 if (!send_present (c))
1510 ZOOM_connection_remove_task (c);
1512 case Z_APDU_sortResponse:
1513 sort_response (c, apdu->u.sortResponse);
1514 if (!send_present (c))
1515 ZOOM_connection_remove_task (c);
1517 case Z_APDU_scanResponse:
1518 scan_response (c, apdu->u.scanResponse);
1519 ZOOM_connection_remove_task (c);
1523 static int do_read (ZOOM_connection c)
1529 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
1530 ZOOM_connection_put_event (c, event);
1532 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1537 c->error= ZOOM_ERROR_CONNECTION_LOST;
1543 odr_reset (c->odr_in);
1544 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1545 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
1546 ZOOM_connection_put_event (c, event);
1547 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1549 c->error = ZOOM_ERROR_DECODE;
1554 handle_apdu (c, apdu);
1560 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
1565 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1566 ZOOM_connection_put_event (c, event);
1568 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1570 if (c->state == STATE_CONNECTING)
1571 c->error = ZOOM_ERROR_CONNECT;
1573 c->error = ZOOM_ERROR_CONNECTION_LOST;
1579 c->mask = ZOOM_SELECT_EXCEPT;
1580 if (c->cs->io_pending & CS_WANT_WRITE)
1581 c->mask += ZOOM_SELECT_WRITE;
1582 if (c->cs->io_pending & CS_WANT_READ)
1583 c->mask += ZOOM_SELECT_READ;
1584 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
1588 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
1589 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
1594 static int do_write(ZOOM_connection c)
1596 return do_write_ex (c, c->buf_out, c->len_out);
1600 const char *ZOOM_connection_option_get (ZOOM_connection c, const char *key)
1602 return ZOOM_options_get (c->options, key);
1605 void ZOOM_connection_option_set (ZOOM_connection c, const char *key,
1608 ZOOM_options_set (c->options, key, val);
1611 const char *ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
1613 return ZOOM_options_get (r->options, key);
1616 void ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
1619 ZOOM_options_set (r->options, key, val);
1623 int ZOOM_connection_errcode (ZOOM_connection c)
1625 return ZOOM_connection_error (c, 0, 0);
1628 const char *ZOOM_connection_errmsg (ZOOM_connection c)
1631 ZOOM_connection_error (c, &msg, 0);
1635 const char *ZOOM_connection_addinfo (ZOOM_connection c)
1637 const char *addinfo;
1638 ZOOM_connection_error (c, 0, &addinfo);
1642 int ZOOM_connection_error (ZOOM_connection c, const char **cp,
1643 const char **addinfo)
1645 int error = c->error;
1650 case ZOOM_ERROR_NONE:
1651 *cp = "No error"; break;
1652 case ZOOM_ERROR_CONNECT:
1653 *cp = "Connect failed"; break;
1654 case ZOOM_ERROR_MEMORY:
1655 *cp = "Out of memory"; break;
1656 case ZOOM_ERROR_ENCODE:
1657 *cp = "Encoding failed"; break;
1658 case ZOOM_ERROR_DECODE:
1659 *cp = "Decoding failed"; break;
1660 case ZOOM_ERROR_CONNECTION_LOST:
1661 *cp = "Connection lost"; break;
1662 case ZOOM_ERROR_INIT:
1663 *cp = "Init rejected"; break;
1664 case ZOOM_ERROR_INTERNAL:
1665 *cp = "Internal failure"; break;
1666 case ZOOM_ERROR_TIMEOUT:
1667 *cp = "Timeout"; break;
1669 *cp = diagbib1_str (error);
1675 *addinfo = c->addinfo;
1682 int ZOOM_connection_do_io(ZOOM_connection c, int mask)
1684 ZOOM_Event event = 0;
1685 int r = cs_look(c->cs);
1686 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
1691 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
1692 c->error = ZOOM_ERROR_CONNECT;
1694 ZOOM_connection_put_event (c, event);
1696 else if (r == CS_CONNECT)
1699 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
1701 ret = cs_rcvconnect (c->cs);
1702 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
1705 c->mask = ZOOM_SELECT_EXCEPT;
1706 if (c->cs->io_pending & CS_WANT_WRITE)
1707 c->mask += ZOOM_SELECT_WRITE;
1708 if (c->cs->io_pending & CS_WANT_READ)
1709 c->mask += ZOOM_SELECT_READ;
1710 ZOOM_connection_put_event (c, event);
1714 ZOOM_connection_put_event (c, event);
1715 ZOOM_connection_send_init (c);
1716 c->state = STATE_ESTABLISHED;
1720 c->error = ZOOM_ERROR_CONNECT;
1722 ZOOM_connection_put_event (c, event);
1727 if (mask & ZOOM_SELECT_READ)
1729 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1735 int ZOOM_connection_last_event(ZOOM_connection cs)
1738 return ZOOM_EVENT_NONE;
1739 return cs->last_event;
1742 int ZOOM_event (int no, ZOOM_connection *cs)
1745 struct pollfd pollfds[1024];
1746 ZOOM_connection poll_cs[1024];
1749 fd_set input, output, except;
1754 for (i = 0; i<no; i++)
1756 ZOOM_connection c = cs[i];
1758 if (c && (event = ZOOM_connection_get_event(c)))
1760 ZOOM_Event_destroy (event);
1764 for (i = 0; i<no; i++)
1766 ZOOM_connection c = cs[i];
1768 if (c && ZOOM_connection_exec_task (c))
1770 if ((event = ZOOM_connection_get_event(c)))
1772 ZOOM_Event_destroy (event);
1788 for (i = 0; i<no; i++)
1790 ZOOM_connection c = cs[i];
1795 fd = z3950_connection_socket(c);
1796 mask = z3950_connection_mask(c);
1806 short poll_events = 0;
1808 if (mask & ZOOM_SELECT_READ)
1809 poll_events += POLLIN;
1810 if (mask & ZOOM_SELECT_WRITE)
1811 poll_events += POLLOUT;
1812 if (mask & ZOOM_SELECT_EXCEPT)
1813 poll_events += POLLERR;
1814 pollfds[nfds].fd = fd;
1815 pollfds[nfds].events = poll_events;
1816 pollfds[nfds].revents = 0;
1821 if (mask & ZOOM_SELECT_READ)
1823 FD_SET (fd, &input);
1826 if (mask & ZOOM_SELECT_WRITE)
1828 FD_SET (fd, &output);
1831 if (mask & ZOOM_SELECT_EXCEPT)
1833 FD_SET (fd, &except);
1841 r = poll (pollfds, nfds, 15000);
1842 for (i = 0; i<nfds; i++)
1844 ZOOM_connection c = poll_cs[i];
1848 if (pollfds[i].revents & POLLIN)
1849 mask += ZOOM_SELECT_READ;
1850 if (pollfds[i].revents & POLLOUT)
1851 mask += ZOOM_SELECT_WRITE;
1852 if (pollfds[i].revents & POLLERR)
1853 mask += ZOOM_SELECT_EXCEPT;
1855 ZOOM_connection_do_io(c, mask);
1857 else if (r == 0 && c->mask)
1859 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
1860 /* timeout and this connection was waiting */
1861 c->error = ZOOM_ERROR_TIMEOUT;
1863 ZOOM_connection_put_event(c, event);
1867 yaz_log (LOG_DEBUG, "select start");
1868 r = select (max_fd+1, &input, &output, &except, &tv);
1869 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
1870 for (i = 0; i<no; i++)
1872 ZOOM_connection c = cs[i];
1877 fd = z3950_connection_socket(c);
1881 /* no timeout and real socket */
1882 if (FD_ISSET(fd, &input))
1883 mask += ZOOM_SELECT_READ;
1884 if (FD_ISSET(fd, &output))
1885 mask += ZOOM_SELECT_WRITE;
1886 if (FD_ISSET(fd, &except))
1887 mask += ZOOM_SELECT_EXCEPT;
1889 ZOOM_connection_do_io(c, mask);
1891 if (r == 0 && c->mask)
1893 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
1894 /* timeout and this connection was waiting */
1895 c->error = ZOOM_ERROR_TIMEOUT;
1897 yaz_log (LOG_DEBUG, "timeout");
1898 ZOOM_connection_put_event(c, event);
1902 for (i = 0; i<no; i++)
1904 ZOOM_connection c = cs[i];
1906 if (c && (event = ZOOM_connection_get_event(c)))
1908 ZOOM_Event_destroy (event);