2 * $Id: zoom-c.c,v 1.40 2002-08-20 08:19:40 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 = strlen(npr->databaseName);
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)
1410 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1411 Z_PresentRequest *req = apdu->u.presentRequest;
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 resultset->start += i;
1459 resultset->count -= i;
1460 *req->resultSetStartPoint = resultset->start + 1;
1461 *req->numberOfRecordsRequested = resultset->count;
1462 assert (*req->numberOfRecordsRequested > 0);
1464 if (syntax && *syntax)
1465 req->preferredRecordSyntax =
1466 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1468 if (schema && *schema)
1470 Z_RecordComposition *compo = (Z_RecordComposition *)
1471 odr_malloc (c->odr_out, sizeof(*compo));
1473 req->recordComposition = compo;
1474 compo->which = Z_RecordComp_complex;
1475 compo->u.complex = (Z_CompSpec *)
1476 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1477 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1478 odr_malloc(c->odr_out, sizeof(bool_t));
1479 *compo->u.complex->selectAlternativeSyntax = 0;
1481 compo->u.complex->generic = (Z_Specification *)
1482 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1484 compo->u.complex->generic->schema = (Odr_oid *)
1485 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1487 if (!compo->u.complex->generic->schema)
1489 /* OID wasn't a schema! Try record syntax instead. */
1491 compo->u.complex->generic->schema = (Odr_oid *)
1492 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1494 if (elementSetName && *elementSetName)
1496 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1497 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1498 compo->u.complex->generic->elementSpec->which =
1499 Z_ElementSpec_elementSetName;
1500 compo->u.complex->generic->elementSpec->u.elementSetName =
1501 odr_strdup (c->odr_out, elementSetName);
1504 compo->u.complex->generic->elementSpec = 0;
1505 compo->u.complex->num_dbSpecific = 0;
1506 compo->u.complex->dbSpecific = 0;
1507 compo->u.complex->num_recordSyntax = 0;
1508 compo->u.complex->recordSyntax = 0;
1510 else if (elementSetName && *elementSetName)
1512 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1513 odr_malloc (c->odr_out, sizeof(*esn));
1514 Z_RecordComposition *compo = (Z_RecordComposition *)
1515 odr_malloc (c->odr_out, sizeof(*compo));
1517 esn->which = Z_ElementSetNames_generic;
1518 esn->u.generic = odr_strdup (c->odr_out, elementSetName);
1519 compo->which = Z_RecordComp_simple;
1520 compo->u.simple = esn;
1521 req->recordComposition = compo;
1523 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1524 send_APDU (c, apdu);
1528 ZOOM_API(ZOOM_scanset)
1529 ZOOM_connection_scan (ZOOM_connection c, const char *start)
1531 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1533 scan->connection = c;
1534 scan->odr = odr_createmem (ODR_DECODE);
1535 scan->options = ZOOM_options_create_with_parent (c->options);
1537 scan->scan_response = 0;
1539 if ((scan->termListAndStartPoint =
1540 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1543 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1544 task->u.scan.scan = scan;
1549 while (ZOOM_event (1, &c))
1557 ZOOM_scanset_destroy (ZOOM_scanset scan)
1562 if (scan->refcount == 0)
1564 odr_destroy (scan->odr);
1566 ZOOM_options_destroy (scan->options);
1571 static int send_package (ZOOM_connection c)
1576 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1578 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
1579 ZOOM_connection_put_event (c, event);
1581 do_write_ex (c, c->tasks->u.package->buf_out,
1582 c->tasks->u.package->len_out);
1586 static int send_scan (ZOOM_connection c)
1589 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1590 Z_ScanRequest *req = apdu->u.scanRequest;
1593 assert (c->tasks->which == ZOOM_TASK_SCAN);
1594 scan = c->tasks->u.scan.scan;
1596 req->termListAndStartPoint = scan->termListAndStartPoint;
1597 req->attributeSet = scan->attributeSet;
1599 *req->numberOfTermsRequested =
1600 ZOOM_options_get_int(scan->options, "number", 10);
1602 req->preferredPositionInResponse =
1603 odr_intdup (c->odr_out,
1604 ZOOM_options_get_int(scan->options, "position", 1));
1607 odr_intdup (c->odr_out,
1608 ZOOM_options_get_int(scan->options, "stepSize", 0));
1610 req->databaseNames = set_DatabaseNames (c, scan->options,
1611 &req->num_databaseNames);
1613 send_APDU (c, apdu);
1619 ZOOM_scanset_size (ZOOM_scanset scan)
1621 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1623 return scan->scan_response->entries->num_entries;
1626 ZOOM_API(const char *)
1627 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1630 const char *term = 0;
1631 size_t noent = ZOOM_scanset_size (scan);
1632 Z_ScanResponse *res = scan->scan_response;
1638 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1640 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1642 if (t->term->which == Z_Term_general)
1644 term = (const char *) t->term->u.general->buf;
1645 *len = t->term->u.general->len;
1647 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1652 ZOOM_API(const char *)
1653 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1655 return ZOOM_options_get (scan->options, key);
1659 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1662 ZOOM_options_set (scan->options, key, val);
1667 static Z_APDU *create_es_package (ZOOM_package p, int type)
1670 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
1671 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1673 *req->function = Z_ExtendedServicesRequest_create;
1675 str = ZOOM_options_get(p->options, "package-name");
1677 req->packageName = nmem_strdup (p->odr_out->mem, str);
1679 str = ZOOM_options_get(p->options, "user-id");
1681 req->userId = nmem_strdup (p->odr_out->mem, str);
1683 req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1686 str = ZOOM_options_get(p->options, "function");
1689 if (!strcmp (str, "create"))
1691 if (!strcmp (str, "delete"))
1693 if (!strcmp (str, "modify"))
1699 static const char *ill_array_lookup (void *clientData, const char *idx)
1701 ZOOM_package p = (ZOOM_package) clientData;
1702 return ZOOM_options_get (p->options, idx+4);
1705 static Z_External *encode_ill_request (ZOOM_package p)
1707 ODR out = p->odr_out;
1710 struct ill_get_ctl ctl;
1712 ctl.odr = p->odr_out;
1714 ctl.f = ill_array_lookup;
1716 req = ill_get_ILLRequest(&ctl, "ill", 0);
1718 if (!ill_Request (out, &req, 0, 0))
1720 int ill_request_size;
1721 char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1722 if (ill_request_buf)
1723 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1729 int illRequest_size = 0;
1730 char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0);
1732 oid.proto = PROTO_GENERAL;
1733 oid.oclass = CLASS_GENERAL;
1734 oid.value = VAL_ISO_ILL_1;
1736 r = (Z_External *) odr_malloc (out, sizeof(*r));
1737 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1738 r->indirect_reference = 0;
1740 r->which = Z_External_single;
1742 r->u.single_ASN1_type = (Odr_oct *)
1743 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1744 r->u.single_ASN1_type->buf = odr_malloc (out, illRequest_size);
1745 r->u.single_ASN1_type->len = illRequest_size;
1746 r->u.single_ASN1_type->size = illRequest_size;
1747 memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size);
1752 static Z_ItemOrder *encode_item_order(ZOOM_package p)
1754 Z_ItemOrder *req = odr_malloc (p->odr_out, sizeof(*req));
1757 req->which=Z_IOItemOrder_esRequest;
1758 req->u.esRequest = (Z_IORequest *)
1759 odr_malloc(p->odr_out,sizeof(Z_IORequest));
1761 /* to keep part ... */
1762 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1763 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
1764 req->u.esRequest->toKeep->supplDescription = 0;
1765 req->u.esRequest->toKeep->contact =
1766 odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
1768 str = ZOOM_options_get(p->options, "contact-name");
1769 req->u.esRequest->toKeep->contact->name = str ?
1770 nmem_strdup (p->odr_out->mem, str) : 0;
1772 str = ZOOM_options_get(p->options, "contact-phone");
1773 req->u.esRequest->toKeep->contact->phone = str ?
1774 nmem_strdup (p->odr_out->mem, str) : 0;
1776 str = ZOOM_options_get(p->options, "contact-email");
1777 req->u.esRequest->toKeep->contact->email = str ?
1778 nmem_strdup (p->odr_out->mem, str) : 0;
1780 req->u.esRequest->toKeep->addlBilling = 0;
1782 /* not to keep part ... */
1783 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1784 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
1786 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
1787 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
1789 str = ZOOM_options_get(p->options, "itemorder-setname");
1792 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
1793 nmem_strdup (p->odr_out->mem, str);
1794 req->u.esRequest->notToKeep->resultSetItem->item =
1795 (int *) odr_malloc(p->odr_out, sizeof(int));
1797 str = ZOOM_options_get(p->options, "itemorder-item");
1798 *req->u.esRequest->notToKeep->resultSetItem->item =
1799 (str ? atoi(str) : 1);
1801 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
1807 ZOOM_package_send (ZOOM_package p, const char *type)
1814 odr_reset (p->odr_out);
1817 if (!strcmp(type, "itemorder"))
1820 apdu = create_es_package (p, VAL_ITEMORDER);
1823 r = odr_malloc (p->odr_out, sizeof(*r));
1825 r->direct_reference =
1826 yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1829 r->which = Z_External_itemOrder;
1830 r->indirect_reference = 0;
1831 r->u.itemOrder = encode_item_order (p);
1833 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
1838 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
1842 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE);
1843 task->u.package = p;
1844 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
1845 p->buf_out = xmalloc (p->len_out);
1846 memcpy (p->buf_out, buf, p->len_out);
1851 while (ZOOM_event (1, &c))
1858 ZOOM_API(ZOOM_package)
1859 ZOOM_connection_package (ZOOM_connection c, ZOOM_options options)
1861 ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p));
1864 p->odr_out = odr_createmem (ODR_ENCODE);
1865 p->options = ZOOM_options_create_with_parent2 (options, c->options);
1873 ZOOM_package_destroy(ZOOM_package p)
1878 if (p->refcount == 0)
1880 odr_destroy (p->odr_out);
1883 ZOOM_options_destroy (p->options);
1888 ZOOM_API(const char *)
1889 ZOOM_package_option_get (ZOOM_package p, const char *key)
1891 return ZOOM_options_get (p->options, key);
1895 ZOOM_package_option_set (ZOOM_package p, const char *key,
1898 ZOOM_options_set (p->options, key, val);
1901 static int ZOOM_connection_exec_task (ZOOM_connection c)
1903 ZOOM_task task = c->tasks;
1905 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1908 if (c->error != ZOOM_ERROR_NONE ||
1909 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1911 ZOOM_connection_remove_tasks (c);
1914 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
1915 task->which, task->running);
1919 switch (task->which)
1921 case ZOOM_TASK_SEARCH:
1922 /* see if search hasn't been sent yet. */
1923 if (ZOOM_connection_send_search (c))
1926 case ZOOM_TASK_RETRIEVE:
1927 if (send_present (c))
1930 case ZOOM_TASK_CONNECT:
1934 case ZOOM_TASK_SCAN:
1938 case ZOOM_TASK_PACKAGE:
1939 if (send_package(c))
1942 ZOOM_connection_remove_task (c);
1946 static int send_sort_present (ZOOM_connection c)
1948 int r = send_sort (c);
1950 r = send_present (c);
1954 static int es_response (ZOOM_connection c,
1955 Z_ExtendedServicesResponse *res)
1957 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
1959 if (res->diagnostics && res->num_diagnostics > 0)
1960 response_diag(c, res->diagnostics[0]);
1961 if (res->taskPackage &&
1962 res->taskPackage->which == Z_External_extendedService)
1964 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1965 Odr_oct *id = taskPackage->targetReference;
1968 ZOOM_options_setl (c->tasks->u.package->options,
1969 "targetReference", id->buf, id->len);
1975 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1977 Z_InitResponse *initrs;
1980 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1983 case Z_APDU_initResponse:
1984 initrs = apdu->u.initResponse;
1985 if (!*initrs->result)
1987 c->error = ZOOM_ERROR_INIT;
1992 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1994 xfree (c->cookie_in);
1997 c->cookie_in = xstrdup(cookie);
1998 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1999 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
2000 c->support_named_resultsets = 1;
2003 assert (c->tasks->which == ZOOM_TASK_CONNECT);
2004 ZOOM_connection_remove_task (c);
2006 ZOOM_connection_exec_task (c);
2008 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
2010 NMEM tmpmem = nmem_create();
2011 Z_CharSetandLanguageNegotiation *p =
2012 yaz_get_charneg_record(initrs->otherInfo);
2016 char *charset=NULL, *lang=NULL;
2019 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &selected);
2020 yaz_log(LOG_DEBUG, "Target accepted: charset - %s,"
2021 "language - %s, select - %d",
2022 charset, lang, selected);
2024 nmem_destroy(tmpmem);
2028 case Z_APDU_searchResponse:
2029 handle_search_response (c, apdu->u.searchResponse);
2030 if (!send_sort_present (c))
2031 ZOOM_connection_remove_task (c);
2033 case Z_APDU_presentResponse:
2034 handle_present_response (c, apdu->u.presentResponse);
2035 if (!send_present (c))
2036 ZOOM_connection_remove_task (c);
2038 case Z_APDU_sortResponse:
2039 sort_response (c, apdu->u.sortResponse);
2040 if (!send_present (c))
2041 ZOOM_connection_remove_task (c);
2043 case Z_APDU_scanResponse:
2044 scan_response (c, apdu->u.scanResponse);
2045 ZOOM_connection_remove_task (c);
2047 case Z_APDU_extendedServicesResponse:
2048 es_response (c, apdu->u.extendedServicesResponse);
2049 ZOOM_connection_remove_task (c);
2052 if (c->reconnect_ok)
2055 c->tasks->running = 0;
2056 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2060 c->error = ZOOM_ERROR_CONNECTION_LOST;
2065 c->error = ZOOM_ERROR_DECODE;
2070 static int do_read (ZOOM_connection c)
2076 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
2077 ZOOM_connection_put_event (c, event);
2079 yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in);
2081 r = cs_get (c->cs, &c->buf_in, &c->len_in);
2086 if (c->reconnect_ok)
2089 c->reconnect_ok = 0;
2090 yaz_log (LOG_DEBUG, "reconnect read");
2091 c->tasks->running = 0;
2092 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2096 c->error= ZOOM_ERROR_CONNECTION_LOST;
2103 odr_reset (c->odr_in);
2104 odr_setbuf (c->odr_in, c->buf_in, r, 0);
2105 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
2106 ZOOM_connection_put_event (c, event);
2107 if (!z_APDU (c->odr_in, &apdu, 0, 0))
2109 c->error = ZOOM_ERROR_DECODE;
2113 handle_apdu (c, apdu);
2114 c->reconnect_ok = 0;
2119 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
2124 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
2125 ZOOM_connection_put_event (c, event);
2127 yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
2128 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
2130 if (c->reconnect_ok)
2133 c->reconnect_ok = 0;
2134 yaz_log (LOG_DEBUG, "reconnect write");
2135 c->tasks->running = 0;
2136 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2139 if (c->state == STATE_CONNECTING)
2140 c->error = ZOOM_ERROR_CONNECT;
2142 c->error = ZOOM_ERROR_CONNECTION_LOST;
2148 c->mask = ZOOM_SELECT_EXCEPT;
2149 if (c->cs->io_pending & CS_WANT_WRITE)
2150 c->mask += ZOOM_SELECT_WRITE;
2151 if (c->cs->io_pending & CS_WANT_READ)
2152 c->mask += ZOOM_SELECT_READ;
2153 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
2157 // c->reconnect_ok = 0;
2158 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
2159 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
2164 static int do_write(ZOOM_connection c)
2166 return do_write_ex (c, c->buf_out, c->len_out);
2170 ZOOM_API(const char *)
2171 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
2173 return ZOOM_options_get (c->options, key);
2177 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
2180 ZOOM_options_set (c->options, key, val);
2183 ZOOM_API(const char *)
2184 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
2186 return ZOOM_options_get (r->options, key);
2190 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
2193 ZOOM_options_set (r->options, key, val);
2198 ZOOM_connection_errcode (ZOOM_connection c)
2200 return ZOOM_connection_error (c, 0, 0);
2203 ZOOM_API(const char *)
2204 ZOOM_connection_errmsg (ZOOM_connection c)
2207 ZOOM_connection_error (c, &msg, 0);
2211 ZOOM_API(const char *)
2212 ZOOM_connection_addinfo (ZOOM_connection c)
2214 const char *addinfo;
2215 ZOOM_connection_error (c, 0, &addinfo);
2220 ZOOM_connection_error (ZOOM_connection c, const char **cp,
2221 const char **addinfo)
2223 int error = c->error;
2228 case ZOOM_ERROR_NONE:
2229 *cp = "No error"; break;
2230 case ZOOM_ERROR_CONNECT:
2231 *cp = "Connect failed"; break;
2232 case ZOOM_ERROR_MEMORY:
2233 *cp = "Out of memory"; break;
2234 case ZOOM_ERROR_ENCODE:
2235 *cp = "Encoding failed"; break;
2236 case ZOOM_ERROR_DECODE:
2237 *cp = "Decoding failed"; break;
2238 case ZOOM_ERROR_CONNECTION_LOST:
2239 *cp = "Connection lost"; break;
2240 case ZOOM_ERROR_INIT:
2241 *cp = "Init rejected"; break;
2242 case ZOOM_ERROR_INTERNAL:
2243 *cp = "Internal failure"; break;
2244 case ZOOM_ERROR_TIMEOUT:
2245 *cp = "Timeout"; break;
2247 *cp = diagbib1_str (error);
2253 *addinfo = c->addinfo;
2260 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
2262 ZOOM_Event event = 0;
2263 int r = cs_look(c->cs);
2264 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
2269 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2270 c->error = ZOOM_ERROR_CONNECT;
2272 ZOOM_connection_put_event (c, event);
2274 else if (r == CS_CONNECT)
2277 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2279 ret = cs_rcvconnect (c->cs);
2280 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
2283 c->mask = ZOOM_SELECT_EXCEPT;
2284 if (c->cs->io_pending & CS_WANT_WRITE)
2285 c->mask += ZOOM_SELECT_WRITE;
2286 if (c->cs->io_pending & CS_WANT_READ)
2287 c->mask += ZOOM_SELECT_READ;
2288 ZOOM_connection_put_event (c, event);
2292 ZOOM_connection_put_event (c, event);
2293 ZOOM_connection_send_init (c);
2294 c->state = STATE_ESTABLISHED;
2298 c->error = ZOOM_ERROR_CONNECT;
2300 ZOOM_connection_put_event (c, event);
2305 if (mask & ZOOM_SELECT_READ)
2307 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2314 ZOOM_connection_last_event(ZOOM_connection cs)
2317 return ZOOM_EVENT_NONE;
2318 return cs->last_event;
2322 ZOOM_event (int no, ZOOM_connection *cs)
2326 struct pollfd pollfds[1024];
2327 ZOOM_connection poll_cs[1024];
2330 fd_set input, output, except;
2335 for (i = 0; i<no; i++)
2337 ZOOM_connection c = cs[i];
2339 if (c && (event = ZOOM_connection_get_event(c)))
2341 ZOOM_Event_destroy (event);
2345 for (i = 0; i<no; i++)
2347 ZOOM_connection c = cs[i];
2349 if (c && ZOOM_connection_exec_task (c))
2351 if ((event = ZOOM_connection_get_event(c)))
2353 ZOOM_Event_destroy (event);
2366 for (i = 0; i<no; i++)
2368 ZOOM_connection c = cs[i];
2374 fd = z3950_connection_socket(c);
2375 mask = z3950_connection_mask(c);
2382 this_timeout = ZOOM_options_get_int (c->options, "timeout", -1);
2383 if (this_timeout != -1 && this_timeout < timeout)
2384 timeout = this_timeout;
2388 short poll_events = 0;
2390 if (mask & ZOOM_SELECT_READ)
2391 poll_events += POLLIN;
2392 if (mask & ZOOM_SELECT_WRITE)
2393 poll_events += POLLOUT;
2394 if (mask & ZOOM_SELECT_EXCEPT)
2395 poll_events += POLLERR;
2396 pollfds[nfds].fd = fd;
2397 pollfds[nfds].events = poll_events;
2398 pollfds[nfds].revents = 0;
2403 if (mask & ZOOM_SELECT_READ)
2405 FD_SET (fd, &input);
2408 if (mask & ZOOM_SELECT_WRITE)
2410 FD_SET (fd, &output);
2413 if (mask & ZOOM_SELECT_EXCEPT)
2415 FD_SET (fd, &except);
2420 if (timeout >= 5000)
2427 r = poll (pollfds, nfds, timeout * 1000);
2428 for (i = 0; i<nfds; i++)
2430 ZOOM_connection c = poll_cs[i];
2434 if (pollfds[i].revents & POLLIN)
2435 mask += ZOOM_SELECT_READ;
2436 if (pollfds[i].revents & POLLOUT)
2437 mask += ZOOM_SELECT_WRITE;
2438 if (pollfds[i].revents & POLLERR)
2439 mask += ZOOM_SELECT_EXCEPT;
2441 ZOOM_connection_do_io(c, mask);
2443 else if (r == 0 && c->mask)
2445 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2446 /* timeout and this connection was waiting */
2447 c->error = ZOOM_ERROR_TIMEOUT;
2449 ZOOM_connection_put_event(c, event);
2453 tv.tv_sec = timeout;
2455 yaz_log (LOG_DEBUG, "select start");
2456 r = select (max_fd+1, &input, &output, &except, &tv);
2457 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
2458 for (i = 0; i<no; i++)
2460 ZOOM_connection c = cs[i];
2465 fd = z3950_connection_socket(c);
2469 /* no timeout and real socket */
2470 if (FD_ISSET(fd, &input))
2471 mask += ZOOM_SELECT_READ;
2472 if (FD_ISSET(fd, &output))
2473 mask += ZOOM_SELECT_WRITE;
2474 if (FD_ISSET(fd, &except))
2475 mask += ZOOM_SELECT_EXCEPT;
2477 ZOOM_connection_do_io(c, mask);
2479 if (r == 0 && c->mask)
2481 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2482 /* timeout and this connection was waiting */
2483 c->error = ZOOM_ERROR_TIMEOUT;
2485 yaz_log (LOG_DEBUG, "timeout");
2486 ZOOM_connection_put_event(c, event);
2490 for (i = 0; i<no; i++)
2492 ZOOM_connection c = cs[i];
2494 if (c && (event = ZOOM_connection_get_event(c)))
2496 ZOOM_Event_destroy (event);