2 * $Id: zoom-c.c,v 1.43 2002-09-03 18:10:13 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>
12 #include <yaz/charneg.h>
21 static int ZOOM_connection_send_init (ZOOM_connection c);
22 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out);
24 static ZOOM_Event ZOOM_Event_create (int kind)
26 ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event));
33 static void ZOOM_Event_destroy (ZOOM_Event event)
38 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
42 c->m_queue_back->prev = event;
43 assert (c->m_queue_front);
47 assert (!c->m_queue_front);
48 c->m_queue_front = event;
50 event->next = c->m_queue_back;
52 c->m_queue_back = event;
55 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
57 ZOOM_Event event = c->m_queue_front;
60 assert (c->m_queue_back);
61 c->m_queue_front = event->prev;
64 assert (c->m_queue_back);
65 c->m_queue_front->next = 0;
69 c->last_event = event->kind;
73 static void clear_error (ZOOM_connection c)
78 case ZOOM_ERROR_CONNECT:
79 case ZOOM_ERROR_MEMORY:
80 case ZOOM_ERROR_DECODE:
81 case ZOOM_ERROR_CONNECTION_LOST:
83 case ZOOM_ERROR_INTERNAL:
86 c->error = ZOOM_ERROR_NONE;
92 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
94 ZOOM_task *taskp = &c->tasks;
96 taskp = &(*taskp)->next;
97 *taskp = (ZOOM_task) xmalloc (sizeof(**taskp));
98 (*taskp)->running = 0;
99 (*taskp)->which = which;
105 ZOOM_task ZOOM_connection_insert_task (ZOOM_connection c, int which)
107 ZOOM_task task = (ZOOM_task) xmalloc (sizeof(*task));
109 task->next = c->tasks;
118 void ZOOM_connection_remove_task (ZOOM_connection c)
120 ZOOM_task task = c->tasks;
124 c->tasks = task->next;
127 case ZOOM_TASK_SEARCH:
128 ZOOM_resultset_destroy (task->u.search.resultset);
130 case ZOOM_TASK_RETRIEVE:
131 ZOOM_resultset_destroy (task->u.retrieve.resultset);
133 case ZOOM_TASK_CONNECT:
136 ZOOM_scanset_destroy (task->u.scan.scan);
138 case ZOOM_TASK_PACKAGE:
139 ZOOM_package_destroy (task->u.package);
148 void ZOOM_connection_remove_tasks (ZOOM_connection c)
151 ZOOM_connection_remove_task(c);
154 static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos);
156 ZOOM_API(ZOOM_connection)
157 ZOOM_connection_create (ZOOM_options options)
159 ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
164 c->state = STATE_IDLE;
165 c->error = ZOOM_ERROR_NONE;
173 c->options = ZOOM_options_create_with_parent(options);
178 c->charset = c->lang = 0;
185 c->odr_in = odr_createmem (ODR_DECODE);
186 c->odr_out = odr_createmem (ODR_ENCODE);
189 c->support_named_resultsets = 0;
190 c->last_event = ZOOM_EVENT_NONE;
192 c->m_queue_front = 0;
197 /* set database names. Take local databases (if set); otherwise
198 take databases given in ZURL (if set); otherwise use Default */
199 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
202 char **databaseNames;
205 const char *cp = ZOOM_options_get (options, "databaseName");
209 if (strncmp (con->host_port, "unix:", 5) == 0)
210 cp = strchr (con->host_port+5, ':');
212 cp = strchr (con->host_port, '/');
219 while ((c = strchr(c, '+')))
227 databaseNames = (char**)
228 odr_malloc (con->odr_out, no * sizeof(*databaseNames));
232 c = strchr (cp, '+');
240 /* cp ptr to first char of db name, c is char
242 databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
243 memcpy (databaseNames[no], cp, c-cp);
244 databaseNames[no++][c-cp] = '\0';
249 databaseNames[no] = NULL;
251 return databaseNames;
254 ZOOM_API(ZOOM_connection)
255 ZOOM_connection_new (const char *host, int portnum)
257 ZOOM_connection c = ZOOM_connection_create (0);
259 ZOOM_connection_connect (c, host, portnum);
264 ZOOM_connection_connect(ZOOM_connection c,
265 const char *host, int portnum)
272 yaz_log (LOG_DEBUG, "reconnect");
276 yaz_log(LOG_DEBUG, "connect");
278 val = ZOOM_options_get (c->options, "proxy");
280 c->proxy = xstrdup (val);
285 val = ZOOM_options_get (c->options, "charset");
287 c->charset = xstrdup (val);
292 val = ZOOM_options_get (c->options, "lang");
294 c->lang = xstrdup (val);
298 xfree (c->host_port);
302 sprintf (hostn, "%.80s:%d", host, portnum);
303 c->host_port = xstrdup(hostn);
306 c->host_port = xstrdup(host);
308 ZOOM_options_set(c->options, "host", c->host_port);
310 val = ZOOM_options_get (c->options, "cookie");
312 c->cookie_out = xstrdup (val);
314 val = ZOOM_options_get (c->options, "clientIP");
316 c->client_IP = xstrdup (val);
318 c->async = ZOOM_options_get_bool (c->options, "async", 0);
320 c->error = ZOOM_ERROR_NONE;
322 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
326 while (ZOOM_event (1, &c))
332 ZOOM_query_create(void)
334 ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
339 s->odr = odr_createmem (ODR_ENCODE);
345 ZOOM_query_destroy(ZOOM_query s)
351 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
352 if (s->refcount == 0)
354 odr_destroy (s->odr);
360 ZOOM_query_prefix(ZOOM_query s, const char *str)
362 s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query));
363 s->query->which = Z_Query_type_1;
364 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
365 if (!s->query->u.type_1)
371 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
373 s->sort_spec = yaz_sort_spec (s->odr, criteria);
379 static int do_write(ZOOM_connection c);
382 ZOOM_connection_destroy(ZOOM_connection c)
389 for (r = c->resultsets; r; r = r->next)
394 odr_destroy (c->odr_in);
395 odr_destroy (c->odr_out);
396 ZOOM_options_destroy (c->options);
397 ZOOM_connection_remove_tasks (c);
398 xfree (c->host_port);
402 xfree (c->cookie_out);
403 xfree (c->cookie_in);
404 xfree (c->client_IP);
408 void ZOOM_resultset_addref (ZOOM_resultset r)
413 ZOOM_resultset ZOOM_resultset_create ()
415 ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
419 r->odr = odr_createmem (ODR_ENCODE);
433 ZOOM_API(ZOOM_resultset)
434 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
437 ZOOM_query s = ZOOM_query_create();
439 ZOOM_query_prefix (s, q);
441 r = ZOOM_connection_search (c, s);
442 ZOOM_query_destroy (s);
446 ZOOM_API(ZOOM_resultset)
447 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
449 ZOOM_resultset r = ZOOM_resultset_create ();
453 r->r_sort_spec = q->sort_spec;
454 r->r_query = q->query;
457 r->options = ZOOM_options_create_with_parent(c->options);
459 r->start = ZOOM_options_get_int(r->options, "start", 0);
460 r->count = ZOOM_options_get_int(r->options, "count", 0);
461 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
462 cp = ZOOM_options_get (r->options, "setname");
464 r->setname = xstrdup (cp);
468 r->next = c->resultsets;
471 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
472 task->u.search.resultset = r;
473 ZOOM_resultset_addref (r);
479 while (ZOOM_event (1, &c))
486 ZOOM_resultset_destroy(ZOOM_resultset r)
491 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
492 if (r->refcount == 0)
494 ZOOM_record_cache rc;
496 for (rc = r->record_cache; rc; rc = rc->next)
497 if (rc->rec.wrbuf_marc)
498 wrbuf_free (rc->rec.wrbuf_marc, 1);
501 /* remove ourselves from the resultsets in connection */
502 ZOOM_resultset *rp = &r->connection->resultsets;
505 assert (*rp); /* we must be in this list!! */
507 { /* OK, we're here - take us out of it */
514 ZOOM_query_destroy (r->search);
515 ZOOM_options_destroy (r->options);
516 odr_destroy (r->odr);
523 ZOOM_resultset_size (ZOOM_resultset r)
528 static void do_close (ZOOM_connection c)
534 c->state = STATE_IDLE;
537 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
538 int force_sync, int start, int count)
548 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
549 task->u.retrieve.resultset = r;
550 task->u.retrieve.start = start;
551 task->u.retrieve.count = count;
553 ZOOM_resultset_addref (r);
555 if (!r->connection->async || force_sync)
556 while (r->connection && ZOOM_event (1, &r->connection))
561 ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
562 size_t start, size_t count)
564 int force_present = 0;
570 ZOOM_resultset_retrieve (r, force_present, start, count);
574 for (i = 0; i< count; i++)
575 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
579 static int do_connect (ZOOM_connection c)
582 const char *effective_host;
585 effective_host = c->proxy;
587 effective_host = c->host_port;
589 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
592 c->cs = cs_create_host (effective_host, 0, &add);
596 int ret = cs_connect (c->cs, add);
599 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
600 ZOOM_connection_put_event(c, event);
601 ZOOM_connection_send_init(c);
602 c->state = STATE_ESTABLISHED;
607 c->state = STATE_CONNECTING;
608 c->mask = ZOOM_SELECT_EXCEPT;
609 if (c->cs->io_pending & CS_WANT_WRITE)
610 c->mask += ZOOM_SELECT_WRITE;
611 if (c->cs->io_pending & CS_WANT_READ)
612 c->mask += ZOOM_SELECT_READ;
616 c->state = STATE_IDLE;
617 c->error = ZOOM_ERROR_CONNECT;
621 int z3950_connection_socket(ZOOM_connection c)
624 return cs_fileno(c->cs);
628 int z3950_connection_mask(ZOOM_connection c)
635 static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out)
638 for (i = 0; i<200; i++)
641 Z_OtherInformation **oi;
647 sprintf (buf, "otherInfo%d", i);
648 val = ZOOM_options_get (c->options, buf);
651 cp = strchr (val, ':');
655 if (len >= sizeof(buf))
657 memcpy (buf, val, len);
659 oidval = oid_getvalbyname (buf);
660 if (oidval == VAL_NONE)
664 yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1);
668 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
673 Z_OtherInformation **oi;
675 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
679 Z_OtherInformation **oi;
681 yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
683 otherInfo_attach (c, a, out);
684 if (!z_APDU(out, &a, 0, 0))
686 FILE *outf = fopen("/tmp/apdu.txt", "a");
689 ODR odr_pr = odr_createmem(ODR_PRINT);
690 fprintf (outf, "a=%p\n", a);
691 odr_setprint(odr_pr, outf);
692 z_APDU(odr_pr, &a, 0, 0);
695 c->error = ZOOM_ERROR_ENCODE;
703 static int send_APDU (ZOOM_connection c, Z_APDU *a)
707 if (encode_APDU(c, a, c->odr_out))
709 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
710 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
711 ZOOM_connection_put_event (c, event);
712 odr_reset(c->odr_out);
717 static int ZOOM_connection_send_init (ZOOM_connection c)
720 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
721 Z_InitRequest *ireq = apdu->u.initRequest;
722 Z_IdAuthentication *auth = (Z_IdAuthentication *)
723 odr_malloc(c->odr_out, sizeof(*auth));
724 const char *auth_groupId = ZOOM_options_get (c->options, "group");
725 const char *auth_userId = ZOOM_options_get (c->options, "user");
726 const char *auth_password = ZOOM_options_get (c->options, "pass");
728 ODR_MASK_SET(ireq->options, Z_Options_search);
729 ODR_MASK_SET(ireq->options, Z_Options_present);
730 ODR_MASK_SET(ireq->options, Z_Options_scan);
731 ODR_MASK_SET(ireq->options, Z_Options_sort);
732 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
733 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
735 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
736 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
737 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
739 impname = ZOOM_options_get (c->options, "implementationName");
740 ireq->implementationName =
741 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
742 strcpy (ireq->implementationName, "");
745 strcat (ireq->implementationName, impname);
746 strcat (ireq->implementationName, "/");
748 strcat (ireq->implementationName, "ZOOM-C/YAZ");
750 *ireq->maximumRecordSize =
751 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
752 *ireq->preferredMessageSize =
753 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
755 if (auth_groupId || auth_password)
757 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
760 if (auth_groupId && *auth_groupId)
762 pass->groupId = (char *)
763 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
764 strcpy(pass->groupId, auth_groupId);
768 if (auth_userId && *auth_userId)
770 pass->userId = (char *)
771 odr_malloc(c->odr_out, strlen(auth_userId)+1);
772 strcpy(pass->userId, auth_userId);
776 if (auth_password && *auth_password)
778 pass->password = (char *)
779 odr_malloc(c->odr_out, strlen(auth_password)+1);
780 strcpy(pass->password, auth_password);
785 auth->which = Z_IdAuthentication_idPass;
786 auth->u.idPass = pass;
787 ireq->idAuthentication = auth;
790 else if (auth_userId)
792 auth->which = Z_IdAuthentication_open;
793 auth->u.open = (char *)
794 odr_malloc(c->odr_out, strlen(auth_userId)+1);
795 strcpy(auth->u.open, auth_userId);
796 ireq->idAuthentication = auth;
799 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
800 VAL_PROXY, 1, c->host_port);
801 if (c->charset||c->lang)
803 Z_OtherInformation **oi;
804 Z_OtherInformationUnit *oi_unit;
806 yaz_oi_APDU(apdu, &oi);
808 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
810 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
812 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
813 oi_unit->information.externallyDefinedInfo =
814 yaz_set_proposal_charneg
816 (const char **)&c->charset, (c->charset) ? 1:0,
817 (const char **)&c->lang, (c->lang) ? 1:0, 1);
826 static int ZOOM_connection_send_search (ZOOM_connection c)
829 int lslb, ssub, mspn;
831 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
832 Z_SearchRequest *search_req = apdu->u.searchRequest;
833 const char *elementSetName;
834 const char *smallSetElementSetName;
835 const char *mediumSetElementSetName;
839 assert (c->tasks->which == ZOOM_TASK_SEARCH);
841 r = c->tasks->u.search.resultset;
844 ZOOM_options_get (r->options, "elementSetName");
845 smallSetElementSetName =
846 ZOOM_options_get (r->options, "smallSetElementSetName");
847 mediumSetElementSetName =
848 ZOOM_options_get (r->options, "mediumSetElementSetName");
850 ZOOM_options_get (r->options, "schema");
852 if (!smallSetElementSetName)
853 smallSetElementSetName = elementSetName;
855 if (!mediumSetElementSetName)
856 mediumSetElementSetName = elementSetName;
861 /* prepare query for the search request */
862 search_req->query = r->r_query;
864 search_req->databaseNames =
865 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
867 /* get syntax (no need to provide unless piggyback is in effect) */
868 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
870 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
871 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
872 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
873 if (lslb != -1 && ssub != -1 && mspn != -1)
875 /* So're a Z39.50 expert? Let's hope you don't do sort */
876 *search_req->largeSetLowerBound = lslb;
877 *search_req->smallSetUpperBound = ssub;
878 *search_req->mediumSetPresentNumber = mspn;
880 else if (r->start == 0 && r->count > 0
881 && r->piggyback && !r->r_sort_spec && !schema)
883 /* Regular piggyback - do it unless we're going to do sort */
884 *search_req->largeSetLowerBound = 2000000000;
885 *search_req->smallSetUpperBound = r->count;
886 *search_req->mediumSetPresentNumber = r->count;
887 smallSetElementSetName = 0; /* no need to provide this */
891 /* non-piggyback. Need not provide elementsets or syntaxes .. */
892 smallSetElementSetName = 0;
893 mediumSetElementSetName = 0;
896 if (smallSetElementSetName && *smallSetElementSetName)
898 Z_ElementSetNames *esn = (Z_ElementSetNames *)
899 odr_malloc (c->odr_out, sizeof(*esn));
901 esn->which = Z_ElementSetNames_generic;
902 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
903 search_req->smallSetElementSetNames = esn;
905 if (mediumSetElementSetName && *mediumSetElementSetName)
907 Z_ElementSetNames *esn = (Z_ElementSetNames *)
908 odr_malloc (c->odr_out, sizeof(*esn));
910 esn->which = Z_ElementSetNames_generic;
911 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
912 search_req->mediumSetElementSetNames = esn;
915 search_req->preferredRecordSyntax =
916 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
920 if (c->support_named_resultsets)
924 /* find the lowest unused ordinal so that we re-use
925 result sets on the server. */
926 for (ord = 1; ; ord++)
929 sprintf (setname, "%d", ord);
930 for (rp = c->resultsets; rp; rp = rp->next)
931 if (rp->setname && !strcmp (rp->setname, setname))
936 r->setname = xstrdup (setname);
937 yaz_log (LOG_DEBUG, "allocating %s", r->setname);
940 r->setname = xstrdup ("default");
941 ZOOM_options_set (r->options, "setname", r->setname);
943 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
944 /* send search request */
949 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
951 Z_DefaultDiagFormat *r;
956 if (p->which != Z_DiagRec_defaultFormat)
958 c->error = ZOOM_ERROR_DECODE;
961 r = p->u.defaultFormat;
964 case Z_DefaultDiagFormat_v2Addinfo:
965 addinfo = r->u.v2Addinfo;
967 case Z_DefaultDiagFormat_v3Addinfo:
968 addinfo = r->u.v3Addinfo;
972 c->addinfo = xstrdup (addinfo);
973 c->error = *r->condition;
976 ZOOM_API(ZOOM_record)
977 ZOOM_record_clone (ZOOM_record srec)
984 odr_enc = odr_createmem(ODR_ENCODE);
985 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
987 buf = odr_getbuf (odr_enc, &size, 0);
989 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
990 nrec->odr = odr_createmem(ODR_DECODE);
991 nrec->wrbuf_marc = 0;
992 odr_setbuf (nrec->odr, buf, size, 0);
993 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
995 odr_destroy (odr_enc);
999 ZOOM_API(ZOOM_record)
1000 ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
1002 return record_cache_lookup (s, pos);
1005 ZOOM_API(ZOOM_record)
1006 ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
1008 ZOOM_resultset_retrieve (r, 1, pos, 1);
1009 return ZOOM_resultset_record_immediate (r, pos);
1013 ZOOM_record_destroy (ZOOM_record rec)
1017 if (rec->wrbuf_marc)
1018 wrbuf_free (rec->wrbuf_marc, 1);
1019 odr_destroy (rec->odr);
1023 ZOOM_API(const char *)
1024 ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
1026 Z_NamePlusRecord *npr;
1029 *len = 0; /* default return */
1036 if (!strcmp (type, "database"))
1039 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
1040 return npr->databaseName;
1042 else if (!strcmp (type, "syntax"))
1044 if (npr->which == Z_NamePlusRecord_databaseRecord)
1046 Z_External *r = (Z_External *) npr->u.databaseRecord;
1047 oident *ent = oid_getentbyoid(r->direct_reference);
1051 *len = strlen(ent->desc);
1057 else if (!strcmp (type, "render") &&
1058 npr->which == Z_NamePlusRecord_databaseRecord)
1060 Z_External *r = (Z_External *) npr->u.databaseRecord;
1061 oident *ent = oid_getentbyoid(r->direct_reference);
1063 if (r->which == Z_External_sutrs)
1065 if (len) *len = r->u.sutrs->len;
1066 return (const char *) r->u.sutrs->buf;
1068 else if (r->which == Z_External_octet)
1077 case VAL_APPLICATION_XML:
1080 if (!rec->wrbuf_marc)
1081 rec->wrbuf_marc = wrbuf_alloc();
1082 wrbuf_rewind (rec->wrbuf_marc);
1083 if (yaz_marc_decode ((const char *)
1084 r->u.octet_aligned->buf,
1086 r->u.octet_aligned->len,
1089 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1090 return wrbuf_buf(rec->wrbuf_marc);
1093 if (len) *len = r->u.octet_aligned->len;
1094 return (const char *) r->u.octet_aligned->buf;
1096 else if (r->which == Z_External_grs1)
1103 else if (!strcmp (type, "xml") &&
1104 npr->which == Z_NamePlusRecord_databaseRecord)
1106 Z_External *r = (Z_External *) npr->u.databaseRecord;
1107 oident *ent = oid_getentbyoid(r->direct_reference);
1109 if (r->which == Z_External_sutrs)
1111 if (len) *len = r->u.sutrs->len;
1112 return (const char *) r->u.sutrs->buf;
1114 else if (r->which == Z_External_octet)
1123 case VAL_APPLICATION_XML:
1126 if (!rec->wrbuf_marc)
1127 rec->wrbuf_marc = wrbuf_alloc();
1128 wrbuf_rewind (rec->wrbuf_marc);
1129 if (yaz_marc_decode ((const char *)
1130 r->u.octet_aligned->buf,
1132 r->u.octet_aligned->len,
1135 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1136 return wrbuf_buf(rec->wrbuf_marc);
1139 if (len) *len = r->u.octet_aligned->len;
1140 return (const char *) r->u.octet_aligned->buf;
1142 else if (r->which == Z_External_grs1)
1149 else if (!strcmp (type, "raw"))
1151 if (npr->which == Z_NamePlusRecord_databaseRecord)
1153 Z_External *r = (Z_External *) npr->u.databaseRecord;
1155 if (r->which == Z_External_sutrs)
1157 if (len) *len = r->u.sutrs->len;
1158 return (const char *) r->u.sutrs->buf;
1160 else if (r->which == Z_External_octet)
1162 if (len) *len = r->u.octet_aligned->len;
1163 return (const char *) r->u.octet_aligned->buf;
1165 else /* grs-1, explain, ... */
1168 return (const char *) npr->u.databaseRecord;
1173 else if (!strcmp (type, "ext"))
1175 if (npr->which == Z_NamePlusRecord_databaseRecord)
1176 return (const char *) npr->u.databaseRecord;
1182 static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, int pos)
1184 ZOOM_record_cache rc;
1185 const char *elementSetName =
1186 ZOOM_resultset_option_get (r, "elementSetName");
1187 const char *syntax =
1188 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1191 for (rc = r->record_cache; rc; rc = rc->next)
1195 if ((!elementSetName && !rc->elementSetName)
1196 || (elementSetName && rc->elementSetName &&
1197 !strcmp (elementSetName, rc->elementSetName)))
1199 if ((!syntax && !rc->syntax)
1200 || (syntax && rc->syntax &&
1201 !strcmp (syntax, rc->syntax)))
1203 /* not destroying rc->npr (it's handled by nmem )*/
1205 /* keeping wrbuf_marc too */
1211 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1214 rc->rec.wrbuf_marc = 0;
1216 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1218 rc->elementSetName = 0;
1221 rc->syntax = odr_strdup (r->odr, syntax);
1226 rc->next = r->record_cache;
1227 r->record_cache = rc;
1230 static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos)
1232 ZOOM_record_cache rc;
1233 const char *elementSetName =
1234 ZOOM_resultset_option_get (r, "elementSetName");
1235 const char *syntax =
1236 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1238 for (rc = r->record_cache; rc; rc = rc->next)
1242 if ((!elementSetName && !rc->elementSetName)
1243 || (elementSetName && rc->elementSetName &&
1244 !strcmp (elementSetName, rc->elementSetName)))
1246 if ((!syntax && !rc->syntax)
1247 || (syntax && rc->syntax &&
1248 !strcmp (syntax, rc->syntax)))
1256 static void handle_records (ZOOM_connection c, Z_Records *sr,
1259 ZOOM_resultset resultset;
1263 switch (c->tasks->which)
1265 case ZOOM_TASK_SEARCH:
1266 resultset = c->tasks->u.search.resultset;
1268 case ZOOM_TASK_RETRIEVE:
1269 resultset = c->tasks->u.retrieve.resultset;
1274 if (sr && sr->which == Z_Records_NSD)
1276 Z_DiagRec dr, *dr_p = &dr;
1277 dr.which = Z_DiagRec_defaultFormat;
1278 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1280 response_diag (c, dr_p);
1282 else if (sr && sr->which == Z_Records_multipleNSD)
1284 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1285 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1287 c->error = ZOOM_ERROR_DECODE;
1291 if (resultset->count + resultset->start > resultset->size)
1292 resultset->count = resultset->size - resultset->start;
1293 if (resultset->count < 0)
1294 resultset->count = 0;
1295 if (sr && sr->which == Z_Records_DBOSD)
1298 NMEM nmem = odr_extract_mem (c->odr_in);
1299 Z_NamePlusRecordList *p =
1300 sr->u.databaseOrSurDiagnostics;
1301 for (i = 0; i<p->num_records; i++)
1303 record_cache_add (resultset, p->records[i],
1304 i+ resultset->start);
1306 /* transfer our response to search_nmem .. we need it later */
1307 nmem_transfer (resultset->odr->mem, nmem);
1308 nmem_destroy (nmem);
1309 if (present_phase && p->num_records == 0)
1311 /* present response and we didn't get any records! */
1312 c->error = ZOOM_ERROR_DECODE;
1315 else if (present_phase)
1317 /* present response and we didn't get any records! */
1318 c->error = ZOOM_ERROR_DECODE;
1323 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1325 handle_records (c, pr->records, 1);
1328 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1330 ZOOM_resultset resultset;
1332 yaz_log (LOG_DEBUG, "got search response");
1334 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1337 resultset = c->tasks->u.search.resultset;
1339 resultset->size = *sr->resultCount;
1340 handle_records (c, sr->records, 0);
1343 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1345 if (res->diagnostics && res->num_diagnostics > 0)
1346 response_diag (c, res->diagnostics[0]);
1349 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1351 NMEM nmem = odr_extract_mem (c->odr_in);
1354 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1356 scan = c->tasks->u.scan.scan;
1358 if (res->entries && res->entries->nonsurrogateDiagnostics)
1359 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1360 scan->scan_response = res;
1361 nmem_transfer (scan->odr->mem, nmem);
1363 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1364 if (res->positionOfTerm)
1365 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1366 if (res->scanStatus)
1367 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1368 if (res->numberOfEntriesReturned)
1369 ZOOM_options_set_int (scan->options, "number",
1370 *res->numberOfEntriesReturned);
1371 nmem_destroy (nmem);
1375 static int send_sort (ZOOM_connection c)
1377 ZOOM_resultset resultset;
1379 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1382 resultset = c->tasks->u.search.resultset;
1386 resultset->r_sort_spec = 0;
1389 if (resultset->r_sort_spec)
1391 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1392 Z_SortRequest *req = apdu->u.sortRequest;
1394 req->num_inputResultSetNames = 1;
1395 req->inputResultSetNames = (Z_InternationalString **)
1396 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1397 req->inputResultSetNames[0] =
1398 odr_strdup (c->odr_out, resultset->setname);
1399 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1400 req->sortSequence = resultset->r_sort_spec;
1401 resultset->r_sort_spec = 0;
1402 send_APDU (c, apdu);
1408 static int send_present (ZOOM_connection c)
1411 Z_PresentRequest *req = 0;
1413 const char *syntax = 0;
1414 const char *elementSetName = 0;
1415 const char *schema = 0;
1416 ZOOM_resultset resultset;
1421 switch (c->tasks->which)
1423 case ZOOM_TASK_SEARCH:
1424 resultset = c->tasks->u.search.resultset;
1426 case ZOOM_TASK_RETRIEVE:
1427 resultset = c->tasks->u.retrieve.resultset;
1428 resultset->start = c->tasks->u.retrieve.start;
1429 resultset->count = c->tasks->u.retrieve.count;
1431 if (resultset->start >= resultset->size)
1433 if (resultset->start + resultset->count > resultset->size)
1434 resultset->count = resultset->size - resultset->start;
1440 syntax = ZOOM_resultset_option_get (resultset, "preferredRecordSyntax");
1441 elementSetName = ZOOM_resultset_option_get (resultset, "elementSetName");
1442 schema = ZOOM_resultset_option_get (resultset, "schema");
1444 if (c->error) /* don't continue on error */
1446 if (resultset->start < 0)
1448 for (i = 0; i<resultset->count; i++)
1451 record_cache_lookup (resultset, i + resultset->start);
1455 if (i == resultset->count)
1458 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1459 req = apdu->u.presentRequest;
1461 resultset->start += i;
1462 resultset->count -= i;
1463 *req->resultSetStartPoint = resultset->start + 1;
1464 *req->numberOfRecordsRequested = resultset->count;
1465 assert (*req->numberOfRecordsRequested > 0);
1467 if (syntax && *syntax)
1468 req->preferredRecordSyntax =
1469 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1471 if (schema && *schema)
1473 Z_RecordComposition *compo = (Z_RecordComposition *)
1474 odr_malloc (c->odr_out, sizeof(*compo));
1476 req->recordComposition = compo;
1477 compo->which = Z_RecordComp_complex;
1478 compo->u.complex = (Z_CompSpec *)
1479 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1480 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1481 odr_malloc(c->odr_out, sizeof(bool_t));
1482 *compo->u.complex->selectAlternativeSyntax = 0;
1484 compo->u.complex->generic = (Z_Specification *)
1485 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1487 compo->u.complex->generic->schema = (Odr_oid *)
1488 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1490 if (!compo->u.complex->generic->schema)
1492 /* OID wasn't a schema! Try record syntax instead. */
1494 compo->u.complex->generic->schema = (Odr_oid *)
1495 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1497 if (elementSetName && *elementSetName)
1499 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1500 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1501 compo->u.complex->generic->elementSpec->which =
1502 Z_ElementSpec_elementSetName;
1503 compo->u.complex->generic->elementSpec->u.elementSetName =
1504 odr_strdup (c->odr_out, elementSetName);
1507 compo->u.complex->generic->elementSpec = 0;
1508 compo->u.complex->num_dbSpecific = 0;
1509 compo->u.complex->dbSpecific = 0;
1510 compo->u.complex->num_recordSyntax = 0;
1511 compo->u.complex->recordSyntax = 0;
1513 else if (elementSetName && *elementSetName)
1515 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1516 odr_malloc (c->odr_out, sizeof(*esn));
1517 Z_RecordComposition *compo = (Z_RecordComposition *)
1518 odr_malloc (c->odr_out, sizeof(*compo));
1520 esn->which = Z_ElementSetNames_generic;
1521 esn->u.generic = odr_strdup (c->odr_out, elementSetName);
1522 compo->which = Z_RecordComp_simple;
1523 compo->u.simple = esn;
1524 req->recordComposition = compo;
1526 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1527 send_APDU (c, apdu);
1531 ZOOM_API(ZOOM_scanset)
1532 ZOOM_connection_scan (ZOOM_connection c, const char *start)
1534 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1536 scan->connection = c;
1537 scan->odr = odr_createmem (ODR_DECODE);
1538 scan->options = ZOOM_options_create_with_parent (c->options);
1540 scan->scan_response = 0;
1542 if ((scan->termListAndStartPoint =
1543 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1546 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1547 task->u.scan.scan = scan;
1552 while (ZOOM_event (1, &c))
1560 ZOOM_scanset_destroy (ZOOM_scanset scan)
1565 if (scan->refcount == 0)
1567 odr_destroy (scan->odr);
1569 ZOOM_options_destroy (scan->options);
1574 static int send_package (ZOOM_connection c)
1579 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1581 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
1582 ZOOM_connection_put_event (c, event);
1584 do_write_ex (c, c->tasks->u.package->buf_out,
1585 c->tasks->u.package->len_out);
1589 static int send_scan (ZOOM_connection c)
1592 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1593 Z_ScanRequest *req = apdu->u.scanRequest;
1596 assert (c->tasks->which == ZOOM_TASK_SCAN);
1597 scan = c->tasks->u.scan.scan;
1599 req->termListAndStartPoint = scan->termListAndStartPoint;
1600 req->attributeSet = scan->attributeSet;
1602 *req->numberOfTermsRequested =
1603 ZOOM_options_get_int(scan->options, "number", 10);
1605 req->preferredPositionInResponse =
1606 odr_intdup (c->odr_out,
1607 ZOOM_options_get_int(scan->options, "position", 1));
1610 odr_intdup (c->odr_out,
1611 ZOOM_options_get_int(scan->options, "stepSize", 0));
1613 req->databaseNames = set_DatabaseNames (c, scan->options,
1614 &req->num_databaseNames);
1616 send_APDU (c, apdu);
1622 ZOOM_scanset_size (ZOOM_scanset scan)
1624 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1626 return scan->scan_response->entries->num_entries;
1629 ZOOM_API(const char *)
1630 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1633 const char *term = 0;
1634 size_t noent = ZOOM_scanset_size (scan);
1635 Z_ScanResponse *res = scan->scan_response;
1641 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1643 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1645 if (t->term->which == Z_Term_general)
1647 term = (const char *) t->term->u.general->buf;
1648 *len = t->term->u.general->len;
1650 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1655 ZOOM_API(const char *)
1656 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1658 return ZOOM_options_get (scan->options, key);
1662 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1665 ZOOM_options_set (scan->options, key, val);
1670 static Z_APDU *create_es_package (ZOOM_package p, int type)
1673 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
1674 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1676 *req->function = Z_ExtendedServicesRequest_create;
1678 str = ZOOM_options_get(p->options, "package-name");
1680 req->packageName = nmem_strdup (p->odr_out->mem, str);
1682 str = ZOOM_options_get(p->options, "user-id");
1684 req->userId = nmem_strdup (p->odr_out->mem, str);
1686 req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1689 str = ZOOM_options_get(p->options, "function");
1692 if (!strcmp (str, "create"))
1694 if (!strcmp (str, "delete"))
1696 if (!strcmp (str, "modify"))
1702 static const char *ill_array_lookup (void *clientData, const char *idx)
1704 ZOOM_package p = (ZOOM_package) clientData;
1705 return ZOOM_options_get (p->options, idx+4);
1708 static Z_External *encode_ill_request (ZOOM_package p)
1710 ODR out = p->odr_out;
1713 struct ill_get_ctl ctl;
1715 ctl.odr = p->odr_out;
1717 ctl.f = ill_array_lookup;
1719 req = ill_get_ILLRequest(&ctl, "ill", 0);
1721 if (!ill_Request (out, &req, 0, 0))
1723 int ill_request_size;
1724 char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1725 if (ill_request_buf)
1726 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1732 int illRequest_size = 0;
1733 char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0);
1735 oid.proto = PROTO_GENERAL;
1736 oid.oclass = CLASS_GENERAL;
1737 oid.value = VAL_ISO_ILL_1;
1739 r = (Z_External *) odr_malloc (out, sizeof(*r));
1740 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1741 r->indirect_reference = 0;
1743 r->which = Z_External_single;
1745 r->u.single_ASN1_type = (Odr_oct *)
1746 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1747 r->u.single_ASN1_type->buf = odr_malloc (out, illRequest_size);
1748 r->u.single_ASN1_type->len = illRequest_size;
1749 r->u.single_ASN1_type->size = illRequest_size;
1750 memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size);
1755 static Z_ItemOrder *encode_item_order(ZOOM_package p)
1757 Z_ItemOrder *req = odr_malloc (p->odr_out, sizeof(*req));
1760 req->which=Z_IOItemOrder_esRequest;
1761 req->u.esRequest = (Z_IORequest *)
1762 odr_malloc(p->odr_out,sizeof(Z_IORequest));
1764 /* to keep part ... */
1765 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1766 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
1767 req->u.esRequest->toKeep->supplDescription = 0;
1768 req->u.esRequest->toKeep->contact =
1769 odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
1771 str = ZOOM_options_get(p->options, "contact-name");
1772 req->u.esRequest->toKeep->contact->name = str ?
1773 nmem_strdup (p->odr_out->mem, str) : 0;
1775 str = ZOOM_options_get(p->options, "contact-phone");
1776 req->u.esRequest->toKeep->contact->phone = str ?
1777 nmem_strdup (p->odr_out->mem, str) : 0;
1779 str = ZOOM_options_get(p->options, "contact-email");
1780 req->u.esRequest->toKeep->contact->email = str ?
1781 nmem_strdup (p->odr_out->mem, str) : 0;
1783 req->u.esRequest->toKeep->addlBilling = 0;
1785 /* not to keep part ... */
1786 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1787 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
1789 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
1790 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
1792 str = ZOOM_options_get(p->options, "itemorder-setname");
1795 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
1796 nmem_strdup (p->odr_out->mem, str);
1797 req->u.esRequest->notToKeep->resultSetItem->item =
1798 (int *) odr_malloc(p->odr_out, sizeof(int));
1800 str = ZOOM_options_get(p->options, "itemorder-item");
1801 *req->u.esRequest->notToKeep->resultSetItem->item =
1802 (str ? atoi(str) : 1);
1804 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
1810 ZOOM_package_send (ZOOM_package p, const char *type)
1817 odr_reset (p->odr_out);
1820 if (!strcmp(type, "itemorder"))
1823 apdu = create_es_package (p, VAL_ITEMORDER);
1826 r = odr_malloc (p->odr_out, sizeof(*r));
1828 r->direct_reference =
1829 yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1832 r->which = Z_External_itemOrder;
1833 r->indirect_reference = 0;
1834 r->u.itemOrder = encode_item_order (p);
1836 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
1841 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
1845 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE);
1846 task->u.package = p;
1847 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
1848 p->buf_out = xmalloc (p->len_out);
1849 memcpy (p->buf_out, buf, p->len_out);
1854 while (ZOOM_event (1, &c))
1861 ZOOM_API(ZOOM_package)
1862 ZOOM_connection_package (ZOOM_connection c, ZOOM_options options)
1864 ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p));
1867 p->odr_out = odr_createmem (ODR_ENCODE);
1868 p->options = ZOOM_options_create_with_parent2 (options, c->options);
1876 ZOOM_package_destroy(ZOOM_package p)
1881 if (p->refcount == 0)
1883 odr_destroy (p->odr_out);
1886 ZOOM_options_destroy (p->options);
1891 ZOOM_API(const char *)
1892 ZOOM_package_option_get (ZOOM_package p, const char *key)
1894 return ZOOM_options_get (p->options, key);
1898 ZOOM_package_option_set (ZOOM_package p, const char *key,
1901 ZOOM_options_set (p->options, key, val);
1904 static int ZOOM_connection_exec_task (ZOOM_connection c)
1906 ZOOM_task task = c->tasks;
1908 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1911 if (c->error != ZOOM_ERROR_NONE ||
1912 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1914 ZOOM_connection_remove_tasks (c);
1917 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
1918 task->which, task->running);
1922 switch (task->which)
1924 case ZOOM_TASK_SEARCH:
1925 /* see if search hasn't been sent yet. */
1926 if (ZOOM_connection_send_search (c))
1929 case ZOOM_TASK_RETRIEVE:
1930 if (send_present (c))
1933 case ZOOM_TASK_CONNECT:
1937 case ZOOM_TASK_SCAN:
1941 case ZOOM_TASK_PACKAGE:
1942 if (send_package(c))
1945 ZOOM_connection_remove_task (c);
1949 static int send_sort_present (ZOOM_connection c)
1951 int r = send_sort (c);
1953 r = send_present (c);
1957 static int es_response (ZOOM_connection c,
1958 Z_ExtendedServicesResponse *res)
1960 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
1962 if (res->diagnostics && res->num_diagnostics > 0)
1963 response_diag(c, res->diagnostics[0]);
1964 if (res->taskPackage &&
1965 res->taskPackage->which == Z_External_extendedService)
1967 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1968 Odr_oct *id = taskPackage->targetReference;
1971 ZOOM_options_setl (c->tasks->u.package->options,
1972 "targetReference", id->buf, id->len);
1978 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1980 Z_InitResponse *initrs;
1983 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1986 case Z_APDU_initResponse:
1987 initrs = apdu->u.initResponse;
1988 if (!*initrs->result)
1990 c->error = ZOOM_ERROR_INIT;
1995 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1997 xfree (c->cookie_in);
2000 c->cookie_in = xstrdup(cookie);
2001 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
2002 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
2003 c->support_named_resultsets = 1;
2006 assert (c->tasks->which == ZOOM_TASK_CONNECT);
2007 ZOOM_connection_remove_task (c);
2009 ZOOM_connection_exec_task (c);
2011 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
2013 NMEM tmpmem = nmem_create();
2014 Z_CharSetandLanguageNegotiation *p =
2015 yaz_get_charneg_record(initrs->otherInfo);
2019 char *charset=NULL, *lang=NULL;
2022 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &selected);
2023 yaz_log(LOG_DEBUG, "Target accepted: charset - %s,"
2024 "language - %s, select - %d",
2025 charset, lang, selected);
2027 nmem_destroy(tmpmem);
2031 case Z_APDU_searchResponse:
2032 handle_search_response (c, apdu->u.searchResponse);
2033 if (!send_sort_present (c))
2034 ZOOM_connection_remove_task (c);
2036 case Z_APDU_presentResponse:
2037 handle_present_response (c, apdu->u.presentResponse);
2038 if (!send_present (c))
2039 ZOOM_connection_remove_task (c);
2041 case Z_APDU_sortResponse:
2042 sort_response (c, apdu->u.sortResponse);
2043 if (!send_present (c))
2044 ZOOM_connection_remove_task (c);
2046 case Z_APDU_scanResponse:
2047 scan_response (c, apdu->u.scanResponse);
2048 ZOOM_connection_remove_task (c);
2050 case Z_APDU_extendedServicesResponse:
2051 es_response (c, apdu->u.extendedServicesResponse);
2052 ZOOM_connection_remove_task (c);
2055 if (c->reconnect_ok)
2058 c->tasks->running = 0;
2059 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2063 c->error = ZOOM_ERROR_CONNECTION_LOST;
2068 c->error = ZOOM_ERROR_DECODE;
2073 static int do_read (ZOOM_connection c)
2079 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
2080 ZOOM_connection_put_event (c, event);
2082 yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in);
2084 r = cs_get (c->cs, &c->buf_in, &c->len_in);
2089 if (c->reconnect_ok)
2092 c->reconnect_ok = 0;
2093 yaz_log (LOG_DEBUG, "reconnect read");
2094 c->tasks->running = 0;
2095 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2099 c->error= ZOOM_ERROR_CONNECTION_LOST;
2106 odr_reset (c->odr_in);
2107 odr_setbuf (c->odr_in, c->buf_in, r, 0);
2108 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
2109 ZOOM_connection_put_event (c, event);
2110 if (!z_APDU (c->odr_in, &apdu, 0, 0))
2112 c->error = ZOOM_ERROR_DECODE;
2116 handle_apdu (c, apdu);
2117 c->reconnect_ok = 0;
2122 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
2127 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
2128 ZOOM_connection_put_event (c, event);
2130 yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
2131 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
2133 if (c->reconnect_ok)
2136 c->reconnect_ok = 0;
2137 yaz_log (LOG_DEBUG, "reconnect write");
2138 c->tasks->running = 0;
2139 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2142 if (c->state == STATE_CONNECTING)
2143 c->error = ZOOM_ERROR_CONNECT;
2145 c->error = ZOOM_ERROR_CONNECTION_LOST;
2151 c->mask = ZOOM_SELECT_EXCEPT;
2152 if (c->cs->io_pending & CS_WANT_WRITE)
2153 c->mask += ZOOM_SELECT_WRITE;
2154 if (c->cs->io_pending & CS_WANT_READ)
2155 c->mask += ZOOM_SELECT_READ;
2156 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
2160 // c->reconnect_ok = 0;
2161 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
2162 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
2167 static int do_write(ZOOM_connection c)
2169 return do_write_ex (c, c->buf_out, c->len_out);
2173 ZOOM_API(const char *)
2174 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
2176 return ZOOM_options_get (c->options, key);
2180 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
2183 ZOOM_options_set (c->options, key, val);
2186 ZOOM_API(const char *)
2187 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
2189 return ZOOM_options_get (r->options, key);
2193 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
2196 ZOOM_options_set (r->options, key, val);
2201 ZOOM_connection_errcode (ZOOM_connection c)
2203 return ZOOM_connection_error (c, 0, 0);
2206 ZOOM_API(const char *)
2207 ZOOM_connection_errmsg (ZOOM_connection c)
2210 ZOOM_connection_error (c, &msg, 0);
2214 ZOOM_API(const char *)
2215 ZOOM_connection_addinfo (ZOOM_connection c)
2217 const char *addinfo;
2218 ZOOM_connection_error (c, 0, &addinfo);
2223 ZOOM_connection_error (ZOOM_connection c, const char **cp,
2224 const char **addinfo)
2226 int error = c->error;
2231 case ZOOM_ERROR_NONE:
2232 *cp = "No error"; break;
2233 case ZOOM_ERROR_CONNECT:
2234 *cp = "Connect failed"; break;
2235 case ZOOM_ERROR_MEMORY:
2236 *cp = "Out of memory"; break;
2237 case ZOOM_ERROR_ENCODE:
2238 *cp = "Encoding failed"; break;
2239 case ZOOM_ERROR_DECODE:
2240 *cp = "Decoding failed"; break;
2241 case ZOOM_ERROR_CONNECTION_LOST:
2242 *cp = "Connection lost"; break;
2243 case ZOOM_ERROR_INIT:
2244 *cp = "Init rejected"; break;
2245 case ZOOM_ERROR_INTERNAL:
2246 *cp = "Internal failure"; break;
2247 case ZOOM_ERROR_TIMEOUT:
2248 *cp = "Timeout"; break;
2250 *cp = diagbib1_str (error);
2256 *addinfo = c->addinfo;
2263 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
2265 ZOOM_Event event = 0;
2266 int r = cs_look(c->cs);
2267 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
2272 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2273 c->error = ZOOM_ERROR_CONNECT;
2275 ZOOM_connection_put_event (c, event);
2277 else if (r == CS_CONNECT)
2280 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2282 ret = cs_rcvconnect (c->cs);
2283 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
2286 c->mask = ZOOM_SELECT_EXCEPT;
2287 if (c->cs->io_pending & CS_WANT_WRITE)
2288 c->mask += ZOOM_SELECT_WRITE;
2289 if (c->cs->io_pending & CS_WANT_READ)
2290 c->mask += ZOOM_SELECT_READ;
2291 ZOOM_connection_put_event (c, event);
2295 ZOOM_connection_put_event (c, event);
2296 ZOOM_connection_send_init (c);
2297 c->state = STATE_ESTABLISHED;
2301 c->error = ZOOM_ERROR_CONNECT;
2303 ZOOM_connection_put_event (c, event);
2308 if (mask & ZOOM_SELECT_READ)
2310 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2317 ZOOM_connection_last_event(ZOOM_connection cs)
2320 return ZOOM_EVENT_NONE;
2321 return cs->last_event;
2325 ZOOM_event (int no, ZOOM_connection *cs)
2329 struct pollfd pollfds[1024];
2330 ZOOM_connection poll_cs[1024];
2333 fd_set input, output, except;
2338 for (i = 0; i<no; i++)
2340 ZOOM_connection c = cs[i];
2342 if (c && (event = ZOOM_connection_get_event(c)))
2344 ZOOM_Event_destroy (event);
2348 for (i = 0; i<no; i++)
2350 ZOOM_connection c = cs[i];
2352 if (c && ZOOM_connection_exec_task (c))
2354 if ((event = ZOOM_connection_get_event(c)))
2356 ZOOM_Event_destroy (event);
2369 for (i = 0; i<no; i++)
2371 ZOOM_connection c = cs[i];
2377 fd = z3950_connection_socket(c);
2378 mask = z3950_connection_mask(c);
2385 this_timeout = ZOOM_options_get_int (c->options, "timeout", -1);
2386 if (this_timeout != -1 && this_timeout < timeout)
2387 timeout = this_timeout;
2391 short poll_events = 0;
2393 if (mask & ZOOM_SELECT_READ)
2394 poll_events += POLLIN;
2395 if (mask & ZOOM_SELECT_WRITE)
2396 poll_events += POLLOUT;
2397 if (mask & ZOOM_SELECT_EXCEPT)
2398 poll_events += POLLERR;
2399 pollfds[nfds].fd = fd;
2400 pollfds[nfds].events = poll_events;
2401 pollfds[nfds].revents = 0;
2406 if (mask & ZOOM_SELECT_READ)
2408 FD_SET (fd, &input);
2411 if (mask & ZOOM_SELECT_WRITE)
2413 FD_SET (fd, &output);
2416 if (mask & ZOOM_SELECT_EXCEPT)
2418 FD_SET (fd, &except);
2423 if (timeout >= 5000)
2430 r = poll (pollfds, nfds, timeout * 1000);
2431 for (i = 0; i<nfds; i++)
2433 ZOOM_connection c = poll_cs[i];
2437 if (pollfds[i].revents & POLLIN)
2438 mask += ZOOM_SELECT_READ;
2439 if (pollfds[i].revents & POLLOUT)
2440 mask += ZOOM_SELECT_WRITE;
2441 if (pollfds[i].revents & POLLERR)
2442 mask += ZOOM_SELECT_EXCEPT;
2444 ZOOM_connection_do_io(c, mask);
2446 else if (r == 0 && c->mask)
2448 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2449 /* timeout and this connection was waiting */
2450 c->error = ZOOM_ERROR_TIMEOUT;
2452 ZOOM_connection_put_event(c, event);
2456 tv.tv_sec = timeout;
2458 yaz_log (LOG_DEBUG, "select start");
2459 r = select (max_fd+1, &input, &output, &except, &tv);
2460 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
2461 for (i = 0; i<no; i++)
2463 ZOOM_connection c = cs[i];
2468 fd = z3950_connection_socket(c);
2472 /* no timeout and real socket */
2473 if (FD_ISSET(fd, &input))
2474 mask += ZOOM_SELECT_READ;
2475 if (FD_ISSET(fd, &output))
2476 mask += ZOOM_SELECT_WRITE;
2477 if (FD_ISSET(fd, &except))
2478 mask += ZOOM_SELECT_EXCEPT;
2480 ZOOM_connection_do_io(c, mask);
2482 if (r == 0 && c->mask)
2484 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2485 /* timeout and this connection was waiting */
2486 c->error = ZOOM_ERROR_TIMEOUT;
2488 yaz_log (LOG_DEBUG, "timeout");
2489 ZOOM_connection_put_event(c, event);
2493 for (i = 0; i<no; i++)
2495 ZOOM_connection c = cs[i];
2497 if (c && (event = ZOOM_connection_get_event(c)))
2499 ZOOM_Event_destroy (event);