2 * $Id: zoom-c.c,v 1.35 2002-07-03 13:36:55 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 if (strncmp (con->host_port, "unix:", 5) == 0)
212 cp = strchr (con->host_port+5, ':');
214 cp = strchr (con->host_port, '/');
221 while ((c = strchr(c, '+')))
229 databaseNames = (char**)
230 odr_malloc (con->odr_out, no * sizeof(*databaseNames));
234 c = strchr (cp, '+');
242 /* cp ptr to first char of db name, c is char
244 databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
245 memcpy (databaseNames[no], cp, c-cp);
246 databaseNames[no++][c-cp] = '\0';
251 databaseNames[no] = NULL;
253 return databaseNames;
256 ZOOM_API(ZOOM_connection)
257 ZOOM_connection_new (const char *host, int portnum)
259 ZOOM_connection c = ZOOM_connection_create (0);
261 ZOOM_connection_connect (c, host, portnum);
266 ZOOM_connection_connect(ZOOM_connection c,
267 const char *host, int portnum)
274 yaz_log (LOG_DEBUG, "reconnect");
278 yaz_log(LOG_DEBUG, "connect");
280 val = ZOOM_options_get (c->options, "proxy");
282 c->proxy = xstrdup (val);
287 val = ZOOM_options_get (c->options, "charset");
289 c->charset = xstrdup (val);
294 val = ZOOM_options_get (c->options, "lang");
296 c->lang = xstrdup (val);
300 xfree (c->host_port);
304 sprintf (hostn, "%.80s:%d", host, portnum);
305 c->host_port = xstrdup(hostn);
308 c->host_port = xstrdup(host);
310 ZOOM_options_set(c->options, "host", c->host_port);
312 val = ZOOM_options_get (c->options, "cookie");
314 c->cookie_out = xstrdup (val);
316 val = ZOOM_options_get (c->options, "clientIP");
318 c->client_IP = xstrdup (val);
320 c->async = ZOOM_options_get_bool (c->options, "async", 0);
322 c->error = ZOOM_ERROR_NONE;
324 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
328 while (ZOOM_event (1, &c))
334 ZOOM_query_create(void)
336 ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
341 s->odr = odr_createmem (ODR_ENCODE);
347 ZOOM_query_destroy(ZOOM_query s)
353 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
354 if (s->refcount == 0)
356 odr_destroy (s->odr);
362 ZOOM_query_prefix(ZOOM_query s, const char *str)
364 s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query));
365 s->query->which = Z_Query_type_1;
366 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
367 if (!s->query->u.type_1)
373 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
375 s->sort_spec = yaz_sort_spec (s->odr, criteria);
381 static int do_write(ZOOM_connection c);
384 ZOOM_connection_destroy(ZOOM_connection c)
391 for (r = c->resultsets; r; r = r->next)
396 odr_destroy (c->odr_in);
397 odr_destroy (c->odr_out);
398 ZOOM_options_destroy (c->options);
399 ZOOM_connection_remove_tasks (c);
400 xfree (c->host_port);
404 xfree (c->cookie_out);
405 xfree (c->cookie_in);
406 xfree (c->client_IP);
410 void ZOOM_resultset_addref (ZOOM_resultset r)
415 ZOOM_resultset ZOOM_resultset_create ()
417 ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
421 r->odr = odr_createmem (ODR_ENCODE);
435 ZOOM_API(ZOOM_resultset)
436 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
439 ZOOM_query s = ZOOM_query_create();
441 ZOOM_query_prefix (s, q);
443 r = ZOOM_connection_search (c, s);
444 ZOOM_query_destroy (s);
448 ZOOM_API(ZOOM_resultset)
449 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
451 ZOOM_resultset r = ZOOM_resultset_create ();
455 r->r_sort_spec = q->sort_spec;
456 r->r_query = q->query;
459 r->options = ZOOM_options_create_with_parent(c->options);
461 r->start = ZOOM_options_get_int(r->options, "start", 0);
462 r->count = ZOOM_options_get_int(r->options, "count", 0);
463 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
464 cp = ZOOM_options_get (r->options, "setname");
466 r->setname = xstrdup (cp);
470 r->next = c->resultsets;
473 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
474 task->u.search.resultset = r;
475 ZOOM_resultset_addref (r);
481 while (ZOOM_event (1, &c))
488 ZOOM_resultset_destroy(ZOOM_resultset r)
493 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
494 if (r->refcount == 0)
496 ZOOM_record_cache rc;
498 for (rc = r->record_cache; rc; rc = rc->next)
499 if (rc->rec.wrbuf_marc)
500 wrbuf_free (rc->rec.wrbuf_marc, 1);
503 /* remove ourselves from the resultsets in connection */
504 ZOOM_resultset *rp = &r->connection->resultsets;
507 assert (*rp); /* we must be in this list!! */
509 { /* OK, we're here - take us out of it */
516 ZOOM_query_destroy (r->search);
517 ZOOM_options_destroy (r->options);
518 odr_destroy (r->odr);
525 ZOOM_resultset_size (ZOOM_resultset r)
530 static void do_close (ZOOM_connection c)
536 c->state = STATE_IDLE;
539 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
540 int force_sync, int start, int count)
550 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
551 task->u.retrieve.resultset = r;
552 task->u.retrieve.start = start;
553 task->u.retrieve.count = count;
555 ZOOM_resultset_addref (r);
557 if (!r->connection->async || force_sync)
558 while (r->connection && ZOOM_event (1, &r->connection))
563 ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
564 size_t start, size_t count)
566 int force_present = 0;
572 ZOOM_resultset_retrieve (r, force_present, start, count);
576 for (i = 0; i< count; i++)
577 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
581 static int do_connect (ZOOM_connection c)
584 const char *effective_host;
587 effective_host = c->proxy;
589 effective_host = c->host_port;
591 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
594 c->cs = cs_create_host (effective_host, 0, &add);
598 int ret = cs_connect (c->cs, add);
601 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
602 ZOOM_connection_put_event(c, event);
603 ZOOM_connection_send_init(c);
604 c->state = STATE_ESTABLISHED;
609 c->state = STATE_CONNECTING;
610 c->mask = ZOOM_SELECT_EXCEPT;
611 if (c->cs->io_pending & CS_WANT_WRITE)
612 c->mask += ZOOM_SELECT_WRITE;
613 if (c->cs->io_pending & CS_WANT_READ)
614 c->mask += ZOOM_SELECT_READ;
618 c->state = STATE_IDLE;
619 c->error = ZOOM_ERROR_CONNECT;
623 int z3950_connection_socket(ZOOM_connection c)
626 return cs_fileno(c->cs);
630 int z3950_connection_mask(ZOOM_connection c)
637 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
642 Z_OtherInformation **oi;
644 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
648 Z_OtherInformation **oi;
650 yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
652 if (!z_APDU(out, &a, 0, 0))
654 FILE *outf = fopen("/tmp/apdu.txt", "a");
657 ODR odr_pr = odr_createmem(ODR_PRINT);
658 fprintf (outf, "a=%p\n", a);
659 odr_setprint(odr_pr, outf);
660 z_APDU(odr_pr, &a, 0, 0);
663 c->error = ZOOM_ERROR_ENCODE;
671 static int send_APDU (ZOOM_connection c, Z_APDU *a)
675 if (encode_APDU(c, a, c->odr_out))
677 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
678 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
679 ZOOM_connection_put_event (c, event);
680 odr_reset(c->odr_out);
685 static int ZOOM_connection_send_init (ZOOM_connection c)
688 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
689 Z_InitRequest *ireq = apdu->u.initRequest;
690 Z_IdAuthentication *auth = (Z_IdAuthentication *)
691 odr_malloc(c->odr_out, sizeof(*auth));
692 const char *auth_groupId = ZOOM_options_get (c->options, "group");
693 const char *auth_userId = ZOOM_options_get (c->options, "user");
694 const char *auth_password = ZOOM_options_get (c->options, "pass");
696 ODR_MASK_SET(ireq->options, Z_Options_search);
697 ODR_MASK_SET(ireq->options, Z_Options_present);
698 ODR_MASK_SET(ireq->options, Z_Options_scan);
699 ODR_MASK_SET(ireq->options, Z_Options_sort);
700 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
701 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
703 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
704 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
705 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
707 impname = ZOOM_options_get (c->options, "implementationName");
708 ireq->implementationName =
709 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
710 strcpy (ireq->implementationName, "");
713 strcat (ireq->implementationName, impname);
714 strcat (ireq->implementationName, "/");
716 strcat (ireq->implementationName, "ZOOM-C/YAZ");
718 *ireq->maximumRecordSize =
719 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
720 *ireq->preferredMessageSize =
721 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
723 if (auth_groupId || auth_password)
725 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
728 if (auth_groupId && *auth_groupId)
730 pass->groupId = (char *)
731 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
732 strcpy(pass->groupId, auth_groupId);
736 if (auth_userId && *auth_userId)
738 pass->userId = (char *)
739 odr_malloc(c->odr_out, strlen(auth_userId)+1);
740 strcpy(pass->userId, auth_userId);
744 if (auth_password && *auth_password)
746 pass->password = (char *)
747 odr_malloc(c->odr_out, strlen(auth_password)+1);
748 strcpy(pass->password, auth_password);
753 auth->which = Z_IdAuthentication_idPass;
754 auth->u.idPass = pass;
755 ireq->idAuthentication = auth;
758 else if (auth_userId)
760 auth->which = Z_IdAuthentication_open;
761 auth->u.open = (char *)
762 odr_malloc(c->odr_out, strlen(auth_userId)+1);
763 strcpy(auth->u.open, auth_userId);
764 ireq->idAuthentication = auth;
767 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
768 VAL_PROXY, 1, c->host_port);
769 if (c->charset||c->lang)
771 Z_OtherInformation **oi;
772 Z_OtherInformationUnit *oi_unit;
774 yaz_oi_APDU(apdu, &oi);
776 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
778 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
780 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
781 oi_unit->information.externallyDefinedInfo =
782 yaz_set_proposal_charneg(c->odr_out,
783 (const char **)&c->charset, (c->charset) ? 1:0,
784 (const char **)&c->lang, (c->lang) ? 1:0, 1);
793 static int ZOOM_connection_send_search (ZOOM_connection c)
796 int lslb, ssub, mspn;
798 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
799 Z_SearchRequest *search_req = apdu->u.searchRequest;
800 const char *elementSetName;
801 const char *smallSetElementSetName;
802 const char *mediumSetElementSetName;
806 assert (c->tasks->which == ZOOM_TASK_SEARCH);
808 r = c->tasks->u.search.resultset;
811 ZOOM_options_get (r->options, "elementSetName");
812 smallSetElementSetName =
813 ZOOM_options_get (r->options, "smallSetElementSetName");
814 mediumSetElementSetName =
815 ZOOM_options_get (r->options, "mediumSetElementSetName");
817 ZOOM_options_get (r->options, "schema");
819 if (!smallSetElementSetName)
820 smallSetElementSetName = elementSetName;
822 if (!mediumSetElementSetName)
823 mediumSetElementSetName = elementSetName;
828 /* prepare query for the search request */
829 search_req->query = r->r_query;
831 search_req->databaseNames =
832 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
834 /* get syntax (no need to provide unless piggyback is in effect) */
835 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
837 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
838 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
839 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
840 if (lslb != -1 && ssub != -1 && mspn != -1)
842 /* So're a Z39.50 expert? Let's hope you don't do sort */
843 *search_req->largeSetLowerBound = lslb;
844 *search_req->smallSetUpperBound = ssub;
845 *search_req->mediumSetPresentNumber = mspn;
847 else if (r->start == 0 && r->count > 0
848 && r->piggyback && !r->r_sort_spec && !schema)
850 /* Regular piggyback - do it unless we're going to do sort */
851 *search_req->largeSetLowerBound = 2000000000;
852 *search_req->smallSetUpperBound = r->count;
853 *search_req->mediumSetPresentNumber = r->count;
854 smallSetElementSetName = 0; /* no need to provide this */
858 /* non-piggyback. Need not provide elementsets or syntaxes .. */
859 smallSetElementSetName = 0;
860 mediumSetElementSetName = 0;
863 if (smallSetElementSetName && *smallSetElementSetName)
865 Z_ElementSetNames *esn = (Z_ElementSetNames *)
866 odr_malloc (c->odr_out, sizeof(*esn));
868 esn->which = Z_ElementSetNames_generic;
869 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
870 search_req->smallSetElementSetNames = esn;
872 if (mediumSetElementSetName && *mediumSetElementSetName)
874 Z_ElementSetNames *esn = (Z_ElementSetNames *)
875 odr_malloc (c->odr_out, sizeof(*esn));
877 esn->which = Z_ElementSetNames_generic;
878 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
879 search_req->mediumSetElementSetNames = esn;
882 search_req->preferredRecordSyntax =
883 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
887 if (c->support_named_resultsets)
891 /* find the lowest unused ordinal so that we re-use
892 result sets on the server. */
893 for (ord = 1; ; ord++)
896 sprintf (setname, "%d", ord);
897 for (rp = c->resultsets; rp; rp = rp->next)
898 if (rp->setname && !strcmp (rp->setname, setname))
903 r->setname = xstrdup (setname);
904 yaz_log (LOG_DEBUG, "allocating %s", r->setname);
907 r->setname = xstrdup ("default");
908 ZOOM_options_set (r->options, "setname", r->setname);
910 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
911 /* send search request */
916 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
918 Z_DefaultDiagFormat *r;
923 if (p->which != Z_DiagRec_defaultFormat)
925 c->error = ZOOM_ERROR_DECODE;
928 r = p->u.defaultFormat;
931 case Z_DefaultDiagFormat_v2Addinfo:
932 addinfo = r->u.v2Addinfo;
934 case Z_DefaultDiagFormat_v3Addinfo:
935 addinfo = r->u.v3Addinfo;
939 c->addinfo = xstrdup (addinfo);
940 c->error = *r->condition;
943 ZOOM_API(ZOOM_record)
944 ZOOM_record_clone (ZOOM_record srec)
951 odr_enc = odr_createmem(ODR_ENCODE);
952 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
954 buf = odr_getbuf (odr_enc, &size, 0);
956 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
957 nrec->odr = odr_createmem(ODR_DECODE);
958 nrec->wrbuf_marc = 0;
959 odr_setbuf (nrec->odr, buf, size, 0);
960 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
962 odr_destroy (odr_enc);
966 ZOOM_API(ZOOM_record)
967 ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
969 return record_cache_lookup (s, pos, 0);
972 ZOOM_API(ZOOM_record)
973 ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
975 ZOOM_resultset_retrieve (r, 1, pos, 1);
976 return ZOOM_resultset_record_immediate (r, pos);
980 ZOOM_record_destroy (ZOOM_record rec)
985 wrbuf_free (rec->wrbuf_marc, 1);
986 odr_destroy (rec->odr);
990 ZOOM_API(const char *)
991 ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
993 Z_NamePlusRecord *npr;
996 *len = 0; /* default return */
1003 if (!strcmp (type, "database"))
1006 *len = strlen(npr->databaseName);
1007 return npr->databaseName;
1009 else if (!strcmp (type, "syntax"))
1011 if (npr->which == Z_NamePlusRecord_databaseRecord)
1013 Z_External *r = (Z_External *) npr->u.databaseRecord;
1014 oident *ent = oid_getentbyoid(r->direct_reference);
1018 *len = strlen(ent->desc);
1024 else if (!strcmp (type, "render") &&
1025 npr->which == Z_NamePlusRecord_databaseRecord)
1027 Z_External *r = (Z_External *) npr->u.databaseRecord;
1028 oident *ent = oid_getentbyoid(r->direct_reference);
1030 if (r->which == Z_External_sutrs)
1032 if (len) *len = r->u.sutrs->len;
1033 return (const char *) r->u.sutrs->buf;
1035 else if (r->which == Z_External_octet)
1044 case VAL_APPLICATION_XML:
1047 if (!rec->wrbuf_marc)
1048 rec->wrbuf_marc = wrbuf_alloc();
1049 wrbuf_rewind (rec->wrbuf_marc);
1050 if (yaz_marc_decode ((const char *)
1051 r->u.octet_aligned->buf,
1053 r->u.octet_aligned->len,
1056 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1057 return wrbuf_buf(rec->wrbuf_marc);
1060 if (len) *len = r->u.octet_aligned->len;
1061 return (const char *) r->u.octet_aligned->buf;
1063 else if (r->which == Z_External_grs1)
1070 else if (!strcmp (type, "xml") &&
1071 npr->which == Z_NamePlusRecord_databaseRecord)
1073 Z_External *r = (Z_External *) npr->u.databaseRecord;
1074 oident *ent = oid_getentbyoid(r->direct_reference);
1076 if (r->which == Z_External_sutrs)
1078 if (len) *len = r->u.sutrs->len;
1079 return (const char *) r->u.sutrs->buf;
1081 else if (r->which == Z_External_octet)
1090 case VAL_APPLICATION_XML:
1093 if (!rec->wrbuf_marc)
1094 rec->wrbuf_marc = wrbuf_alloc();
1095 wrbuf_rewind (rec->wrbuf_marc);
1096 if (yaz_marc_decode ((const char *)
1097 r->u.octet_aligned->buf,
1099 r->u.octet_aligned->len,
1102 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1103 return wrbuf_buf(rec->wrbuf_marc);
1106 if (len) *len = r->u.octet_aligned->len;
1107 return (const char *) r->u.octet_aligned->buf;
1109 else if (r->which == Z_External_grs1)
1116 else if (!strcmp (type, "raw"))
1118 if (npr->which == Z_NamePlusRecord_databaseRecord)
1119 return (const char *) npr->u.databaseRecord;
1125 static void record_cache_add (ZOOM_resultset r,
1126 Z_NamePlusRecord *npr,
1128 const char *elementSetName)
1130 ZOOM_record_cache rc;
1132 for (rc = r->record_cache; rc; rc = rc->next)
1136 if ((!elementSetName && !rc->elementSetName)
1137 || (elementSetName && rc->elementSetName &&
1138 !strcmp (elementSetName, rc->elementSetName)))
1140 /* not destroying rc->npr (it's handled by nmem )*/
1142 /* keeping wrbuf_marc too */
1147 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1150 rc->rec.wrbuf_marc = 0;
1152 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1154 rc->elementSetName = 0;
1156 rc->next = r->record_cache;
1157 r->record_cache = rc;
1160 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
1162 const char *elementSetName)
1164 ZOOM_record_cache rc;
1166 for (rc = r->record_cache; rc; rc = rc->next)
1170 if ((!elementSetName && !rc->elementSetName)
1171 || (elementSetName && rc->elementSetName &&
1172 !strcmp (elementSetName, rc->elementSetName)))
1179 static void handle_records (ZOOM_connection c, Z_Records *sr,
1182 ZOOM_resultset resultset;
1186 switch (c->tasks->which)
1188 case ZOOM_TASK_SEARCH:
1189 resultset = c->tasks->u.search.resultset;
1191 case ZOOM_TASK_RETRIEVE:
1192 resultset = c->tasks->u.retrieve.resultset;
1197 if (sr && sr->which == Z_Records_NSD)
1199 Z_DiagRec dr, *dr_p = &dr;
1200 dr.which = Z_DiagRec_defaultFormat;
1201 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1203 response_diag (c, dr_p);
1205 else if (sr && sr->which == Z_Records_multipleNSD)
1207 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1208 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1210 c->error = ZOOM_ERROR_DECODE;
1214 if (resultset->count + resultset->start > resultset->size)
1215 resultset->count = resultset->size - resultset->start;
1216 if (resultset->count < 0)
1217 resultset->count = 0;
1218 if (sr && sr->which == Z_Records_DBOSD)
1221 NMEM nmem = odr_extract_mem (c->odr_in);
1222 Z_NamePlusRecordList *p =
1223 sr->u.databaseOrSurDiagnostics;
1224 for (i = 0; i<p->num_records; i++)
1226 record_cache_add (resultset, p->records[i],
1227 i+ resultset->start, 0);
1229 /* transfer our response to search_nmem .. we need it later */
1230 nmem_transfer (resultset->odr->mem, nmem);
1231 nmem_destroy (nmem);
1232 if (present_phase && p->num_records == 0)
1234 /* present response and we didn't get any records! */
1235 c->error = ZOOM_ERROR_DECODE;
1238 else if (present_phase)
1240 /* present response and we didn't get any records! */
1241 c->error = ZOOM_ERROR_DECODE;
1246 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1248 handle_records (c, pr->records, 1);
1251 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1253 ZOOM_resultset resultset;
1255 yaz_log (LOG_DEBUG, "got search response");
1257 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1260 resultset = c->tasks->u.search.resultset;
1262 resultset->size = *sr->resultCount;
1263 handle_records (c, sr->records, 0);
1266 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1268 if (res->diagnostics && res->num_diagnostics > 0)
1269 response_diag (c, res->diagnostics[0]);
1272 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1274 NMEM nmem = odr_extract_mem (c->odr_in);
1277 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1279 scan = c->tasks->u.scan.scan;
1281 if (res->entries && res->entries->nonsurrogateDiagnostics)
1282 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1283 scan->scan_response = res;
1284 nmem_transfer (scan->odr->mem, nmem);
1286 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1287 if (res->positionOfTerm)
1288 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1289 if (res->scanStatus)
1290 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1291 if (res->numberOfEntriesReturned)
1292 ZOOM_options_set_int (scan->options, "number",
1293 *res->numberOfEntriesReturned);
1294 nmem_destroy (nmem);
1298 static int send_sort (ZOOM_connection c)
1300 ZOOM_resultset resultset;
1302 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1305 resultset = c->tasks->u.search.resultset;
1309 resultset->r_sort_spec = 0;
1312 if (resultset->r_sort_spec)
1314 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1315 Z_SortRequest *req = apdu->u.sortRequest;
1317 req->num_inputResultSetNames = 1;
1318 req->inputResultSetNames = (Z_InternationalString **)
1319 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1320 req->inputResultSetNames[0] =
1321 odr_strdup (c->odr_out, resultset->setname);
1322 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1323 req->sortSequence = resultset->r_sort_spec;
1324 resultset->r_sort_spec = 0;
1325 send_APDU (c, apdu);
1331 static int send_present (ZOOM_connection c)
1333 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1334 Z_PresentRequest *req = apdu->u.presentRequest;
1336 const char *syntax =
1337 ZOOM_options_get (c->options, "preferredRecordSyntax");
1338 const char *element =
1339 ZOOM_options_get (c->options, "elementSetName");
1340 const char *schema =
1341 ZOOM_options_get (c->options, "schema");
1342 ZOOM_resultset resultset;
1347 switch (c->tasks->which)
1349 case ZOOM_TASK_SEARCH:
1350 resultset = c->tasks->u.search.resultset;
1352 case ZOOM_TASK_RETRIEVE:
1353 resultset = c->tasks->u.retrieve.resultset;
1354 resultset->start = c->tasks->u.retrieve.start;
1355 resultset->count = c->tasks->u.retrieve.count;
1357 if (resultset->start >= resultset->size)
1359 if (resultset->start + resultset->count > resultset->size)
1360 resultset->count = resultset->size - resultset->start;
1366 if (c->error) /* don't continue on error */
1368 if (resultset->start < 0)
1370 for (i = 0; i<resultset->count; i++)
1373 record_cache_lookup (resultset, i + resultset->start, 0);
1377 if (i == resultset->count)
1380 resultset->start += i;
1381 resultset->count -= i;
1382 *req->resultSetStartPoint = resultset->start + 1;
1383 *req->numberOfRecordsRequested = resultset->count;
1384 assert (*req->numberOfRecordsRequested > 0);
1386 if (syntax && *syntax)
1387 req->preferredRecordSyntax =
1388 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1390 if (schema && *schema)
1392 Z_RecordComposition *compo = (Z_RecordComposition *)
1393 odr_malloc (c->odr_out, sizeof(*compo));
1395 req->recordComposition = compo;
1396 compo->which = Z_RecordComp_complex;
1397 compo->u.complex = (Z_CompSpec *)
1398 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1399 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1400 odr_malloc(c->odr_out, sizeof(bool_t));
1401 *compo->u.complex->selectAlternativeSyntax = 0;
1403 compo->u.complex->generic = (Z_Specification *)
1404 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1406 compo->u.complex->generic->schema = (Odr_oid *)
1407 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1409 if (!compo->u.complex->generic->schema)
1411 /* OID wasn't a schema! Try record syntax instead. */
1413 compo->u.complex->generic->schema = (Odr_oid *)
1414 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1416 if (element && *element)
1418 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1419 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1420 compo->u.complex->generic->elementSpec->which =
1421 Z_ElementSpec_elementSetName;
1422 compo->u.complex->generic->elementSpec->u.elementSetName =
1423 odr_strdup (c->odr_out, element);
1426 compo->u.complex->generic->elementSpec = 0;
1427 compo->u.complex->num_dbSpecific = 0;
1428 compo->u.complex->dbSpecific = 0;
1429 compo->u.complex->num_recordSyntax = 0;
1430 compo->u.complex->recordSyntax = 0;
1432 else if (element && *element)
1434 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1435 odr_malloc (c->odr_out, sizeof(*esn));
1436 Z_RecordComposition *compo = (Z_RecordComposition *)
1437 odr_malloc (c->odr_out, sizeof(*compo));
1439 esn->which = Z_ElementSetNames_generic;
1440 esn->u.generic = odr_strdup (c->odr_out, element);
1441 compo->which = Z_RecordComp_simple;
1442 compo->u.simple = esn;
1443 req->recordComposition = compo;
1445 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1446 send_APDU (c, apdu);
1450 ZOOM_API(ZOOM_scanset)
1451 ZOOM_connection_scan (ZOOM_connection c, const char *start)
1453 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1455 scan->connection = c;
1456 scan->odr = odr_createmem (ODR_DECODE);
1457 scan->options = ZOOM_options_create_with_parent (c->options);
1459 scan->scan_response = 0;
1461 if ((scan->termListAndStartPoint =
1462 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1465 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1466 task->u.scan.scan = scan;
1471 while (ZOOM_event (1, &c))
1479 ZOOM_scanset_destroy (ZOOM_scanset scan)
1484 if (scan->refcount == 0)
1486 odr_destroy (scan->odr);
1488 ZOOM_options_destroy (scan->options);
1493 static int send_package (ZOOM_connection c)
1498 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1500 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
1501 ZOOM_connection_put_event (c, event);
1503 do_write_ex (c, c->tasks->u.package->buf_out,
1504 c->tasks->u.package->len_out);
1508 static int send_scan (ZOOM_connection c)
1511 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1512 Z_ScanRequest *req = apdu->u.scanRequest;
1515 assert (c->tasks->which == ZOOM_TASK_SCAN);
1516 scan = c->tasks->u.scan.scan;
1518 req->termListAndStartPoint = scan->termListAndStartPoint;
1519 req->attributeSet = scan->attributeSet;
1521 *req->numberOfTermsRequested =
1522 ZOOM_options_get_int(scan->options, "number", 10);
1524 req->preferredPositionInResponse =
1525 odr_intdup (c->odr_out,
1526 ZOOM_options_get_int(scan->options, "position", 1));
1529 odr_intdup (c->odr_out,
1530 ZOOM_options_get_int(scan->options, "stepSize", 0));
1532 req->databaseNames = set_DatabaseNames (c, scan->options,
1533 &req->num_databaseNames);
1535 send_APDU (c, apdu);
1541 ZOOM_scanset_size (ZOOM_scanset scan)
1543 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1545 return scan->scan_response->entries->num_entries;
1548 ZOOM_API(const char *)
1549 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1552 const char *term = 0;
1553 size_t noent = ZOOM_scanset_size (scan);
1554 Z_ScanResponse *res = scan->scan_response;
1560 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1562 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1564 if (t->term->which == Z_Term_general)
1566 term = (const char *) t->term->u.general->buf;
1567 *len = t->term->u.general->len;
1569 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1574 ZOOM_API(const char *)
1575 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1577 return ZOOM_options_get (scan->options, key);
1581 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1584 ZOOM_options_set (scan->options, key, val);
1589 static Z_APDU *create_es_package (ZOOM_package p, int type)
1592 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
1593 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1595 *req->function = Z_ExtendedServicesRequest_create;
1597 str = ZOOM_options_get(p->options, "package-name");
1599 req->packageName = nmem_strdup (p->odr_out->mem, str);
1601 str = ZOOM_options_get(p->options, "user-id");
1603 req->userId = nmem_strdup (p->odr_out->mem, str);
1605 req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1608 str = ZOOM_options_get(p->options, "function");
1611 if (!strcmp (str, "create"))
1613 if (!strcmp (str, "delete"))
1615 if (!strcmp (str, "modify"))
1621 static const char *ill_array_lookup (void *clientData, const char *idx)
1623 ZOOM_package p = (ZOOM_package) clientData;
1624 return ZOOM_options_get (p->options, idx+4);
1627 static Z_External *encode_ill_request (ZOOM_package p)
1629 ODR out = p->odr_out;
1632 struct ill_get_ctl ctl;
1634 ctl.odr = p->odr_out;
1636 ctl.f = ill_array_lookup;
1638 req = ill_get_ILLRequest(&ctl, "ill", 0);
1640 if (!ill_Request (out, &req, 0, 0))
1642 int ill_request_size;
1643 char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1644 if (ill_request_buf)
1645 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1651 int illRequest_size = 0;
1652 char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0);
1654 oid.proto = PROTO_GENERAL;
1655 oid.oclass = CLASS_GENERAL;
1656 oid.value = VAL_ISO_ILL_1;
1658 r = (Z_External *) odr_malloc (out, sizeof(*r));
1659 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1660 r->indirect_reference = 0;
1662 r->which = Z_External_single;
1664 r->u.single_ASN1_type = (Odr_oct *)
1665 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1666 r->u.single_ASN1_type->buf = odr_malloc (out, illRequest_size);
1667 r->u.single_ASN1_type->len = illRequest_size;
1668 r->u.single_ASN1_type->size = illRequest_size;
1669 memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size);
1674 static Z_ItemOrder *encode_item_order(ZOOM_package p)
1676 Z_ItemOrder *req = odr_malloc (p->odr_out, sizeof(*req));
1679 req->which=Z_IOItemOrder_esRequest;
1680 req->u.esRequest = (Z_IORequest *)
1681 odr_malloc(p->odr_out,sizeof(Z_IORequest));
1683 /* to keep part ... */
1684 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1685 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
1686 req->u.esRequest->toKeep->supplDescription = 0;
1687 req->u.esRequest->toKeep->contact =
1688 odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
1690 str = ZOOM_options_get(p->options, "contact-name");
1691 req->u.esRequest->toKeep->contact->name = str ?
1692 nmem_strdup (p->odr_out->mem, str) : 0;
1694 str = ZOOM_options_get(p->options, "contact-phone");
1695 req->u.esRequest->toKeep->contact->phone = str ?
1696 nmem_strdup (p->odr_out->mem, str) : 0;
1698 str = ZOOM_options_get(p->options, "contact-email");
1699 req->u.esRequest->toKeep->contact->email = str ?
1700 nmem_strdup (p->odr_out->mem, str) : 0;
1702 req->u.esRequest->toKeep->addlBilling = 0;
1704 /* not to keep part ... */
1705 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1706 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
1708 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
1709 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
1711 str = ZOOM_options_get(p->options, "itemorder-setname");
1714 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
1715 nmem_strdup (p->odr_out->mem, str);
1716 req->u.esRequest->notToKeep->resultSetItem->item =
1717 (int *) odr_malloc(p->odr_out, sizeof(int));
1719 str = ZOOM_options_get(p->options, "itemorder-item");
1720 *req->u.esRequest->notToKeep->resultSetItem->item =
1721 (str ? atoi(str) : 1);
1723 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
1729 ZOOM_package_send (ZOOM_package p, const char *type)
1736 odr_reset (p->odr_out);
1739 if (!strcmp(type, "itemorder"))
1742 apdu = create_es_package (p, VAL_ITEMORDER);
1745 r = odr_malloc (p->odr_out, sizeof(*r));
1747 r->direct_reference =
1748 yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1751 r->which = Z_External_itemOrder;
1752 r->indirect_reference = 0;
1753 r->u.itemOrder = encode_item_order (p);
1755 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
1760 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
1764 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE);
1765 task->u.package = p;
1766 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
1767 p->buf_out = xmalloc (p->len_out);
1768 memcpy (p->buf_out, buf, p->len_out);
1773 while (ZOOM_event (1, &c))
1780 ZOOM_API(ZOOM_package)
1781 ZOOM_connection_package (ZOOM_connection c, ZOOM_options options)
1783 ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p));
1786 p->odr_out = odr_createmem (ODR_ENCODE);
1787 p->options = ZOOM_options_create_with_parent2 (options, c->options);
1795 ZOOM_package_destroy(ZOOM_package p)
1800 if (p->refcount == 0)
1802 odr_destroy (p->odr_out);
1805 ZOOM_options_destroy (p->options);
1810 ZOOM_API(const char *)
1811 ZOOM_package_option_get (ZOOM_package p, const char *key)
1813 return ZOOM_options_get (p->options, key);
1817 ZOOM_package_option_set (ZOOM_package p, const char *key,
1820 ZOOM_options_set (p->options, key, val);
1823 static int ZOOM_connection_exec_task (ZOOM_connection c)
1825 ZOOM_task task = c->tasks;
1827 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1830 if (c->error != ZOOM_ERROR_NONE ||
1831 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1833 ZOOM_connection_remove_tasks (c);
1836 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
1837 task->which, task->running);
1841 switch (task->which)
1843 case ZOOM_TASK_SEARCH:
1844 /* see if search hasn't been sent yet. */
1845 if (ZOOM_connection_send_search (c))
1848 case ZOOM_TASK_RETRIEVE:
1849 if (send_present (c))
1852 case ZOOM_TASK_CONNECT:
1856 case ZOOM_TASK_SCAN:
1860 case ZOOM_TASK_PACKAGE:
1861 if (send_package(c))
1864 ZOOM_connection_remove_task (c);
1868 static int send_sort_present (ZOOM_connection c)
1870 int r = send_sort (c);
1872 r = send_present (c);
1876 static int es_response (ZOOM_connection c,
1877 Z_ExtendedServicesResponse *res)
1879 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
1881 if (res->diagnostics && res->num_diagnostics > 0)
1882 response_diag(c, res->diagnostics[0]);
1883 if (res->taskPackage &&
1884 res->taskPackage->which == Z_External_extendedService)
1886 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1887 Odr_oct *id = taskPackage->targetReference;
1890 ZOOM_options_setl (c->tasks->u.package->options,
1891 "targetReference", id->buf, id->len);
1897 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1899 Z_InitResponse *initrs;
1902 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1905 case Z_APDU_initResponse:
1906 initrs = apdu->u.initResponse;
1907 if (!*initrs->result)
1909 c->error = ZOOM_ERROR_INIT;
1914 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1916 xfree (c->cookie_in);
1919 c->cookie_in = xstrdup(cookie);
1920 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1921 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1922 c->support_named_resultsets = 1;
1925 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1926 ZOOM_connection_remove_task (c);
1928 ZOOM_connection_exec_task (c);
1930 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
1932 NMEM tmpmem = nmem_create();
1933 Z_CharSetandLanguageNegotiation *p =
1934 yaz_get_charneg_record(initrs->otherInfo);
1938 char *charset=NULL, *lang=NULL;
1941 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &selected);
1942 yaz_log(LOG_DEBUG, "Target accepted: charset - %s,"
1943 "language - %s, select - %d",
1944 charset, lang, selected);
1946 nmem_destroy(tmpmem);
1950 case Z_APDU_searchResponse:
1951 handle_search_response (c, apdu->u.searchResponse);
1952 if (!send_sort_present (c))
1953 ZOOM_connection_remove_task (c);
1955 case Z_APDU_presentResponse:
1956 handle_present_response (c, apdu->u.presentResponse);
1957 if (!send_present (c))
1958 ZOOM_connection_remove_task (c);
1960 case Z_APDU_sortResponse:
1961 sort_response (c, apdu->u.sortResponse);
1962 if (!send_present (c))
1963 ZOOM_connection_remove_task (c);
1965 case Z_APDU_scanResponse:
1966 scan_response (c, apdu->u.scanResponse);
1967 ZOOM_connection_remove_task (c);
1969 case Z_APDU_extendedServicesResponse:
1970 es_response (c, apdu->u.extendedServicesResponse);
1971 ZOOM_connection_remove_task (c);
1974 if (c->reconnect_ok)
1977 c->tasks->running = 0;
1978 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
1982 c->error = ZOOM_ERROR_CONNECTION_LOST;
1987 c->error = ZOOM_ERROR_DECODE;
1992 static int do_read (ZOOM_connection c)
1998 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
1999 ZOOM_connection_put_event (c, event);
2001 yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in);
2003 r = cs_get (c->cs, &c->buf_in, &c->len_in);
2008 if (c->reconnect_ok)
2011 c->reconnect_ok = 0;
2012 yaz_log (LOG_DEBUG, "reconnect read");
2013 c->tasks->running = 0;
2014 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2018 c->error= ZOOM_ERROR_CONNECTION_LOST;
2025 odr_reset (c->odr_in);
2026 odr_setbuf (c->odr_in, c->buf_in, r, 0);
2027 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
2028 ZOOM_connection_put_event (c, event);
2029 if (!z_APDU (c->odr_in, &apdu, 0, 0))
2031 c->error = ZOOM_ERROR_DECODE;
2035 handle_apdu (c, apdu);
2036 c->reconnect_ok = 0;
2041 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
2046 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
2047 ZOOM_connection_put_event (c, event);
2049 yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
2050 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
2052 if (c->reconnect_ok)
2055 c->reconnect_ok = 0;
2056 yaz_log (LOG_DEBUG, "reconnect write");
2057 c->tasks->running = 0;
2058 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2061 if (c->state == STATE_CONNECTING)
2062 c->error = ZOOM_ERROR_CONNECT;
2064 c->error = ZOOM_ERROR_CONNECTION_LOST;
2070 c->mask = ZOOM_SELECT_EXCEPT;
2071 if (c->cs->io_pending & CS_WANT_WRITE)
2072 c->mask += ZOOM_SELECT_WRITE;
2073 if (c->cs->io_pending & CS_WANT_READ)
2074 c->mask += ZOOM_SELECT_READ;
2075 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
2079 // c->reconnect_ok = 0;
2080 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
2081 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
2086 static int do_write(ZOOM_connection c)
2088 return do_write_ex (c, c->buf_out, c->len_out);
2092 ZOOM_API(const char *)
2093 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
2095 return ZOOM_options_get (c->options, key);
2099 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
2102 ZOOM_options_set (c->options, key, val);
2105 ZOOM_API(const char *)
2106 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
2108 return ZOOM_options_get (r->options, key);
2112 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
2115 ZOOM_options_set (r->options, key, val);
2120 ZOOM_connection_errcode (ZOOM_connection c)
2122 return ZOOM_connection_error (c, 0, 0);
2125 ZOOM_API(const char *)
2126 ZOOM_connection_errmsg (ZOOM_connection c)
2129 ZOOM_connection_error (c, &msg, 0);
2133 ZOOM_API(const char *)
2134 ZOOM_connection_addinfo (ZOOM_connection c)
2136 const char *addinfo;
2137 ZOOM_connection_error (c, 0, &addinfo);
2142 ZOOM_connection_error (ZOOM_connection c, const char **cp,
2143 const char **addinfo)
2145 int error = c->error;
2150 case ZOOM_ERROR_NONE:
2151 *cp = "No error"; break;
2152 case ZOOM_ERROR_CONNECT:
2153 *cp = "Connect failed"; break;
2154 case ZOOM_ERROR_MEMORY:
2155 *cp = "Out of memory"; break;
2156 case ZOOM_ERROR_ENCODE:
2157 *cp = "Encoding failed"; break;
2158 case ZOOM_ERROR_DECODE:
2159 *cp = "Decoding failed"; break;
2160 case ZOOM_ERROR_CONNECTION_LOST:
2161 *cp = "Connection lost"; break;
2162 case ZOOM_ERROR_INIT:
2163 *cp = "Init rejected"; break;
2164 case ZOOM_ERROR_INTERNAL:
2165 *cp = "Internal failure"; break;
2166 case ZOOM_ERROR_TIMEOUT:
2167 *cp = "Timeout"; break;
2169 *cp = diagbib1_str (error);
2175 *addinfo = c->addinfo;
2182 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
2184 ZOOM_Event event = 0;
2185 int r = cs_look(c->cs);
2186 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
2191 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2192 c->error = ZOOM_ERROR_CONNECT;
2194 ZOOM_connection_put_event (c, event);
2196 else if (r == CS_CONNECT)
2199 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2201 ret = cs_rcvconnect (c->cs);
2202 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
2205 c->mask = ZOOM_SELECT_EXCEPT;
2206 if (c->cs->io_pending & CS_WANT_WRITE)
2207 c->mask += ZOOM_SELECT_WRITE;
2208 if (c->cs->io_pending & CS_WANT_READ)
2209 c->mask += ZOOM_SELECT_READ;
2210 ZOOM_connection_put_event (c, event);
2214 ZOOM_connection_put_event (c, event);
2215 ZOOM_connection_send_init (c);
2216 c->state = STATE_ESTABLISHED;
2220 c->error = ZOOM_ERROR_CONNECT;
2222 ZOOM_connection_put_event (c, event);
2227 if (mask & ZOOM_SELECT_READ)
2229 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2236 ZOOM_connection_last_event(ZOOM_connection cs)
2239 return ZOOM_EVENT_NONE;
2240 return cs->last_event;
2244 ZOOM_event (int no, ZOOM_connection *cs)
2247 struct pollfd pollfds[1024];
2248 ZOOM_connection poll_cs[1024];
2251 fd_set input, output, except;
2256 for (i = 0; i<no; i++)
2258 ZOOM_connection c = cs[i];
2260 if (c && (event = ZOOM_connection_get_event(c)))
2262 ZOOM_Event_destroy (event);
2266 for (i = 0; i<no; i++)
2268 ZOOM_connection c = cs[i];
2270 if (c && ZOOM_connection_exec_task (c))
2272 if ((event = ZOOM_connection_get_event(c)))
2274 ZOOM_Event_destroy (event);
2290 for (i = 0; i<no; i++)
2292 ZOOM_connection c = cs[i];
2297 fd = z3950_connection_socket(c);
2298 mask = z3950_connection_mask(c);
2308 short poll_events = 0;
2310 if (mask & ZOOM_SELECT_READ)
2311 poll_events += POLLIN;
2312 if (mask & ZOOM_SELECT_WRITE)
2313 poll_events += POLLOUT;
2314 if (mask & ZOOM_SELECT_EXCEPT)
2315 poll_events += POLLERR;
2316 pollfds[nfds].fd = fd;
2317 pollfds[nfds].events = poll_events;
2318 pollfds[nfds].revents = 0;
2323 if (mask & ZOOM_SELECT_READ)
2325 FD_SET (fd, &input);
2328 if (mask & ZOOM_SELECT_WRITE)
2330 FD_SET (fd, &output);
2333 if (mask & ZOOM_SELECT_EXCEPT)
2335 FD_SET (fd, &except);
2343 r = poll (pollfds, nfds, 15000);
2344 for (i = 0; i<nfds; i++)
2346 ZOOM_connection c = poll_cs[i];
2350 if (pollfds[i].revents & POLLIN)
2351 mask += ZOOM_SELECT_READ;
2352 if (pollfds[i].revents & POLLOUT)
2353 mask += ZOOM_SELECT_WRITE;
2354 if (pollfds[i].revents & POLLERR)
2355 mask += ZOOM_SELECT_EXCEPT;
2357 ZOOM_connection_do_io(c, mask);
2359 else if (r == 0 && c->mask)
2361 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2362 /* timeout and this connection was waiting */
2363 c->error = ZOOM_ERROR_TIMEOUT;
2365 ZOOM_connection_put_event(c, event);
2369 yaz_log (LOG_DEBUG, "select start");
2370 r = select (max_fd+1, &input, &output, &except, &tv);
2371 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
2372 for (i = 0; i<no; i++)
2374 ZOOM_connection c = cs[i];
2379 fd = z3950_connection_socket(c);
2383 /* no timeout and real socket */
2384 if (FD_ISSET(fd, &input))
2385 mask += ZOOM_SELECT_READ;
2386 if (FD_ISSET(fd, &output))
2387 mask += ZOOM_SELECT_WRITE;
2388 if (FD_ISSET(fd, &except))
2389 mask += ZOOM_SELECT_EXCEPT;
2391 ZOOM_connection_do_io(c, mask);
2393 if (r == 0 && c->mask)
2395 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2396 /* timeout and this connection was waiting */
2397 c->error = ZOOM_ERROR_TIMEOUT;
2399 yaz_log (LOG_DEBUG, "timeout");
2400 ZOOM_connection_put_event(c, event);
2404 for (i = 0; i<no; i++)
2406 ZOOM_connection c = cs[i];
2408 if (c && (event = ZOOM_connection_get_event(c)))
2410 ZOOM_Event_destroy (event);