2 * $Id: zoom-c.c,v 1.33 2002-06-02 21:27:17 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,
156 const char *elementSetName);
158 ZOOM_API(ZOOM_connection)
159 ZOOM_connection_create (ZOOM_options options)
161 ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
166 c->state = STATE_IDLE;
167 c->error = ZOOM_ERROR_NONE;
175 c->options = ZOOM_options_create_with_parent(options);
180 c->charset = c->lang = 0;
187 c->odr_in = odr_createmem (ODR_DECODE);
188 c->odr_out = odr_createmem (ODR_ENCODE);
191 c->support_named_resultsets = 0;
192 c->last_event = ZOOM_EVENT_NONE;
194 c->m_queue_front = 0;
199 /* set database names. Take local databases (if set); otherwise
200 take databases given in ZURL (if set); otherwise use Default */
201 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
204 char **databaseNames;
207 const char *cp = ZOOM_options_get (options, "databaseName");
211 cp = strchr (con->host_port, '/');
218 while ((c = strchr(c, '+')))
226 databaseNames = (char**)
227 odr_malloc (con->odr_out, no * sizeof(*databaseNames));
231 c = strchr (cp, '+');
239 /* cp ptr to first char of db name, c is char
241 databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
242 memcpy (databaseNames[no], cp, c-cp);
243 databaseNames[no++][c-cp] = '\0';
248 databaseNames[no] = NULL;
250 return databaseNames;
253 ZOOM_API(ZOOM_connection)
254 ZOOM_connection_new (const char *host, int portnum)
256 ZOOM_connection c = ZOOM_connection_create (0);
258 ZOOM_connection_connect (c, host, portnum);
263 ZOOM_connection_connect(ZOOM_connection c,
264 const char *host, int portnum)
275 val = ZOOM_options_get (c->options, "proxy");
277 c->proxy = xstrdup (val);
282 val = ZOOM_options_get (c->options, "charset");
284 c->charset = xstrdup (val);
289 val = ZOOM_options_get (c->options, "lang");
291 c->lang = xstrdup (val);
295 xfree (c->host_port);
299 sprintf (hostn, "%.80s:%d", host, portnum);
300 c->host_port = xstrdup(hostn);
303 c->host_port = xstrdup(host);
305 ZOOM_options_set(c->options, "host", c->host_port);
307 val = ZOOM_options_get (c->options, "cookie");
309 c->cookie_out = xstrdup (val);
311 val = ZOOM_options_get (c->options, "clientIP");
313 c->client_IP = xstrdup (val);
315 c->async = ZOOM_options_get_bool (c->options, "async", 0);
317 c->error = ZOOM_ERROR_NONE;
319 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
323 while (ZOOM_event (1, &c))
329 ZOOM_query_create(void)
331 ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
336 s->odr = odr_createmem (ODR_ENCODE);
342 ZOOM_query_destroy(ZOOM_query s)
348 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
349 if (s->refcount == 0)
351 odr_destroy (s->odr);
357 ZOOM_query_prefix(ZOOM_query s, const char *str)
359 s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query));
360 s->query->which = Z_Query_type_1;
361 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
362 if (!s->query->u.type_1)
368 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
370 s->sort_spec = yaz_sort_spec (s->odr, criteria);
376 static int do_write(ZOOM_connection c);
379 ZOOM_connection_destroy(ZOOM_connection c)
386 for (r = c->resultsets; r; r = r->next)
391 odr_destroy (c->odr_in);
392 odr_destroy (c->odr_out);
393 ZOOM_options_destroy (c->options);
394 ZOOM_connection_remove_tasks (c);
395 xfree (c->host_port);
399 xfree (c->cookie_out);
400 xfree (c->cookie_in);
401 xfree (c->client_IP);
405 void ZOOM_resultset_addref (ZOOM_resultset r)
410 ZOOM_resultset ZOOM_resultset_create ()
412 ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
416 r->odr = odr_createmem (ODR_ENCODE);
430 ZOOM_API(ZOOM_resultset)
431 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
434 ZOOM_query s = ZOOM_query_create();
436 ZOOM_query_prefix (s, q);
438 r = ZOOM_connection_search (c, s);
439 ZOOM_query_destroy (s);
443 ZOOM_API(ZOOM_resultset)
444 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
446 ZOOM_resultset r = ZOOM_resultset_create ();
450 r->r_sort_spec = q->sort_spec;
451 r->r_query = q->query;
454 r->options = ZOOM_options_create_with_parent(c->options);
456 r->start = ZOOM_options_get_int(r->options, "start", 0);
457 r->count = ZOOM_options_get_int(r->options, "count", 0);
458 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
459 cp = ZOOM_options_get (r->options, "setname");
461 r->setname = xstrdup (cp);
465 r->next = c->resultsets;
468 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
469 task->u.search.resultset = r;
470 ZOOM_resultset_addref (r);
476 while (ZOOM_event (1, &c))
483 ZOOM_resultset_destroy(ZOOM_resultset r)
488 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
489 if (r->refcount == 0)
491 ZOOM_record_cache rc;
493 for (rc = r->record_cache; rc; rc = rc->next)
494 if (rc->rec.wrbuf_marc)
495 wrbuf_free (rc->rec.wrbuf_marc, 1);
498 /* remove ourselves from the resultsets in connection */
499 ZOOM_resultset *rp = &r->connection->resultsets;
502 assert (*rp); /* we must be in this list!! */
504 { /* OK, we're here - take us out of it */
511 ZOOM_query_destroy (r->search);
512 ZOOM_options_destroy (r->options);
513 odr_destroy (r->odr);
520 ZOOM_resultset_size (ZOOM_resultset r)
525 static void do_close (ZOOM_connection c)
531 c->state = STATE_IDLE;
534 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
535 int force_sync, int start, int count)
545 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
546 task->u.retrieve.resultset = r;
547 task->u.retrieve.start = start;
548 task->u.retrieve.count = count;
550 ZOOM_resultset_addref (r);
552 if (!r->connection->async || force_sync)
553 while (r->connection && ZOOM_event (1, &r->connection))
558 ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
559 size_t start, size_t count)
561 int force_present = 0;
567 ZOOM_resultset_retrieve (r, force_present, start, count);
571 for (i = 0; i< count; i++)
572 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
576 static int do_connect (ZOOM_connection c)
579 const char *effective_host;
582 effective_host = c->proxy;
584 effective_host = c->host_port;
586 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
589 c->cs = cs_create_host (effective_host, 0, &add);
593 int ret = cs_connect (c->cs, add);
596 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
597 ZOOM_connection_put_event(c, event);
598 ZOOM_connection_send_init(c);
599 c->state = STATE_ESTABLISHED;
604 c->state = STATE_CONNECTING;
605 c->mask = ZOOM_SELECT_EXCEPT;
606 if (c->cs->io_pending & CS_WANT_WRITE)
607 c->mask += ZOOM_SELECT_WRITE;
608 if (c->cs->io_pending & CS_WANT_READ)
609 c->mask += ZOOM_SELECT_READ;
613 c->state = STATE_IDLE;
614 c->error = ZOOM_ERROR_CONNECT;
618 int z3950_connection_socket(ZOOM_connection c)
621 return cs_fileno(c->cs);
625 int z3950_connection_mask(ZOOM_connection c)
632 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
637 Z_OtherInformation **oi;
639 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
643 Z_OtherInformation **oi;
645 yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
647 if (!z_APDU(out, &a, 0, 0))
649 FILE *outf = fopen("/tmp/apdu.txt", "a");
652 ODR odr_pr = odr_createmem(ODR_PRINT);
653 fprintf (outf, "a=%p\n", a);
654 odr_setprint(odr_pr, outf);
655 z_APDU(odr_pr, &a, 0, 0);
658 c->error = ZOOM_ERROR_ENCODE;
666 static int send_APDU (ZOOM_connection c, Z_APDU *a)
670 if (encode_APDU(c, a, c->odr_out))
672 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
673 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
674 ZOOM_connection_put_event (c, event);
675 odr_reset(c->odr_out);
680 static int ZOOM_connection_send_init (ZOOM_connection c)
683 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
684 Z_InitRequest *ireq = apdu->u.initRequest;
685 Z_IdAuthentication *auth = (Z_IdAuthentication *)
686 odr_malloc(c->odr_out, sizeof(*auth));
687 const char *auth_groupId = ZOOM_options_get (c->options, "group");
688 const char *auth_userId = ZOOM_options_get (c->options, "user");
689 const char *auth_password = ZOOM_options_get (c->options, "pass");
691 ODR_MASK_SET(ireq->options, Z_Options_search);
692 ODR_MASK_SET(ireq->options, Z_Options_present);
693 ODR_MASK_SET(ireq->options, Z_Options_scan);
694 ODR_MASK_SET(ireq->options, Z_Options_sort);
695 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
696 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
698 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
699 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
700 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
702 impname = ZOOM_options_get (c->options, "implementationName");
703 ireq->implementationName =
704 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
705 strcpy (ireq->implementationName, "");
708 strcat (ireq->implementationName, impname);
709 strcat (ireq->implementationName, "/");
711 strcat (ireq->implementationName, "ZOOM-C/YAZ");
713 *ireq->maximumRecordSize =
714 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
715 *ireq->preferredMessageSize =
716 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
718 if (auth_groupId || auth_password)
720 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
723 if (auth_groupId && *auth_groupId)
725 pass->groupId = (char *)
726 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
727 strcpy(pass->groupId, auth_groupId);
731 if (auth_userId && *auth_userId)
733 pass->userId = (char *)
734 odr_malloc(c->odr_out, strlen(auth_userId)+1);
735 strcpy(pass->userId, auth_userId);
739 if (auth_password && *auth_password)
741 pass->password = (char *)
742 odr_malloc(c->odr_out, strlen(auth_password)+1);
743 strcpy(pass->password, auth_password);
748 auth->which = Z_IdAuthentication_idPass;
749 auth->u.idPass = pass;
750 ireq->idAuthentication = auth;
753 else if (auth_userId)
755 auth->which = Z_IdAuthentication_open;
756 auth->u.open = (char *)
757 odr_malloc(c->odr_out, strlen(auth_userId)+1);
758 strcpy(auth->u.open, auth_userId);
759 ireq->idAuthentication = auth;
762 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
763 VAL_PROXY, 1, c->host_port);
764 if (c->charset||c->lang)
766 Z_OtherInformation **oi;
767 Z_OtherInformationUnit *oi_unit;
769 yaz_oi_APDU(apdu, &oi);
771 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
773 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
775 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
776 oi_unit->information.externallyDefinedInfo =
777 yaz_set_proposal_charneg(c->odr_out,
778 (const char **)&c->charset, (c->charset) ? 1:0,
779 (const char **)&c->lang, (c->lang) ? 1:0, 1);
788 static int ZOOM_connection_send_search (ZOOM_connection c)
791 int lslb, ssub, mspn;
793 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
794 Z_SearchRequest *search_req = apdu->u.searchRequest;
795 const char *elementSetName;
796 const char *smallSetElementSetName;
797 const char *mediumSetElementSetName;
801 assert (c->tasks->which == ZOOM_TASK_SEARCH);
803 r = c->tasks->u.search.resultset;
806 ZOOM_options_get (r->options, "elementSetName");
807 smallSetElementSetName =
808 ZOOM_options_get (r->options, "smallSetElementSetName");
809 mediumSetElementSetName =
810 ZOOM_options_get (r->options, "mediumSetElementSetName");
812 ZOOM_options_get (r->options, "schema");
814 if (!smallSetElementSetName)
815 smallSetElementSetName = elementSetName;
817 if (!mediumSetElementSetName)
818 mediumSetElementSetName = elementSetName;
823 /* prepare query for the search request */
824 search_req->query = r->r_query;
826 search_req->databaseNames =
827 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
829 /* get syntax (no need to provide unless piggyback is in effect) */
830 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
832 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
833 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
834 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
835 if (lslb != -1 && ssub != -1 && mspn != -1)
837 /* So're a Z39.50 expert? Let's hope you don't do sort */
838 *search_req->largeSetLowerBound = lslb;
839 *search_req->smallSetUpperBound = ssub;
840 *search_req->mediumSetPresentNumber = mspn;
842 else if (r->start == 0 && r->count > 0
843 && r->piggyback && !r->r_sort_spec && !schema)
845 /* Regular piggyback - do it unless we're going to do sort */
846 *search_req->largeSetLowerBound = 2000000000;
847 *search_req->smallSetUpperBound = r->count;
848 *search_req->mediumSetPresentNumber = r->count;
849 smallSetElementSetName = 0; /* no need to provide this */
853 /* non-piggyback. Need not provide elementsets or syntaxes .. */
854 smallSetElementSetName = 0;
855 mediumSetElementSetName = 0;
858 if (smallSetElementSetName && *smallSetElementSetName)
860 Z_ElementSetNames *esn = (Z_ElementSetNames *)
861 odr_malloc (c->odr_out, sizeof(*esn));
863 esn->which = Z_ElementSetNames_generic;
864 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
865 search_req->smallSetElementSetNames = esn;
867 if (mediumSetElementSetName && *mediumSetElementSetName)
869 Z_ElementSetNames *esn = (Z_ElementSetNames *)
870 odr_malloc (c->odr_out, sizeof(*esn));
872 esn->which = Z_ElementSetNames_generic;
873 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
874 search_req->mediumSetElementSetNames = esn;
877 search_req->preferredRecordSyntax =
878 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
882 if (c->support_named_resultsets)
886 /* find the lowest unused ordinal so that we re-use
887 result sets on the server. */
888 for (ord = 1; ; ord++)
891 sprintf (setname, "%d", ord);
892 for (rp = c->resultsets; rp; rp = rp->next)
893 if (rp->setname && !strcmp (rp->setname, setname))
898 r->setname = xstrdup (setname);
899 yaz_log (LOG_DEBUG, "allocating %s", r->setname);
902 r->setname = xstrdup ("default");
903 ZOOM_options_set (r->options, "setname", r->setname);
905 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
906 /* send search request */
911 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
913 Z_DefaultDiagFormat *r;
918 if (p->which != Z_DiagRec_defaultFormat)
920 c->error = ZOOM_ERROR_DECODE;
923 r = p->u.defaultFormat;
926 case Z_DefaultDiagFormat_v2Addinfo:
927 addinfo = r->u.v2Addinfo;
929 case Z_DefaultDiagFormat_v3Addinfo:
930 addinfo = r->u.v3Addinfo;
934 c->addinfo = xstrdup (addinfo);
935 c->error = *r->condition;
938 ZOOM_API(ZOOM_record)
939 ZOOM_record_clone (ZOOM_record srec)
946 odr_enc = odr_createmem(ODR_ENCODE);
947 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
949 buf = odr_getbuf (odr_enc, &size, 0);
951 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
952 nrec->odr = odr_createmem(ODR_DECODE);
953 nrec->wrbuf_marc = 0;
954 odr_setbuf (nrec->odr, buf, size, 0);
955 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
957 odr_destroy (odr_enc);
961 ZOOM_API(ZOOM_record)
962 ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
964 return record_cache_lookup (s, pos, 0);
967 ZOOM_API(ZOOM_record)
968 ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
970 ZOOM_resultset_retrieve (r, 1, pos, 1);
971 return ZOOM_resultset_record_immediate (r, pos);
975 ZOOM_record_destroy (ZOOM_record rec)
980 wrbuf_free (rec->wrbuf_marc, 1);
981 odr_destroy (rec->odr);
985 ZOOM_API(const char *)
986 ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
988 Z_NamePlusRecord *npr;
991 *len = 0; /* default return */
998 if (!strcmp (type, "database"))
1001 *len = strlen(npr->databaseName);
1002 return npr->databaseName;
1004 else if (!strcmp (type, "syntax"))
1006 if (npr->which == Z_NamePlusRecord_databaseRecord)
1008 Z_External *r = (Z_External *) npr->u.databaseRecord;
1009 oident *ent = oid_getentbyoid(r->direct_reference);
1013 *len = strlen(ent->desc);
1019 else if (!strcmp (type, "render") &&
1020 npr->which == Z_NamePlusRecord_databaseRecord)
1022 Z_External *r = (Z_External *) npr->u.databaseRecord;
1023 oident *ent = oid_getentbyoid(r->direct_reference);
1025 if (r->which == Z_External_sutrs)
1027 if (len) *len = r->u.sutrs->len;
1028 return (const char *) r->u.sutrs->buf;
1030 else if (r->which == Z_External_octet)
1039 case VAL_APPLICATION_XML:
1042 if (!rec->wrbuf_marc)
1043 rec->wrbuf_marc = wrbuf_alloc();
1044 wrbuf_rewind (rec->wrbuf_marc);
1045 if (yaz_marc_decode ((const char *)
1046 r->u.octet_aligned->buf,
1048 r->u.octet_aligned->len,
1051 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1052 return wrbuf_buf(rec->wrbuf_marc);
1055 if (len) *len = r->u.octet_aligned->len;
1056 return (const char *) r->u.octet_aligned->buf;
1058 else if (r->which == Z_External_grs1)
1065 else if (!strcmp (type, "xml") &&
1066 npr->which == Z_NamePlusRecord_databaseRecord)
1068 Z_External *r = (Z_External *) npr->u.databaseRecord;
1069 oident *ent = oid_getentbyoid(r->direct_reference);
1071 if (r->which == Z_External_sutrs)
1073 if (len) *len = r->u.sutrs->len;
1074 return (const char *) r->u.sutrs->buf;
1076 else if (r->which == Z_External_octet)
1085 case VAL_APPLICATION_XML:
1088 if (!rec->wrbuf_marc)
1089 rec->wrbuf_marc = wrbuf_alloc();
1090 wrbuf_rewind (rec->wrbuf_marc);
1091 if (yaz_marc_decode ((const char *)
1092 r->u.octet_aligned->buf,
1094 r->u.octet_aligned->len,
1097 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1098 return wrbuf_buf(rec->wrbuf_marc);
1101 if (len) *len = r->u.octet_aligned->len;
1102 return (const char *) r->u.octet_aligned->buf;
1104 else if (r->which == Z_External_grs1)
1111 else if (!strcmp (type, "raw"))
1113 if (npr->which == Z_NamePlusRecord_databaseRecord)
1114 return (const char *) npr->u.databaseRecord;
1120 static void record_cache_add (ZOOM_resultset r,
1121 Z_NamePlusRecord *npr,
1123 const char *elementSetName)
1125 ZOOM_record_cache rc;
1127 for (rc = r->record_cache; rc; rc = rc->next)
1131 if ((!elementSetName && !rc->elementSetName)
1132 || (elementSetName && rc->elementSetName &&
1133 !strcmp (elementSetName, rc->elementSetName)))
1135 /* not destroying rc->npr (it's handled by nmem )*/
1137 /* keeping wrbuf_marc too */
1142 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1145 rc->rec.wrbuf_marc = 0;
1147 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1149 rc->elementSetName = 0;
1151 rc->next = r->record_cache;
1152 r->record_cache = rc;
1155 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
1157 const char *elementSetName)
1159 ZOOM_record_cache rc;
1161 for (rc = r->record_cache; rc; rc = rc->next)
1165 if ((!elementSetName && !rc->elementSetName)
1166 || (elementSetName && rc->elementSetName &&
1167 !strcmp (elementSetName, rc->elementSetName)))
1174 static void handle_records (ZOOM_connection c, Z_Records *sr,
1177 ZOOM_resultset resultset;
1181 switch (c->tasks->which)
1183 case ZOOM_TASK_SEARCH:
1184 resultset = c->tasks->u.search.resultset;
1186 case ZOOM_TASK_RETRIEVE:
1187 resultset = c->tasks->u.retrieve.resultset;
1192 if (sr && sr->which == Z_Records_NSD)
1194 Z_DiagRec dr, *dr_p = &dr;
1195 dr.which = Z_DiagRec_defaultFormat;
1196 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1198 response_diag (c, dr_p);
1200 else if (sr && sr->which == Z_Records_multipleNSD)
1202 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1203 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1205 c->error = ZOOM_ERROR_DECODE;
1209 if (resultset->count + resultset->start > resultset->size)
1210 resultset->count = resultset->size - resultset->start;
1211 if (resultset->count < 0)
1212 resultset->count = 0;
1213 if (sr && sr->which == Z_Records_DBOSD)
1216 NMEM nmem = odr_extract_mem (c->odr_in);
1217 Z_NamePlusRecordList *p =
1218 sr->u.databaseOrSurDiagnostics;
1219 for (i = 0; i<p->num_records; i++)
1221 record_cache_add (resultset, p->records[i],
1222 i+ resultset->start, 0);
1224 /* transfer our response to search_nmem .. we need it later */
1225 nmem_transfer (resultset->odr->mem, nmem);
1226 nmem_destroy (nmem);
1227 if (present_phase && p->num_records == 0)
1229 /* present response and we didn't get any records! */
1230 c->error = ZOOM_ERROR_DECODE;
1233 else if (present_phase)
1235 /* present response and we didn't get any records! */
1236 c->error = ZOOM_ERROR_DECODE;
1241 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1243 handle_records (c, pr->records, 1);
1246 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1248 ZOOM_resultset resultset;
1250 yaz_log (LOG_DEBUG, "got search response");
1252 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1255 resultset = c->tasks->u.search.resultset;
1257 resultset->size = *sr->resultCount;
1258 handle_records (c, sr->records, 0);
1261 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1263 if (res->diagnostics && res->num_diagnostics > 0)
1264 response_diag (c, res->diagnostics[0]);
1267 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1269 NMEM nmem = odr_extract_mem (c->odr_in);
1272 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1274 scan = c->tasks->u.scan.scan;
1276 if (res->entries && res->entries->nonsurrogateDiagnostics)
1277 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1278 scan->scan_response = res;
1279 nmem_transfer (scan->odr->mem, nmem);
1281 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1282 if (res->positionOfTerm)
1283 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1284 if (res->scanStatus)
1285 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1286 if (res->numberOfEntriesReturned)
1287 ZOOM_options_set_int (scan->options, "number",
1288 *res->numberOfEntriesReturned);
1289 nmem_destroy (nmem);
1293 static int send_sort (ZOOM_connection c)
1295 ZOOM_resultset resultset;
1297 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1300 resultset = c->tasks->u.search.resultset;
1304 resultset->r_sort_spec = 0;
1307 if (resultset->r_sort_spec)
1309 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1310 Z_SortRequest *req = apdu->u.sortRequest;
1312 req->num_inputResultSetNames = 1;
1313 req->inputResultSetNames = (Z_InternationalString **)
1314 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1315 req->inputResultSetNames[0] =
1316 odr_strdup (c->odr_out, resultset->setname);
1317 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1318 req->sortSequence = resultset->r_sort_spec;
1319 resultset->r_sort_spec = 0;
1320 send_APDU (c, apdu);
1326 static int send_present (ZOOM_connection c)
1328 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1329 Z_PresentRequest *req = apdu->u.presentRequest;
1331 const char *syntax =
1332 ZOOM_options_get (c->options, "preferredRecordSyntax");
1333 const char *element =
1334 ZOOM_options_get (c->options, "elementSetName");
1335 const char *schema =
1336 ZOOM_options_get (c->options, "schema");
1337 ZOOM_resultset resultset;
1342 switch (c->tasks->which)
1344 case ZOOM_TASK_SEARCH:
1345 resultset = c->tasks->u.search.resultset;
1347 case ZOOM_TASK_RETRIEVE:
1348 resultset = c->tasks->u.retrieve.resultset;
1349 resultset->start = c->tasks->u.retrieve.start;
1350 resultset->count = c->tasks->u.retrieve.count;
1352 if (resultset->start >= resultset->size)
1354 if (resultset->start + resultset->count > resultset->size)
1355 resultset->count = resultset->size - resultset->start;
1361 if (c->error) /* don't continue on error */
1363 if (resultset->start < 0)
1365 for (i = 0; i<resultset->count; i++)
1368 record_cache_lookup (resultset, i + resultset->start, 0);
1372 if (i == resultset->count)
1375 resultset->start += i;
1376 resultset->count -= i;
1377 *req->resultSetStartPoint = resultset->start + 1;
1378 *req->numberOfRecordsRequested = resultset->count;
1379 assert (*req->numberOfRecordsRequested > 0);
1381 if (syntax && *syntax)
1382 req->preferredRecordSyntax =
1383 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1385 if (schema && *schema)
1387 Z_RecordComposition *compo = (Z_RecordComposition *)
1388 odr_malloc (c->odr_out, sizeof(*compo));
1390 req->recordComposition = compo;
1391 compo->which = Z_RecordComp_complex;
1392 compo->u.complex = (Z_CompSpec *)
1393 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1394 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1395 odr_malloc(c->odr_out, sizeof(bool_t));
1396 *compo->u.complex->selectAlternativeSyntax = 0;
1398 compo->u.complex->generic = (Z_Specification *)
1399 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1401 compo->u.complex->generic->schema = (Odr_oid *)
1402 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1404 if (!compo->u.complex->generic->schema)
1406 /* OID wasn't a schema! Try record syntax instead. */
1408 compo->u.complex->generic->schema = (Odr_oid *)
1409 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1411 if (element && *element)
1413 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1414 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1415 compo->u.complex->generic->elementSpec->which =
1416 Z_ElementSpec_elementSetName;
1417 compo->u.complex->generic->elementSpec->u.elementSetName =
1418 odr_strdup (c->odr_out, element);
1421 compo->u.complex->generic->elementSpec = 0;
1422 compo->u.complex->num_dbSpecific = 0;
1423 compo->u.complex->dbSpecific = 0;
1424 compo->u.complex->num_recordSyntax = 0;
1425 compo->u.complex->recordSyntax = 0;
1427 else if (element && *element)
1429 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1430 odr_malloc (c->odr_out, sizeof(*esn));
1431 Z_RecordComposition *compo = (Z_RecordComposition *)
1432 odr_malloc (c->odr_out, sizeof(*compo));
1434 esn->which = Z_ElementSetNames_generic;
1435 esn->u.generic = odr_strdup (c->odr_out, element);
1436 compo->which = Z_RecordComp_simple;
1437 compo->u.simple = esn;
1438 req->recordComposition = compo;
1440 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1441 send_APDU (c, apdu);
1445 ZOOM_API(ZOOM_scanset)
1446 ZOOM_connection_scan (ZOOM_connection c, const char *start)
1448 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1450 scan->connection = c;
1451 scan->odr = odr_createmem (ODR_DECODE);
1452 scan->options = ZOOM_options_create_with_parent (c->options);
1454 scan->scan_response = 0;
1456 if ((scan->termListAndStartPoint =
1457 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1460 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1461 task->u.scan.scan = scan;
1466 while (ZOOM_event (1, &c))
1474 ZOOM_scanset_destroy (ZOOM_scanset scan)
1479 if (scan->refcount == 0)
1481 odr_destroy (scan->odr);
1483 ZOOM_options_destroy (scan->options);
1488 static int send_package (ZOOM_connection c)
1493 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1495 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
1496 ZOOM_connection_put_event (c, event);
1498 do_write_ex (c, c->tasks->u.package->buf_out,
1499 c->tasks->u.package->len_out);
1503 static int send_scan (ZOOM_connection c)
1506 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1507 Z_ScanRequest *req = apdu->u.scanRequest;
1510 assert (c->tasks->which == ZOOM_TASK_SCAN);
1511 scan = c->tasks->u.scan.scan;
1513 req->termListAndStartPoint = scan->termListAndStartPoint;
1514 req->attributeSet = scan->attributeSet;
1516 *req->numberOfTermsRequested =
1517 ZOOM_options_get_int(scan->options, "number", 10);
1519 req->preferredPositionInResponse =
1520 odr_intdup (c->odr_out,
1521 ZOOM_options_get_int(scan->options, "position", 1));
1524 odr_intdup (c->odr_out,
1525 ZOOM_options_get_int(scan->options, "stepSize", 0));
1527 req->databaseNames = set_DatabaseNames (c, scan->options,
1528 &req->num_databaseNames);
1530 send_APDU (c, apdu);
1536 ZOOM_scanset_size (ZOOM_scanset scan)
1538 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1540 return scan->scan_response->entries->num_entries;
1543 ZOOM_API(const char *)
1544 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1547 const char *term = 0;
1548 size_t noent = ZOOM_scanset_size (scan);
1549 Z_ScanResponse *res = scan->scan_response;
1555 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1557 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1559 if (t->term->which == Z_Term_general)
1561 term = (const char *) t->term->u.general->buf;
1562 *len = t->term->u.general->len;
1564 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1569 ZOOM_API(const char *)
1570 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1572 return ZOOM_options_get (scan->options, key);
1576 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1579 ZOOM_options_set (scan->options, key, val);
1584 static Z_APDU *create_es_package (ZOOM_package p, int type)
1587 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
1588 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1590 *req->function = Z_ExtendedServicesRequest_create;
1592 str = ZOOM_options_get(p->options, "package-name");
1594 req->packageName = nmem_strdup (p->odr_out->mem, str);
1596 str = ZOOM_options_get(p->options, "user-id");
1598 req->userId = nmem_strdup (p->odr_out->mem, str);
1600 req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1603 str = ZOOM_options_get(p->options, "function");
1606 if (!strcmp (str, "create"))
1608 if (!strcmp (str, "delete"))
1610 if (!strcmp (str, "modify"))
1616 static const char *ill_array_lookup (void *clientData, const char *idx)
1618 ZOOM_package p = (ZOOM_package) clientData;
1619 return ZOOM_options_get (p->options, idx+4);
1622 static Z_External *encode_ill_request (ZOOM_package p)
1624 ODR out = p->odr_out;
1627 struct ill_get_ctl ctl;
1629 ctl.odr = p->odr_out;
1631 ctl.f = ill_array_lookup;
1633 req = ill_get_ILLRequest(&ctl, "ill", 0);
1635 if (!ill_Request (out, &req, 0, 0))
1637 int ill_request_size;
1638 char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1639 if (ill_request_buf)
1640 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1646 int illRequest_size = 0;
1647 char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0);
1649 oid.proto = PROTO_GENERAL;
1650 oid.oclass = CLASS_GENERAL;
1651 oid.value = VAL_ISO_ILL_1;
1653 r = (Z_External *) odr_malloc (out, sizeof(*r));
1654 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1655 r->indirect_reference = 0;
1657 r->which = Z_External_single;
1659 r->u.single_ASN1_type = (Odr_oct *)
1660 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1661 r->u.single_ASN1_type->buf = odr_malloc (out, illRequest_size);
1662 r->u.single_ASN1_type->len = illRequest_size;
1663 r->u.single_ASN1_type->size = illRequest_size;
1664 memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size);
1669 static Z_ItemOrder *encode_item_order(ZOOM_package p)
1671 Z_ItemOrder *req = odr_malloc (p->odr_out, sizeof(*req));
1674 req->which=Z_IOItemOrder_esRequest;
1675 req->u.esRequest = (Z_IORequest *)
1676 odr_malloc(p->odr_out,sizeof(Z_IORequest));
1678 /* to keep part ... */
1679 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1680 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
1681 req->u.esRequest->toKeep->supplDescription = 0;
1682 req->u.esRequest->toKeep->contact =
1683 odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
1685 str = ZOOM_options_get(p->options, "contact-name");
1686 req->u.esRequest->toKeep->contact->name = str ?
1687 nmem_strdup (p->odr_out->mem, str) : 0;
1689 str = ZOOM_options_get(p->options, "contact-phone");
1690 req->u.esRequest->toKeep->contact->phone = str ?
1691 nmem_strdup (p->odr_out->mem, str) : 0;
1693 str = ZOOM_options_get(p->options, "contact-email");
1694 req->u.esRequest->toKeep->contact->email = str ?
1695 nmem_strdup (p->odr_out->mem, str) : 0;
1697 req->u.esRequest->toKeep->addlBilling = 0;
1699 /* not to keep part ... */
1700 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1701 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
1703 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
1704 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
1706 str = ZOOM_options_get(p->options, "itemorder-setname");
1709 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
1710 nmem_strdup (p->odr_out->mem, str);
1711 req->u.esRequest->notToKeep->resultSetItem->item =
1712 (int *) odr_malloc(p->odr_out, sizeof(int));
1714 str = ZOOM_options_get(p->options, "itemorder-item");
1715 *req->u.esRequest->notToKeep->resultSetItem->item =
1716 (str ? atoi(str) : 1);
1718 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
1724 ZOOM_package_send (ZOOM_package p, const char *type)
1731 odr_reset (p->odr_out);
1734 if (!strcmp(type, "itemorder"))
1737 apdu = create_es_package (p, VAL_ITEMORDER);
1740 r = odr_malloc (p->odr_out, sizeof(*r));
1742 r->direct_reference =
1743 yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1746 r->which = Z_External_itemOrder;
1747 r->indirect_reference = 0;
1748 r->u.itemOrder = encode_item_order (p);
1750 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
1755 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
1759 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE);
1760 task->u.package = p;
1761 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
1762 p->buf_out = xmalloc (p->len_out);
1763 memcpy (p->buf_out, buf, p->len_out);
1768 while (ZOOM_event (1, &c))
1775 ZOOM_API(ZOOM_package)
1776 ZOOM_connection_package (ZOOM_connection c, ZOOM_options options)
1778 ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p));
1781 p->odr_out = odr_createmem (ODR_ENCODE);
1782 p->options = ZOOM_options_create_with_parent2 (options, c->options);
1790 ZOOM_package_destroy(ZOOM_package p)
1795 if (p->refcount == 0)
1797 odr_destroy (p->odr_out);
1800 ZOOM_options_destroy (p->options);
1805 ZOOM_API(const char *)
1806 ZOOM_package_option_get (ZOOM_package p, const char *key)
1808 return ZOOM_options_get (p->options, key);
1812 ZOOM_package_option_set (ZOOM_package p, const char *key,
1815 ZOOM_options_set (p->options, key, val);
1818 static int ZOOM_connection_exec_task (ZOOM_connection c)
1820 ZOOM_task task = c->tasks;
1822 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1825 if (c->error != ZOOM_ERROR_NONE ||
1826 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1828 ZOOM_connection_remove_tasks (c);
1831 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
1832 task->which, task->running);
1836 switch (task->which)
1838 case ZOOM_TASK_SEARCH:
1839 /* see if search hasn't been sent yet. */
1840 if (ZOOM_connection_send_search (c))
1843 case ZOOM_TASK_RETRIEVE:
1844 if (send_present (c))
1847 case ZOOM_TASK_CONNECT:
1851 case ZOOM_TASK_SCAN:
1855 case ZOOM_TASK_PACKAGE:
1856 if (send_package(c))
1859 ZOOM_connection_remove_task (c);
1863 static int send_sort_present (ZOOM_connection c)
1865 int r = send_sort (c);
1867 r = send_present (c);
1871 static int es_response (ZOOM_connection c,
1872 Z_ExtendedServicesResponse *res)
1874 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
1876 if (res->diagnostics && res->num_diagnostics > 0)
1877 response_diag(c, res->diagnostics[0]);
1878 if (res->taskPackage &&
1879 res->taskPackage->which == Z_External_extendedService)
1881 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1882 Odr_oct *id = taskPackage->targetReference;
1885 ZOOM_options_setl (c->tasks->u.package->options,
1886 "targetReference", id->buf, id->len);
1892 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1894 Z_InitResponse *initrs;
1896 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1900 case Z_APDU_initResponse:
1901 initrs = apdu->u.initResponse;
1902 if (!*initrs->result)
1904 c->error = ZOOM_ERROR_INIT;
1909 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1911 xfree (c->cookie_in);
1914 c->cookie_in = xstrdup(cookie);
1915 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1916 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1917 c->support_named_resultsets = 1;
1920 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1921 ZOOM_connection_remove_task (c);
1923 ZOOM_connection_exec_task (c);
1925 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
1927 NMEM tmpmem = nmem_create();
1928 Z_CharSetandLanguageNegotiation *p =
1929 yaz_get_charneg_record(initrs->otherInfo);
1933 char *charset=NULL, *lang=NULL;
1936 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &selected);
1937 yaz_log(LOG_DEBUG, "Target accepted: charset - %s,"
1938 "language - %s, select - %d",
1939 charset, lang, selected);
1941 nmem_destroy(tmpmem);
1945 case Z_APDU_searchResponse:
1946 handle_search_response (c, apdu->u.searchResponse);
1947 if (!send_sort_present (c))
1948 ZOOM_connection_remove_task (c);
1950 case Z_APDU_presentResponse:
1951 handle_present_response (c, apdu->u.presentResponse);
1952 if (!send_present (c))
1953 ZOOM_connection_remove_task (c);
1955 case Z_APDU_sortResponse:
1956 sort_response (c, apdu->u.sortResponse);
1957 if (!send_present (c))
1958 ZOOM_connection_remove_task (c);
1960 case Z_APDU_scanResponse:
1961 scan_response (c, apdu->u.scanResponse);
1962 ZOOM_connection_remove_task (c);
1964 case Z_APDU_extendedServicesResponse:
1965 es_response (c, apdu->u.extendedServicesResponse);
1966 ZOOM_connection_remove_task (c);
1971 static int do_read (ZOOM_connection c)
1977 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
1978 ZOOM_connection_put_event (c, event);
1980 yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in);
1982 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1987 if (c->reconnect_ok)
1990 c->reconnect_ok = 0;
1991 yaz_log (LOG_DEBUG, "reconnect read");
1992 c->tasks->running = 0;
1993 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
1997 c->error= ZOOM_ERROR_CONNECTION_LOST;
2004 c->reconnect_ok = 0;
2005 odr_reset (c->odr_in);
2006 odr_setbuf (c->odr_in, c->buf_in, r, 0);
2007 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
2008 ZOOM_connection_put_event (c, event);
2009 if (!z_APDU (c->odr_in, &apdu, 0, 0))
2011 c->error = ZOOM_ERROR_DECODE;
2016 handle_apdu (c, apdu);
2022 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
2027 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
2028 ZOOM_connection_put_event (c, event);
2030 yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
2031 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
2033 if (c->reconnect_ok)
2036 c->reconnect_ok = 0;
2037 yaz_log (LOG_DEBUG, "reconnect write");
2038 c->tasks->running = 0;
2039 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2042 if (c->state == STATE_CONNECTING)
2043 c->error = ZOOM_ERROR_CONNECT;
2045 c->error = ZOOM_ERROR_CONNECTION_LOST;
2051 c->mask = ZOOM_SELECT_EXCEPT;
2052 if (c->cs->io_pending & CS_WANT_WRITE)
2053 c->mask += ZOOM_SELECT_WRITE;
2054 if (c->cs->io_pending & CS_WANT_READ)
2055 c->mask += ZOOM_SELECT_READ;
2056 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
2060 // c->reconnect_ok = 0;
2061 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
2062 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
2067 static int do_write(ZOOM_connection c)
2069 return do_write_ex (c, c->buf_out, c->len_out);
2073 ZOOM_API(const char *)
2074 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
2076 return ZOOM_options_get (c->options, key);
2080 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
2083 ZOOM_options_set (c->options, key, val);
2086 ZOOM_API(const char *)
2087 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
2089 return ZOOM_options_get (r->options, key);
2093 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
2096 ZOOM_options_set (r->options, key, val);
2101 ZOOM_connection_errcode (ZOOM_connection c)
2103 return ZOOM_connection_error (c, 0, 0);
2106 ZOOM_API(const char *)
2107 ZOOM_connection_errmsg (ZOOM_connection c)
2110 ZOOM_connection_error (c, &msg, 0);
2114 ZOOM_API(const char *)
2115 ZOOM_connection_addinfo (ZOOM_connection c)
2117 const char *addinfo;
2118 ZOOM_connection_error (c, 0, &addinfo);
2123 ZOOM_connection_error (ZOOM_connection c, const char **cp,
2124 const char **addinfo)
2126 int error = c->error;
2131 case ZOOM_ERROR_NONE:
2132 *cp = "No error"; break;
2133 case ZOOM_ERROR_CONNECT:
2134 *cp = "Connect failed"; break;
2135 case ZOOM_ERROR_MEMORY:
2136 *cp = "Out of memory"; break;
2137 case ZOOM_ERROR_ENCODE:
2138 *cp = "Encoding failed"; break;
2139 case ZOOM_ERROR_DECODE:
2140 *cp = "Decoding failed"; break;
2141 case ZOOM_ERROR_CONNECTION_LOST:
2142 *cp = "Connection lost"; break;
2143 case ZOOM_ERROR_INIT:
2144 *cp = "Init rejected"; break;
2145 case ZOOM_ERROR_INTERNAL:
2146 *cp = "Internal failure"; break;
2147 case ZOOM_ERROR_TIMEOUT:
2148 *cp = "Timeout"; break;
2150 *cp = diagbib1_str (error);
2156 *addinfo = c->addinfo;
2163 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
2165 ZOOM_Event event = 0;
2166 int r = cs_look(c->cs);
2167 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
2172 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2173 c->error = ZOOM_ERROR_CONNECT;
2175 ZOOM_connection_put_event (c, event);
2177 else if (r == CS_CONNECT)
2180 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2182 ret = cs_rcvconnect (c->cs);
2183 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
2186 c->mask = ZOOM_SELECT_EXCEPT;
2187 if (c->cs->io_pending & CS_WANT_WRITE)
2188 c->mask += ZOOM_SELECT_WRITE;
2189 if (c->cs->io_pending & CS_WANT_READ)
2190 c->mask += ZOOM_SELECT_READ;
2191 ZOOM_connection_put_event (c, event);
2195 ZOOM_connection_put_event (c, event);
2196 ZOOM_connection_send_init (c);
2197 c->state = STATE_ESTABLISHED;
2201 c->error = ZOOM_ERROR_CONNECT;
2203 ZOOM_connection_put_event (c, event);
2208 if (mask & ZOOM_SELECT_READ)
2210 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2217 ZOOM_connection_last_event(ZOOM_connection cs)
2220 return ZOOM_EVENT_NONE;
2221 return cs->last_event;
2225 ZOOM_event (int no, ZOOM_connection *cs)
2228 struct pollfd pollfds[1024];
2229 ZOOM_connection poll_cs[1024];
2232 fd_set input, output, except;
2237 for (i = 0; i<no; i++)
2239 ZOOM_connection c = cs[i];
2241 if (c && (event = ZOOM_connection_get_event(c)))
2243 ZOOM_Event_destroy (event);
2247 for (i = 0; i<no; i++)
2249 ZOOM_connection c = cs[i];
2251 if (c && ZOOM_connection_exec_task (c))
2253 if ((event = ZOOM_connection_get_event(c)))
2255 ZOOM_Event_destroy (event);
2271 for (i = 0; i<no; i++)
2273 ZOOM_connection c = cs[i];
2278 fd = z3950_connection_socket(c);
2279 mask = z3950_connection_mask(c);
2289 short poll_events = 0;
2291 if (mask & ZOOM_SELECT_READ)
2292 poll_events += POLLIN;
2293 if (mask & ZOOM_SELECT_WRITE)
2294 poll_events += POLLOUT;
2295 if (mask & ZOOM_SELECT_EXCEPT)
2296 poll_events += POLLERR;
2297 pollfds[nfds].fd = fd;
2298 pollfds[nfds].events = poll_events;
2299 pollfds[nfds].revents = 0;
2304 if (mask & ZOOM_SELECT_READ)
2306 FD_SET (fd, &input);
2309 if (mask & ZOOM_SELECT_WRITE)
2311 FD_SET (fd, &output);
2314 if (mask & ZOOM_SELECT_EXCEPT)
2316 FD_SET (fd, &except);
2324 r = poll (pollfds, nfds, 15000);
2325 for (i = 0; i<nfds; i++)
2327 ZOOM_connection c = poll_cs[i];
2331 if (pollfds[i].revents & POLLIN)
2332 mask += ZOOM_SELECT_READ;
2333 if (pollfds[i].revents & POLLOUT)
2334 mask += ZOOM_SELECT_WRITE;
2335 if (pollfds[i].revents & POLLERR)
2336 mask += ZOOM_SELECT_EXCEPT;
2338 ZOOM_connection_do_io(c, mask);
2340 else if (r == 0 && c->mask)
2342 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2343 /* timeout and this connection was waiting */
2344 c->error = ZOOM_ERROR_TIMEOUT;
2346 ZOOM_connection_put_event(c, event);
2350 yaz_log (LOG_DEBUG, "select start");
2351 r = select (max_fd+1, &input, &output, &except, &tv);
2352 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
2353 for (i = 0; i<no; i++)
2355 ZOOM_connection c = cs[i];
2360 fd = z3950_connection_socket(c);
2364 /* no timeout and real socket */
2365 if (FD_ISSET(fd, &input))
2366 mask += ZOOM_SELECT_READ;
2367 if (FD_ISSET(fd, &output))
2368 mask += ZOOM_SELECT_WRITE;
2369 if (FD_ISSET(fd, &except))
2370 mask += ZOOM_SELECT_EXCEPT;
2372 ZOOM_connection_do_io(c, mask);
2374 if (r == 0 && c->mask)
2376 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2377 /* timeout and this connection was waiting */
2378 c->error = ZOOM_ERROR_TIMEOUT;
2380 yaz_log (LOG_DEBUG, "timeout");
2381 ZOOM_connection_put_event(c, event);
2385 for (i = 0; i<no; i++)
2387 ZOOM_connection c = cs[i];
2389 if (c && (event = ZOOM_connection_get_event(c)))
2391 ZOOM_Event_destroy (event);