2 * Copyright (c) 2000-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: zoom-c.c,v 1.11 2002-12-10 13:14:14 adam Exp $
7 * ZOOM layer for C, connections, result sets, queries.
10 #include <yaz/xmalloc.h>
11 #include <yaz/otherinfo.h>
13 #include <yaz/pquery.h>
14 #include <yaz/marcdisp.h>
15 #include <yaz/diagbib1.h>
16 #include <yaz/charneg.h>
31 static zoom_ret ZOOM_connection_send_init (ZOOM_connection c);
32 static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out);
34 static ZOOM_Event ZOOM_Event_create (int kind)
36 ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event));
43 static void ZOOM_Event_destroy (ZOOM_Event event)
48 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
52 c->m_queue_back->prev = event;
53 assert (c->m_queue_front);
57 assert (!c->m_queue_front);
58 c->m_queue_front = event;
60 event->next = c->m_queue_back;
62 c->m_queue_back = event;
65 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
67 ZOOM_Event event = c->m_queue_front;
70 assert (c->m_queue_back);
71 c->m_queue_front = event->prev;
74 assert (c->m_queue_back);
75 c->m_queue_front->next = 0;
79 c->last_event = event->kind;
83 static void set_bib1_error (ZOOM_connection c, int error)
91 static void clear_error (ZOOM_connection c)
96 case ZOOM_ERROR_CONNECT:
97 case ZOOM_ERROR_MEMORY:
98 case ZOOM_ERROR_DECODE:
99 case ZOOM_ERROR_CONNECTION_LOST:
100 case ZOOM_ERROR_INIT:
101 case ZOOM_ERROR_INTERNAL:
104 set_bib1_error(c, ZOOM_ERROR_NONE);
108 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
110 ZOOM_task *taskp = &c->tasks;
112 taskp = &(*taskp)->next;
113 *taskp = (ZOOM_task) xmalloc (sizeof(**taskp));
114 (*taskp)->running = 0;
115 (*taskp)->which = which;
121 ZOOM_task ZOOM_connection_insert_task (ZOOM_connection c, int which)
123 ZOOM_task task = (ZOOM_task) xmalloc (sizeof(*task));
125 task->next = c->tasks;
134 void ZOOM_connection_remove_task (ZOOM_connection c)
136 ZOOM_task task = c->tasks;
140 c->tasks = task->next;
143 case ZOOM_TASK_SEARCH:
144 ZOOM_resultset_destroy (task->u.search.resultset);
146 case ZOOM_TASK_RETRIEVE:
147 ZOOM_resultset_destroy (task->u.retrieve.resultset);
149 case ZOOM_TASK_CONNECT:
152 ZOOM_scanset_destroy (task->u.scan.scan);
154 case ZOOM_TASK_PACKAGE:
155 ZOOM_package_destroy (task->u.package);
164 void ZOOM_connection_remove_tasks (ZOOM_connection c)
167 ZOOM_connection_remove_task(c);
170 static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos);
172 ZOOM_API(ZOOM_connection)
173 ZOOM_connection_create (ZOOM_options options)
175 ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
180 c->state = STATE_IDLE;
182 set_bib1_error(c, ZOOM_ERROR_NONE);
189 c->options = ZOOM_options_create_with_parent(options);
194 c->charset = c->lang = 0;
201 c->odr_in = odr_createmem (ODR_DECODE);
202 c->odr_out = odr_createmem (ODR_ENCODE);
205 c->support_named_resultsets = 0;
206 c->last_event = ZOOM_EVENT_NONE;
208 c->m_queue_front = 0;
213 /* set database names. Take local databases (if set); otherwise
214 take databases given in ZURL (if set); otherwise use Default */
215 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
218 char **databaseNames;
221 const char *cp = ZOOM_options_get (options, "databaseName");
225 if (strncmp (con->host_port, "unix:", 5) == 0)
226 cp = strchr (con->host_port+5, ':');
228 cp = strchr (con->host_port, '/');
235 while ((c = strchr(c, '+')))
243 databaseNames = (char**)
244 odr_malloc (con->odr_out, no * sizeof(*databaseNames));
248 c = strchr (cp, '+');
256 /* cp ptr to first char of db name, c is char
258 databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
259 memcpy (databaseNames[no], cp, c-cp);
260 databaseNames[no++][c-cp] = '\0';
265 databaseNames[no] = NULL;
267 return databaseNames;
270 ZOOM_API(ZOOM_connection)
271 ZOOM_connection_new (const char *host, int portnum)
273 ZOOM_connection c = ZOOM_connection_create (0);
275 ZOOM_connection_connect (c, host, portnum);
280 ZOOM_connection_connect(ZOOM_connection c,
281 const char *host, int portnum)
288 yaz_log (LOG_DEBUG, "reconnect");
292 yaz_log(LOG_DEBUG, "connect");
294 val = ZOOM_options_get (c->options, "proxy");
296 c->proxy = xstrdup (val);
301 val = ZOOM_options_get (c->options, "charset");
303 c->charset = xstrdup (val);
308 val = ZOOM_options_get (c->options, "lang");
310 c->lang = xstrdup (val);
314 xfree (c->host_port);
318 sprintf (hostn, "%.80s:%d", host, portnum);
319 c->host_port = xstrdup(hostn);
322 c->host_port = xstrdup(host);
324 ZOOM_options_set(c->options, "host", c->host_port);
326 val = ZOOM_options_get (c->options, "cookie");
328 c->cookie_out = xstrdup (val);
330 val = ZOOM_options_get (c->options, "clientIP");
332 c->client_IP = xstrdup (val);
334 c->async = ZOOM_options_get_bool (c->options, "async", 0);
336 set_bib1_error(c, ZOOM_ERROR_NONE);
338 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
342 while (ZOOM_event (1, &c))
348 ZOOM_query_create(void)
350 ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
355 s->odr = odr_createmem (ODR_ENCODE);
361 ZOOM_query_destroy(ZOOM_query s)
367 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
368 if (s->refcount == 0)
370 odr_destroy (s->odr);
376 ZOOM_query_prefix(ZOOM_query s, const char *str)
378 s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query));
379 s->query->which = Z_Query_type_1;
380 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
381 if (!s->query->u.type_1)
387 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
389 s->sort_spec = yaz_sort_spec (s->odr, criteria);
395 static zoom_ret do_write(ZOOM_connection c);
398 ZOOM_connection_destroy(ZOOM_connection c)
405 for (r = c->resultsets; r; r = r->next)
410 odr_destroy (c->odr_in);
411 odr_destroy (c->odr_out);
412 ZOOM_options_destroy (c->options);
413 ZOOM_connection_remove_tasks (c);
414 xfree (c->host_port);
418 xfree (c->cookie_out);
419 xfree (c->cookie_in);
420 xfree (c->client_IP);
424 void ZOOM_resultset_addref (ZOOM_resultset r)
429 yaz_log (LOG_DEBUG, "ZOOM_resultset_addref r=%p count=%d",
433 ZOOM_resultset ZOOM_resultset_create ()
435 ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
437 yaz_log (LOG_DEBUG, "ZOOM_resultset_create r = %p", r);
440 r->odr = odr_createmem (ODR_ENCODE);
454 ZOOM_API(ZOOM_resultset)
455 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
458 ZOOM_query s = ZOOM_query_create();
460 ZOOM_query_prefix (s, q);
462 r = ZOOM_connection_search (c, s);
463 ZOOM_query_destroy (s);
467 ZOOM_API(ZOOM_resultset)
468 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
470 ZOOM_resultset r = ZOOM_resultset_create ();
474 r->r_sort_spec = q->sort_spec;
475 r->r_query = q->query;
478 r->options = ZOOM_options_create_with_parent(c->options);
480 r->start = ZOOM_options_get_int(r->options, "start", 0);
481 r->count = ZOOM_options_get_int(r->options, "count", 0);
482 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
483 cp = ZOOM_options_get (r->options, "setname");
485 r->setname = xstrdup (cp);
489 r->next = c->resultsets;
492 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
493 task->u.search.resultset = r;
494 ZOOM_resultset_addref (r);
500 while (ZOOM_event (1, &c))
507 ZOOM_resultset_destroy(ZOOM_resultset r)
512 yaz_log (LOG_DEBUG, "ZOOM_resultset_destroy r = %p count=%d",
514 if (r->refcount == 0)
516 ZOOM_record_cache rc;
518 for (rc = r->record_cache; rc; rc = rc->next)
519 if (rc->rec.wrbuf_marc)
520 wrbuf_free (rc->rec.wrbuf_marc, 1);
523 /* remove ourselves from the resultsets in connection */
524 ZOOM_resultset *rp = &r->connection->resultsets;
527 assert (*rp); /* we must be in this list!! */
529 { /* OK, we're here - take us out of it */
536 ZOOM_query_destroy (r->search);
537 ZOOM_options_destroy (r->options);
538 odr_destroy (r->odr);
545 ZOOM_resultset_size (ZOOM_resultset r)
550 static void do_close (ZOOM_connection c)
556 c->state = STATE_IDLE;
559 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
560 int force_sync, int start, int count)
570 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
571 task->u.retrieve.resultset = r;
572 task->u.retrieve.start = start;
573 task->u.retrieve.count = count;
575 ZOOM_resultset_addref (r);
577 if (!r->connection->async || force_sync)
578 while (r->connection && ZOOM_event (1, &r->connection))
583 ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
584 size_t start, size_t count)
586 int force_present = 0;
592 ZOOM_resultset_retrieve (r, force_present, start, count);
596 for (i = 0; i< count; i++)
597 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
601 static zoom_ret do_connect (ZOOM_connection c)
604 const char *effective_host;
607 effective_host = c->proxy;
609 effective_host = c->host_port;
611 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
614 c->cs = cs_create_host (effective_host, 0, &add);
618 int ret = cs_connect (c->cs, add);
621 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
622 ZOOM_connection_put_event(c, event);
623 ZOOM_connection_send_init(c);
624 c->state = STATE_ESTABLISHED;
629 c->state = STATE_CONNECTING;
630 c->mask = ZOOM_SELECT_EXCEPT;
631 if (c->cs->io_pending & CS_WANT_WRITE)
632 c->mask += ZOOM_SELECT_WRITE;
633 if (c->cs->io_pending & CS_WANT_READ)
634 c->mask += ZOOM_SELECT_READ;
638 c->state = STATE_IDLE;
639 set_bib1_error(c, ZOOM_ERROR_CONNECT);
640 return zoom_complete;
643 int z3950_connection_socket(ZOOM_connection c)
646 return cs_fileno(c->cs);
650 int z3950_connection_mask(ZOOM_connection c)
657 static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out)
660 for (i = 0; i<200; i++)
663 Z_OtherInformation **oi;
669 sprintf (buf, "otherInfo%d", i);
670 val = ZOOM_options_get (c->options, buf);
673 cp = strchr (val, ':');
677 if (len >= sizeof(buf))
679 memcpy (buf, val, len);
681 oidval = oid_getvalbyname (buf);
682 if (oidval == VAL_NONE)
686 yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1);
690 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
695 Z_OtherInformation **oi;
697 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
701 Z_OtherInformation **oi;
703 yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
705 otherInfo_attach (c, a, out);
706 if (!z_APDU(out, &a, 0, 0))
708 FILE *outf = fopen("/tmp/apdu.txt", "a");
711 ODR odr_pr = odr_createmem(ODR_PRINT);
712 fprintf (outf, "a=%p\n", a);
713 odr_setprint(odr_pr, outf);
714 z_APDU(odr_pr, &a, 0, 0);
717 yaz_log (LOG_DEBUG, "encoding failed");
718 set_bib1_error(c, ZOOM_ERROR_ENCODE);
726 static zoom_ret send_APDU (ZOOM_connection c, Z_APDU *a)
730 if (encode_APDU(c, a, c->odr_out))
731 return zoom_complete;
732 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
733 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
734 ZOOM_connection_put_event (c, event);
735 odr_reset(c->odr_out);
739 /* returns 1 if PDU was sent OK (still pending )
740 0 if PDU was not sent OK (nothing to wait for)
743 static zoom_ret ZOOM_connection_send_init (ZOOM_connection c)
746 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
747 Z_InitRequest *ireq = apdu->u.initRequest;
748 Z_IdAuthentication *auth = (Z_IdAuthentication *)
749 odr_malloc(c->odr_out, sizeof(*auth));
750 const char *auth_groupId = ZOOM_options_get (c->options, "group");
751 const char *auth_userId = ZOOM_options_get (c->options, "user");
752 const char *auth_password = ZOOM_options_get (c->options, "pass");
754 ODR_MASK_SET(ireq->options, Z_Options_search);
755 ODR_MASK_SET(ireq->options, Z_Options_present);
756 ODR_MASK_SET(ireq->options, Z_Options_scan);
757 ODR_MASK_SET(ireq->options, Z_Options_sort);
758 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
759 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
761 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
762 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
763 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
765 impname = ZOOM_options_get (c->options, "implementationName");
766 ireq->implementationName =
767 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
768 strcpy (ireq->implementationName, "");
771 strcat (ireq->implementationName, impname);
772 strcat (ireq->implementationName, "/");
774 strcat (ireq->implementationName, "ZOOM-C/YAZ");
776 *ireq->maximumRecordSize =
777 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
778 *ireq->preferredMessageSize =
779 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
781 if (auth_groupId || auth_password)
783 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
786 if (auth_groupId && *auth_groupId)
788 pass->groupId = (char *)
789 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
790 strcpy(pass->groupId, auth_groupId);
794 if (auth_userId && *auth_userId)
796 pass->userId = (char *)
797 odr_malloc(c->odr_out, strlen(auth_userId)+1);
798 strcpy(pass->userId, auth_userId);
802 if (auth_password && *auth_password)
804 pass->password = (char *)
805 odr_malloc(c->odr_out, strlen(auth_password)+1);
806 strcpy(pass->password, auth_password);
811 auth->which = Z_IdAuthentication_idPass;
812 auth->u.idPass = pass;
813 ireq->idAuthentication = auth;
816 else if (auth_userId)
818 auth->which = Z_IdAuthentication_open;
819 auth->u.open = (char *)
820 odr_malloc(c->odr_out, strlen(auth_userId)+1);
821 strcpy(auth->u.open, auth_userId);
822 ireq->idAuthentication = auth;
825 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
826 VAL_PROXY, 1, c->host_port);
827 if (c->charset||c->lang)
829 Z_OtherInformation **oi;
830 Z_OtherInformationUnit *oi_unit;
832 yaz_oi_APDU(apdu, &oi);
834 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
836 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
838 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
839 oi_unit->information.externallyDefinedInfo =
840 yaz_set_proposal_charneg
842 (const char **)&c->charset, (c->charset) ? 1:0,
843 (const char **)&c->lang, (c->lang) ? 1:0, 1);
847 return send_APDU (c, apdu);
850 static zoom_ret ZOOM_connection_send_search (ZOOM_connection c)
853 int lslb, ssub, mspn;
855 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
856 Z_SearchRequest *search_req = apdu->u.searchRequest;
857 const char *elementSetName;
858 const char *smallSetElementSetName;
859 const char *mediumSetElementSetName;
863 assert (c->tasks->which == ZOOM_TASK_SEARCH);
865 r = c->tasks->u.search.resultset;
868 ZOOM_options_get (r->options, "elementSetName");
869 smallSetElementSetName =
870 ZOOM_options_get (r->options, "smallSetElementSetName");
871 mediumSetElementSetName =
872 ZOOM_options_get (r->options, "mediumSetElementSetName");
874 ZOOM_options_get (r->options, "schema");
876 if (!smallSetElementSetName)
877 smallSetElementSetName = elementSetName;
879 if (!mediumSetElementSetName)
880 mediumSetElementSetName = elementSetName;
885 /* prepare query for the search request */
886 search_req->query = r->r_query;
888 search_req->databaseNames =
889 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
891 /* get syntax (no need to provide unless piggyback is in effect) */
892 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
894 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
895 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
896 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
897 if (lslb != -1 && ssub != -1 && mspn != -1)
899 /* So're a Z39.50 expert? Let's hope you don't do sort */
900 *search_req->largeSetLowerBound = lslb;
901 *search_req->smallSetUpperBound = ssub;
902 *search_req->mediumSetPresentNumber = mspn;
904 else if (r->start == 0 && r->count > 0
905 && r->piggyback && !r->r_sort_spec && !schema)
907 /* Regular piggyback - do it unless we're going to do sort */
908 *search_req->largeSetLowerBound = 2000000000;
909 *search_req->smallSetUpperBound = r->count;
910 *search_req->mediumSetPresentNumber = r->count;
911 smallSetElementSetName = 0; /* no need to provide this */
915 /* non-piggyback. Need not provide elementsets or syntaxes .. */
916 smallSetElementSetName = 0;
917 mediumSetElementSetName = 0;
920 if (smallSetElementSetName && *smallSetElementSetName)
922 Z_ElementSetNames *esn = (Z_ElementSetNames *)
923 odr_malloc (c->odr_out, sizeof(*esn));
925 esn->which = Z_ElementSetNames_generic;
926 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
927 search_req->smallSetElementSetNames = esn;
929 if (mediumSetElementSetName && *mediumSetElementSetName)
931 Z_ElementSetNames *esn = (Z_ElementSetNames *)
932 odr_malloc (c->odr_out, sizeof(*esn));
934 esn->which = Z_ElementSetNames_generic;
935 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
936 search_req->mediumSetElementSetNames = esn;
939 search_req->preferredRecordSyntax =
940 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
944 if (c->support_named_resultsets)
948 /* find the lowest unused ordinal so that we re-use
949 result sets on the server. */
950 for (ord = 1; ; ord++)
953 sprintf (setname, "%d", ord);
954 for (rp = c->resultsets; rp; rp = rp->next)
955 if (rp->setname && !strcmp (rp->setname, setname))
960 r->setname = xstrdup (setname);
961 yaz_log (LOG_DEBUG, "allocating set %s", r->setname);
964 r->setname = xstrdup ("default");
965 ZOOM_options_set (r->options, "setname", r->setname);
967 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
968 /* send search request */
969 return send_APDU (c, apdu);
972 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
975 Z_DefaultDiagFormat *r;
980 if (p->which != Z_DiagRec_defaultFormat)
982 set_bib1_error(c, ZOOM_ERROR_DECODE);
985 r = p->u.defaultFormat;
986 c->diagset = yaz_z3950oid_to_str(r->diagnosticSetId, &oclass);
990 case Z_DefaultDiagFormat_v2Addinfo:
991 addinfo = r->u.v2Addinfo;
993 case Z_DefaultDiagFormat_v3Addinfo:
994 addinfo = r->u.v3Addinfo;
998 c->addinfo = xstrdup (addinfo);
999 c->error = *r->condition;
1002 ZOOM_API(ZOOM_record)
1003 ZOOM_record_clone (ZOOM_record srec)
1010 odr_enc = odr_createmem(ODR_ENCODE);
1011 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
1013 buf = odr_getbuf (odr_enc, &size, 0);
1015 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
1016 nrec->odr = odr_createmem(ODR_DECODE);
1017 nrec->wrbuf_marc = 0;
1018 odr_setbuf (nrec->odr, buf, size, 0);
1019 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
1021 odr_destroy (odr_enc);
1025 ZOOM_API(ZOOM_record)
1026 ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
1028 return record_cache_lookup (s, pos);
1031 ZOOM_API(ZOOM_record)
1032 ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
1034 ZOOM_resultset_retrieve (r, 1, pos, 1);
1035 return ZOOM_resultset_record_immediate (r, pos);
1039 ZOOM_record_destroy (ZOOM_record rec)
1043 if (rec->wrbuf_marc)
1044 wrbuf_free (rec->wrbuf_marc, 1);
1045 odr_destroy (rec->odr);
1049 ZOOM_API(const char *)
1050 ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
1052 Z_NamePlusRecord *npr;
1055 *len = 0; /* default return */
1062 if (!strcmp (type, "database"))
1065 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
1066 return npr->databaseName;
1068 else if (!strcmp (type, "syntax"))
1070 const char *desc = 0;
1071 if (npr->which == Z_NamePlusRecord_databaseRecord)
1073 Z_External *r = (Z_External *) npr->u.databaseRecord;
1074 oident *ent = oid_getentbyoid(r->direct_reference);
1081 *len = strlen(desc);
1084 else if (!strcmp (type, "render") &&
1085 npr->which == Z_NamePlusRecord_databaseRecord)
1087 Z_External *r = (Z_External *) npr->u.databaseRecord;
1088 oident *ent = oid_getentbyoid(r->direct_reference);
1090 if (r->which == Z_External_sutrs)
1092 if (len) *len = r->u.sutrs->len;
1093 return (const char *) r->u.sutrs->buf;
1095 else if (r->which == Z_External_octet)
1104 case VAL_APPLICATION_XML:
1107 if (!rec->wrbuf_marc)
1108 rec->wrbuf_marc = wrbuf_alloc();
1109 wrbuf_rewind (rec->wrbuf_marc);
1110 if (yaz_marc_decode ((const char *)
1111 r->u.octet_aligned->buf,
1113 r->u.octet_aligned->len,
1116 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1117 return wrbuf_buf(rec->wrbuf_marc);
1120 if (len) *len = r->u.octet_aligned->len;
1121 return (const char *) r->u.octet_aligned->buf;
1123 else if (r->which == Z_External_grs1)
1125 if (!rec->wrbuf_marc)
1126 rec->wrbuf_marc = wrbuf_alloc();
1127 wrbuf_rewind (rec->wrbuf_marc);
1128 yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
1130 *len = wrbuf_len(rec->wrbuf_marc);
1131 return wrbuf_buf(rec->wrbuf_marc);
1135 else if (npr->which == Z_NamePlusRecord_databaseRecord &&
1136 (!strcmp (type, "xml") || !strcmp(type, "oai")))
1138 Z_External *r = (Z_External *) npr->u.databaseRecord;
1139 oident *ent = oid_getentbyoid(r->direct_reference);
1141 if (r->which == Z_External_sutrs)
1143 if (len) *len = r->u.sutrs->len;
1144 return (const char *) r->u.sutrs->buf;
1146 else if (r->which == Z_External_octet)
1148 int marc_decode_type = YAZ_MARC_MARCXML;
1150 if (!strcmp(type, "oai"))
1151 marc_decode_type = YAZ_MARC_OAIMARC;
1159 case VAL_APPLICATION_XML:
1162 if (!rec->wrbuf_marc)
1163 rec->wrbuf_marc = wrbuf_alloc();
1164 wrbuf_rewind (rec->wrbuf_marc);
1165 if (yaz_marc_decode ((const char *)
1166 r->u.octet_aligned->buf,
1168 r->u.octet_aligned->len,
1169 marc_decode_type) > 0)
1171 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1172 return wrbuf_buf(rec->wrbuf_marc);
1175 if (len) *len = r->u.octet_aligned->len;
1176 return (const char *) r->u.octet_aligned->buf;
1178 else if (r->which == Z_External_grs1)
1185 else if (!strcmp (type, "raw"))
1187 if (npr->which == Z_NamePlusRecord_databaseRecord)
1189 Z_External *r = (Z_External *) npr->u.databaseRecord;
1191 if (r->which == Z_External_sutrs)
1193 if (len) *len = r->u.sutrs->len;
1194 return (const char *) r->u.sutrs->buf;
1196 else if (r->which == Z_External_octet)
1198 if (len) *len = r->u.octet_aligned->len;
1199 return (const char *) r->u.octet_aligned->buf;
1201 else /* grs-1, explain, ... */
1204 return (const char *) npr->u.databaseRecord;
1209 else if (!strcmp (type, "ext"))
1211 if (npr->which == Z_NamePlusRecord_databaseRecord)
1212 return (const char *) npr->u.databaseRecord;
1218 static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, int pos)
1220 ZOOM_record_cache rc;
1221 const char *elementSetName =
1222 ZOOM_resultset_option_get (r, "elementSetName");
1223 const char *syntax =
1224 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1227 for (rc = r->record_cache; rc; rc = rc->next)
1231 if ((!elementSetName && !rc->elementSetName)
1232 || (elementSetName && rc->elementSetName &&
1233 !strcmp (elementSetName, rc->elementSetName)))
1235 if ((!syntax && !rc->syntax)
1236 || (syntax && rc->syntax &&
1237 !strcmp (syntax, rc->syntax)))
1239 /* not destroying rc->npr (it's handled by nmem )*/
1241 /* keeping wrbuf_marc too */
1247 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1250 rc->rec.wrbuf_marc = 0;
1252 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1254 rc->elementSetName = 0;
1257 rc->syntax = odr_strdup (r->odr, syntax);
1262 rc->next = r->record_cache;
1263 r->record_cache = rc;
1266 static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos)
1268 ZOOM_record_cache rc;
1269 const char *elementSetName =
1270 ZOOM_resultset_option_get (r, "elementSetName");
1271 const char *syntax =
1272 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1274 for (rc = r->record_cache; rc; rc = rc->next)
1278 if ((!elementSetName && !rc->elementSetName)
1279 || (elementSetName && rc->elementSetName &&
1280 !strcmp (elementSetName, rc->elementSetName)))
1282 if ((!syntax && !rc->syntax)
1283 || (syntax && rc->syntax &&
1284 !strcmp (syntax, rc->syntax)))
1292 static void handle_records (ZOOM_connection c, Z_Records *sr,
1295 ZOOM_resultset resultset;
1299 switch (c->tasks->which)
1301 case ZOOM_TASK_SEARCH:
1302 resultset = c->tasks->u.search.resultset;
1304 case ZOOM_TASK_RETRIEVE:
1305 resultset = c->tasks->u.retrieve.resultset;
1310 if (sr && sr->which == Z_Records_NSD)
1312 Z_DiagRec dr, *dr_p = &dr;
1313 dr.which = Z_DiagRec_defaultFormat;
1314 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1316 response_diag (c, dr_p);
1318 else if (sr && sr->which == Z_Records_multipleNSD)
1320 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1321 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1323 set_bib1_error(c, ZOOM_ERROR_DECODE);
1327 if (resultset->count + resultset->start > resultset->size)
1328 resultset->count = resultset->size - resultset->start;
1329 if (resultset->count < 0)
1330 resultset->count = 0;
1331 if (sr && sr->which == Z_Records_DBOSD)
1334 NMEM nmem = odr_extract_mem (c->odr_in);
1335 Z_NamePlusRecordList *p =
1336 sr->u.databaseOrSurDiagnostics;
1337 for (i = 0; i<p->num_records; i++)
1339 record_cache_add (resultset, p->records[i],
1340 i+ resultset->start);
1342 /* transfer our response to search_nmem .. we need it later */
1343 nmem_transfer (resultset->odr->mem, nmem);
1344 nmem_destroy (nmem);
1345 if (present_phase && p->num_records == 0)
1347 /* present response and we didn't get any records! */
1348 c->error = ZOOM_ERROR_DECODE;
1351 else if (present_phase)
1353 /* present response and we didn't get any records! */
1354 set_bib1_error(c, ZOOM_ERROR_DECODE);
1359 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1361 handle_records (c, pr->records, 1);
1364 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1366 ZOOM_resultset resultset;
1368 yaz_log (LOG_DEBUG, "got search response");
1370 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1373 resultset = c->tasks->u.search.resultset;
1375 resultset->size = *sr->resultCount;
1376 handle_records (c, sr->records, 0);
1379 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1381 if (res->diagnostics && res->num_diagnostics > 0)
1382 response_diag (c, res->diagnostics[0]);
1385 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1387 NMEM nmem = odr_extract_mem (c->odr_in);
1390 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1392 scan = c->tasks->u.scan.scan;
1394 if (res->entries && res->entries->nonsurrogateDiagnostics)
1395 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1396 scan->scan_response = res;
1397 nmem_transfer (scan->odr->mem, nmem);
1399 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1400 if (res->positionOfTerm)
1401 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1402 if (res->scanStatus)
1403 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1404 if (res->numberOfEntriesReturned)
1405 ZOOM_options_set_int (scan->options, "number",
1406 *res->numberOfEntriesReturned);
1407 nmem_destroy (nmem);
1411 static zoom_ret send_sort (ZOOM_connection c)
1413 ZOOM_resultset resultset;
1415 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1416 return zoom_complete;
1418 resultset = c->tasks->u.search.resultset;
1422 resultset->r_sort_spec = 0;
1423 return zoom_complete;
1425 if (resultset->r_sort_spec)
1427 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1428 Z_SortRequest *req = apdu->u.sortRequest;
1430 req->num_inputResultSetNames = 1;
1431 req->inputResultSetNames = (Z_InternationalString **)
1432 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1433 req->inputResultSetNames[0] =
1434 odr_strdup (c->odr_out, resultset->setname);
1435 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1436 req->sortSequence = resultset->r_sort_spec;
1437 resultset->r_sort_spec = 0;
1438 return send_APDU (c, apdu);
1440 return zoom_complete;
1443 static zoom_ret send_present (ZOOM_connection c)
1446 Z_PresentRequest *req = 0;
1448 const char *syntax = 0;
1449 const char *elementSetName = 0;
1450 const char *schema = 0;
1451 ZOOM_resultset resultset;
1454 return zoom_complete;
1456 switch (c->tasks->which)
1458 case ZOOM_TASK_SEARCH:
1459 resultset = c->tasks->u.search.resultset;
1461 case ZOOM_TASK_RETRIEVE:
1462 resultset = c->tasks->u.retrieve.resultset;
1463 resultset->start = c->tasks->u.retrieve.start;
1464 resultset->count = c->tasks->u.retrieve.count;
1466 if (resultset->start >= resultset->size)
1467 return zoom_complete;
1468 if (resultset->start + resultset->count > resultset->size)
1469 resultset->count = resultset->size - resultset->start;
1472 return zoom_complete;
1475 syntax = ZOOM_resultset_option_get (resultset, "preferredRecordSyntax");
1476 elementSetName = ZOOM_resultset_option_get (resultset, "elementSetName");
1477 schema = ZOOM_resultset_option_get (resultset, "schema");
1479 if (c->error) /* don't continue on error */
1480 return zoom_complete;
1481 if (resultset->start < 0)
1482 return zoom_complete;
1483 for (i = 0; i<resultset->count; i++)
1486 record_cache_lookup (resultset, i + resultset->start);
1490 if (i == resultset->count)
1491 return zoom_complete;
1493 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1494 req = apdu->u.presentRequest;
1496 resultset->start += i;
1497 resultset->count -= i;
1498 *req->resultSetStartPoint = resultset->start + 1;
1499 *req->numberOfRecordsRequested = resultset->count;
1500 assert (*req->numberOfRecordsRequested > 0);
1502 if (syntax && *syntax)
1503 req->preferredRecordSyntax =
1504 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1506 if (schema && *schema)
1508 Z_RecordComposition *compo = (Z_RecordComposition *)
1509 odr_malloc (c->odr_out, sizeof(*compo));
1511 req->recordComposition = compo;
1512 compo->which = Z_RecordComp_complex;
1513 compo->u.complex = (Z_CompSpec *)
1514 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1515 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1516 odr_malloc(c->odr_out, sizeof(bool_t));
1517 *compo->u.complex->selectAlternativeSyntax = 0;
1519 compo->u.complex->generic = (Z_Specification *)
1520 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1522 compo->u.complex->generic->schema = (Odr_oid *)
1523 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1525 if (!compo->u.complex->generic->schema)
1527 /* OID wasn't a schema! Try record syntax instead. */
1529 compo->u.complex->generic->schema = (Odr_oid *)
1530 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1532 if (elementSetName && *elementSetName)
1534 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1535 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1536 compo->u.complex->generic->elementSpec->which =
1537 Z_ElementSpec_elementSetName;
1538 compo->u.complex->generic->elementSpec->u.elementSetName =
1539 odr_strdup (c->odr_out, elementSetName);
1542 compo->u.complex->generic->elementSpec = 0;
1543 compo->u.complex->num_dbSpecific = 0;
1544 compo->u.complex->dbSpecific = 0;
1545 compo->u.complex->num_recordSyntax = 0;
1546 compo->u.complex->recordSyntax = 0;
1548 else if (elementSetName && *elementSetName)
1550 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1551 odr_malloc (c->odr_out, sizeof(*esn));
1552 Z_RecordComposition *compo = (Z_RecordComposition *)
1553 odr_malloc (c->odr_out, sizeof(*compo));
1555 esn->which = Z_ElementSetNames_generic;
1556 esn->u.generic = odr_strdup (c->odr_out, elementSetName);
1557 compo->which = Z_RecordComp_simple;
1558 compo->u.simple = esn;
1559 req->recordComposition = compo;
1561 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1562 return send_APDU (c, apdu);
1565 ZOOM_API(ZOOM_scanset)
1566 ZOOM_connection_scan (ZOOM_connection c, const char *start)
1568 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1570 scan->connection = c;
1571 scan->odr = odr_createmem (ODR_DECODE);
1572 scan->options = ZOOM_options_create_with_parent (c->options);
1574 scan->scan_response = 0;
1576 if ((scan->termListAndStartPoint =
1577 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1580 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1581 task->u.scan.scan = scan;
1586 while (ZOOM_event (1, &c))
1594 ZOOM_scanset_destroy (ZOOM_scanset scan)
1599 if (scan->refcount == 0)
1601 odr_destroy (scan->odr);
1603 ZOOM_options_destroy (scan->options);
1608 static zoom_ret send_package (ZOOM_connection c)
1612 return zoom_complete;
1613 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1615 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
1616 ZOOM_connection_put_event (c, event);
1618 return do_write_ex (c, c->tasks->u.package->buf_out,
1619 c->tasks->u.package->len_out);
1622 static zoom_ret send_scan (ZOOM_connection c)
1625 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1626 Z_ScanRequest *req = apdu->u.scanRequest;
1628 return zoom_complete;
1629 assert (c->tasks->which == ZOOM_TASK_SCAN);
1630 scan = c->tasks->u.scan.scan;
1632 req->termListAndStartPoint = scan->termListAndStartPoint;
1633 req->attributeSet = scan->attributeSet;
1635 *req->numberOfTermsRequested =
1636 ZOOM_options_get_int(scan->options, "number", 10);
1638 req->preferredPositionInResponse =
1639 odr_intdup (c->odr_out,
1640 ZOOM_options_get_int(scan->options, "position", 1));
1643 odr_intdup (c->odr_out,
1644 ZOOM_options_get_int(scan->options, "stepSize", 0));
1646 req->databaseNames = set_DatabaseNames (c, scan->options,
1647 &req->num_databaseNames);
1649 return send_APDU (c, apdu);
1653 ZOOM_scanset_size (ZOOM_scanset scan)
1655 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1657 return scan->scan_response->entries->num_entries;
1660 ZOOM_API(const char *)
1661 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1664 const char *term = 0;
1665 size_t noent = ZOOM_scanset_size (scan);
1666 Z_ScanResponse *res = scan->scan_response;
1672 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1674 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1676 if (t->term->which == Z_Term_general)
1678 term = (const char *) t->term->u.general->buf;
1679 *len = t->term->u.general->len;
1681 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1686 ZOOM_API(const char *)
1687 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1689 return ZOOM_options_get (scan->options, key);
1693 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1696 ZOOM_options_set (scan->options, key, val);
1699 static Z_APDU *create_es_package (ZOOM_package p, int type)
1702 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
1703 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1705 *req->function = Z_ExtendedServicesRequest_create;
1707 str = ZOOM_options_get(p->options, "package-name");
1709 req->packageName = nmem_strdup (p->odr_out->mem, str);
1711 str = ZOOM_options_get(p->options, "user-id");
1713 req->userId = nmem_strdup (p->odr_out->mem, str);
1715 req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1718 str = ZOOM_options_get(p->options, "function");
1721 if (!strcmp (str, "create"))
1723 if (!strcmp (str, "delete"))
1725 if (!strcmp (str, "modify"))
1731 static const char *ill_array_lookup (void *clientData, const char *idx)
1733 ZOOM_package p = (ZOOM_package) clientData;
1734 return ZOOM_options_get (p->options, idx+4);
1737 static Z_External *encode_ill_request (ZOOM_package p)
1739 ODR out = p->odr_out;
1742 struct ill_get_ctl ctl;
1744 ctl.odr = p->odr_out;
1746 ctl.f = ill_array_lookup;
1748 req = ill_get_ILLRequest(&ctl, "ill", 0);
1750 if (!ill_Request (out, &req, 0, 0))
1752 int ill_request_size;
1753 char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1754 if (ill_request_buf)
1755 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1761 int illRequest_size = 0;
1762 char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0);
1764 oid.proto = PROTO_GENERAL;
1765 oid.oclass = CLASS_GENERAL;
1766 oid.value = VAL_ISO_ILL_1;
1768 r = (Z_External *) odr_malloc (out, sizeof(*r));
1769 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1770 r->indirect_reference = 0;
1772 r->which = Z_External_single;
1774 r->u.single_ASN1_type = (Odr_oct *)
1775 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1776 r->u.single_ASN1_type->buf = (unsigned char*)
1777 odr_malloc (out, illRequest_size);
1778 r->u.single_ASN1_type->len = illRequest_size;
1779 r->u.single_ASN1_type->size = illRequest_size;
1780 memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size);
1785 static Z_ItemOrder *encode_item_order(ZOOM_package p)
1787 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc (p->odr_out, sizeof(*req));
1790 req->which=Z_IOItemOrder_esRequest;
1791 req->u.esRequest = (Z_IORequest *)
1792 odr_malloc(p->odr_out,sizeof(Z_IORequest));
1794 /* to keep part ... */
1795 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1796 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
1797 req->u.esRequest->toKeep->supplDescription = 0;
1798 req->u.esRequest->toKeep->contact = (Z_IOContact *)
1799 odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
1801 str = ZOOM_options_get(p->options, "contact-name");
1802 req->u.esRequest->toKeep->contact->name = str ?
1803 nmem_strdup (p->odr_out->mem, str) : 0;
1805 str = ZOOM_options_get(p->options, "contact-phone");
1806 req->u.esRequest->toKeep->contact->phone = str ?
1807 nmem_strdup (p->odr_out->mem, str) : 0;
1809 str = ZOOM_options_get(p->options, "contact-email");
1810 req->u.esRequest->toKeep->contact->email = str ?
1811 nmem_strdup (p->odr_out->mem, str) : 0;
1813 req->u.esRequest->toKeep->addlBilling = 0;
1815 /* not to keep part ... */
1816 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1817 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
1819 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
1820 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
1822 str = ZOOM_options_get(p->options, "itemorder-setname");
1825 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
1826 nmem_strdup (p->odr_out->mem, str);
1827 req->u.esRequest->notToKeep->resultSetItem->item =
1828 (int *) odr_malloc(p->odr_out, sizeof(int));
1830 str = ZOOM_options_get(p->options, "itemorder-item");
1831 *req->u.esRequest->notToKeep->resultSetItem->item =
1832 (str ? atoi(str) : 1);
1834 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
1840 ZOOM_package_send (ZOOM_package p, const char *type)
1847 odr_reset (p->odr_out);
1850 if (!strcmp(type, "itemorder"))
1853 apdu = create_es_package (p, VAL_ITEMORDER);
1856 r = (Z_External *) odr_malloc (p->odr_out, sizeof(*r));
1858 r->direct_reference =
1859 yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1862 r->which = Z_External_itemOrder;
1863 r->indirect_reference = 0;
1864 r->u.itemOrder = encode_item_order (p);
1866 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
1871 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
1875 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE);
1876 task->u.package = p;
1877 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
1878 p->buf_out = (char *) xmalloc (p->len_out);
1879 memcpy (p->buf_out, buf, p->len_out);
1884 while (ZOOM_event (1, &c))
1891 ZOOM_API(ZOOM_package)
1892 ZOOM_connection_package (ZOOM_connection c, ZOOM_options options)
1894 ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p));
1897 p->odr_out = odr_createmem (ODR_ENCODE);
1898 p->options = ZOOM_options_create_with_parent2 (options, c->options);
1906 ZOOM_package_destroy(ZOOM_package p)
1911 if (p->refcount == 0)
1913 odr_destroy (p->odr_out);
1916 ZOOM_options_destroy (p->options);
1921 ZOOM_API(const char *)
1922 ZOOM_package_option_get (ZOOM_package p, const char *key)
1924 return ZOOM_options_get (p->options, key);
1928 ZOOM_package_option_set (ZOOM_package p, const char *key,
1931 ZOOM_options_set (p->options, key, val);
1934 static int ZOOM_connection_exec_task (ZOOM_connection c)
1936 ZOOM_task task = c->tasks;
1937 zoom_ret ret = zoom_complete;
1941 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task task=<null>");
1944 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
1945 task->which, task->running);
1946 if (c->error != ZOOM_ERROR_NONE ||
1947 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1949 yaz_log (LOG_DEBUG, "remove tasks because of error = %d", c->error);
1950 ZOOM_connection_remove_tasks (c);
1955 yaz_log (LOG_DEBUG, "task already running");
1959 switch (task->which)
1961 case ZOOM_TASK_SEARCH:
1962 ret = ZOOM_connection_send_search (c);
1964 case ZOOM_TASK_RETRIEVE:
1965 ret = send_present (c);
1967 case ZOOM_TASK_CONNECT:
1968 ret = do_connect(c);
1970 case ZOOM_TASK_SCAN:
1973 case ZOOM_TASK_PACKAGE:
1974 ret = send_package(c);
1977 if (ret == zoom_complete)
1979 yaz_log (LOG_DEBUG, "task removed (complete)");
1980 ZOOM_connection_remove_task (c);
1983 yaz_log (LOG_DEBUG, "task pending");
1987 static zoom_ret send_sort_present (ZOOM_connection c)
1989 zoom_ret r = send_sort (c);
1990 if (r == zoom_complete)
1991 r = send_present (c);
1995 static int es_response (ZOOM_connection c,
1996 Z_ExtendedServicesResponse *res)
1998 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
2000 if (res->diagnostics && res->num_diagnostics > 0)
2001 response_diag(c, res->diagnostics[0]);
2002 if (res->taskPackage &&
2003 res->taskPackage->which == Z_External_extendedService)
2005 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
2006 Odr_oct *id = taskPackage->targetReference;
2009 ZOOM_options_setl (c->tasks->u.package->options,
2010 "targetReference", (char*) id->buf, id->len);
2016 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
2018 Z_InitResponse *initrs;
2021 yaz_log (LOG_DEBUG, "handle_apdu type=%d", apdu->which);
2024 case Z_APDU_initResponse:
2025 initrs = apdu->u.initResponse;
2026 ZOOM_connection_option_set(c, "targetImplementationId",
2027 initrs->implementationId ?
2028 initrs->implementationId : "");
2029 ZOOM_connection_option_set(c, "targetImplementationName",
2030 initrs->implementationName ?
2031 initrs->implementationName : "");
2032 ZOOM_connection_option_set(c, "targetImplementationVersion",
2033 initrs->implementationVersion ?
2034 initrs->implementationVersion : "");
2035 if (!*initrs->result)
2037 set_bib1_error(c, ZOOM_ERROR_INIT);
2042 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
2044 xfree (c->cookie_in);
2047 c->cookie_in = xstrdup(cookie);
2048 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
2049 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
2050 c->support_named_resultsets = 1;
2053 assert (c->tasks->which == ZOOM_TASK_CONNECT);
2054 ZOOM_connection_remove_task (c);
2056 ZOOM_connection_exec_task (c);
2058 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
2060 NMEM tmpmem = nmem_create();
2061 Z_CharSetandLanguageNegotiation *p =
2062 yaz_get_charneg_record(initrs->otherInfo);
2066 char *charset=NULL, *lang=NULL;
2069 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
2070 yaz_log(LOG_DEBUG, "Target accepted: charset %s, "
2071 "language %s, select %d",
2072 charset ? charset : "none", lang ? lang : "none", sel);
2074 ZOOM_connection_option_set (c, "negotiation-charset",
2077 ZOOM_connection_option_set (c, "negotiation-lang",
2079 nmem_destroy(tmpmem);
2083 case Z_APDU_searchResponse:
2084 handle_search_response (c, apdu->u.searchResponse);
2085 if (send_sort_present (c) == zoom_complete)
2086 ZOOM_connection_remove_task (c);
2088 case Z_APDU_presentResponse:
2089 handle_present_response (c, apdu->u.presentResponse);
2090 if (send_present (c) == zoom_complete)
2091 ZOOM_connection_remove_task (c);
2093 case Z_APDU_sortResponse:
2094 sort_response (c, apdu->u.sortResponse);
2095 if (send_present (c) == zoom_complete)
2096 ZOOM_connection_remove_task (c);
2098 case Z_APDU_scanResponse:
2099 scan_response (c, apdu->u.scanResponse);
2100 ZOOM_connection_remove_task (c);
2102 case Z_APDU_extendedServicesResponse:
2103 es_response (c, apdu->u.extendedServicesResponse);
2104 ZOOM_connection_remove_task (c);
2107 if (c->reconnect_ok)
2110 c->tasks->running = 0;
2111 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2115 set_bib1_error(c, ZOOM_ERROR_CONNECTION_LOST);
2120 set_bib1_error(c, ZOOM_ERROR_DECODE);
2125 static int do_read (ZOOM_connection c)
2131 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
2132 ZOOM_connection_put_event (c, event);
2134 yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in);
2136 r = cs_get (c->cs, &c->buf_in, &c->len_in);
2141 if (c->reconnect_ok)
2144 c->reconnect_ok = 0;
2145 yaz_log (LOG_DEBUG, "reconnect read");
2146 c->tasks->running = 0;
2147 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2151 c->error= ZOOM_ERROR_CONNECTION_LOST;
2158 odr_reset (c->odr_in);
2159 odr_setbuf (c->odr_in, c->buf_in, r, 0);
2160 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
2161 ZOOM_connection_put_event (c, event);
2162 if (!z_APDU (c->odr_in, &apdu, 0, 0))
2164 set_bib1_error(c, ZOOM_ERROR_DECODE);
2168 handle_apdu (c, apdu);
2169 c->reconnect_ok = 0;
2174 static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
2179 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
2180 ZOOM_connection_put_event (c, event);
2182 yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
2183 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
2185 if (c->reconnect_ok)
2188 c->reconnect_ok = 0;
2189 yaz_log (LOG_DEBUG, "reconnect write");
2190 c->tasks->running = 0;
2191 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2192 return zoom_complete;
2194 if (c->state == STATE_CONNECTING)
2195 set_bib1_error(c, ZOOM_ERROR_CONNECT);
2197 set_bib1_error(c, ZOOM_ERROR_CONNECTION_LOST);
2199 return zoom_complete;
2203 c->mask = ZOOM_SELECT_EXCEPT;
2204 if (c->cs->io_pending & CS_WANT_WRITE)
2205 c->mask += ZOOM_SELECT_WRITE;
2206 if (c->cs->io_pending & CS_WANT_READ)
2207 c->mask += ZOOM_SELECT_READ;
2208 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
2212 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
2213 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
2215 return zoom_pending;
2218 static zoom_ret do_write(ZOOM_connection c)
2220 return do_write_ex (c, c->buf_out, c->len_out);
2224 ZOOM_API(const char *)
2225 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
2227 return ZOOM_options_get (c->options, key);
2231 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
2234 ZOOM_options_set (c->options, key, val);
2237 ZOOM_API(const char *)
2238 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
2240 return ZOOM_options_get (r->options, key);
2244 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
2247 ZOOM_options_set (r->options, key, val);
2252 ZOOM_connection_errcode (ZOOM_connection c)
2254 return ZOOM_connection_error (c, 0, 0);
2257 ZOOM_API(const char *)
2258 ZOOM_connection_errmsg (ZOOM_connection c)
2261 ZOOM_connection_error (c, &msg, 0);
2265 ZOOM_API(const char *)
2266 ZOOM_connection_addinfo (ZOOM_connection c)
2268 const char *addinfo;
2269 ZOOM_connection_error (c, 0, &addinfo);
2273 ZOOM_API(const char *)
2274 ZOOM_diag_str (int error)
2278 case ZOOM_ERROR_NONE:
2280 case ZOOM_ERROR_CONNECT:
2281 return "Connect failed";
2282 case ZOOM_ERROR_MEMORY:
2283 return "Out of memory";
2284 case ZOOM_ERROR_ENCODE:
2285 return "Encoding failed";
2286 case ZOOM_ERROR_DECODE:
2287 return "Decoding failed";
2288 case ZOOM_ERROR_CONNECTION_LOST:
2289 return "Connection lost";
2290 case ZOOM_ERROR_INIT:
2291 return "Init rejected";
2292 case ZOOM_ERROR_INTERNAL:
2293 return "Internal failure";
2294 case ZOOM_ERROR_TIMEOUT:
2297 return diagbib1_str (error);
2302 ZOOM_connection_error_x (ZOOM_connection c, const char **cp,
2303 const char **addinfo, const char **diagset)
2305 int error = c->error;
2308 *cp = ZOOM_diag_str(error);
2311 *addinfo = c->addinfo ? c->addinfo : "";
2313 *diagset = c->diagset ? c->diagset : "";
2318 ZOOM_connection_error (ZOOM_connection c, const char **cp,
2319 const char **addinfo)
2321 return ZOOM_connection_error_x(c, cp, addinfo, 0);
2324 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
2326 ZOOM_Event event = 0;
2327 int r = cs_look(c->cs);
2328 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
2333 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2334 set_bib1_error(c, ZOOM_ERROR_CONNECT);
2336 ZOOM_connection_put_event (c, event);
2338 else if (r == CS_CONNECT)
2341 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2343 ret = cs_rcvconnect (c->cs);
2344 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
2347 c->mask = ZOOM_SELECT_EXCEPT;
2348 if (c->cs->io_pending & CS_WANT_WRITE)
2349 c->mask += ZOOM_SELECT_WRITE;
2350 if (c->cs->io_pending & CS_WANT_READ)
2351 c->mask += ZOOM_SELECT_READ;
2352 ZOOM_connection_put_event (c, event);
2356 ZOOM_connection_put_event (c, event);
2357 ZOOM_connection_send_init (c);
2358 c->state = STATE_ESTABLISHED;
2362 set_bib1_error(c, ZOOM_ERROR_CONNECT);
2364 ZOOM_connection_put_event (c, event);
2369 if (mask & ZOOM_SELECT_READ)
2371 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2378 ZOOM_connection_last_event(ZOOM_connection cs)
2381 return ZOOM_EVENT_NONE;
2382 return cs->last_event;
2386 ZOOM_event (int no, ZOOM_connection *cs)
2390 struct pollfd pollfds[1024];
2391 ZOOM_connection poll_cs[1024];
2394 fd_set input, output, except;
2399 for (i = 0; i<no; i++)
2401 ZOOM_connection c = cs[i];
2403 if (c && (event = ZOOM_connection_get_event(c)))
2405 ZOOM_Event_destroy (event);
2409 for (i = 0; i<no; i++)
2411 ZOOM_connection c = cs[i];
2413 if (c && ZOOM_connection_exec_task (c))
2415 if ((event = ZOOM_connection_get_event(c)))
2417 ZOOM_Event_destroy (event);
2430 for (i = 0; i<no; i++)
2432 ZOOM_connection c = cs[i];
2438 fd = z3950_connection_socket(c);
2439 mask = z3950_connection_mask(c);
2446 this_timeout = ZOOM_options_get_int (c->options, "timeout", -1);
2447 if (this_timeout != -1 && this_timeout < timeout)
2448 timeout = this_timeout;
2452 short poll_events = 0;
2454 if (mask & ZOOM_SELECT_READ)
2455 poll_events += POLLIN;
2456 if (mask & ZOOM_SELECT_WRITE)
2457 poll_events += POLLOUT;
2458 if (mask & ZOOM_SELECT_EXCEPT)
2459 poll_events += POLLERR;
2460 pollfds[nfds].fd = fd;
2461 pollfds[nfds].events = poll_events;
2462 pollfds[nfds].revents = 0;
2467 if (mask & ZOOM_SELECT_READ)
2469 FD_SET (fd, &input);
2472 if (mask & ZOOM_SELECT_WRITE)
2474 FD_SET (fd, &output);
2477 if (mask & ZOOM_SELECT_EXCEPT)
2479 FD_SET (fd, &except);
2484 if (timeout >= 5000)
2491 r = poll (pollfds, nfds, timeout * 1000);
2492 for (i = 0; i<nfds; i++)
2494 ZOOM_connection c = poll_cs[i];
2498 if (pollfds[i].revents & POLLIN)
2499 mask += ZOOM_SELECT_READ;
2500 if (pollfds[i].revents & POLLOUT)
2501 mask += ZOOM_SELECT_WRITE;
2502 if (pollfds[i].revents & POLLERR)
2503 mask += ZOOM_SELECT_EXCEPT;
2505 ZOOM_connection_do_io(c, mask);
2507 else if (r == 0 && c->mask)
2509 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2510 /* timeout and this connection was waiting */
2511 set_bib1_error(c, ZOOM_ERROR_TIMEOUT);
2513 ZOOM_connection_put_event(c, event);
2517 tv.tv_sec = timeout;
2519 yaz_log (LOG_DEBUG, "select start");
2520 r = select (max_fd+1, &input, &output, &except, &tv);
2521 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
2522 for (i = 0; i<no; i++)
2524 ZOOM_connection c = cs[i];
2529 fd = z3950_connection_socket(c);
2533 /* no timeout and real socket */
2534 if (FD_ISSET(fd, &input))
2535 mask += ZOOM_SELECT_READ;
2536 if (FD_ISSET(fd, &output))
2537 mask += ZOOM_SELECT_WRITE;
2538 if (FD_ISSET(fd, &except))
2539 mask += ZOOM_SELECT_EXCEPT;
2541 ZOOM_connection_do_io(c, mask);
2543 if (r == 0 && c->mask)
2545 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2546 /* timeout and this connection was waiting */
2547 set_bib1_error(c, ZOOM_ERROR_TIMEOUT);
2549 yaz_log (LOG_DEBUG, "timeout");
2550 ZOOM_connection_put_event(c, event);
2554 for (i = 0; i<no; i++)
2556 ZOOM_connection c = cs[i];
2558 if (c && (event = ZOOM_connection_get_event(c)))
2560 ZOOM_Event_destroy (event);