2 * $Id: zoom-c.c,v 1.34 2002-07-01 12:59:12 adam Exp $
4 * ZOOM layer for C, connections, result sets, queries.
7 #include <yaz/xmalloc.h>
8 #include <yaz/otherinfo.h>
10 #include <yaz/pquery.h>
11 #include <yaz/diagbib1.h>
12 #include <yaz/charneg.h>
21 static int ZOOM_connection_send_init (ZOOM_connection c);
22 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out);
24 static ZOOM_Event ZOOM_Event_create (int kind)
26 ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event));
33 static void ZOOM_Event_destroy (ZOOM_Event event)
38 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
42 c->m_queue_back->prev = event;
43 assert (c->m_queue_front);
47 assert (!c->m_queue_front);
48 c->m_queue_front = event;
50 event->next = c->m_queue_back;
52 c->m_queue_back = event;
55 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
57 ZOOM_Event event = c->m_queue_front;
60 assert (c->m_queue_back);
61 c->m_queue_front = event->prev;
64 assert (c->m_queue_back);
65 c->m_queue_front->next = 0;
69 c->last_event = event->kind;
73 static void clear_error (ZOOM_connection c)
78 case ZOOM_ERROR_CONNECT:
79 case ZOOM_ERROR_MEMORY:
80 case ZOOM_ERROR_DECODE:
81 case ZOOM_ERROR_CONNECTION_LOST:
83 case ZOOM_ERROR_INTERNAL:
86 c->error = ZOOM_ERROR_NONE;
92 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
94 ZOOM_task *taskp = &c->tasks;
96 taskp = &(*taskp)->next;
97 *taskp = (ZOOM_task) xmalloc (sizeof(**taskp));
98 (*taskp)->running = 0;
99 (*taskp)->which = which;
105 ZOOM_task ZOOM_connection_insert_task (ZOOM_connection c, int which)
107 ZOOM_task task = (ZOOM_task) xmalloc (sizeof(*task));
109 task->next = c->tasks;
118 void ZOOM_connection_remove_task (ZOOM_connection c)
120 ZOOM_task task = c->tasks;
124 c->tasks = task->next;
127 case ZOOM_TASK_SEARCH:
128 ZOOM_resultset_destroy (task->u.search.resultset);
130 case ZOOM_TASK_RETRIEVE:
131 ZOOM_resultset_destroy (task->u.retrieve.resultset);
133 case ZOOM_TASK_CONNECT:
136 ZOOM_scanset_destroy (task->u.scan.scan);
138 case ZOOM_TASK_PACKAGE:
139 ZOOM_package_destroy (task->u.package);
148 void ZOOM_connection_remove_tasks (ZOOM_connection c)
151 ZOOM_connection_remove_task(c);
154 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
156 const char *elementSetName);
158 ZOOM_API(ZOOM_connection)
159 ZOOM_connection_create (ZOOM_options options)
161 ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
166 c->state = STATE_IDLE;
167 c->error = ZOOM_ERROR_NONE;
175 c->options = ZOOM_options_create_with_parent(options);
180 c->charset = c->lang = 0;
187 c->odr_in = odr_createmem (ODR_DECODE);
188 c->odr_out = odr_createmem (ODR_ENCODE);
191 c->support_named_resultsets = 0;
192 c->last_event = ZOOM_EVENT_NONE;
194 c->m_queue_front = 0;
199 /* set database names. Take local databases (if set); otherwise
200 take databases given in ZURL (if set); otherwise use Default */
201 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
204 char **databaseNames;
207 const char *cp = ZOOM_options_get (options, "databaseName");
211 cp = strchr (con->host_port, '/');
218 while ((c = strchr(c, '+')))
226 databaseNames = (char**)
227 odr_malloc (con->odr_out, no * sizeof(*databaseNames));
231 c = strchr (cp, '+');
239 /* cp ptr to first char of db name, c is char
241 databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
242 memcpy (databaseNames[no], cp, c-cp);
243 databaseNames[no++][c-cp] = '\0';
248 databaseNames[no] = NULL;
250 return databaseNames;
253 ZOOM_API(ZOOM_connection)
254 ZOOM_connection_new (const char *host, int portnum)
256 ZOOM_connection c = ZOOM_connection_create (0);
258 ZOOM_connection_connect (c, host, portnum);
263 ZOOM_connection_connect(ZOOM_connection c,
264 const char *host, int portnum)
271 yaz_log (LOG_DEBUG, "reconnect");
275 yaz_log(LOG_DEBUG, "connect");
277 val = ZOOM_options_get (c->options, "proxy");
279 c->proxy = xstrdup (val);
284 val = ZOOM_options_get (c->options, "charset");
286 c->charset = xstrdup (val);
291 val = ZOOM_options_get (c->options, "lang");
293 c->lang = xstrdup (val);
297 xfree (c->host_port);
301 sprintf (hostn, "%.80s:%d", host, portnum);
302 c->host_port = xstrdup(hostn);
305 c->host_port = xstrdup(host);
307 ZOOM_options_set(c->options, "host", c->host_port);
309 val = ZOOM_options_get (c->options, "cookie");
311 c->cookie_out = xstrdup (val);
313 val = ZOOM_options_get (c->options, "clientIP");
315 c->client_IP = xstrdup (val);
317 c->async = ZOOM_options_get_bool (c->options, "async", 0);
319 c->error = ZOOM_ERROR_NONE;
321 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
325 while (ZOOM_event (1, &c))
331 ZOOM_query_create(void)
333 ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
338 s->odr = odr_createmem (ODR_ENCODE);
344 ZOOM_query_destroy(ZOOM_query s)
350 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
351 if (s->refcount == 0)
353 odr_destroy (s->odr);
359 ZOOM_query_prefix(ZOOM_query s, const char *str)
361 s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query));
362 s->query->which = Z_Query_type_1;
363 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
364 if (!s->query->u.type_1)
370 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
372 s->sort_spec = yaz_sort_spec (s->odr, criteria);
378 static int do_write(ZOOM_connection c);
381 ZOOM_connection_destroy(ZOOM_connection c)
388 for (r = c->resultsets; r; r = r->next)
393 odr_destroy (c->odr_in);
394 odr_destroy (c->odr_out);
395 ZOOM_options_destroy (c->options);
396 ZOOM_connection_remove_tasks (c);
397 xfree (c->host_port);
401 xfree (c->cookie_out);
402 xfree (c->cookie_in);
403 xfree (c->client_IP);
407 void ZOOM_resultset_addref (ZOOM_resultset r)
412 ZOOM_resultset ZOOM_resultset_create ()
414 ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
418 r->odr = odr_createmem (ODR_ENCODE);
432 ZOOM_API(ZOOM_resultset)
433 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
436 ZOOM_query s = ZOOM_query_create();
438 ZOOM_query_prefix (s, q);
440 r = ZOOM_connection_search (c, s);
441 ZOOM_query_destroy (s);
445 ZOOM_API(ZOOM_resultset)
446 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
448 ZOOM_resultset r = ZOOM_resultset_create ();
452 r->r_sort_spec = q->sort_spec;
453 r->r_query = q->query;
456 r->options = ZOOM_options_create_with_parent(c->options);
458 r->start = ZOOM_options_get_int(r->options, "start", 0);
459 r->count = ZOOM_options_get_int(r->options, "count", 0);
460 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
461 cp = ZOOM_options_get (r->options, "setname");
463 r->setname = xstrdup (cp);
467 r->next = c->resultsets;
470 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
471 task->u.search.resultset = r;
472 ZOOM_resultset_addref (r);
478 while (ZOOM_event (1, &c))
485 ZOOM_resultset_destroy(ZOOM_resultset r)
490 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
491 if (r->refcount == 0)
493 ZOOM_record_cache rc;
495 for (rc = r->record_cache; rc; rc = rc->next)
496 if (rc->rec.wrbuf_marc)
497 wrbuf_free (rc->rec.wrbuf_marc, 1);
500 /* remove ourselves from the resultsets in connection */
501 ZOOM_resultset *rp = &r->connection->resultsets;
504 assert (*rp); /* we must be in this list!! */
506 { /* OK, we're here - take us out of it */
513 ZOOM_query_destroy (r->search);
514 ZOOM_options_destroy (r->options);
515 odr_destroy (r->odr);
522 ZOOM_resultset_size (ZOOM_resultset r)
527 static void do_close (ZOOM_connection c)
533 c->state = STATE_IDLE;
536 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
537 int force_sync, int start, int count)
547 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
548 task->u.retrieve.resultset = r;
549 task->u.retrieve.start = start;
550 task->u.retrieve.count = count;
552 ZOOM_resultset_addref (r);
554 if (!r->connection->async || force_sync)
555 while (r->connection && ZOOM_event (1, &r->connection))
560 ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
561 size_t start, size_t count)
563 int force_present = 0;
569 ZOOM_resultset_retrieve (r, force_present, start, count);
573 for (i = 0; i< count; i++)
574 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
578 static int do_connect (ZOOM_connection c)
581 const char *effective_host;
584 effective_host = c->proxy;
586 effective_host = c->host_port;
588 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
591 c->cs = cs_create_host (effective_host, 0, &add);
595 int ret = cs_connect (c->cs, add);
598 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
599 ZOOM_connection_put_event(c, event);
600 ZOOM_connection_send_init(c);
601 c->state = STATE_ESTABLISHED;
606 c->state = STATE_CONNECTING;
607 c->mask = ZOOM_SELECT_EXCEPT;
608 if (c->cs->io_pending & CS_WANT_WRITE)
609 c->mask += ZOOM_SELECT_WRITE;
610 if (c->cs->io_pending & CS_WANT_READ)
611 c->mask += ZOOM_SELECT_READ;
615 c->state = STATE_IDLE;
616 c->error = ZOOM_ERROR_CONNECT;
620 int z3950_connection_socket(ZOOM_connection c)
623 return cs_fileno(c->cs);
627 int z3950_connection_mask(ZOOM_connection c)
634 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
639 Z_OtherInformation **oi;
641 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
645 Z_OtherInformation **oi;
647 yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
649 if (!z_APDU(out, &a, 0, 0))
651 FILE *outf = fopen("/tmp/apdu.txt", "a");
654 ODR odr_pr = odr_createmem(ODR_PRINT);
655 fprintf (outf, "a=%p\n", a);
656 odr_setprint(odr_pr, outf);
657 z_APDU(odr_pr, &a, 0, 0);
660 c->error = ZOOM_ERROR_ENCODE;
668 static int send_APDU (ZOOM_connection c, Z_APDU *a)
672 if (encode_APDU(c, a, c->odr_out))
674 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
675 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
676 ZOOM_connection_put_event (c, event);
677 odr_reset(c->odr_out);
682 static int ZOOM_connection_send_init (ZOOM_connection c)
685 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
686 Z_InitRequest *ireq = apdu->u.initRequest;
687 Z_IdAuthentication *auth = (Z_IdAuthentication *)
688 odr_malloc(c->odr_out, sizeof(*auth));
689 const char *auth_groupId = ZOOM_options_get (c->options, "group");
690 const char *auth_userId = ZOOM_options_get (c->options, "user");
691 const char *auth_password = ZOOM_options_get (c->options, "pass");
693 ODR_MASK_SET(ireq->options, Z_Options_search);
694 ODR_MASK_SET(ireq->options, Z_Options_present);
695 ODR_MASK_SET(ireq->options, Z_Options_scan);
696 ODR_MASK_SET(ireq->options, Z_Options_sort);
697 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
698 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
700 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
701 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
702 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
704 impname = ZOOM_options_get (c->options, "implementationName");
705 ireq->implementationName =
706 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
707 strcpy (ireq->implementationName, "");
710 strcat (ireq->implementationName, impname);
711 strcat (ireq->implementationName, "/");
713 strcat (ireq->implementationName, "ZOOM-C/YAZ");
715 *ireq->maximumRecordSize =
716 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
717 *ireq->preferredMessageSize =
718 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
720 if (auth_groupId || auth_password)
722 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
725 if (auth_groupId && *auth_groupId)
727 pass->groupId = (char *)
728 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
729 strcpy(pass->groupId, auth_groupId);
733 if (auth_userId && *auth_userId)
735 pass->userId = (char *)
736 odr_malloc(c->odr_out, strlen(auth_userId)+1);
737 strcpy(pass->userId, auth_userId);
741 if (auth_password && *auth_password)
743 pass->password = (char *)
744 odr_malloc(c->odr_out, strlen(auth_password)+1);
745 strcpy(pass->password, auth_password);
750 auth->which = Z_IdAuthentication_idPass;
751 auth->u.idPass = pass;
752 ireq->idAuthentication = auth;
755 else if (auth_userId)
757 auth->which = Z_IdAuthentication_open;
758 auth->u.open = (char *)
759 odr_malloc(c->odr_out, strlen(auth_userId)+1);
760 strcpy(auth->u.open, auth_userId);
761 ireq->idAuthentication = auth;
764 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
765 VAL_PROXY, 1, c->host_port);
766 if (c->charset||c->lang)
768 Z_OtherInformation **oi;
769 Z_OtherInformationUnit *oi_unit;
771 yaz_oi_APDU(apdu, &oi);
773 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
775 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
777 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
778 oi_unit->information.externallyDefinedInfo =
779 yaz_set_proposal_charneg(c->odr_out,
780 (const char **)&c->charset, (c->charset) ? 1:0,
781 (const char **)&c->lang, (c->lang) ? 1:0, 1);
790 static int ZOOM_connection_send_search (ZOOM_connection c)
793 int lslb, ssub, mspn;
795 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
796 Z_SearchRequest *search_req = apdu->u.searchRequest;
797 const char *elementSetName;
798 const char *smallSetElementSetName;
799 const char *mediumSetElementSetName;
803 assert (c->tasks->which == ZOOM_TASK_SEARCH);
805 r = c->tasks->u.search.resultset;
808 ZOOM_options_get (r->options, "elementSetName");
809 smallSetElementSetName =
810 ZOOM_options_get (r->options, "smallSetElementSetName");
811 mediumSetElementSetName =
812 ZOOM_options_get (r->options, "mediumSetElementSetName");
814 ZOOM_options_get (r->options, "schema");
816 if (!smallSetElementSetName)
817 smallSetElementSetName = elementSetName;
819 if (!mediumSetElementSetName)
820 mediumSetElementSetName = elementSetName;
825 /* prepare query for the search request */
826 search_req->query = r->r_query;
828 search_req->databaseNames =
829 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
831 /* get syntax (no need to provide unless piggyback is in effect) */
832 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
834 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
835 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
836 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
837 if (lslb != -1 && ssub != -1 && mspn != -1)
839 /* So're a Z39.50 expert? Let's hope you don't do sort */
840 *search_req->largeSetLowerBound = lslb;
841 *search_req->smallSetUpperBound = ssub;
842 *search_req->mediumSetPresentNumber = mspn;
844 else if (r->start == 0 && r->count > 0
845 && r->piggyback && !r->r_sort_spec && !schema)
847 /* Regular piggyback - do it unless we're going to do sort */
848 *search_req->largeSetLowerBound = 2000000000;
849 *search_req->smallSetUpperBound = r->count;
850 *search_req->mediumSetPresentNumber = r->count;
851 smallSetElementSetName = 0; /* no need to provide this */
855 /* non-piggyback. Need not provide elementsets or syntaxes .. */
856 smallSetElementSetName = 0;
857 mediumSetElementSetName = 0;
860 if (smallSetElementSetName && *smallSetElementSetName)
862 Z_ElementSetNames *esn = (Z_ElementSetNames *)
863 odr_malloc (c->odr_out, sizeof(*esn));
865 esn->which = Z_ElementSetNames_generic;
866 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
867 search_req->smallSetElementSetNames = esn;
869 if (mediumSetElementSetName && *mediumSetElementSetName)
871 Z_ElementSetNames *esn = (Z_ElementSetNames *)
872 odr_malloc (c->odr_out, sizeof(*esn));
874 esn->which = Z_ElementSetNames_generic;
875 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
876 search_req->mediumSetElementSetNames = esn;
879 search_req->preferredRecordSyntax =
880 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
884 if (c->support_named_resultsets)
888 /* find the lowest unused ordinal so that we re-use
889 result sets on the server. */
890 for (ord = 1; ; ord++)
893 sprintf (setname, "%d", ord);
894 for (rp = c->resultsets; rp; rp = rp->next)
895 if (rp->setname && !strcmp (rp->setname, setname))
900 r->setname = xstrdup (setname);
901 yaz_log (LOG_DEBUG, "allocating %s", r->setname);
904 r->setname = xstrdup ("default");
905 ZOOM_options_set (r->options, "setname", r->setname);
907 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
908 /* send search request */
913 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
915 Z_DefaultDiagFormat *r;
920 if (p->which != Z_DiagRec_defaultFormat)
922 c->error = ZOOM_ERROR_DECODE;
925 r = p->u.defaultFormat;
928 case Z_DefaultDiagFormat_v2Addinfo:
929 addinfo = r->u.v2Addinfo;
931 case Z_DefaultDiagFormat_v3Addinfo:
932 addinfo = r->u.v3Addinfo;
936 c->addinfo = xstrdup (addinfo);
937 c->error = *r->condition;
940 ZOOM_API(ZOOM_record)
941 ZOOM_record_clone (ZOOM_record srec)
948 odr_enc = odr_createmem(ODR_ENCODE);
949 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
951 buf = odr_getbuf (odr_enc, &size, 0);
953 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
954 nrec->odr = odr_createmem(ODR_DECODE);
955 nrec->wrbuf_marc = 0;
956 odr_setbuf (nrec->odr, buf, size, 0);
957 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
959 odr_destroy (odr_enc);
963 ZOOM_API(ZOOM_record)
964 ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
966 return record_cache_lookup (s, pos, 0);
969 ZOOM_API(ZOOM_record)
970 ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
972 ZOOM_resultset_retrieve (r, 1, pos, 1);
973 return ZOOM_resultset_record_immediate (r, pos);
977 ZOOM_record_destroy (ZOOM_record rec)
982 wrbuf_free (rec->wrbuf_marc, 1);
983 odr_destroy (rec->odr);
987 ZOOM_API(const char *)
988 ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
990 Z_NamePlusRecord *npr;
993 *len = 0; /* default return */
1000 if (!strcmp (type, "database"))
1003 *len = strlen(npr->databaseName);
1004 return npr->databaseName;
1006 else if (!strcmp (type, "syntax"))
1008 if (npr->which == Z_NamePlusRecord_databaseRecord)
1010 Z_External *r = (Z_External *) npr->u.databaseRecord;
1011 oident *ent = oid_getentbyoid(r->direct_reference);
1015 *len = strlen(ent->desc);
1021 else if (!strcmp (type, "render") &&
1022 npr->which == Z_NamePlusRecord_databaseRecord)
1024 Z_External *r = (Z_External *) npr->u.databaseRecord;
1025 oident *ent = oid_getentbyoid(r->direct_reference);
1027 if (r->which == Z_External_sutrs)
1029 if (len) *len = r->u.sutrs->len;
1030 return (const char *) r->u.sutrs->buf;
1032 else if (r->which == Z_External_octet)
1041 case VAL_APPLICATION_XML:
1044 if (!rec->wrbuf_marc)
1045 rec->wrbuf_marc = wrbuf_alloc();
1046 wrbuf_rewind (rec->wrbuf_marc);
1047 if (yaz_marc_decode ((const char *)
1048 r->u.octet_aligned->buf,
1050 r->u.octet_aligned->len,
1053 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1054 return wrbuf_buf(rec->wrbuf_marc);
1057 if (len) *len = r->u.octet_aligned->len;
1058 return (const char *) r->u.octet_aligned->buf;
1060 else if (r->which == Z_External_grs1)
1067 else if (!strcmp (type, "xml") &&
1068 npr->which == Z_NamePlusRecord_databaseRecord)
1070 Z_External *r = (Z_External *) npr->u.databaseRecord;
1071 oident *ent = oid_getentbyoid(r->direct_reference);
1073 if (r->which == Z_External_sutrs)
1075 if (len) *len = r->u.sutrs->len;
1076 return (const char *) r->u.sutrs->buf;
1078 else if (r->which == Z_External_octet)
1087 case VAL_APPLICATION_XML:
1090 if (!rec->wrbuf_marc)
1091 rec->wrbuf_marc = wrbuf_alloc();
1092 wrbuf_rewind (rec->wrbuf_marc);
1093 if (yaz_marc_decode ((const char *)
1094 r->u.octet_aligned->buf,
1096 r->u.octet_aligned->len,
1099 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1100 return wrbuf_buf(rec->wrbuf_marc);
1103 if (len) *len = r->u.octet_aligned->len;
1104 return (const char *) r->u.octet_aligned->buf;
1106 else if (r->which == Z_External_grs1)
1113 else if (!strcmp (type, "raw"))
1115 if (npr->which == Z_NamePlusRecord_databaseRecord)
1116 return (const char *) npr->u.databaseRecord;
1122 static void record_cache_add (ZOOM_resultset r,
1123 Z_NamePlusRecord *npr,
1125 const char *elementSetName)
1127 ZOOM_record_cache rc;
1129 for (rc = r->record_cache; rc; rc = rc->next)
1133 if ((!elementSetName && !rc->elementSetName)
1134 || (elementSetName && rc->elementSetName &&
1135 !strcmp (elementSetName, rc->elementSetName)))
1137 /* not destroying rc->npr (it's handled by nmem )*/
1139 /* keeping wrbuf_marc too */
1144 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1147 rc->rec.wrbuf_marc = 0;
1149 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1151 rc->elementSetName = 0;
1153 rc->next = r->record_cache;
1154 r->record_cache = rc;
1157 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
1159 const char *elementSetName)
1161 ZOOM_record_cache rc;
1163 for (rc = r->record_cache; rc; rc = rc->next)
1167 if ((!elementSetName && !rc->elementSetName)
1168 || (elementSetName && rc->elementSetName &&
1169 !strcmp (elementSetName, rc->elementSetName)))
1176 static void handle_records (ZOOM_connection c, Z_Records *sr,
1179 ZOOM_resultset resultset;
1183 switch (c->tasks->which)
1185 case ZOOM_TASK_SEARCH:
1186 resultset = c->tasks->u.search.resultset;
1188 case ZOOM_TASK_RETRIEVE:
1189 resultset = c->tasks->u.retrieve.resultset;
1194 if (sr && sr->which == Z_Records_NSD)
1196 Z_DiagRec dr, *dr_p = &dr;
1197 dr.which = Z_DiagRec_defaultFormat;
1198 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1200 response_diag (c, dr_p);
1202 else if (sr && sr->which == Z_Records_multipleNSD)
1204 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1205 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1207 c->error = ZOOM_ERROR_DECODE;
1211 if (resultset->count + resultset->start > resultset->size)
1212 resultset->count = resultset->size - resultset->start;
1213 if (resultset->count < 0)
1214 resultset->count = 0;
1215 if (sr && sr->which == Z_Records_DBOSD)
1218 NMEM nmem = odr_extract_mem (c->odr_in);
1219 Z_NamePlusRecordList *p =
1220 sr->u.databaseOrSurDiagnostics;
1221 for (i = 0; i<p->num_records; i++)
1223 record_cache_add (resultset, p->records[i],
1224 i+ resultset->start, 0);
1226 /* transfer our response to search_nmem .. we need it later */
1227 nmem_transfer (resultset->odr->mem, nmem);
1228 nmem_destroy (nmem);
1229 if (present_phase && p->num_records == 0)
1231 /* present response and we didn't get any records! */
1232 c->error = ZOOM_ERROR_DECODE;
1235 else if (present_phase)
1237 /* present response and we didn't get any records! */
1238 c->error = ZOOM_ERROR_DECODE;
1243 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1245 handle_records (c, pr->records, 1);
1248 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1250 ZOOM_resultset resultset;
1252 yaz_log (LOG_DEBUG, "got search response");
1254 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1257 resultset = c->tasks->u.search.resultset;
1259 resultset->size = *sr->resultCount;
1260 handle_records (c, sr->records, 0);
1263 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1265 if (res->diagnostics && res->num_diagnostics > 0)
1266 response_diag (c, res->diagnostics[0]);
1269 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1271 NMEM nmem = odr_extract_mem (c->odr_in);
1274 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1276 scan = c->tasks->u.scan.scan;
1278 if (res->entries && res->entries->nonsurrogateDiagnostics)
1279 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1280 scan->scan_response = res;
1281 nmem_transfer (scan->odr->mem, nmem);
1283 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1284 if (res->positionOfTerm)
1285 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1286 if (res->scanStatus)
1287 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1288 if (res->numberOfEntriesReturned)
1289 ZOOM_options_set_int (scan->options, "number",
1290 *res->numberOfEntriesReturned);
1291 nmem_destroy (nmem);
1295 static int send_sort (ZOOM_connection c)
1297 ZOOM_resultset resultset;
1299 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1302 resultset = c->tasks->u.search.resultset;
1306 resultset->r_sort_spec = 0;
1309 if (resultset->r_sort_spec)
1311 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1312 Z_SortRequest *req = apdu->u.sortRequest;
1314 req->num_inputResultSetNames = 1;
1315 req->inputResultSetNames = (Z_InternationalString **)
1316 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1317 req->inputResultSetNames[0] =
1318 odr_strdup (c->odr_out, resultset->setname);
1319 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1320 req->sortSequence = resultset->r_sort_spec;
1321 resultset->r_sort_spec = 0;
1322 send_APDU (c, apdu);
1328 static int send_present (ZOOM_connection c)
1330 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1331 Z_PresentRequest *req = apdu->u.presentRequest;
1333 const char *syntax =
1334 ZOOM_options_get (c->options, "preferredRecordSyntax");
1335 const char *element =
1336 ZOOM_options_get (c->options, "elementSetName");
1337 const char *schema =
1338 ZOOM_options_get (c->options, "schema");
1339 ZOOM_resultset resultset;
1344 switch (c->tasks->which)
1346 case ZOOM_TASK_SEARCH:
1347 resultset = c->tasks->u.search.resultset;
1349 case ZOOM_TASK_RETRIEVE:
1350 resultset = c->tasks->u.retrieve.resultset;
1351 resultset->start = c->tasks->u.retrieve.start;
1352 resultset->count = c->tasks->u.retrieve.count;
1354 if (resultset->start >= resultset->size)
1356 if (resultset->start + resultset->count > resultset->size)
1357 resultset->count = resultset->size - resultset->start;
1363 if (c->error) /* don't continue on error */
1365 if (resultset->start < 0)
1367 for (i = 0; i<resultset->count; i++)
1370 record_cache_lookup (resultset, i + resultset->start, 0);
1374 if (i == resultset->count)
1377 resultset->start += i;
1378 resultset->count -= i;
1379 *req->resultSetStartPoint = resultset->start + 1;
1380 *req->numberOfRecordsRequested = resultset->count;
1381 assert (*req->numberOfRecordsRequested > 0);
1383 if (syntax && *syntax)
1384 req->preferredRecordSyntax =
1385 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1387 if (schema && *schema)
1389 Z_RecordComposition *compo = (Z_RecordComposition *)
1390 odr_malloc (c->odr_out, sizeof(*compo));
1392 req->recordComposition = compo;
1393 compo->which = Z_RecordComp_complex;
1394 compo->u.complex = (Z_CompSpec *)
1395 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1396 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1397 odr_malloc(c->odr_out, sizeof(bool_t));
1398 *compo->u.complex->selectAlternativeSyntax = 0;
1400 compo->u.complex->generic = (Z_Specification *)
1401 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1403 compo->u.complex->generic->schema = (Odr_oid *)
1404 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1406 if (!compo->u.complex->generic->schema)
1408 /* OID wasn't a schema! Try record syntax instead. */
1410 compo->u.complex->generic->schema = (Odr_oid *)
1411 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1413 if (element && *element)
1415 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1416 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1417 compo->u.complex->generic->elementSpec->which =
1418 Z_ElementSpec_elementSetName;
1419 compo->u.complex->generic->elementSpec->u.elementSetName =
1420 odr_strdup (c->odr_out, element);
1423 compo->u.complex->generic->elementSpec = 0;
1424 compo->u.complex->num_dbSpecific = 0;
1425 compo->u.complex->dbSpecific = 0;
1426 compo->u.complex->num_recordSyntax = 0;
1427 compo->u.complex->recordSyntax = 0;
1429 else if (element && *element)
1431 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1432 odr_malloc (c->odr_out, sizeof(*esn));
1433 Z_RecordComposition *compo = (Z_RecordComposition *)
1434 odr_malloc (c->odr_out, sizeof(*compo));
1436 esn->which = Z_ElementSetNames_generic;
1437 esn->u.generic = odr_strdup (c->odr_out, element);
1438 compo->which = Z_RecordComp_simple;
1439 compo->u.simple = esn;
1440 req->recordComposition = compo;
1442 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1443 send_APDU (c, apdu);
1447 ZOOM_API(ZOOM_scanset)
1448 ZOOM_connection_scan (ZOOM_connection c, const char *start)
1450 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1452 scan->connection = c;
1453 scan->odr = odr_createmem (ODR_DECODE);
1454 scan->options = ZOOM_options_create_with_parent (c->options);
1456 scan->scan_response = 0;
1458 if ((scan->termListAndStartPoint =
1459 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1462 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1463 task->u.scan.scan = scan;
1468 while (ZOOM_event (1, &c))
1476 ZOOM_scanset_destroy (ZOOM_scanset scan)
1481 if (scan->refcount == 0)
1483 odr_destroy (scan->odr);
1485 ZOOM_options_destroy (scan->options);
1490 static int send_package (ZOOM_connection c)
1495 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1497 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
1498 ZOOM_connection_put_event (c, event);
1500 do_write_ex (c, c->tasks->u.package->buf_out,
1501 c->tasks->u.package->len_out);
1505 static int send_scan (ZOOM_connection c)
1508 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1509 Z_ScanRequest *req = apdu->u.scanRequest;
1512 assert (c->tasks->which == ZOOM_TASK_SCAN);
1513 scan = c->tasks->u.scan.scan;
1515 req->termListAndStartPoint = scan->termListAndStartPoint;
1516 req->attributeSet = scan->attributeSet;
1518 *req->numberOfTermsRequested =
1519 ZOOM_options_get_int(scan->options, "number", 10);
1521 req->preferredPositionInResponse =
1522 odr_intdup (c->odr_out,
1523 ZOOM_options_get_int(scan->options, "position", 1));
1526 odr_intdup (c->odr_out,
1527 ZOOM_options_get_int(scan->options, "stepSize", 0));
1529 req->databaseNames = set_DatabaseNames (c, scan->options,
1530 &req->num_databaseNames);
1532 send_APDU (c, apdu);
1538 ZOOM_scanset_size (ZOOM_scanset scan)
1540 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1542 return scan->scan_response->entries->num_entries;
1545 ZOOM_API(const char *)
1546 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1549 const char *term = 0;
1550 size_t noent = ZOOM_scanset_size (scan);
1551 Z_ScanResponse *res = scan->scan_response;
1557 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1559 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1561 if (t->term->which == Z_Term_general)
1563 term = (const char *) t->term->u.general->buf;
1564 *len = t->term->u.general->len;
1566 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1571 ZOOM_API(const char *)
1572 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1574 return ZOOM_options_get (scan->options, key);
1578 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1581 ZOOM_options_set (scan->options, key, val);
1586 static Z_APDU *create_es_package (ZOOM_package p, int type)
1589 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
1590 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1592 *req->function = Z_ExtendedServicesRequest_create;
1594 str = ZOOM_options_get(p->options, "package-name");
1596 req->packageName = nmem_strdup (p->odr_out->mem, str);
1598 str = ZOOM_options_get(p->options, "user-id");
1600 req->userId = nmem_strdup (p->odr_out->mem, str);
1602 req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1605 str = ZOOM_options_get(p->options, "function");
1608 if (!strcmp (str, "create"))
1610 if (!strcmp (str, "delete"))
1612 if (!strcmp (str, "modify"))
1618 static const char *ill_array_lookup (void *clientData, const char *idx)
1620 ZOOM_package p = (ZOOM_package) clientData;
1621 return ZOOM_options_get (p->options, idx+4);
1624 static Z_External *encode_ill_request (ZOOM_package p)
1626 ODR out = p->odr_out;
1629 struct ill_get_ctl ctl;
1631 ctl.odr = p->odr_out;
1633 ctl.f = ill_array_lookup;
1635 req = ill_get_ILLRequest(&ctl, "ill", 0);
1637 if (!ill_Request (out, &req, 0, 0))
1639 int ill_request_size;
1640 char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1641 if (ill_request_buf)
1642 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1648 int illRequest_size = 0;
1649 char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0);
1651 oid.proto = PROTO_GENERAL;
1652 oid.oclass = CLASS_GENERAL;
1653 oid.value = VAL_ISO_ILL_1;
1655 r = (Z_External *) odr_malloc (out, sizeof(*r));
1656 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1657 r->indirect_reference = 0;
1659 r->which = Z_External_single;
1661 r->u.single_ASN1_type = (Odr_oct *)
1662 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1663 r->u.single_ASN1_type->buf = odr_malloc (out, illRequest_size);
1664 r->u.single_ASN1_type->len = illRequest_size;
1665 r->u.single_ASN1_type->size = illRequest_size;
1666 memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size);
1671 static Z_ItemOrder *encode_item_order(ZOOM_package p)
1673 Z_ItemOrder *req = odr_malloc (p->odr_out, sizeof(*req));
1676 req->which=Z_IOItemOrder_esRequest;
1677 req->u.esRequest = (Z_IORequest *)
1678 odr_malloc(p->odr_out,sizeof(Z_IORequest));
1680 /* to keep part ... */
1681 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1682 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
1683 req->u.esRequest->toKeep->supplDescription = 0;
1684 req->u.esRequest->toKeep->contact =
1685 odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
1687 str = ZOOM_options_get(p->options, "contact-name");
1688 req->u.esRequest->toKeep->contact->name = str ?
1689 nmem_strdup (p->odr_out->mem, str) : 0;
1691 str = ZOOM_options_get(p->options, "contact-phone");
1692 req->u.esRequest->toKeep->contact->phone = str ?
1693 nmem_strdup (p->odr_out->mem, str) : 0;
1695 str = ZOOM_options_get(p->options, "contact-email");
1696 req->u.esRequest->toKeep->contact->email = str ?
1697 nmem_strdup (p->odr_out->mem, str) : 0;
1699 req->u.esRequest->toKeep->addlBilling = 0;
1701 /* not to keep part ... */
1702 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1703 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
1705 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
1706 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
1708 str = ZOOM_options_get(p->options, "itemorder-setname");
1711 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
1712 nmem_strdup (p->odr_out->mem, str);
1713 req->u.esRequest->notToKeep->resultSetItem->item =
1714 (int *) odr_malloc(p->odr_out, sizeof(int));
1716 str = ZOOM_options_get(p->options, "itemorder-item");
1717 *req->u.esRequest->notToKeep->resultSetItem->item =
1718 (str ? atoi(str) : 1);
1720 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
1726 ZOOM_package_send (ZOOM_package p, const char *type)
1733 odr_reset (p->odr_out);
1736 if (!strcmp(type, "itemorder"))
1739 apdu = create_es_package (p, VAL_ITEMORDER);
1742 r = odr_malloc (p->odr_out, sizeof(*r));
1744 r->direct_reference =
1745 yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1748 r->which = Z_External_itemOrder;
1749 r->indirect_reference = 0;
1750 r->u.itemOrder = encode_item_order (p);
1752 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
1757 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
1761 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE);
1762 task->u.package = p;
1763 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
1764 p->buf_out = xmalloc (p->len_out);
1765 memcpy (p->buf_out, buf, p->len_out);
1770 while (ZOOM_event (1, &c))
1777 ZOOM_API(ZOOM_package)
1778 ZOOM_connection_package (ZOOM_connection c, ZOOM_options options)
1780 ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p));
1783 p->odr_out = odr_createmem (ODR_ENCODE);
1784 p->options = ZOOM_options_create_with_parent2 (options, c->options);
1792 ZOOM_package_destroy(ZOOM_package p)
1797 if (p->refcount == 0)
1799 odr_destroy (p->odr_out);
1802 ZOOM_options_destroy (p->options);
1807 ZOOM_API(const char *)
1808 ZOOM_package_option_get (ZOOM_package p, const char *key)
1810 return ZOOM_options_get (p->options, key);
1814 ZOOM_package_option_set (ZOOM_package p, const char *key,
1817 ZOOM_options_set (p->options, key, val);
1820 static int ZOOM_connection_exec_task (ZOOM_connection c)
1822 ZOOM_task task = c->tasks;
1824 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1827 if (c->error != ZOOM_ERROR_NONE ||
1828 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1830 ZOOM_connection_remove_tasks (c);
1833 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
1834 task->which, task->running);
1838 switch (task->which)
1840 case ZOOM_TASK_SEARCH:
1841 /* see if search hasn't been sent yet. */
1842 if (ZOOM_connection_send_search (c))
1845 case ZOOM_TASK_RETRIEVE:
1846 if (send_present (c))
1849 case ZOOM_TASK_CONNECT:
1853 case ZOOM_TASK_SCAN:
1857 case ZOOM_TASK_PACKAGE:
1858 if (send_package(c))
1861 ZOOM_connection_remove_task (c);
1865 static int send_sort_present (ZOOM_connection c)
1867 int r = send_sort (c);
1869 r = send_present (c);
1873 static int es_response (ZOOM_connection c,
1874 Z_ExtendedServicesResponse *res)
1876 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
1878 if (res->diagnostics && res->num_diagnostics > 0)
1879 response_diag(c, res->diagnostics[0]);
1880 if (res->taskPackage &&
1881 res->taskPackage->which == Z_External_extendedService)
1883 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1884 Odr_oct *id = taskPackage->targetReference;
1887 ZOOM_options_setl (c->tasks->u.package->options,
1888 "targetReference", id->buf, id->len);
1894 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1896 Z_InitResponse *initrs;
1899 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1902 case Z_APDU_initResponse:
1903 initrs = apdu->u.initResponse;
1904 if (!*initrs->result)
1906 c->error = ZOOM_ERROR_INIT;
1911 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1913 xfree (c->cookie_in);
1916 c->cookie_in = xstrdup(cookie);
1917 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1918 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1919 c->support_named_resultsets = 1;
1922 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1923 ZOOM_connection_remove_task (c);
1925 ZOOM_connection_exec_task (c);
1927 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
1929 NMEM tmpmem = nmem_create();
1930 Z_CharSetandLanguageNegotiation *p =
1931 yaz_get_charneg_record(initrs->otherInfo);
1935 char *charset=NULL, *lang=NULL;
1938 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &selected);
1939 yaz_log(LOG_DEBUG, "Target accepted: charset - %s,"
1940 "language - %s, select - %d",
1941 charset, lang, selected);
1943 nmem_destroy(tmpmem);
1947 case Z_APDU_searchResponse:
1948 handle_search_response (c, apdu->u.searchResponse);
1949 if (!send_sort_present (c))
1950 ZOOM_connection_remove_task (c);
1952 case Z_APDU_presentResponse:
1953 handle_present_response (c, apdu->u.presentResponse);
1954 if (!send_present (c))
1955 ZOOM_connection_remove_task (c);
1957 case Z_APDU_sortResponse:
1958 sort_response (c, apdu->u.sortResponse);
1959 if (!send_present (c))
1960 ZOOM_connection_remove_task (c);
1962 case Z_APDU_scanResponse:
1963 scan_response (c, apdu->u.scanResponse);
1964 ZOOM_connection_remove_task (c);
1966 case Z_APDU_extendedServicesResponse:
1967 es_response (c, apdu->u.extendedServicesResponse);
1968 ZOOM_connection_remove_task (c);
1971 if (c->reconnect_ok)
1974 c->tasks->running = 0;
1975 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
1979 c->error = ZOOM_ERROR_CONNECTION_LOST;
1984 c->error = ZOOM_ERROR_DECODE;
1989 static int do_read (ZOOM_connection c)
1995 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
1996 ZOOM_connection_put_event (c, event);
1998 yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in);
2000 r = cs_get (c->cs, &c->buf_in, &c->len_in);
2005 if (c->reconnect_ok)
2008 c->reconnect_ok = 0;
2009 yaz_log (LOG_DEBUG, "reconnect read");
2010 c->tasks->running = 0;
2011 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2015 c->error= ZOOM_ERROR_CONNECTION_LOST;
2022 odr_reset (c->odr_in);
2023 odr_setbuf (c->odr_in, c->buf_in, r, 0);
2024 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
2025 ZOOM_connection_put_event (c, event);
2026 if (!z_APDU (c->odr_in, &apdu, 0, 0))
2028 c->error = ZOOM_ERROR_DECODE;
2032 handle_apdu (c, apdu);
2033 c->reconnect_ok = 0;
2038 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
2043 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
2044 ZOOM_connection_put_event (c, event);
2046 yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
2047 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
2049 if (c->reconnect_ok)
2052 c->reconnect_ok = 0;
2053 yaz_log (LOG_DEBUG, "reconnect write");
2054 c->tasks->running = 0;
2055 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2058 if (c->state == STATE_CONNECTING)
2059 c->error = ZOOM_ERROR_CONNECT;
2061 c->error = ZOOM_ERROR_CONNECTION_LOST;
2067 c->mask = ZOOM_SELECT_EXCEPT;
2068 if (c->cs->io_pending & CS_WANT_WRITE)
2069 c->mask += ZOOM_SELECT_WRITE;
2070 if (c->cs->io_pending & CS_WANT_READ)
2071 c->mask += ZOOM_SELECT_READ;
2072 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
2076 // c->reconnect_ok = 0;
2077 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
2078 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
2083 static int do_write(ZOOM_connection c)
2085 return do_write_ex (c, c->buf_out, c->len_out);
2089 ZOOM_API(const char *)
2090 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
2092 return ZOOM_options_get (c->options, key);
2096 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
2099 ZOOM_options_set (c->options, key, val);
2102 ZOOM_API(const char *)
2103 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
2105 return ZOOM_options_get (r->options, key);
2109 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
2112 ZOOM_options_set (r->options, key, val);
2117 ZOOM_connection_errcode (ZOOM_connection c)
2119 return ZOOM_connection_error (c, 0, 0);
2122 ZOOM_API(const char *)
2123 ZOOM_connection_errmsg (ZOOM_connection c)
2126 ZOOM_connection_error (c, &msg, 0);
2130 ZOOM_API(const char *)
2131 ZOOM_connection_addinfo (ZOOM_connection c)
2133 const char *addinfo;
2134 ZOOM_connection_error (c, 0, &addinfo);
2139 ZOOM_connection_error (ZOOM_connection c, const char **cp,
2140 const char **addinfo)
2142 int error = c->error;
2147 case ZOOM_ERROR_NONE:
2148 *cp = "No error"; break;
2149 case ZOOM_ERROR_CONNECT:
2150 *cp = "Connect failed"; break;
2151 case ZOOM_ERROR_MEMORY:
2152 *cp = "Out of memory"; break;
2153 case ZOOM_ERROR_ENCODE:
2154 *cp = "Encoding failed"; break;
2155 case ZOOM_ERROR_DECODE:
2156 *cp = "Decoding failed"; break;
2157 case ZOOM_ERROR_CONNECTION_LOST:
2158 *cp = "Connection lost"; break;
2159 case ZOOM_ERROR_INIT:
2160 *cp = "Init rejected"; break;
2161 case ZOOM_ERROR_INTERNAL:
2162 *cp = "Internal failure"; break;
2163 case ZOOM_ERROR_TIMEOUT:
2164 *cp = "Timeout"; break;
2166 *cp = diagbib1_str (error);
2172 *addinfo = c->addinfo;
2179 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
2181 ZOOM_Event event = 0;
2182 int r = cs_look(c->cs);
2183 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
2188 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2189 c->error = ZOOM_ERROR_CONNECT;
2191 ZOOM_connection_put_event (c, event);
2193 else if (r == CS_CONNECT)
2196 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2198 ret = cs_rcvconnect (c->cs);
2199 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
2202 c->mask = ZOOM_SELECT_EXCEPT;
2203 if (c->cs->io_pending & CS_WANT_WRITE)
2204 c->mask += ZOOM_SELECT_WRITE;
2205 if (c->cs->io_pending & CS_WANT_READ)
2206 c->mask += ZOOM_SELECT_READ;
2207 ZOOM_connection_put_event (c, event);
2211 ZOOM_connection_put_event (c, event);
2212 ZOOM_connection_send_init (c);
2213 c->state = STATE_ESTABLISHED;
2217 c->error = ZOOM_ERROR_CONNECT;
2219 ZOOM_connection_put_event (c, event);
2224 if (mask & ZOOM_SELECT_READ)
2226 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2233 ZOOM_connection_last_event(ZOOM_connection cs)
2236 return ZOOM_EVENT_NONE;
2237 return cs->last_event;
2241 ZOOM_event (int no, ZOOM_connection *cs)
2244 struct pollfd pollfds[1024];
2245 ZOOM_connection poll_cs[1024];
2248 fd_set input, output, except;
2253 for (i = 0; i<no; i++)
2255 ZOOM_connection c = cs[i];
2257 if (c && (event = ZOOM_connection_get_event(c)))
2259 ZOOM_Event_destroy (event);
2263 for (i = 0; i<no; i++)
2265 ZOOM_connection c = cs[i];
2267 if (c && ZOOM_connection_exec_task (c))
2269 if ((event = ZOOM_connection_get_event(c)))
2271 ZOOM_Event_destroy (event);
2287 for (i = 0; i<no; i++)
2289 ZOOM_connection c = cs[i];
2294 fd = z3950_connection_socket(c);
2295 mask = z3950_connection_mask(c);
2305 short poll_events = 0;
2307 if (mask & ZOOM_SELECT_READ)
2308 poll_events += POLLIN;
2309 if (mask & ZOOM_SELECT_WRITE)
2310 poll_events += POLLOUT;
2311 if (mask & ZOOM_SELECT_EXCEPT)
2312 poll_events += POLLERR;
2313 pollfds[nfds].fd = fd;
2314 pollfds[nfds].events = poll_events;
2315 pollfds[nfds].revents = 0;
2320 if (mask & ZOOM_SELECT_READ)
2322 FD_SET (fd, &input);
2325 if (mask & ZOOM_SELECT_WRITE)
2327 FD_SET (fd, &output);
2330 if (mask & ZOOM_SELECT_EXCEPT)
2332 FD_SET (fd, &except);
2340 r = poll (pollfds, nfds, 15000);
2341 for (i = 0; i<nfds; i++)
2343 ZOOM_connection c = poll_cs[i];
2347 if (pollfds[i].revents & POLLIN)
2348 mask += ZOOM_SELECT_READ;
2349 if (pollfds[i].revents & POLLOUT)
2350 mask += ZOOM_SELECT_WRITE;
2351 if (pollfds[i].revents & POLLERR)
2352 mask += ZOOM_SELECT_EXCEPT;
2354 ZOOM_connection_do_io(c, mask);
2356 else if (r == 0 && c->mask)
2358 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2359 /* timeout and this connection was waiting */
2360 c->error = ZOOM_ERROR_TIMEOUT;
2362 ZOOM_connection_put_event(c, event);
2366 yaz_log (LOG_DEBUG, "select start");
2367 r = select (max_fd+1, &input, &output, &except, &tv);
2368 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
2369 for (i = 0; i<no; i++)
2371 ZOOM_connection c = cs[i];
2376 fd = z3950_connection_socket(c);
2380 /* no timeout and real socket */
2381 if (FD_ISSET(fd, &input))
2382 mask += ZOOM_SELECT_READ;
2383 if (FD_ISSET(fd, &output))
2384 mask += ZOOM_SELECT_WRITE;
2385 if (FD_ISSET(fd, &except))
2386 mask += ZOOM_SELECT_EXCEPT;
2388 ZOOM_connection_do_io(c, mask);
2390 if (r == 0 && c->mask)
2392 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2393 /* timeout and this connection was waiting */
2394 c->error = ZOOM_ERROR_TIMEOUT;
2396 yaz_log (LOG_DEBUG, "timeout");
2397 ZOOM_connection_put_event(c, event);
2401 for (i = 0; i<no; i++)
2403 ZOOM_connection c = cs[i];
2405 if (c && (event = ZOOM_connection_get_event(c)))
2407 ZOOM_Event_destroy (event);