2 * $Id: zoom-c.c,v 1.31 2002-05-20 09:13:39 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_proposal_charneg(c->odr_out,
739 (const char **)&c->charset, (c->charset) ? 1:0,
740 (const char **)&c->lang, (c->lang) ? 1:0, 1);
749 static int ZOOM_connection_send_search (ZOOM_connection c)
752 int lslb, ssub, mspn;
754 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
755 Z_SearchRequest *search_req = apdu->u.searchRequest;
756 const char *elementSetName;
757 const char *smallSetElementSetName;
758 const char *mediumSetElementSetName;
762 assert (c->tasks->which == ZOOM_TASK_SEARCH);
764 r = c->tasks->u.search.resultset;
767 ZOOM_options_get (r->options, "elementSetName");
768 smallSetElementSetName =
769 ZOOM_options_get (r->options, "smallSetElementSetName");
770 mediumSetElementSetName =
771 ZOOM_options_get (r->options, "mediumSetElementSetName");
773 ZOOM_options_get (r->options, "schema");
775 if (!smallSetElementSetName)
776 smallSetElementSetName = elementSetName;
778 if (!mediumSetElementSetName)
779 mediumSetElementSetName = elementSetName;
784 /* prepare query for the search request */
785 search_req->query = r->r_query;
787 search_req->databaseNames =
788 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
790 /* get syntax (no need to provide unless piggyback is in effect) */
791 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
793 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
794 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
795 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
796 if (lslb != -1 && ssub != -1 && mspn != -1)
798 /* So're a Z39.50 expert? Let's hope you don't do sort */
799 *search_req->largeSetLowerBound = lslb;
800 *search_req->smallSetUpperBound = ssub;
801 *search_req->mediumSetPresentNumber = mspn;
803 else if (r->start == 0 && r->count > 0
804 && r->piggyback && !r->r_sort_spec && !schema)
806 /* Regular piggyback - do it unless we're going to do sort */
807 *search_req->largeSetLowerBound = 2000000000;
808 *search_req->smallSetUpperBound = r->count;
809 *search_req->mediumSetPresentNumber = r->count;
810 smallSetElementSetName = 0; /* no need to provide this */
814 /* non-piggyback. Need not provide elementsets or syntaxes .. */
815 smallSetElementSetName = 0;
816 mediumSetElementSetName = 0;
819 if (smallSetElementSetName && *smallSetElementSetName)
821 Z_ElementSetNames *esn = (Z_ElementSetNames *)
822 odr_malloc (c->odr_out, sizeof(*esn));
824 esn->which = Z_ElementSetNames_generic;
825 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
826 search_req->smallSetElementSetNames = esn;
828 if (mediumSetElementSetName && *mediumSetElementSetName)
830 Z_ElementSetNames *esn = (Z_ElementSetNames *)
831 odr_malloc (c->odr_out, sizeof(*esn));
833 esn->which = Z_ElementSetNames_generic;
834 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
835 search_req->mediumSetElementSetNames = esn;
838 search_req->preferredRecordSyntax =
839 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
843 if (c->support_named_resultsets)
847 /* find the lowest unused ordinal so that we re-use
848 result sets on the server. */
849 for (ord = 1; ; ord++)
852 sprintf (setname, "%d", ord);
853 for (rp = c->resultsets; rp; rp = rp->next)
854 if (rp->setname && !strcmp (rp->setname, setname))
859 r->setname = xstrdup (setname);
860 yaz_log (LOG_DEBUG, "allocating %s", r->setname);
863 r->setname = xstrdup ("default");
864 ZOOM_options_set (r->options, "setname", r->setname);
866 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
867 /* send search request */
873 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
875 Z_DefaultDiagFormat *r;
880 if (p->which != Z_DiagRec_defaultFormat)
882 c->error = ZOOM_ERROR_DECODE;
885 r = p->u.defaultFormat;
888 case Z_DefaultDiagFormat_v2Addinfo:
889 addinfo = r->u.v2Addinfo;
891 case Z_DefaultDiagFormat_v3Addinfo:
892 addinfo = r->u.v3Addinfo;
896 c->addinfo = xstrdup (addinfo);
897 c->error = *r->condition;
900 ZOOM_API(ZOOM_record)
901 ZOOM_record_clone (ZOOM_record srec)
908 odr_enc = odr_createmem(ODR_ENCODE);
909 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
911 buf = odr_getbuf (odr_enc, &size, 0);
913 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
914 nrec->odr = odr_createmem(ODR_DECODE);
915 nrec->wrbuf_marc = 0;
916 odr_setbuf (nrec->odr, buf, size, 0);
917 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
919 odr_destroy (odr_enc);
923 ZOOM_API(ZOOM_record)
924 ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
926 return record_cache_lookup (s, pos, 0);
929 ZOOM_API(ZOOM_record)
930 ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
932 ZOOM_resultset_retrieve (r, 1, pos, 1);
933 return ZOOM_resultset_record_immediate (r, pos);
937 ZOOM_record_destroy (ZOOM_record rec)
942 wrbuf_free (rec->wrbuf_marc, 1);
943 odr_destroy (rec->odr);
947 ZOOM_API(const char *)
948 ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
950 Z_NamePlusRecord *npr;
953 *len = 0; /* default return */
960 if (!strcmp (type, "database"))
962 if (len) *len = strlen(npr->databaseName)+1;
963 return npr->databaseName;
965 else if (!strcmp (type, "syntax"))
967 if (npr->which == Z_NamePlusRecord_databaseRecord)
969 Z_External *r = (Z_External *) npr->u.databaseRecord;
970 oident *ent = oid_getentbyoid(r->direct_reference);
973 if (len) *len = strlen(ent->desc)+1;
979 else if (!strcmp (type, "render") &&
980 npr->which == Z_NamePlusRecord_databaseRecord)
982 Z_External *r = (Z_External *) npr->u.databaseRecord;
983 oident *ent = oid_getentbyoid(r->direct_reference);
985 if (r->which == Z_External_sutrs)
987 if (len) *len = r->u.sutrs->len;
988 return (const char *) r->u.sutrs->buf;
990 else if (r->which == Z_External_octet)
999 case VAL_APPLICATION_XML:
1002 if (!rec->wrbuf_marc)
1003 rec->wrbuf_marc = wrbuf_alloc();
1004 wrbuf_rewind (rec->wrbuf_marc);
1005 if (yaz_marc_decode ((const char *)
1006 r->u.octet_aligned->buf,
1008 r->u.octet_aligned->len,
1011 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1012 return wrbuf_buf(rec->wrbuf_marc);
1015 if (len) *len = r->u.octet_aligned->len;
1016 return (const char *) r->u.octet_aligned->buf;
1018 else if (r->which == Z_External_grs1)
1025 else if (!strcmp (type, "xml") &&
1026 npr->which == Z_NamePlusRecord_databaseRecord)
1028 Z_External *r = (Z_External *) npr->u.databaseRecord;
1029 oident *ent = oid_getentbyoid(r->direct_reference);
1031 if (r->which == Z_External_sutrs)
1033 if (len) *len = r->u.sutrs->len;
1034 return (const char *) r->u.sutrs->buf;
1036 else if (r->which == Z_External_octet)
1045 case VAL_APPLICATION_XML:
1048 if (!rec->wrbuf_marc)
1049 rec->wrbuf_marc = wrbuf_alloc();
1050 wrbuf_rewind (rec->wrbuf_marc);
1051 if (yaz_marc_decode ((const char *)
1052 r->u.octet_aligned->buf,
1054 r->u.octet_aligned->len,
1057 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1058 return wrbuf_buf(rec->wrbuf_marc);
1061 if (len) *len = r->u.octet_aligned->len;
1062 return (const char *) r->u.octet_aligned->buf;
1064 else if (r->which == Z_External_grs1)
1071 else if (!strcmp (type, "raw"))
1073 if (npr->which == Z_NamePlusRecord_databaseRecord)
1075 Z_External *r = (Z_External *) npr->u.databaseRecord;
1077 if (r->which == Z_External_sutrs)
1079 if (len) *len = r->u.sutrs->len;
1080 return (const char *) r->u.sutrs->buf;
1082 else if (r->which == Z_External_octet)
1084 if (len) *len = r->u.octet_aligned->len;
1085 return (const char *) r->u.octet_aligned->buf;
1087 else /* grs-1, explain, ... */
1090 return (const char *) npr->u.databaseRecord;
1098 static void record_cache_add (ZOOM_resultset r,
1099 Z_NamePlusRecord *npr,
1101 const char *elementSetName)
1103 ZOOM_record_cache rc;
1105 for (rc = r->record_cache; rc; rc = rc->next)
1109 if ((!elementSetName && !rc->elementSetName)
1110 || (elementSetName && rc->elementSetName &&
1111 !strcmp (elementSetName, rc->elementSetName)))
1113 /* not destroying rc->npr (it's handled by nmem )*/
1115 /* keeping wrbuf_marc too */
1120 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1123 rc->rec.wrbuf_marc = 0;
1125 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1127 rc->elementSetName = 0;
1129 rc->next = r->record_cache;
1130 r->record_cache = rc;
1133 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
1135 const char *elementSetName)
1137 ZOOM_record_cache rc;
1139 for (rc = r->record_cache; rc; rc = rc->next)
1143 if ((!elementSetName && !rc->elementSetName)
1144 || (elementSetName && rc->elementSetName &&
1145 !strcmp (elementSetName, rc->elementSetName)))
1152 static void handle_records (ZOOM_connection c, Z_Records *sr,
1155 ZOOM_resultset resultset;
1159 switch (c->tasks->which)
1161 case ZOOM_TASK_SEARCH:
1162 resultset = c->tasks->u.search.resultset;
1164 case ZOOM_TASK_RETRIEVE:
1165 resultset = c->tasks->u.retrieve.resultset;
1170 if (sr && sr->which == Z_Records_NSD)
1172 Z_DiagRec dr, *dr_p = &dr;
1173 dr.which = Z_DiagRec_defaultFormat;
1174 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1176 response_diag (c, dr_p);
1178 else if (sr && sr->which == Z_Records_multipleNSD)
1180 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1181 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1183 c->error = ZOOM_ERROR_DECODE;
1187 if (resultset->count + resultset->start > resultset->size)
1188 resultset->count = resultset->size - resultset->start;
1189 if (resultset->count < 0)
1190 resultset->count = 0;
1191 if (sr && sr->which == Z_Records_DBOSD)
1194 NMEM nmem = odr_extract_mem (c->odr_in);
1195 Z_NamePlusRecordList *p =
1196 sr->u.databaseOrSurDiagnostics;
1197 for (i = 0; i<p->num_records; i++)
1199 record_cache_add (resultset, p->records[i],
1200 i+ resultset->start, 0);
1202 /* transfer our response to search_nmem .. we need it later */
1203 nmem_transfer (resultset->odr->mem, nmem);
1204 nmem_destroy (nmem);
1205 if (present_phase && p->num_records == 0)
1207 /* present response and we didn't get any records! */
1208 c->error = ZOOM_ERROR_DECODE;
1211 else if (present_phase)
1213 /* present response and we didn't get any records! */
1214 c->error = ZOOM_ERROR_DECODE;
1219 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1221 handle_records (c, pr->records, 1);
1224 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1226 ZOOM_resultset resultset;
1228 yaz_log (LOG_DEBUG, "got search response");
1230 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1233 resultset = c->tasks->u.search.resultset;
1235 resultset->size = *sr->resultCount;
1236 handle_records (c, sr->records, 0);
1239 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1241 if (res->diagnostics && res->num_diagnostics > 0)
1242 response_diag (c, res->diagnostics[0]);
1245 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1247 NMEM nmem = odr_extract_mem (c->odr_in);
1250 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1252 scan = c->tasks->u.scan.scan;
1254 if (res->entries && res->entries->nonsurrogateDiagnostics)
1255 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1256 scan->scan_response = res;
1257 nmem_transfer (scan->odr->mem, nmem);
1259 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1260 if (res->positionOfTerm)
1261 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1262 if (res->scanStatus)
1263 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1264 if (res->numberOfEntriesReturned)
1265 ZOOM_options_set_int (scan->options, "number",
1266 *res->numberOfEntriesReturned);
1267 nmem_destroy (nmem);
1271 static int send_sort (ZOOM_connection c)
1273 ZOOM_resultset resultset;
1275 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1278 resultset = c->tasks->u.search.resultset;
1282 resultset->r_sort_spec = 0;
1285 if (resultset->r_sort_spec)
1287 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1288 Z_SortRequest *req = apdu->u.sortRequest;
1290 req->num_inputResultSetNames = 1;
1291 req->inputResultSetNames = (Z_InternationalString **)
1292 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1293 req->inputResultSetNames[0] =
1294 odr_strdup (c->odr_out, resultset->setname);
1295 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1296 req->sortSequence = resultset->r_sort_spec;
1297 resultset->r_sort_spec = 0;
1298 send_APDU (c, apdu);
1304 static int send_present (ZOOM_connection c)
1306 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1307 Z_PresentRequest *req = apdu->u.presentRequest;
1309 const char *syntax =
1310 ZOOM_options_get (c->options, "preferredRecordSyntax");
1311 const char *element =
1312 ZOOM_options_get (c->options, "elementSetName");
1313 const char *schema =
1314 ZOOM_options_get (c->options, "schema");
1315 ZOOM_resultset resultset;
1320 switch (c->tasks->which)
1322 case ZOOM_TASK_SEARCH:
1323 resultset = c->tasks->u.search.resultset;
1325 case ZOOM_TASK_RETRIEVE:
1326 resultset = c->tasks->u.retrieve.resultset;
1327 resultset->start = c->tasks->u.retrieve.start;
1328 resultset->count = c->tasks->u.retrieve.count;
1330 if (resultset->start >= resultset->size)
1332 if (resultset->start + resultset->count > resultset->size)
1333 resultset->count = resultset->size - resultset->start;
1339 if (c->error) /* don't continue on error */
1341 if (resultset->start < 0)
1343 for (i = 0; i<resultset->count; i++)
1346 record_cache_lookup (resultset, i + resultset->start, 0);
1350 if (i == resultset->count)
1353 resultset->start += i;
1354 resultset->count -= i;
1355 *req->resultSetStartPoint = resultset->start + 1;
1356 *req->numberOfRecordsRequested = resultset->count;
1357 assert (*req->numberOfRecordsRequested > 0);
1359 if (syntax && *syntax)
1360 req->preferredRecordSyntax =
1361 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1363 if (schema && *schema)
1365 Z_RecordComposition *compo = (Z_RecordComposition *)
1366 odr_malloc (c->odr_out, sizeof(*compo));
1368 req->recordComposition = compo;
1369 compo->which = Z_RecordComp_complex;
1370 compo->u.complex = (Z_CompSpec *)
1371 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1372 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1373 odr_malloc(c->odr_out, sizeof(bool_t));
1374 *compo->u.complex->selectAlternativeSyntax = 0;
1376 compo->u.complex->generic = (Z_Specification *)
1377 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1379 compo->u.complex->generic->schema = (Odr_oid *)
1380 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1382 if (!compo->u.complex->generic->schema)
1384 /* OID wasn't a schema! Try record syntax instead. */
1386 compo->u.complex->generic->schema = (Odr_oid *)
1387 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1389 if (element && *element)
1391 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1392 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1393 compo->u.complex->generic->elementSpec->which =
1394 Z_ElementSpec_elementSetName;
1395 compo->u.complex->generic->elementSpec->u.elementSetName =
1396 odr_strdup (c->odr_out, element);
1399 compo->u.complex->generic->elementSpec = 0;
1400 compo->u.complex->num_dbSpecific = 0;
1401 compo->u.complex->dbSpecific = 0;
1402 compo->u.complex->num_recordSyntax = 0;
1403 compo->u.complex->recordSyntax = 0;
1405 else if (element && *element)
1407 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1408 odr_malloc (c->odr_out, sizeof(*esn));
1409 Z_RecordComposition *compo = (Z_RecordComposition *)
1410 odr_malloc (c->odr_out, sizeof(*compo));
1412 esn->which = Z_ElementSetNames_generic;
1413 esn->u.generic = odr_strdup (c->odr_out, element);
1414 compo->which = Z_RecordComp_simple;
1415 compo->u.simple = esn;
1416 req->recordComposition = compo;
1418 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1419 send_APDU (c, apdu);
1423 ZOOM_API(ZOOM_scanset)
1424 ZOOM_connection_scan (ZOOM_connection c, const char *start)
1426 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1428 scan->connection = c;
1429 scan->odr = odr_createmem (ODR_DECODE);
1430 scan->options = ZOOM_options_create_with_parent (c->options);
1432 scan->scan_response = 0;
1434 if ((scan->termListAndStartPoint =
1435 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1438 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1439 task->u.scan.scan = scan;
1444 while (ZOOM_event (1, &c))
1452 ZOOM_scanset_destroy (ZOOM_scanset scan)
1457 if (scan->refcount == 0)
1459 odr_destroy (scan->odr);
1461 ZOOM_options_destroy (scan->options);
1466 static int send_scan (ZOOM_connection c)
1469 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1470 Z_ScanRequest *req = apdu->u.scanRequest;
1473 assert (c->tasks->which == ZOOM_TASK_SCAN);
1474 scan = c->tasks->u.scan.scan;
1476 req->termListAndStartPoint = scan->termListAndStartPoint;
1477 req->attributeSet = scan->attributeSet;
1479 *req->numberOfTermsRequested =
1480 ZOOM_options_get_int(scan->options, "number", 10);
1482 req->preferredPositionInResponse =
1483 odr_intdup (c->odr_out,
1484 ZOOM_options_get_int(scan->options, "position", 1));
1487 odr_intdup (c->odr_out,
1488 ZOOM_options_get_int(scan->options, "stepSize", 0));
1490 req->databaseNames = set_DatabaseNames (c, scan->options,
1491 &req->num_databaseNames);
1493 send_APDU (c, apdu);
1499 ZOOM_scanset_size (ZOOM_scanset scan)
1501 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1503 return scan->scan_response->entries->num_entries;
1506 ZOOM_API(const char *)
1507 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1510 const char *term = 0;
1511 size_t noent = ZOOM_scanset_size (scan);
1512 Z_ScanResponse *res = scan->scan_response;
1518 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1520 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1522 if (t->term->which == Z_Term_general)
1524 term = (const char *) t->term->u.general->buf;
1525 *len = t->term->u.general->len;
1527 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1532 ZOOM_API(const char *)
1533 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1535 return ZOOM_options_get (scan->options, key);
1539 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1542 ZOOM_options_set (scan->options, key, val);
1545 static int ZOOM_connection_exec_task (ZOOM_connection c)
1547 ZOOM_task task = c->tasks;
1549 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1552 if (c->error != ZOOM_ERROR_NONE ||
1553 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1555 ZOOM_connection_remove_tasks (c);
1558 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d", task->which);
1562 switch (task->which)
1564 case ZOOM_TASK_SEARCH:
1565 /* see if search hasn't been sent yet. */
1566 if (ZOOM_connection_send_search (c))
1569 case ZOOM_TASK_RETRIEVE:
1570 if (send_present (c))
1573 case ZOOM_TASK_CONNECT:
1577 case ZOOM_TASK_SCAN:
1581 ZOOM_connection_remove_task (c);
1585 static int send_sort_present (ZOOM_connection c)
1587 int r = send_sort (c);
1589 r = send_present (c);
1593 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1595 Z_InitResponse *initrs;
1597 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1601 case Z_APDU_initResponse:
1602 initrs = apdu->u.initResponse;
1603 if (!*initrs->result)
1605 c->error = ZOOM_ERROR_INIT;
1610 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1612 xfree (c->cookie_in);
1615 c->cookie_in = xstrdup(cookie);
1616 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1617 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1618 c->support_named_resultsets = 1;
1621 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1622 ZOOM_connection_remove_task (c);
1624 ZOOM_connection_exec_task (c);
1626 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
1628 NMEM tmpmem = nmem_create();
1629 Z_CharSetandLanguageNegotiation *p =
1630 yaz_get_charneg_record(initrs->otherInfo);
1634 char *charset=NULL, *lang=NULL;
1637 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &selected);
1638 yaz_log(LOG_DEBUG, "Target accepted: charset - %s, language - %s, select - %d",
1639 charset, lang, selected);
1641 nmem_destroy(tmpmem);
1645 case Z_APDU_searchResponse:
1646 handle_search_response (c, apdu->u.searchResponse);
1647 if (!send_sort_present (c))
1648 ZOOM_connection_remove_task (c);
1650 case Z_APDU_presentResponse:
1651 handle_present_response (c, apdu->u.presentResponse);
1652 if (!send_present (c))
1653 ZOOM_connection_remove_task (c);
1655 case Z_APDU_sortResponse:
1656 sort_response (c, apdu->u.sortResponse);
1657 if (!send_present (c))
1658 ZOOM_connection_remove_task (c);
1660 case Z_APDU_scanResponse:
1661 scan_response (c, apdu->u.scanResponse);
1662 ZOOM_connection_remove_task (c);
1666 static int do_read (ZOOM_connection c)
1672 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
1673 ZOOM_connection_put_event (c, event);
1675 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1680 c->error= ZOOM_ERROR_CONNECTION_LOST;
1686 odr_reset (c->odr_in);
1687 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1688 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
1689 ZOOM_connection_put_event (c, event);
1690 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1692 c->error = ZOOM_ERROR_DECODE;
1697 handle_apdu (c, apdu);
1703 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
1708 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1709 ZOOM_connection_put_event (c, event);
1711 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1713 if (c->state == STATE_CONNECTING)
1714 c->error = ZOOM_ERROR_CONNECT;
1716 c->error = ZOOM_ERROR_CONNECTION_LOST;
1722 c->mask = ZOOM_SELECT_EXCEPT;
1723 if (c->cs->io_pending & CS_WANT_WRITE)
1724 c->mask += ZOOM_SELECT_WRITE;
1725 if (c->cs->io_pending & CS_WANT_READ)
1726 c->mask += ZOOM_SELECT_READ;
1727 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
1731 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
1732 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
1737 static int do_write(ZOOM_connection c)
1739 return do_write_ex (c, c->buf_out, c->len_out);
1743 ZOOM_API(const char *)
1744 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
1746 return ZOOM_options_get (c->options, key);
1750 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
1753 ZOOM_options_set (c->options, key, val);
1756 ZOOM_API(const char *)
1757 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
1759 return ZOOM_options_get (r->options, key);
1763 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
1766 ZOOM_options_set (r->options, key, val);
1771 ZOOM_connection_errcode (ZOOM_connection c)
1773 return ZOOM_connection_error (c, 0, 0);
1776 ZOOM_API(const char *)
1777 ZOOM_connection_errmsg (ZOOM_connection c)
1780 ZOOM_connection_error (c, &msg, 0);
1784 ZOOM_API(const char *)
1785 ZOOM_connection_addinfo (ZOOM_connection c)
1787 const char *addinfo;
1788 ZOOM_connection_error (c, 0, &addinfo);
1793 ZOOM_connection_error (ZOOM_connection c, const char **cp,
1794 const char **addinfo)
1796 int error = c->error;
1801 case ZOOM_ERROR_NONE:
1802 *cp = "No error"; break;
1803 case ZOOM_ERROR_CONNECT:
1804 *cp = "Connect failed"; break;
1805 case ZOOM_ERROR_MEMORY:
1806 *cp = "Out of memory"; break;
1807 case ZOOM_ERROR_ENCODE:
1808 *cp = "Encoding failed"; break;
1809 case ZOOM_ERROR_DECODE:
1810 *cp = "Decoding failed"; break;
1811 case ZOOM_ERROR_CONNECTION_LOST:
1812 *cp = "Connection lost"; break;
1813 case ZOOM_ERROR_INIT:
1814 *cp = "Init rejected"; break;
1815 case ZOOM_ERROR_INTERNAL:
1816 *cp = "Internal failure"; break;
1817 case ZOOM_ERROR_TIMEOUT:
1818 *cp = "Timeout"; break;
1820 *cp = diagbib1_str (error);
1826 *addinfo = c->addinfo;
1833 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
1835 ZOOM_Event event = 0;
1836 int r = cs_look(c->cs);
1837 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
1842 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
1843 c->error = ZOOM_ERROR_CONNECT;
1845 ZOOM_connection_put_event (c, event);
1847 else if (r == CS_CONNECT)
1850 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
1852 ret = cs_rcvconnect (c->cs);
1853 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
1856 c->mask = ZOOM_SELECT_EXCEPT;
1857 if (c->cs->io_pending & CS_WANT_WRITE)
1858 c->mask += ZOOM_SELECT_WRITE;
1859 if (c->cs->io_pending & CS_WANT_READ)
1860 c->mask += ZOOM_SELECT_READ;
1861 ZOOM_connection_put_event (c, event);
1865 ZOOM_connection_put_event (c, event);
1866 ZOOM_connection_send_init (c);
1867 c->state = STATE_ESTABLISHED;
1871 c->error = ZOOM_ERROR_CONNECT;
1873 ZOOM_connection_put_event (c, event);
1878 if (mask & ZOOM_SELECT_READ)
1880 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1887 ZOOM_connection_last_event(ZOOM_connection cs)
1890 return ZOOM_EVENT_NONE;
1891 return cs->last_event;
1895 ZOOM_event (int no, ZOOM_connection *cs)
1898 struct pollfd pollfds[1024];
1899 ZOOM_connection poll_cs[1024];
1902 fd_set input, output, except;
1907 for (i = 0; i<no; i++)
1909 ZOOM_connection c = cs[i];
1911 if (c && (event = ZOOM_connection_get_event(c)))
1913 ZOOM_Event_destroy (event);
1917 for (i = 0; i<no; i++)
1919 ZOOM_connection c = cs[i];
1921 if (c && ZOOM_connection_exec_task (c))
1923 if ((event = ZOOM_connection_get_event(c)))
1925 ZOOM_Event_destroy (event);
1941 for (i = 0; i<no; i++)
1943 ZOOM_connection c = cs[i];
1948 fd = z3950_connection_socket(c);
1949 mask = z3950_connection_mask(c);
1959 short poll_events = 0;
1961 if (mask & ZOOM_SELECT_READ)
1962 poll_events += POLLIN;
1963 if (mask & ZOOM_SELECT_WRITE)
1964 poll_events += POLLOUT;
1965 if (mask & ZOOM_SELECT_EXCEPT)
1966 poll_events += POLLERR;
1967 pollfds[nfds].fd = fd;
1968 pollfds[nfds].events = poll_events;
1969 pollfds[nfds].revents = 0;
1974 if (mask & ZOOM_SELECT_READ)
1976 FD_SET (fd, &input);
1979 if (mask & ZOOM_SELECT_WRITE)
1981 FD_SET (fd, &output);
1984 if (mask & ZOOM_SELECT_EXCEPT)
1986 FD_SET (fd, &except);
1994 r = poll (pollfds, nfds, 15000);
1995 for (i = 0; i<nfds; i++)
1997 ZOOM_connection c = poll_cs[i];
2001 if (pollfds[i].revents & POLLIN)
2002 mask += ZOOM_SELECT_READ;
2003 if (pollfds[i].revents & POLLOUT)
2004 mask += ZOOM_SELECT_WRITE;
2005 if (pollfds[i].revents & POLLERR)
2006 mask += ZOOM_SELECT_EXCEPT;
2008 ZOOM_connection_do_io(c, mask);
2010 else if (r == 0 && c->mask)
2012 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2013 /* timeout and this connection was waiting */
2014 c->error = ZOOM_ERROR_TIMEOUT;
2016 ZOOM_connection_put_event(c, event);
2020 yaz_log (LOG_DEBUG, "select start");
2021 r = select (max_fd+1, &input, &output, &except, &tv);
2022 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
2023 for (i = 0; i<no; i++)
2025 ZOOM_connection c = cs[i];
2030 fd = z3950_connection_socket(c);
2034 /* no timeout and real socket */
2035 if (FD_ISSET(fd, &input))
2036 mask += ZOOM_SELECT_READ;
2037 if (FD_ISSET(fd, &output))
2038 mask += ZOOM_SELECT_WRITE;
2039 if (FD_ISSET(fd, &except))
2040 mask += ZOOM_SELECT_EXCEPT;
2042 ZOOM_connection_do_io(c, mask);
2044 if (r == 0 && c->mask)
2046 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2047 /* timeout and this connection was waiting */
2048 c->error = ZOOM_ERROR_TIMEOUT;
2050 yaz_log (LOG_DEBUG, "timeout");
2051 ZOOM_connection_put_event(c, event);
2055 for (i = 0; i<no; i++)
2057 ZOOM_connection c = cs[i];
2059 if (c && (event = ZOOM_connection_get_event(c)))
2061 ZOOM_Event_destroy (event);