2 * $Id: zoom-c.c,v 1.29 2002-05-18 09:52:37 oleg 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>
20 static int ZOOM_connection_send_init (ZOOM_connection c);
22 static ZOOM_Event ZOOM_Event_create (int kind)
24 ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event));
31 static void ZOOM_Event_destroy (ZOOM_Event event)
36 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
40 c->m_queue_back->prev = event;
41 assert (c->m_queue_front);
45 assert (!c->m_queue_front);
46 c->m_queue_front = event;
48 event->next = c->m_queue_back;
50 c->m_queue_back = event;
53 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
55 ZOOM_Event event = c->m_queue_front;
58 assert (c->m_queue_back);
59 c->m_queue_front = event->prev;
62 assert (c->m_queue_back);
63 c->m_queue_front->next = 0;
67 c->last_event = event->kind;
71 static void clear_error (ZOOM_connection c)
76 case ZOOM_ERROR_CONNECT:
77 case ZOOM_ERROR_MEMORY:
78 case ZOOM_ERROR_DECODE:
79 case ZOOM_ERROR_CONNECTION_LOST:
81 case ZOOM_ERROR_INTERNAL:
84 c->error = ZOOM_ERROR_NONE;
90 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
92 ZOOM_task *taskp = &c->tasks;
94 taskp = &(*taskp)->next;
95 *taskp = (ZOOM_task) xmalloc (sizeof(**taskp));
96 (*taskp)->running = 0;
97 (*taskp)->which = which;
103 void ZOOM_connection_remove_task (ZOOM_connection c)
105 ZOOM_task task = c->tasks;
109 c->tasks = task->next;
112 case ZOOM_TASK_SEARCH:
113 ZOOM_resultset_destroy (task->u.search.resultset);
115 case ZOOM_TASK_RETRIEVE:
116 ZOOM_resultset_destroy (task->u.retrieve.resultset);
118 case ZOOM_TASK_CONNECT:
121 ZOOM_scanset_destroy (task->u.scan.scan);
130 void ZOOM_connection_remove_tasks (ZOOM_connection c)
133 ZOOM_connection_remove_task(c);
136 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
138 const char *elementSetName);
140 ZOOM_API(ZOOM_connection)
141 ZOOM_connection_create (ZOOM_options options)
143 ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
147 c->state = STATE_IDLE;
148 c->error = ZOOM_ERROR_NONE;
156 c->options = ZOOM_options_create_with_parent(options);
161 c->charset = c->lang = 0;
167 c->odr_in = odr_createmem (ODR_DECODE);
168 c->odr_out = odr_createmem (ODR_ENCODE);
171 c->support_named_resultsets = 0;
172 c->last_event = ZOOM_EVENT_NONE;
174 c->m_queue_front = 0;
179 /* set database names. Take local databases (if set); otherwise
180 take databases given in ZURL (if set); otherwise use Default */
181 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
184 char **databaseNames;
187 const char *cp = ZOOM_options_get (options, "databaseName");
191 cp = strchr (con->host_port, '/');
198 while ((c = strchr(c, '+')))
206 databaseNames = (char**)
207 odr_malloc (con->odr_out, no * sizeof(*databaseNames));
211 c = strchr (cp, '+');
219 /* cp ptr to first char of db name, c is char
221 databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
222 memcpy (databaseNames[no], cp, c-cp);
223 databaseNames[no++][c-cp] = '\0';
228 databaseNames[no] = NULL;
230 return databaseNames;
233 ZOOM_API(ZOOM_connection)
234 ZOOM_connection_new (const char *host, int portnum)
236 ZOOM_connection c = ZOOM_connection_create (0);
238 ZOOM_connection_connect (c, host, portnum);
243 ZOOM_connection_connect(ZOOM_connection c,
244 const char *host, int portnum)
249 val = ZOOM_options_get (c->options, "proxy");
251 c->proxy = xstrdup (val);
255 val = ZOOM_options_get (c->options, "charset");
257 c->charset = xstrdup (val);
261 val = ZOOM_options_get (c->options, "lang");
263 c->lang = xstrdup (val);
270 sprintf (hostn, "%.80s:%d", host, portnum);
271 c->host_port = xstrdup(hostn);
274 c->host_port = xstrdup(host);
276 ZOOM_options_set(c->options, "host", c->host_port);
278 c->async = ZOOM_options_get_bool (c->options, "async", 0);
280 c->error = ZOOM_ERROR_NONE;
282 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
286 while (ZOOM_event (1, &c))
292 ZOOM_query_create(void)
294 ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
299 s->odr = odr_createmem (ODR_ENCODE);
305 ZOOM_query_destroy(ZOOM_query s)
311 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
312 if (s->refcount == 0)
314 odr_destroy (s->odr);
320 ZOOM_query_prefix(ZOOM_query s, const char *str)
322 s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query));
323 s->query->which = Z_Query_type_1;
324 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
325 if (!s->query->u.type_1)
331 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
333 s->sort_spec = yaz_sort_spec (s->odr, criteria);
339 static int do_write(ZOOM_connection c);
342 ZOOM_connection_destroy(ZOOM_connection c)
349 for (r = c->resultsets; r; r = r->next)
354 odr_destroy (c->odr_in);
355 odr_destroy (c->odr_out);
356 ZOOM_options_destroy (c->options);
357 ZOOM_connection_remove_tasks (c);
358 xfree (c->host_port);
362 xfree (c->cookie_out);
363 xfree (c->cookie_in);
367 void ZOOM_resultset_addref (ZOOM_resultset r)
372 ZOOM_resultset ZOOM_resultset_create ()
374 ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
378 r->odr = odr_createmem (ODR_ENCODE);
392 ZOOM_API(ZOOM_resultset)
393 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
396 ZOOM_query s = ZOOM_query_create();
398 ZOOM_query_prefix (s, q);
400 r = ZOOM_connection_search (c, s);
401 ZOOM_query_destroy (s);
405 ZOOM_API(ZOOM_resultset)
406 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
408 ZOOM_resultset r = ZOOM_resultset_create ();
412 r->r_sort_spec = q->sort_spec;
413 r->r_query = q->query;
416 r->options = ZOOM_options_create_with_parent(c->options);
418 r->start = ZOOM_options_get_int(r->options, "start", 0);
419 r->count = ZOOM_options_get_int(r->options, "count", 0);
420 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
421 cp = ZOOM_options_get (r->options, "setname");
423 r->setname = xstrdup (cp);
427 r->next = c->resultsets;
430 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
431 task->u.search.resultset = r;
432 ZOOM_resultset_addref (r);
438 while (ZOOM_event (1, &c))
445 ZOOM_resultset_destroy(ZOOM_resultset r)
450 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
451 if (r->refcount == 0)
453 ZOOM_record_cache rc;
455 for (rc = r->record_cache; rc; rc = rc->next)
456 if (rc->rec.wrbuf_marc)
457 wrbuf_free (rc->rec.wrbuf_marc, 1);
460 /* remove ourselves from the resultsets in connection */
461 ZOOM_resultset *rp = &r->connection->resultsets;
464 assert (*rp); /* we must be in this list!! */
466 { /* OK, we're here - take us out of it */
473 ZOOM_query_destroy (r->search);
474 ZOOM_options_destroy (r->options);
475 odr_destroy (r->odr);
482 ZOOM_resultset_size (ZOOM_resultset r)
487 static void do_close (ZOOM_connection c)
493 c->state = STATE_IDLE;
496 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
497 int force_sync, int start, int count)
507 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
508 task->u.retrieve.resultset = r;
509 task->u.retrieve.start = start;
510 task->u.retrieve.count = count;
512 ZOOM_resultset_addref (r);
514 if (!r->connection->async || force_sync)
515 while (r->connection && ZOOM_event (1, &r->connection))
520 ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
521 size_t start, size_t count)
523 int force_present = 0;
529 ZOOM_resultset_retrieve (r, force_present, start, count);
533 for (i = 0; i< count; i++)
534 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
538 static int do_connect (ZOOM_connection c)
541 const char *effective_host;
544 effective_host = c->proxy;
546 effective_host = c->host_port;
548 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
551 c->cs = cs_create_host (effective_host, 0, &add);
555 int ret = cs_connect (c->cs, add);
556 yaz_log (LOG_DEBUG, "cs_connect returned %d", ret);
559 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
560 ZOOM_connection_put_event(c, event);
561 ZOOM_connection_send_init(c);
562 c->state = STATE_ESTABLISHED;
567 c->state = STATE_CONNECTING;
568 c->mask = ZOOM_SELECT_EXCEPT;
569 if (c->cs->io_pending & CS_WANT_WRITE)
570 c->mask += ZOOM_SELECT_WRITE;
571 if (c->cs->io_pending & CS_WANT_READ)
572 c->mask += ZOOM_SELECT_READ;
576 c->state = STATE_IDLE;
577 c->error = ZOOM_ERROR_CONNECT;
581 int z3950_connection_socket(ZOOM_connection c)
584 return cs_fileno(c->cs);
588 int z3950_connection_mask(ZOOM_connection c)
595 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
600 sprintf (str, "send_APDU t=%p type=%d", c, a->which);
603 Z_OtherInformation **oi;
605 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
607 if (!z_APDU(out, &a, 0, 0))
609 FILE *outf = fopen("/tmp/apdu.txt", "w");
612 ODR odr_pr = odr_createmem(ODR_PRINT);
613 fprintf (outf, "a=%p\n", a);
614 odr_setprint(odr_pr, outf);
615 z_APDU(odr_pr, &a, 0, 0);
619 c->error = ZOOM_ERROR_ENCODE;
627 static int send_APDU (ZOOM_connection c, Z_APDU *a)
631 if (encode_APDU(c, a, c->odr_out))
633 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
634 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
635 ZOOM_connection_put_event (c, event);
636 odr_reset(c->odr_out);
641 static int ZOOM_connection_send_init (ZOOM_connection c)
644 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
645 Z_InitRequest *ireq = apdu->u.initRequest;
646 Z_IdAuthentication *auth = (Z_IdAuthentication *)
647 odr_malloc(c->odr_out, sizeof(*auth));
648 const char *auth_groupId = ZOOM_options_get (c->options, "group");
649 const char *auth_userId = ZOOM_options_get (c->options, "user");
650 const char *auth_password = ZOOM_options_get (c->options, "pass");
652 ODR_MASK_SET(ireq->options, Z_Options_search);
653 ODR_MASK_SET(ireq->options, Z_Options_present);
654 ODR_MASK_SET(ireq->options, Z_Options_scan);
655 ODR_MASK_SET(ireq->options, Z_Options_sort);
656 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
657 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
659 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
660 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
661 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
663 impname = ZOOM_options_get (c->options, "implementationName");
664 ireq->implementationName =
665 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
666 strcpy (ireq->implementationName, "");
669 strcat (ireq->implementationName, impname);
670 strcat (ireq->implementationName, "/");
672 strcat (ireq->implementationName, "ZOOM-C/YAZ");
674 *ireq->maximumRecordSize =
675 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
676 *ireq->preferredMessageSize =
677 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
679 if (auth_groupId || auth_password)
681 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
684 if (auth_groupId && *auth_groupId)
686 pass->groupId = (char *)
687 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
688 strcpy(pass->groupId, auth_groupId);
692 if (auth_userId && *auth_userId)
694 pass->userId = (char *)
695 odr_malloc(c->odr_out, strlen(auth_userId)+1);
696 strcpy(pass->userId, auth_userId);
700 if (auth_password && *auth_password)
702 pass->password = (char *)
703 odr_malloc(c->odr_out, strlen(auth_password)+1);
704 strcpy(pass->password, auth_password);
709 auth->which = Z_IdAuthentication_idPass;
710 auth->u.idPass = pass;
711 ireq->idAuthentication = auth;
714 else if (auth_userId)
716 auth->which = Z_IdAuthentication_open;
717 auth->u.open = (char *)
718 odr_malloc(c->odr_out, strlen(auth_userId)+1);
719 strcpy(auth->u.open, auth_userId);
720 ireq->idAuthentication = auth;
723 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
724 VAL_PROXY, 1, c->host_port);
725 if (c->charset||c->lang)
727 Z_OtherInformation **oi;
728 Z_OtherInformationUnit *oi_unit;
730 yaz_oi_APDU(apdu, &oi);
732 if (oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0))
734 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
736 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
737 oi_unit->information.externallyDefinedInfo =
738 yaz_set_charset_and_lang(c->odr_out,
739 CLASS_NEGOT, VAL_CHARNEG3,
740 (const char **)&c->charset, (c->charset) ? 1:0,
741 (const char **)&c->lang, (c->lang) ? 1:0);
750 static int ZOOM_connection_send_search (ZOOM_connection c)
753 int lslb, ssub, mspn;
755 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
756 Z_SearchRequest *search_req = apdu->u.searchRequest;
757 const char *elementSetName;
758 const char *smallSetElementSetName;
759 const char *mediumSetElementSetName;
763 assert (c->tasks->which == ZOOM_TASK_SEARCH);
765 r = c->tasks->u.search.resultset;
768 ZOOM_options_get (r->options, "elementSetName");
769 smallSetElementSetName =
770 ZOOM_options_get (r->options, "smallSetElementSetName");
771 mediumSetElementSetName =
772 ZOOM_options_get (r->options, "mediumSetElementSetName");
774 ZOOM_options_get (r->options, "schema");
776 if (!smallSetElementSetName)
777 smallSetElementSetName = elementSetName;
779 if (!mediumSetElementSetName)
780 mediumSetElementSetName = elementSetName;
785 /* prepare query for the search request */
786 search_req->query = r->r_query;
788 search_req->databaseNames =
789 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
791 /* get syntax (no need to provide unless piggyback is in effect) */
792 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
794 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
795 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
796 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
797 if (lslb != -1 && ssub != -1 && mspn != -1)
799 /* So're a Z39.50 expert? Let's hope you don't do sort */
800 *search_req->largeSetLowerBound = lslb;
801 *search_req->smallSetUpperBound = ssub;
802 *search_req->mediumSetPresentNumber = mspn;
804 else if (r->start == 0 && r->count > 0
805 && r->piggyback && !r->r_sort_spec && !schema)
807 /* Regular piggyback - do it unless we're going to do sort */
808 *search_req->largeSetLowerBound = 2000000000;
809 *search_req->smallSetUpperBound = r->count;
810 *search_req->mediumSetPresentNumber = r->count;
811 smallSetElementSetName = 0; /* no need to provide this */
815 /* non-piggyback. Need not provide elementsets or syntaxes .. */
816 smallSetElementSetName = 0;
817 mediumSetElementSetName = 0;
820 if (smallSetElementSetName && *smallSetElementSetName)
822 Z_ElementSetNames *esn = (Z_ElementSetNames *)
823 odr_malloc (c->odr_out, sizeof(*esn));
825 esn->which = Z_ElementSetNames_generic;
826 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
827 search_req->smallSetElementSetNames = esn;
829 if (mediumSetElementSetName && *mediumSetElementSetName)
831 Z_ElementSetNames *esn = (Z_ElementSetNames *)
832 odr_malloc (c->odr_out, sizeof(*esn));
834 esn->which = Z_ElementSetNames_generic;
835 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
836 search_req->mediumSetElementSetNames = esn;
839 search_req->preferredRecordSyntax =
840 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
844 if (c->support_named_resultsets)
848 /* find the lowest unused ordinal so that we re-use
849 result sets on the server. */
850 for (ord = 1; ; ord++)
853 sprintf (setname, "%d", ord);
854 for (rp = c->resultsets; rp; rp = rp->next)
855 if (rp->setname && !strcmp (rp->setname, setname))
860 r->setname = xstrdup (setname);
861 yaz_log (LOG_DEBUG, "allocating %s", r->setname);
864 r->setname = xstrdup ("default");
865 ZOOM_options_set (r->options, "setname", r->setname);
867 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
868 /* send search request */
874 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
876 Z_DefaultDiagFormat *r;
881 if (p->which != Z_DiagRec_defaultFormat)
883 c->error = ZOOM_ERROR_DECODE;
886 r = p->u.defaultFormat;
889 case Z_DefaultDiagFormat_v2Addinfo:
890 addinfo = r->u.v2Addinfo;
892 case Z_DefaultDiagFormat_v3Addinfo:
893 addinfo = r->u.v3Addinfo;
897 c->addinfo = xstrdup (addinfo);
898 c->error = *r->condition;
901 ZOOM_API(ZOOM_record)
902 ZOOM_record_clone (ZOOM_record srec)
909 odr_enc = odr_createmem(ODR_ENCODE);
910 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
912 buf = odr_getbuf (odr_enc, &size, 0);
914 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
915 nrec->odr = odr_createmem(ODR_DECODE);
916 nrec->wrbuf_marc = 0;
917 odr_setbuf (nrec->odr, buf, size, 0);
918 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
920 odr_destroy (odr_enc);
924 ZOOM_API(ZOOM_record)
925 ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
927 return record_cache_lookup (s, pos, 0);
930 ZOOM_API(ZOOM_record)
931 ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
933 ZOOM_resultset_retrieve (r, 1, pos, 1);
934 return ZOOM_resultset_record_immediate (r, pos);
938 ZOOM_record_destroy (ZOOM_record rec)
943 wrbuf_free (rec->wrbuf_marc, 1);
944 odr_destroy (rec->odr);
948 ZOOM_API(const char *)
949 ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
951 Z_NamePlusRecord *npr;
954 *len = 0; /* default return */
961 if (!strcmp (type, "database"))
963 if (len) *len = strlen(npr->databaseName)+1;
964 return npr->databaseName;
966 else if (!strcmp (type, "syntax"))
968 if (npr->which == Z_NamePlusRecord_databaseRecord)
970 Z_External *r = (Z_External *) npr->u.databaseRecord;
971 oident *ent = oid_getentbyoid(r->direct_reference);
974 if (len) *len = strlen(ent->desc)+1;
980 else if (!strcmp (type, "render") &&
981 npr->which == Z_NamePlusRecord_databaseRecord)
983 Z_External *r = (Z_External *) npr->u.databaseRecord;
984 oident *ent = oid_getentbyoid(r->direct_reference);
986 if (r->which == Z_External_sutrs)
988 if (len) *len = r->u.sutrs->len;
989 return (const char *) r->u.sutrs->buf;
991 else if (r->which == Z_External_octet)
1000 case VAL_APPLICATION_XML:
1003 if (!rec->wrbuf_marc)
1004 rec->wrbuf_marc = wrbuf_alloc();
1005 wrbuf_rewind (rec->wrbuf_marc);
1006 if (yaz_marc_decode ((const char *)
1007 r->u.octet_aligned->buf,
1009 r->u.octet_aligned->len,
1012 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1013 return wrbuf_buf(rec->wrbuf_marc);
1016 if (len) *len = r->u.octet_aligned->len;
1017 return (const char *) r->u.octet_aligned->buf;
1019 else if (r->which == Z_External_grs1)
1026 else if (!strcmp (type, "xml") &&
1027 npr->which == Z_NamePlusRecord_databaseRecord)
1029 Z_External *r = (Z_External *) npr->u.databaseRecord;
1030 oident *ent = oid_getentbyoid(r->direct_reference);
1032 if (r->which == Z_External_sutrs)
1034 if (len) *len = r->u.sutrs->len;
1035 return (const char *) r->u.sutrs->buf;
1037 else if (r->which == Z_External_octet)
1046 case VAL_APPLICATION_XML:
1049 if (!rec->wrbuf_marc)
1050 rec->wrbuf_marc = wrbuf_alloc();
1051 wrbuf_rewind (rec->wrbuf_marc);
1052 if (yaz_marc_decode ((const char *)
1053 r->u.octet_aligned->buf,
1055 r->u.octet_aligned->len,
1058 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1059 return wrbuf_buf(rec->wrbuf_marc);
1062 if (len) *len = r->u.octet_aligned->len;
1063 return (const char *) r->u.octet_aligned->buf;
1065 else if (r->which == Z_External_grs1)
1072 else if (!strcmp (type, "raw"))
1074 if (npr->which == Z_NamePlusRecord_databaseRecord)
1076 Z_External *r = (Z_External *) npr->u.databaseRecord;
1078 if (r->which == Z_External_sutrs)
1080 if (len) *len = r->u.sutrs->len;
1081 return (const char *) r->u.sutrs->buf;
1083 else if (r->which == Z_External_octet)
1085 if (len) *len = r->u.octet_aligned->len;
1086 return (const char *) r->u.octet_aligned->buf;
1088 else /* grs-1, explain, ... */
1091 return (const char *) npr->u.databaseRecord;
1099 static void record_cache_add (ZOOM_resultset r,
1100 Z_NamePlusRecord *npr,
1102 const char *elementSetName)
1104 ZOOM_record_cache rc;
1106 for (rc = r->record_cache; rc; rc = rc->next)
1110 if ((!elementSetName && !rc->elementSetName)
1111 || (elementSetName && rc->elementSetName &&
1112 !strcmp (elementSetName, rc->elementSetName)))
1114 /* not destroying rc->npr (it's handled by nmem )*/
1116 /* keeping wrbuf_marc too */
1121 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1124 rc->rec.wrbuf_marc = 0;
1126 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1128 rc->elementSetName = 0;
1130 rc->next = r->record_cache;
1131 r->record_cache = rc;
1134 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
1136 const char *elementSetName)
1138 ZOOM_record_cache rc;
1140 for (rc = r->record_cache; rc; rc = rc->next)
1144 if ((!elementSetName && !rc->elementSetName)
1145 || (elementSetName && rc->elementSetName &&
1146 !strcmp (elementSetName, rc->elementSetName)))
1153 static void handle_records (ZOOM_connection c, Z_Records *sr,
1156 ZOOM_resultset resultset;
1160 switch (c->tasks->which)
1162 case ZOOM_TASK_SEARCH:
1163 resultset = c->tasks->u.search.resultset;
1165 case ZOOM_TASK_RETRIEVE:
1166 resultset = c->tasks->u.retrieve.resultset;
1171 if (sr && sr->which == Z_Records_NSD)
1173 Z_DiagRec dr, *dr_p = &dr;
1174 dr.which = Z_DiagRec_defaultFormat;
1175 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1177 response_diag (c, dr_p);
1179 else if (sr && sr->which == Z_Records_multipleNSD)
1181 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1182 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1184 c->error = ZOOM_ERROR_DECODE;
1188 if (resultset->count + resultset->start > resultset->size)
1189 resultset->count = resultset->size - resultset->start;
1190 if (resultset->count < 0)
1191 resultset->count = 0;
1192 if (sr && sr->which == Z_Records_DBOSD)
1195 NMEM nmem = odr_extract_mem (c->odr_in);
1196 Z_NamePlusRecordList *p =
1197 sr->u.databaseOrSurDiagnostics;
1198 for (i = 0; i<p->num_records; i++)
1200 record_cache_add (resultset, p->records[i],
1201 i+ resultset->start, 0);
1203 /* transfer our response to search_nmem .. we need it later */
1204 nmem_transfer (resultset->odr->mem, nmem);
1205 nmem_destroy (nmem);
1206 if (present_phase && p->num_records == 0)
1208 /* present response and we didn't get any records! */
1209 c->error = ZOOM_ERROR_DECODE;
1212 else if (present_phase)
1214 /* present response and we didn't get any records! */
1215 c->error = ZOOM_ERROR_DECODE;
1220 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1222 handle_records (c, pr->records, 1);
1225 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1227 ZOOM_resultset resultset;
1229 yaz_log (LOG_DEBUG, "got search response");
1231 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1234 resultset = c->tasks->u.search.resultset;
1236 resultset->size = *sr->resultCount;
1237 handle_records (c, sr->records, 0);
1240 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1242 if (res->diagnostics && res->num_diagnostics > 0)
1243 response_diag (c, res->diagnostics[0]);
1246 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1248 NMEM nmem = odr_extract_mem (c->odr_in);
1251 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1253 scan = c->tasks->u.scan.scan;
1255 if (res->entries && res->entries->nonsurrogateDiagnostics)
1256 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1257 scan->scan_response = res;
1258 nmem_transfer (scan->odr->mem, nmem);
1260 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1261 if (res->positionOfTerm)
1262 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1263 if (res->scanStatus)
1264 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1265 if (res->numberOfEntriesReturned)
1266 ZOOM_options_set_int (scan->options, "number",
1267 *res->numberOfEntriesReturned);
1268 nmem_destroy (nmem);
1272 static int send_sort (ZOOM_connection c)
1274 ZOOM_resultset resultset;
1276 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1279 resultset = c->tasks->u.search.resultset;
1283 resultset->r_sort_spec = 0;
1286 if (resultset->r_sort_spec)
1288 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1289 Z_SortRequest *req = apdu->u.sortRequest;
1291 req->num_inputResultSetNames = 1;
1292 req->inputResultSetNames = (Z_InternationalString **)
1293 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1294 req->inputResultSetNames[0] =
1295 odr_strdup (c->odr_out, resultset->setname);
1296 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1297 req->sortSequence = resultset->r_sort_spec;
1298 resultset->r_sort_spec = 0;
1299 send_APDU (c, apdu);
1305 static int send_present (ZOOM_connection c)
1307 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1308 Z_PresentRequest *req = apdu->u.presentRequest;
1310 const char *syntax =
1311 ZOOM_options_get (c->options, "preferredRecordSyntax");
1312 const char *element =
1313 ZOOM_options_get (c->options, "elementSetName");
1314 const char *schema =
1315 ZOOM_options_get (c->options, "schema");
1316 ZOOM_resultset resultset;
1321 switch (c->tasks->which)
1323 case ZOOM_TASK_SEARCH:
1324 resultset = c->tasks->u.search.resultset;
1326 case ZOOM_TASK_RETRIEVE:
1327 resultset = c->tasks->u.retrieve.resultset;
1328 resultset->start = c->tasks->u.retrieve.start;
1329 resultset->count = c->tasks->u.retrieve.count;
1331 if (resultset->start >= resultset->size)
1333 if (resultset->start + resultset->count > resultset->size)
1334 resultset->count = resultset->size - resultset->start;
1340 if (c->error) /* don't continue on error */
1342 if (resultset->start < 0)
1344 for (i = 0; i<resultset->count; i++)
1347 record_cache_lookup (resultset, i + resultset->start, 0);
1351 if (i == resultset->count)
1354 resultset->start += i;
1355 resultset->count -= i;
1356 *req->resultSetStartPoint = resultset->start + 1;
1357 *req->numberOfRecordsRequested = resultset->count;
1358 assert (*req->numberOfRecordsRequested > 0);
1360 if (syntax && *syntax)
1361 req->preferredRecordSyntax =
1362 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1364 if (schema && *schema)
1366 Z_RecordComposition *compo = (Z_RecordComposition *)
1367 odr_malloc (c->odr_out, sizeof(*compo));
1369 req->recordComposition = compo;
1370 compo->which = Z_RecordComp_complex;
1371 compo->u.complex = (Z_CompSpec *)
1372 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1373 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1374 odr_malloc(c->odr_out, sizeof(bool_t));
1375 *compo->u.complex->selectAlternativeSyntax = 0;
1377 compo->u.complex->generic = (Z_Specification *)
1378 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1380 compo->u.complex->generic->schema = (Odr_oid *)
1381 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1383 if (!compo->u.complex->generic->schema)
1385 /* OID wasn't a schema! Try record syntax instead. */
1387 compo->u.complex->generic->schema = (Odr_oid *)
1388 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1390 if (element && *element)
1392 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1393 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1394 compo->u.complex->generic->elementSpec->which =
1395 Z_ElementSpec_elementSetName;
1396 compo->u.complex->generic->elementSpec->u.elementSetName =
1397 odr_strdup (c->odr_out, element);
1400 compo->u.complex->generic->elementSpec = 0;
1401 compo->u.complex->num_dbSpecific = 0;
1402 compo->u.complex->dbSpecific = 0;
1403 compo->u.complex->num_recordSyntax = 0;
1404 compo->u.complex->recordSyntax = 0;
1406 else if (element && *element)
1408 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1409 odr_malloc (c->odr_out, sizeof(*esn));
1410 Z_RecordComposition *compo = (Z_RecordComposition *)
1411 odr_malloc (c->odr_out, sizeof(*compo));
1413 esn->which = Z_ElementSetNames_generic;
1414 esn->u.generic = odr_strdup (c->odr_out, element);
1415 compo->which = Z_RecordComp_simple;
1416 compo->u.simple = esn;
1417 req->recordComposition = compo;
1419 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1420 send_APDU (c, apdu);
1424 ZOOM_API(ZOOM_scanset)
1425 ZOOM_connection_scan (ZOOM_connection c, const char *start)
1427 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1429 scan->connection = c;
1430 scan->odr = odr_createmem (ODR_DECODE);
1431 scan->options = ZOOM_options_create_with_parent (c->options);
1433 scan->scan_response = 0;
1435 if ((scan->termListAndStartPoint =
1436 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1439 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1440 task->u.scan.scan = scan;
1445 while (ZOOM_event (1, &c))
1453 ZOOM_scanset_destroy (ZOOM_scanset scan)
1458 if (scan->refcount == 0)
1460 odr_destroy (scan->odr);
1462 ZOOM_options_destroy (scan->options);
1467 static int send_scan (ZOOM_connection c)
1470 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1471 Z_ScanRequest *req = apdu->u.scanRequest;
1474 assert (c->tasks->which == ZOOM_TASK_SCAN);
1475 scan = c->tasks->u.scan.scan;
1477 req->termListAndStartPoint = scan->termListAndStartPoint;
1478 req->attributeSet = scan->attributeSet;
1480 *req->numberOfTermsRequested =
1481 ZOOM_options_get_int(scan->options, "number", 10);
1483 req->preferredPositionInResponse =
1484 odr_intdup (c->odr_out,
1485 ZOOM_options_get_int(scan->options, "position", 1));
1488 odr_intdup (c->odr_out,
1489 ZOOM_options_get_int(scan->options, "stepSize", 0));
1491 req->databaseNames = set_DatabaseNames (c, scan->options,
1492 &req->num_databaseNames);
1494 send_APDU (c, apdu);
1500 ZOOM_scanset_size (ZOOM_scanset scan)
1502 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1504 return scan->scan_response->entries->num_entries;
1507 ZOOM_API(const char *)
1508 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1511 const char *term = 0;
1512 size_t noent = ZOOM_scanset_size (scan);
1513 Z_ScanResponse *res = scan->scan_response;
1519 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1521 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1523 if (t->term->which == Z_Term_general)
1525 term = (const char *) t->term->u.general->buf;
1526 *len = t->term->u.general->len;
1528 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1533 ZOOM_API(const char *)
1534 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1536 return ZOOM_options_get (scan->options, key);
1540 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1543 ZOOM_options_set (scan->options, key, val);
1546 static int ZOOM_connection_exec_task (ZOOM_connection c)
1548 ZOOM_task task = c->tasks;
1550 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1553 if (c->error != ZOOM_ERROR_NONE ||
1554 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1556 ZOOM_connection_remove_tasks (c);
1559 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d", task->which);
1563 switch (task->which)
1565 case ZOOM_TASK_SEARCH:
1566 /* see if search hasn't been sent yet. */
1567 if (ZOOM_connection_send_search (c))
1570 case ZOOM_TASK_RETRIEVE:
1571 if (send_present (c))
1574 case ZOOM_TASK_CONNECT:
1578 case ZOOM_TASK_SCAN:
1582 ZOOM_connection_remove_task (c);
1586 static int send_sort_present (ZOOM_connection c)
1588 int r = send_sort (c);
1590 r = send_present (c);
1594 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1596 Z_InitResponse *initrs;
1598 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1602 case Z_APDU_initResponse:
1603 initrs = apdu->u.initResponse;
1604 if (!*initrs->result)
1606 c->error = ZOOM_ERROR_INIT;
1611 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1613 xfree (c->cookie_in);
1616 c->cookie_in = xstrdup(cookie);
1617 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1618 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1619 c->support_named_resultsets = 1;
1622 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1623 ZOOM_connection_remove_task (c);
1625 ZOOM_connection_exec_task (c);
1628 case Z_APDU_searchResponse:
1629 handle_search_response (c, apdu->u.searchResponse);
1630 if (!send_sort_present (c))
1631 ZOOM_connection_remove_task (c);
1633 case Z_APDU_presentResponse:
1634 handle_present_response (c, apdu->u.presentResponse);
1635 if (!send_present (c))
1636 ZOOM_connection_remove_task (c);
1638 case Z_APDU_sortResponse:
1639 sort_response (c, apdu->u.sortResponse);
1640 if (!send_present (c))
1641 ZOOM_connection_remove_task (c);
1643 case Z_APDU_scanResponse:
1644 scan_response (c, apdu->u.scanResponse);
1645 ZOOM_connection_remove_task (c);
1649 static int do_read (ZOOM_connection c)
1655 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
1656 ZOOM_connection_put_event (c, event);
1658 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1663 c->error= ZOOM_ERROR_CONNECTION_LOST;
1669 odr_reset (c->odr_in);
1670 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1671 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
1672 ZOOM_connection_put_event (c, event);
1673 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1675 c->error = ZOOM_ERROR_DECODE;
1680 handle_apdu (c, apdu);
1686 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
1691 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1692 ZOOM_connection_put_event (c, event);
1694 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1696 if (c->state == STATE_CONNECTING)
1697 c->error = ZOOM_ERROR_CONNECT;
1699 c->error = ZOOM_ERROR_CONNECTION_LOST;
1705 c->mask = ZOOM_SELECT_EXCEPT;
1706 if (c->cs->io_pending & CS_WANT_WRITE)
1707 c->mask += ZOOM_SELECT_WRITE;
1708 if (c->cs->io_pending & CS_WANT_READ)
1709 c->mask += ZOOM_SELECT_READ;
1710 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
1714 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
1715 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
1720 static int do_write(ZOOM_connection c)
1722 return do_write_ex (c, c->buf_out, c->len_out);
1726 ZOOM_API(const char *)
1727 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
1729 return ZOOM_options_get (c->options, key);
1733 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
1736 ZOOM_options_set (c->options, key, val);
1739 ZOOM_API(const char *)
1740 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
1742 return ZOOM_options_get (r->options, key);
1746 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
1749 ZOOM_options_set (r->options, key, val);
1754 ZOOM_connection_errcode (ZOOM_connection c)
1756 return ZOOM_connection_error (c, 0, 0);
1759 ZOOM_API(const char *)
1760 ZOOM_connection_errmsg (ZOOM_connection c)
1763 ZOOM_connection_error (c, &msg, 0);
1767 ZOOM_API(const char *)
1768 ZOOM_connection_addinfo (ZOOM_connection c)
1770 const char *addinfo;
1771 ZOOM_connection_error (c, 0, &addinfo);
1776 ZOOM_connection_error (ZOOM_connection c, const char **cp,
1777 const char **addinfo)
1779 int error = c->error;
1784 case ZOOM_ERROR_NONE:
1785 *cp = "No error"; break;
1786 case ZOOM_ERROR_CONNECT:
1787 *cp = "Connect failed"; break;
1788 case ZOOM_ERROR_MEMORY:
1789 *cp = "Out of memory"; break;
1790 case ZOOM_ERROR_ENCODE:
1791 *cp = "Encoding failed"; break;
1792 case ZOOM_ERROR_DECODE:
1793 *cp = "Decoding failed"; break;
1794 case ZOOM_ERROR_CONNECTION_LOST:
1795 *cp = "Connection lost"; break;
1796 case ZOOM_ERROR_INIT:
1797 *cp = "Init rejected"; break;
1798 case ZOOM_ERROR_INTERNAL:
1799 *cp = "Internal failure"; break;
1800 case ZOOM_ERROR_TIMEOUT:
1801 *cp = "Timeout"; break;
1803 *cp = diagbib1_str (error);
1809 *addinfo = c->addinfo;
1816 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
1818 ZOOM_Event event = 0;
1819 int r = cs_look(c->cs);
1820 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
1825 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
1826 c->error = ZOOM_ERROR_CONNECT;
1828 ZOOM_connection_put_event (c, event);
1830 else if (r == CS_CONNECT)
1833 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
1835 ret = cs_rcvconnect (c->cs);
1836 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
1839 c->mask = ZOOM_SELECT_EXCEPT;
1840 if (c->cs->io_pending & CS_WANT_WRITE)
1841 c->mask += ZOOM_SELECT_WRITE;
1842 if (c->cs->io_pending & CS_WANT_READ)
1843 c->mask += ZOOM_SELECT_READ;
1844 ZOOM_connection_put_event (c, event);
1848 ZOOM_connection_put_event (c, event);
1849 ZOOM_connection_send_init (c);
1850 c->state = STATE_ESTABLISHED;
1854 c->error = ZOOM_ERROR_CONNECT;
1856 ZOOM_connection_put_event (c, event);
1861 if (mask & ZOOM_SELECT_READ)
1863 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1870 ZOOM_connection_last_event(ZOOM_connection cs)
1873 return ZOOM_EVENT_NONE;
1874 return cs->last_event;
1878 ZOOM_event (int no, ZOOM_connection *cs)
1881 struct pollfd pollfds[1024];
1882 ZOOM_connection poll_cs[1024];
1885 fd_set input, output, except;
1890 for (i = 0; i<no; i++)
1892 ZOOM_connection c = cs[i];
1894 if (c && (event = ZOOM_connection_get_event(c)))
1896 ZOOM_Event_destroy (event);
1900 for (i = 0; i<no; i++)
1902 ZOOM_connection c = cs[i];
1904 if (c && ZOOM_connection_exec_task (c))
1906 if ((event = ZOOM_connection_get_event(c)))
1908 ZOOM_Event_destroy (event);
1924 for (i = 0; i<no; i++)
1926 ZOOM_connection c = cs[i];
1931 fd = z3950_connection_socket(c);
1932 mask = z3950_connection_mask(c);
1942 short poll_events = 0;
1944 if (mask & ZOOM_SELECT_READ)
1945 poll_events += POLLIN;
1946 if (mask & ZOOM_SELECT_WRITE)
1947 poll_events += POLLOUT;
1948 if (mask & ZOOM_SELECT_EXCEPT)
1949 poll_events += POLLERR;
1950 pollfds[nfds].fd = fd;
1951 pollfds[nfds].events = poll_events;
1952 pollfds[nfds].revents = 0;
1957 if (mask & ZOOM_SELECT_READ)
1959 FD_SET (fd, &input);
1962 if (mask & ZOOM_SELECT_WRITE)
1964 FD_SET (fd, &output);
1967 if (mask & ZOOM_SELECT_EXCEPT)
1969 FD_SET (fd, &except);
1977 r = poll (pollfds, nfds, 15000);
1978 for (i = 0; i<nfds; i++)
1980 ZOOM_connection c = poll_cs[i];
1984 if (pollfds[i].revents & POLLIN)
1985 mask += ZOOM_SELECT_READ;
1986 if (pollfds[i].revents & POLLOUT)
1987 mask += ZOOM_SELECT_WRITE;
1988 if (pollfds[i].revents & POLLERR)
1989 mask += ZOOM_SELECT_EXCEPT;
1991 ZOOM_connection_do_io(c, mask);
1993 else if (r == 0 && c->mask)
1995 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
1996 /* timeout and this connection was waiting */
1997 c->error = ZOOM_ERROR_TIMEOUT;
1999 ZOOM_connection_put_event(c, event);
2003 yaz_log (LOG_DEBUG, "select start");
2004 r = select (max_fd+1, &input, &output, &except, &tv);
2005 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
2006 for (i = 0; i<no; i++)
2008 ZOOM_connection c = cs[i];
2013 fd = z3950_connection_socket(c);
2017 /* no timeout and real socket */
2018 if (FD_ISSET(fd, &input))
2019 mask += ZOOM_SELECT_READ;
2020 if (FD_ISSET(fd, &output))
2021 mask += ZOOM_SELECT_WRITE;
2022 if (FD_ISSET(fd, &except))
2023 mask += ZOOM_SELECT_EXCEPT;
2025 ZOOM_connection_do_io(c, mask);
2027 if (r == 0 && c->mask)
2029 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2030 /* timeout and this connection was waiting */
2031 c->error = ZOOM_ERROR_TIMEOUT;
2033 yaz_log (LOG_DEBUG, "timeout");
2034 ZOOM_connection_put_event(c, event);
2038 for (i = 0; i<no; i++)
2040 ZOOM_connection c = cs[i];
2042 if (c && (event = ZOOM_connection_get_event(c)))
2044 ZOOM_Event_destroy (event);