1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ZOOM C interface.
18 #include <yaz/yaz-util.h>
19 #include <yaz/xmalloc.h>
20 #include <yaz/otherinfo.h>
22 #include <yaz/diagbib1.h>
23 #include <yaz/charneg.h>
24 #include <yaz/query-charset.h>
25 #include <yaz/snprintf.h>
26 #include <yaz/facet.h>
28 #include <yaz/shptr.h>
34 static int log_api0 = 0;
35 static int log_details0 = 0;
37 static void resultset_destroy(ZOOM_resultset r);
38 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out);
40 static void initlog(void)
42 static int log_level_initialized = 0;
44 if (!log_level_initialized)
46 log_api0 = yaz_log_module_level("zoom");
47 log_details0 = yaz_log_module_level("zoomdetails");
48 log_level_initialized = 1;
52 void ZOOM_connection_remove_tasks(ZOOM_connection c);
54 void ZOOM_set_dset_error(ZOOM_connection c, int error,
56 const char *addinfo, const char *addinfo2)
63 if (!c->diagset || strcmp(dset, c->diagset))
66 c->diagset = xstrdup(dset);
67 /* remove integer part from SRW diagset .. */
68 if ((cp = strrchr(c->diagset, '/')))
71 if (addinfo && addinfo2)
73 c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
74 strcpy(c->addinfo, addinfo);
75 strcat(c->addinfo, addinfo2);
78 c->addinfo = xstrdup(addinfo);
79 if (error != ZOOM_ERROR_NONE)
81 yaz_log(c->log_api, "%p set_dset_error %s %s:%d %s %s",
82 c, c->host_port ? c->host_port : "<>", dset, error,
83 addinfo ? addinfo : "",
84 addinfo2 ? addinfo2 : "");
85 ZOOM_connection_remove_tasks(c);
89 int ZOOM_uri_to_code(const char *uri)
93 if ((cp = strrchr(uri, '/')))
100 void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo)
102 ZOOM_set_dset_error(c, error, "ZOOM", addinfo, 0);
105 static void clear_error(ZOOM_connection c)
108 * If an error is tied to an operation then it's ok to clear: for
109 * example, a diagnostic returned from a search is cleared by a
110 * subsequent search. However, problems such as Connection Lost
111 * or Init Refused are not cleared, because they are not
112 * recoverable: doing another search doesn't help.
115 ZOOM_connection_remove_events(c);
118 case ZOOM_ERROR_CONNECT:
119 case ZOOM_ERROR_MEMORY:
120 case ZOOM_ERROR_DECODE:
121 case ZOOM_ERROR_CONNECTION_LOST:
122 case ZOOM_ERROR_INIT:
123 case ZOOM_ERROR_INTERNAL:
124 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
127 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
131 void ZOOM_connection_show_task(ZOOM_task task)
135 case ZOOM_TASK_SEARCH:
136 yaz_log(YLOG_LOG, "search p=%p", task);
138 case ZOOM_TASK_RETRIEVE:
139 yaz_log(YLOG_LOG, "retrieve p=%p", task);
141 case ZOOM_TASK_CONNECT:
142 yaz_log(YLOG_LOG, "connect p=%p", task);
145 yaz_log(YLOG_LOG, "scan p=%p", task);
150 void ZOOM_connection_show_tasks(ZOOM_connection c)
153 yaz_log(YLOG_LOG, "connection p=%p tasks", c);
154 for (task = c->tasks; task; task = task->next)
155 ZOOM_connection_show_task(task);
158 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
160 ZOOM_task *taskp = &c->tasks;
162 taskp = &(*taskp)->next;
163 *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
164 (*taskp)->running = 0;
165 (*taskp)->which = which;
171 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
173 return c->tasks ? 0 : 1;
176 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
178 ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
180 task->next = c->tasks;
188 void ZOOM_connection_remove_task(ZOOM_connection c)
190 ZOOM_task task = c->tasks;
194 c->tasks = task->next;
197 case ZOOM_TASK_SEARCH:
198 resultset_destroy(task->u.search.resultset);
199 xfree(task->u.search.syntax);
200 xfree(task->u.search.elementSetName);
202 case ZOOM_TASK_RETRIEVE:
203 resultset_destroy(task->u.retrieve.resultset);
204 xfree(task->u.retrieve.syntax);
205 xfree(task->u.retrieve.elementSetName);
207 case ZOOM_TASK_CONNECT:
210 ZOOM_scanset_destroy(task->u.scan.scan);
212 case ZOOM_TASK_PACKAGE:
213 ZOOM_package_destroy(task->u.package);
216 resultset_destroy(task->u.sort.resultset);
217 ZOOM_query_destroy(task->u.sort.q);
226 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
227 ZOOM_connection_put_event(c, event);
232 void ZOOM_connection_remove_tasks(ZOOM_connection c)
235 ZOOM_connection_remove_task(c);
239 ZOOM_API(ZOOM_connection)
240 ZOOM_connection_create(ZOOM_options options)
242 ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
246 c->log_api = log_api0;
247 c->log_details = log_details0;
249 yaz_log(c->log_api, "%p ZOOM_connection_create", c);
251 c->proto = PROTO_Z3950;
253 ZOOM_connection_set_mask(c, 0);
255 c->state = STATE_IDLE;
258 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
265 c->options = ZOOM_options_create_with_parent(options);
270 c->charset = c->lang = 0;
281 c->maximum_record_size = 0;
282 c->preferred_message_size = 0;
284 c->odr_in = odr_createmem(ODR_DECODE);
285 c->odr_out = odr_createmem(ODR_ENCODE);
289 c->support_named_resultsets = 0;
290 c->last_event = ZOOM_EVENT_NONE;
292 c->m_queue_front = 0;
301 /* set database names. Take local databases (if set); otherwise
302 take databases given in ZURL (if set); otherwise use Default */
303 char **ZOOM_connection_get_databases(ZOOM_connection con, ZOOM_options options,
306 char **databaseNames;
307 const char *cp = ZOOM_options_get(options, "databaseName");
309 if ((!cp || !*cp) && con->host_port)
311 if (strncmp(con->host_port, "unix:", 5) == 0)
312 cp = strchr(con->host_port+5, ':');
314 cp = strchr(con->host_port, '/');
320 nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
321 return databaseNames;
324 ZOOM_API(ZOOM_connection)
325 ZOOM_connection_new(const char *host, int portnum)
327 ZOOM_connection c = ZOOM_connection_create(0);
329 ZOOM_connection_connect(c, host, portnum);
333 static zoom_sru_mode get_sru_mode_from_string(const char *s)
336 return zoom_sru_soap;
337 if (!yaz_matchstr(s, "soap"))
338 return zoom_sru_soap;
339 else if (!yaz_matchstr(s, "get"))
341 else if (!yaz_matchstr(s, "post"))
342 return zoom_sru_post;
343 else if (!yaz_matchstr(s, "solr"))
344 return zoom_sru_solr;
345 return zoom_sru_error;
349 ZOOM_connection_connect(ZOOM_connection c,
350 const char *host, int portnum)
356 yaz_log(c->log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
357 c, host ? host : "null", portnum);
359 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
360 ZOOM_connection_remove_tasks(c);
364 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
365 odr_destroy(c->odr_print);
367 if (ZOOM_options_get_bool(c->options, "apdulog", 0))
369 c->odr_print = odr_createmem(ODR_PRINT);
370 odr_setprint(c->odr_print, yaz_log_file());
377 yaz_log(c->log_details, "%p ZOOM_connection_connect reconnect ok", c);
381 yaz_log(c->log_details, "%p ZOOM_connection_connect connect", c);
384 val = ZOOM_options_get(c->options, "proxy");
387 yaz_log(c->log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
388 c->proxy = xstrdup(val);
393 val = ZOOM_options_get(c->options, "charset");
396 yaz_log(c->log_details, "%p ZOOM_connection_connect charset=%s", c, val);
397 c->charset = xstrdup(val);
401 val = ZOOM_options_get(c->options, "lang");
404 yaz_log(c->log_details, "%p ZOOM_connection_connect lang=%s", c, val);
405 c->lang = xstrdup(val);
416 sprintf(hostn, "%.80s:%d", host, portnum);
417 c->host_port = xstrdup(hostn);
420 c->host_port = xstrdup(host);
425 * If the "<scheme>:" part of the host string is preceded by one
426 * or more comma-separated <name>=<value> pairs, these are taken
427 * to be options to be set on the connection object. Among other
428 * applications, this facility can be used to embed authentication
430 * user=admin,password=secret,tcp:localhost:9999
432 char *remainder = c->host_port;
433 char *pcolon = strchr(remainder, ':');
436 while ((pcomma = strchr(remainder, ',')) != 0 &&
437 (pcolon == 0 || pcomma < pcolon)) {
439 if ((pequals = strchr(remainder, '=')) != 0) {
441 /*printf("# setting '%s'='%s'\n", remainder, pequals+1);*/
442 ZOOM_connection_option_set(c, remainder, pequals+1);
444 remainder = pcomma+1;
447 if (remainder != c->host_port) {
449 c->host_port = xstrdup(remainder);
450 /*printf("# reset hp='%s'\n", remainder);*/
454 val = ZOOM_options_get(c->options, "sru");
455 c->sru_mode = get_sru_mode_from_string(val);
457 xfree(c->sru_version);
458 val = ZOOM_options_get(c->options, "sru_version");
459 c->sru_version = xstrdup(val ? val : "1.2");
461 ZOOM_options_set(c->options, "host", c->host_port);
463 xfree(c->cookie_out);
465 val = ZOOM_options_get(c->options, "cookie");
468 yaz_log(c->log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
469 c->cookie_out = xstrdup(val);
474 val = ZOOM_options_get(c->options, "clientIP");
477 yaz_log(c->log_details, "%p ZOOM_connection_connect clientIP=%s",
479 c->client_IP = xstrdup(val);
484 val = ZOOM_options_get(c->options, "group");
486 c->group = xstrdup(val);
490 val = ZOOM_options_get(c->options, "user");
492 c->user = xstrdup(val);
496 val = ZOOM_options_get(c->options, "password");
498 val = ZOOM_options_get(c->options, "pass");
501 c->password = xstrdup(val);
503 c->maximum_record_size =
504 ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
505 c->preferred_message_size =
506 ZOOM_options_get_int(c->options, "preferredMessageSize", 1024*1024);
508 c->async = ZOOM_options_get_bool(c->options, "async", 0);
509 yaz_log(c->log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
511 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
515 while (ZOOM_event(1, &c))
520 ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
522 #if ZOOM_RESULT_LISTS
526 /* remove ourselves from the resultsets in connection */
527 ZOOM_resultset *rp = &r->connection->resultsets;
530 assert(*rp); /* we must be in this list!! */
532 { /* OK, we're here - take us out of it */
544 ZOOM_connection_destroy(ZOOM_connection c)
546 #if ZOOM_RESULT_LISTS
547 ZOOM_resultsets list;
553 yaz_log(c->log_api, "%p ZOOM_connection_destroy", c);
557 #if ZOOM_RESULT_LISTS
558 /* Remove the connection's usage of resultsets */
559 list = c->resultsets;
561 ZOOM_resultsets removed = list;
562 ZOOM_resultset_destroy(list->resultset);
567 for (r = c->resultsets; r; r = r->next)
574 odr_destroy(c->odr_in);
575 odr_destroy(c->odr_out);
578 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
579 odr_destroy(c->odr_print);
581 ZOOM_options_destroy(c->options);
582 ZOOM_connection_remove_tasks(c);
583 ZOOM_connection_remove_events(c);
588 xfree(c->cookie_out);
594 xfree(c->sru_version);
598 void ZOOM_resultset_addref(ZOOM_resultset r)
602 yaz_mutex_enter(r->mutex);
604 yaz_log(log_details0, "%p ZOOM_resultset_addref count=%d",
606 yaz_mutex_leave(r->mutex);
610 static int g_resultsets = 0;
611 static YAZ_MUTEX g_resultset_mutex = 0;
613 /* TODO We need to initialize this before running threaded:
614 * call resultset_use(0) */
616 static int resultset_use(int delta) {
618 if (g_resultset_mutex == 0)
619 yaz_mutex_create(&g_resultset_mutex);
620 yaz_mutex_enter(g_resultset_mutex);
621 g_resultsets += delta;
622 resultset_count = g_resultsets;
623 yaz_mutex_leave(g_resultset_mutex);
624 return resultset_count;
627 int resultsets_count(void) {
628 return resultset_use(0);
631 ZOOM_resultset ZOOM_resultset_create(void)
634 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
638 yaz_log(log_details0, "%p ZOOM_resultset_create", r);
641 r->odr = odr_createmem(ODR_ENCODE);
646 for (i = 0; i<RECORD_HASH_SIZE; i++)
647 r->record_hash[i] = 0;
651 r->databaseNames = 0;
652 r->num_databaseNames = 0;
657 yaz_mutex_create(&r->mutex);
660 WRBUF w = wrbuf_alloc();
661 YAZ_SHPTR_INIT(r->record_wrbuf, w);
668 ZOOM_API(ZOOM_resultset)
669 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
672 ZOOM_query s = ZOOM_query_create();
674 ZOOM_query_prefix(s, q);
676 r = ZOOM_connection_search(c, s);
677 ZOOM_query_destroy(s);
681 ZOOM_API(ZOOM_resultset)
682 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
684 ZOOM_resultset r = ZOOM_resultset_create();
688 const char *syntax, *elementSetName;
689 #if ZOOM_RESULT_LISTS
693 yaz_log(c->log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
694 r->r_sort_spec = ZOOM_query_get_sortspec(q);
697 r->options = ZOOM_options_create_with_parent(c->options);
699 start = ZOOM_options_get_int(r->options, "start", 0);
700 count = ZOOM_options_get_int(r->options, "count", 0);
702 /* If "presentChunk" is defined use that; otherwise "step" */
703 const char *cp = ZOOM_options_get(r->options, "presentChunk");
704 r->step = ZOOM_options_get_int(r->options,
705 (cp != 0 ? "presentChunk": "step"), 0);
707 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
708 cp = ZOOM_options_get(r->options, "setname");
710 r->setname = xstrdup(cp);
711 cp = ZOOM_options_get(r->options, "schema");
713 r->schema = xstrdup(cp);
715 r->databaseNames = ZOOM_connection_get_databases(c, c->options, &r->num_databaseNames,
720 #if ZOOM_RESULT_LISTS
721 yaz_log(log_details, "%p ZOOM_connection_search: Adding new resultset (%p) to resultsets (%p) ", c, r, c->resultsets);
722 set = xmalloc(sizeof(*set));
723 ZOOM_resultset_addref(r);
725 set->next = c->resultsets;
728 r->next = c->resultsets;
731 if (c->host_port && c->proto == PROTO_HTTP)
735 yaz_log(c->log_details, "ZOOM_connection_search: no comstack");
736 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
740 yaz_log(c->log_details, "ZOOM_connection_search: reconnect");
745 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
746 task->u.search.resultset = r;
747 task->u.search.start = start;
748 task->u.search.count = count;
749 task->u.search.recv_search_fired = 0;
751 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
752 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
753 elementSetName = ZOOM_options_get(r->options, "elementSetName");
754 task->u.search.elementSetName = elementSetName
755 ? xstrdup(elementSetName) : 0;
757 ZOOM_resultset_addref(r);
759 ZOOM_query_addref(q);
763 while (ZOOM_event(1, &c))
770 ZOOM_resultset_sort(ZOOM_resultset r,
771 const char *sort_type, const char *sort_spec)
773 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
777 ZOOM_resultset_sort1(ZOOM_resultset r,
778 const char *sort_type, const char *sort_spec)
780 ZOOM_connection c = r->connection;
784 newq = ZOOM_query_create();
785 if (ZOOM_query_sortby(newq, sort_spec) < 0)
788 yaz_log(c->log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
789 r, r, sort_type, sort_spec);
793 if (c->host_port && c->proto == PROTO_HTTP)
797 yaz_log(c->log_details, "%p ZOOM_resultset_sort: no comstack", r);
798 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
802 yaz_log(c->log_details, "%p ZOOM_resultset_sort: prepare reconnect",
808 ZOOM_resultset_cache_reset(r);
809 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
810 task->u.sort.resultset = r;
811 task->u.sort.q = newq;
813 ZOOM_resultset_addref(r);
817 while (ZOOM_event(1, &c))
825 ZOOM_resultset_destroy(ZOOM_resultset r)
827 resultset_destroy(r);
830 static void resultset_destroy(ZOOM_resultset r)
834 yaz_mutex_enter(r->mutex);
836 yaz_log(log_details0, "%p ZOOM_resultset_destroy r=%p count=%d",
838 if (r->refcount == 0)
840 yaz_mutex_leave(r->mutex);
842 yaz_log(log_details0, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r);
843 ZOOM_resultset_cache_reset(r);
844 ZOOM_resultset_release(r);
845 ZOOM_query_destroy(r->query);
846 ZOOM_options_destroy(r->options);
850 yaz_mutex_destroy(&r->mutex);
852 YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
858 yaz_mutex_leave(r->mutex);
862 ZOOM_resultset_size(ZOOM_resultset r)
867 int ZOOM_test_reconnect(ZOOM_connection c)
871 if (!c->reconnect_ok)
873 ZOOM_connection_close(c);
875 c->tasks->running = 0;
876 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
878 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
879 ZOOM_connection_put_event(c, event);
884 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
885 int force_sync, int start, int count)
890 const char *syntax, *elementSetName;
894 yaz_log(log_details0, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
895 " count=%d", r, force_sync, start, count);
900 if (c->host_port && c->proto == PROTO_HTTP)
904 yaz_log(log_details0, "%p ZOOM_resultset_retrieve: no comstack", r);
905 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
909 yaz_log(log_details0, "%p ZOOM_resultset_retrieve: prepare "
914 task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
915 task->u.retrieve.resultset = r;
916 task->u.retrieve.start = start;
917 task->u.retrieve.count = count;
919 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
920 task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
921 elementSetName = ZOOM_options_get(r->options, "elementSetName");
922 task->u.retrieve.elementSetName = elementSetName
923 ? xstrdup(elementSetName) : 0;
925 cp = ZOOM_options_get(r->options, "schema");
928 if (!r->schema || strcmp(r->schema, cp))
931 r->schema = xstrdup(cp);
935 ZOOM_resultset_addref(r);
937 if (!r->connection->async || force_sync)
938 while (r->connection && ZOOM_event(1, &r->connection))
943 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
944 size_t start, size_t count)
946 int force_present = 0;
950 yaz_log(log_api0, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
951 r, r, (long) start, (long) count);
954 ZOOM_resultset_retrieve(r, force_present, start, count);
958 for (i = 0; i< count; i++)
959 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
964 ZOOM_resultset_facets_size(ZOOM_resultset r) {
965 return r->num_facets;
968 ZOOM_API(ZOOM_facet_field)
969 ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name) {
970 int num = r->num_facets;
971 ZOOM_facet_field *facets = r->facets;
973 for (index = 0; index < num; index++) {
974 if (!strcmp(facets[index]->facet_name, name)) {
975 return facets[index];
982 ZOOM_API(ZOOM_facet_field *)
983 ZOOM_resultset_facets(ZOOM_resultset r)
988 ZOOM_API(const char**)
989 ZOOM_resultset_facet_names(ZOOM_resultset r)
991 return (const char **) r->facets_names;
994 ZOOM_API(const char*)
995 ZOOM_facet_field_name(ZOOM_facet_field field)
997 return field->facet_name;
1001 ZOOM_facet_field_term_count(ZOOM_facet_field field)
1003 return field->num_terms;
1006 ZOOM_API(const char*)
1007 ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq) {
1008 *freq = field->facet_terms[idx].frequency;
1009 return field->facet_terms[idx].term;
1013 static void get_cert(ZOOM_connection c)
1018 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1020 ZOOM_connection_option_setl(c, "sslPeerCert",
1021 cert_buf, cert_len);
1026 static zoom_ret do_connect_host(ZOOM_connection c,
1027 const char *logical_url);
1029 static zoom_ret do_connect(ZOOM_connection c)
1031 return do_connect_host(c, c->host_port);
1034 static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
1040 c->cs = cs_create_host_proxy(logical_url, 0, &add, c->proxy);
1042 if (c->cs && c->cs->protocol == PROTO_HTTP)
1045 c->proto = PROTO_HTTP;
1047 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1048 ZOOM_connection_close(c);
1049 return zoom_complete;
1054 int ret = cs_connect(c->cs, add);
1057 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1058 ZOOM_connection_put_event(c, event);
1060 if (c->proto == PROTO_Z3950)
1061 ZOOM_connection_Z3950_send_init(c);
1064 /* no init request for SRW .. */
1065 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1066 ZOOM_connection_remove_task(c);
1067 ZOOM_connection_set_mask(c, 0);
1068 ZOOM_connection_exec_task(c);
1070 c->state = STATE_ESTABLISHED;
1071 return zoom_pending;
1075 int mask = ZOOM_SELECT_EXCEPT;
1076 if (c->cs->io_pending & CS_WANT_WRITE)
1077 mask += ZOOM_SELECT_WRITE;
1078 if (c->cs->io_pending & CS_WANT_READ)
1079 mask += ZOOM_SELECT_READ;
1080 ZOOM_connection_set_mask(c, mask);
1081 c->state = STATE_CONNECTING;
1082 return zoom_pending;
1085 c->state = STATE_IDLE;
1086 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, logical_url);
1087 return zoom_complete;
1090 /* returns 1 if PDU was sent OK (still pending )
1091 0 if PDU was not sent OK (nothing to wait for)
1094 ZOOM_API(ZOOM_record)
1095 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1097 const char *syntax =
1098 ZOOM_options_get(s->options, "preferredRecordSyntax");
1099 const char *elementSetName =
1100 ZOOM_options_get(s->options, "elementSetName");
1102 return ZOOM_record_cache_lookup(s, pos, syntax, elementSetName);
1105 ZOOM_API(ZOOM_record)
1106 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1108 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1113 * MIKE: I think force_sync should always be zero, but I don't
1114 * want to make this change until I get the go-ahead from
1115 * Adam, in case something depends on the old synchronous
1119 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1120 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1121 rec = ZOOM_resultset_record_immediate(r, pos);
1126 ZOOM_API(ZOOM_scanset)
1127 ZOOM_connection_scan(ZOOM_connection c, const char *start)
1130 ZOOM_query q = ZOOM_query_create();
1132 ZOOM_query_prefix(q, start);
1134 s = ZOOM_connection_scan1(c, q);
1135 ZOOM_query_destroy(q);
1140 ZOOM_API(ZOOM_scanset)
1141 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
1143 ZOOM_scanset scan = 0;
1144 Z_Query *z_query = ZOOM_query_get_Z_Query(q);
1148 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
1149 scan->connection = c;
1150 scan->odr = odr_createmem(ODR_DECODE);
1151 scan->options = ZOOM_options_create_with_parent(c->options);
1153 scan->scan_response = 0;
1154 scan->srw_scan_response = 0;
1157 ZOOM_query_addref(q);
1158 scan->databaseNames = ZOOM_connection_get_databases(c, c->options,
1159 &scan->num_databaseNames,
1164 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
1165 task->u.scan.scan = scan;
1170 while (ZOOM_event(1, &c))
1178 ZOOM_scanset_destroy(ZOOM_scanset scan)
1183 if (scan->refcount == 0)
1185 ZOOM_query_destroy(scan->query);
1187 odr_destroy(scan->odr);
1189 ZOOM_options_destroy(scan->options);
1194 static zoom_ret send_package(ZOOM_connection c)
1198 yaz_log(c->log_details, "%p send_package", c);
1200 return zoom_complete;
1201 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1203 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1204 ZOOM_connection_put_event(c, event);
1206 c->buf_out = c->tasks->u.package->buf_out;
1207 c->len_out = c->tasks->u.package->len_out;
1209 return ZOOM_send_buf(c);
1213 ZOOM_scanset_size(ZOOM_scanset scan)
1218 if (scan->scan_response && scan->scan_response->entries)
1219 return scan->scan_response->entries->num_entries;
1220 else if (scan->srw_scan_response)
1221 return scan->srw_scan_response->num_terms;
1225 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
1227 const char **value_term, size_t *value_len,
1228 const char **disp_term, size_t *disp_len)
1230 size_t noent = ZOOM_scanset_size(scan);
1241 if (scan->scan_response)
1243 Z_ScanResponse *res = scan->scan_response;
1244 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1246 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1248 *value_term = (const char *) t->term->u.general->buf;
1249 *value_len = t->term->u.general->len;
1252 *disp_term = t->displayTerm;
1253 *disp_len = strlen(*disp_term);
1255 else if (t->term->which == Z_Term_general)
1257 *disp_term = (const char *) t->term->u.general->buf;
1258 *disp_len = t->term->u.general->len;
1260 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1263 if (scan->srw_scan_response)
1265 Z_SRW_scanResponse *res = scan->srw_scan_response;
1266 Z_SRW_scanTerm *t = res->terms + pos;
1269 *value_term = t->value;
1270 *value_len = strlen(*value_term);
1273 *disp_term = t->displayTerm;
1275 *disp_term = t->value;
1276 *disp_len = strlen(*disp_term);
1277 *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
1282 ZOOM_API(const char *)
1283 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
1284 size_t *occ, size_t *len)
1286 const char *value_term = 0;
1287 size_t value_len = 0;
1288 const char *disp_term = 0;
1289 size_t disp_len = 0;
1291 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1292 &disp_term, &disp_len);
1298 ZOOM_API(const char *)
1299 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
1300 size_t *occ, size_t *len)
1302 const char *value_term = 0;
1303 size_t value_len = 0;
1304 const char *disp_term = 0;
1305 size_t disp_len = 0;
1307 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1308 &disp_term, &disp_len);
1314 ZOOM_API(const char *)
1315 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
1317 return ZOOM_options_get(scan->options, key);
1321 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
1324 ZOOM_options_set(scan->options, key, val);
1328 ZOOM_API(ZOOM_package)
1329 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
1331 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
1334 p->odr_out = odr_createmem(ODR_ENCODE);
1335 p->options = ZOOM_options_create_with_parent2(options, c->options);
1343 ZOOM_package_destroy(ZOOM_package p)
1348 if (p->refcount == 0)
1350 odr_destroy(p->odr_out);
1353 ZOOM_options_destroy(p->options);
1358 ZOOM_API(const char *)
1359 ZOOM_package_option_get(ZOOM_package p, const char *key)
1361 return ZOOM_options_get(p->options, key);
1364 ZOOM_API(const char *)
1365 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
1367 return ZOOM_options_getl(p->options, key, lenp);
1371 ZOOM_package_option_set(ZOOM_package p, const char *key,
1374 ZOOM_options_set(p->options, key, val);
1378 ZOOM_package_option_setl(ZOOM_package p, const char *key,
1379 const char *val, int len)
1381 ZOOM_options_setl(p->options, key, val, len);
1385 ZOOM_connection_exec_task(ZOOM_connection c)
1387 ZOOM_task task = c->tasks;
1388 zoom_ret ret = zoom_complete;
1392 yaz_log(c->log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
1393 c, task->which, task->running);
1394 if (c->error != ZOOM_ERROR_NONE)
1396 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1397 "removing tasks because of error = %d", c, c->error);
1398 ZOOM_connection_remove_tasks(c);
1403 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1404 "task already running", c);
1408 ret = zoom_complete;
1409 if (c->cs || task->which == ZOOM_TASK_CONNECT)
1411 switch (task->which)
1413 case ZOOM_TASK_SEARCH:
1414 if (c->proto == PROTO_HTTP)
1415 ret = ZOOM_connection_srw_send_search(c);
1417 ret = ZOOM_connection_Z3950_send_search(c);
1419 case ZOOM_TASK_RETRIEVE:
1420 if (c->proto == PROTO_HTTP)
1421 ret = ZOOM_connection_srw_send_search(c);
1423 ret = send_Z3950_present(c);
1425 case ZOOM_TASK_CONNECT:
1426 ret = do_connect(c);
1428 case ZOOM_TASK_SCAN:
1429 if (c->proto == PROTO_HTTP)
1430 ret = ZOOM_connection_srw_send_scan(c);
1432 ret = ZOOM_connection_Z3950_send_scan(c);
1434 case ZOOM_TASK_PACKAGE:
1435 ret = send_package(c);
1437 case ZOOM_TASK_SORT:
1438 c->tasks->u.sort.resultset->r_sort_spec =
1439 ZOOM_query_get_sortspec(c->tasks->u.sort.q);
1440 ret = send_Z3950_sort(c, c->tasks->u.sort.resultset);
1446 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1447 "remove tasks because no connection exist", c);
1448 ZOOM_connection_remove_tasks(c);
1450 if (ret == zoom_complete)
1452 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1453 "task removed (complete)", c);
1454 ZOOM_connection_remove_task(c);
1457 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1464 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri,
1465 Z_HTTP_Response *cookie_hres)
1467 struct Z_HTTP_Header *h;
1468 char *combined_cookies = 0;
1469 int combined_cookies_len = 0;
1470 Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 0);
1472 gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
1473 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
1476 for (h = cookie_hres->headers; h; h = h->next)
1478 if (!strcmp(h->name, "Set-Cookie"))
1482 if (!(cp = strchr(h->value, ';')))
1483 cp = h->value + strlen(h->value);
1484 if (cp - h->value >= 1) {
1485 combined_cookies = xrealloc(combined_cookies, combined_cookies_len + cp - h->value + 3);
1486 memcpy(combined_cookies+combined_cookies_len, h->value, cp - h->value);
1487 combined_cookies[combined_cookies_len + cp - h->value] = '\0';
1488 strcat(combined_cookies,"; ");
1489 combined_cookies_len = strlen(combined_cookies);
1494 if (combined_cookies_len)
1496 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
1497 "Cookie", combined_cookies);
1498 xfree(combined_cookies);
1501 if (c->user && c->password)
1503 z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
1504 c->user, c->password);
1506 if (!z_GDU(c->odr_out, &gdu, 0, 0))
1507 return zoom_complete;
1509 z_GDU(c->odr_print, &gdu, 0, 0);
1510 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1512 odr_reset(c->odr_out);
1513 return ZOOM_send_buf(c);
1517 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
1518 const char *addinfo, const char *addinfo2)
1520 ZOOM_set_dset_error(c, error, "HTTP", addinfo, addinfo2);
1525 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
1527 zoom_ret cret = zoom_complete;
1529 const char *addinfo = 0;
1530 const char *connection_head = z_HTTP_header_lookup(hres->headers,
1532 const char *location;
1534 ZOOM_connection_set_mask(c, 0);
1535 yaz_log(c->log_details, "%p handle_http", c);
1537 if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
1538 && (location = z_HTTP_header_lookup(hres->headers, "Location")))
1541 if (c->no_redirects > 10)
1543 ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1544 c->no_redirects = 0;
1545 ZOOM_connection_close(c);
1549 /* since redirect may change host we just reconnect. A smarter
1550 implementation might check whether it's the same server */
1551 do_connect_host(c, location);
1552 send_HTTP_redirect(c, location, hres);
1553 /* we're OK for now. Operation is not really complete */
1559 ret = ZOOM_handle_sru(c, hres, &cret);
1562 if (c->no_redirects) /* end of redirect. change hosts again */
1563 ZOOM_connection_close(c);
1565 c->no_redirects = 0;
1569 if (hres->code != 200)
1570 ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1572 ZOOM_set_error(c, ZOOM_ERROR_DECODE, addinfo);
1573 ZOOM_connection_close(c);
1575 if (cret == zoom_complete)
1577 yaz_log(c->log_details, "removing tasks in handle_http");
1578 ZOOM_connection_remove_task(c);
1582 if (!strcmp(hres->version, "1.0"))
1584 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
1585 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
1590 /* HTTP 1.1: only if no close we stay alive.. */
1591 if (connection_head && !strcmp(connection_head, "close"))
1596 ZOOM_connection_close(c);
1599 c->tasks->running = 0;
1600 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1601 c->reconnect_ok = 0;
1608 static int do_read(ZOOM_connection c)
1613 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
1614 ZOOM_connection_put_event(c, event);
1616 r = cs_get(c->cs, &c->buf_in, &c->len_in);
1617 more = cs_more(c->cs);
1618 yaz_log(c->log_details, "%p do_read len=%d more=%d", c, r, more);
1623 if (!ZOOM_test_reconnect(c))
1625 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1626 ZOOM_connection_close(c);
1634 odr_reset(c->odr_in);
1635 odr_setbuf(c->odr_in, c->buf_in, r, 0);
1636 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
1637 ZOOM_connection_put_event(c, event);
1639 if (!z_GDU(c->odr_in, &gdu, 0, 0))
1642 int err = odr_geterrorx(c->odr_in, &x);
1644 const char *element = odr_getelement(c->odr_in);
1645 yaz_snprintf(msg, sizeof(msg),
1646 "ODR code %d:%d element=%s offset=%d",
1647 err, x, element ? element : "<unknown>",
1648 odr_offset(c->odr_in));
1649 ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg);
1652 FILE *ber_file = yaz_log_file();
1654 odr_dumpBER(ber_file, c->buf_in, r);
1656 ZOOM_connection_close(c);
1661 z_GDU(c->odr_print, &gdu, 0, 0);
1662 if (gdu->which == Z_GDU_Z3950)
1663 ZOOM_handle_Z3950_apdu(c, gdu->u.z3950);
1664 else if (gdu->which == Z_GDU_HTTP_Response)
1667 handle_http(c, gdu->u.HTTP_Response);
1669 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1670 ZOOM_connection_close(c);
1674 c->reconnect_ok = 0;
1679 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
1684 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1685 ZOOM_connection_put_event(c, event);
1687 yaz_log(c->log_details, "%p do_write_ex len=%d", c, len_out);
1688 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
1690 yaz_log(c->log_details, "%p do_write_ex write failed", c);
1691 if (ZOOM_test_reconnect(c))
1693 return zoom_pending;
1695 if (c->state == STATE_CONNECTING)
1696 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1698 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1699 ZOOM_connection_close(c);
1700 return zoom_complete;
1704 int mask = ZOOM_SELECT_EXCEPT;
1705 if (c->cs->io_pending & CS_WANT_WRITE)
1706 mask += ZOOM_SELECT_WRITE;
1707 if (c->cs->io_pending & CS_WANT_READ)
1708 mask += ZOOM_SELECT_READ;
1709 ZOOM_connection_set_mask(c, mask);
1710 yaz_log(c->log_details, "%p do_write_ex write incomplete mask=%d",
1715 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
1716 yaz_log(c->log_details, "%p do_write_ex write complete mask=%d",
1719 return zoom_pending;
1722 zoom_ret ZOOM_send_buf(ZOOM_connection c)
1724 return do_write_ex(c, c->buf_out, c->len_out);
1728 ZOOM_API(const char *)
1729 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
1731 return ZOOM_options_get(c->options, key);
1734 ZOOM_API(const char *)
1735 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
1737 return ZOOM_options_getl(c->options, key, lenp);
1741 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
1744 ZOOM_options_set(c->options, key, val);
1748 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
1749 const char *val, int len)
1751 ZOOM_options_setl(c->options, key, val, len);
1754 ZOOM_API(const char *)
1755 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
1757 return ZOOM_options_get(r->options, key);
1761 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
1764 ZOOM_options_set(r->options, key, val);
1769 ZOOM_connection_errcode(ZOOM_connection c)
1771 return ZOOM_connection_error(c, 0, 0);
1774 ZOOM_API(const char *)
1775 ZOOM_connection_errmsg(ZOOM_connection c)
1778 ZOOM_connection_error(c, &msg, 0);
1782 ZOOM_API(const char *)
1783 ZOOM_connection_addinfo(ZOOM_connection c)
1785 const char *addinfo;
1786 ZOOM_connection_error(c, 0, &addinfo);
1790 ZOOM_API(const char *)
1791 ZOOM_connection_diagset(ZOOM_connection c)
1793 const char *diagset;
1794 ZOOM_connection_error_x(c, 0, 0, &diagset);
1798 ZOOM_API(const char *)
1799 ZOOM_diag_str(int error)
1803 case ZOOM_ERROR_NONE:
1805 case ZOOM_ERROR_CONNECT:
1806 return "Connect failed";
1807 case ZOOM_ERROR_MEMORY:
1808 return "Out of memory";
1809 case ZOOM_ERROR_ENCODE:
1810 return "Encoding failed";
1811 case ZOOM_ERROR_DECODE:
1812 return "Decoding failed";
1813 case ZOOM_ERROR_CONNECTION_LOST:
1814 return "Connection lost";
1815 case ZOOM_ERROR_INIT:
1816 return "Init rejected";
1817 case ZOOM_ERROR_INTERNAL:
1818 return "Internal failure";
1819 case ZOOM_ERROR_TIMEOUT:
1821 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
1822 return "Unsupported protocol";
1823 case ZOOM_ERROR_UNSUPPORTED_QUERY:
1824 return "Unsupported query type";
1825 case ZOOM_ERROR_INVALID_QUERY:
1826 return "Invalid query";
1827 case ZOOM_ERROR_CQL_PARSE:
1828 return "CQL parsing error";
1829 case ZOOM_ERROR_CQL_TRANSFORM:
1830 return "CQL transformation error";
1831 case ZOOM_ERROR_CCL_CONFIG:
1832 return "CCL configuration error";
1833 case ZOOM_ERROR_CCL_PARSE:
1834 return "CCL parsing error";
1835 case ZOOM_ERROR_ES_INVALID_ACTION:
1836 return "Extended Service. invalid action";
1837 case ZOOM_ERROR_ES_INVALID_VERSION:
1838 return "Extended Service. invalid version";
1839 case ZOOM_ERROR_ES_INVALID_SYNTAX:
1840 return "Extended Service. invalid syntax";
1842 return diagbib1_str(error);
1847 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
1848 const char **addinfo, const char **diagset)
1850 int error = c->error;
1853 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
1854 *cp = ZOOM_diag_str(error);
1855 else if (!strcmp(c->diagset, "HTTP"))
1856 *cp = z_HTTP_errmsg(c->error);
1857 else if (!strcmp(c->diagset, "Bib-1"))
1858 *cp = ZOOM_diag_str(error);
1859 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
1860 *cp = yaz_diag_srw_str(c->error);
1862 *cp = "Unknown error and diagnostic set";
1865 *addinfo = c->addinfo ? c->addinfo : "";
1867 *diagset = c->diagset ? c->diagset : "";
1872 ZOOM_connection_error(ZOOM_connection c, const char **cp,
1873 const char **addinfo)
1875 return ZOOM_connection_error_x(c, cp, addinfo, 0);
1878 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
1880 ZOOM_Event event = 0;
1881 int r = cs_look(c->cs);
1882 yaz_log(c->log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
1887 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1888 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1889 ZOOM_connection_close(c);
1890 ZOOM_connection_put_event(c, event);
1892 else if (r == CS_CONNECT)
1894 int ret = ret = cs_rcvconnect(c->cs);
1895 yaz_log(c->log_details, "%p ZOOM_connection_do_io "
1896 "cs_rcvconnect returned %d", c, ret);
1899 int mask = ZOOM_SELECT_EXCEPT;
1900 if (c->cs->io_pending & CS_WANT_WRITE)
1901 mask += ZOOM_SELECT_WRITE;
1902 if (c->cs->io_pending & CS_WANT_READ)
1903 mask += ZOOM_SELECT_READ;
1904 ZOOM_connection_set_mask(c, mask);
1905 event = ZOOM_Event_create(ZOOM_EVENT_NONE);
1906 ZOOM_connection_put_event(c, event);
1910 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1911 ZOOM_connection_put_event(c, event);
1913 if (c->proto == PROTO_Z3950)
1914 ZOOM_connection_Z3950_send_init(c);
1917 /* no init request for SRW .. */
1918 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1919 ZOOM_connection_remove_task(c);
1920 ZOOM_connection_set_mask(c, 0);
1921 ZOOM_connection_exec_task(c);
1923 c->state = STATE_ESTABLISHED;
1927 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1928 ZOOM_connection_close(c);
1933 if (mask & ZOOM_SELECT_EXCEPT)
1935 if (!ZOOM_test_reconnect(c))
1937 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1938 ZOOM_connection_close(c);
1942 if (mask & ZOOM_SELECT_READ)
1944 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1950 ZOOM_connection_last_event(ZOOM_connection cs)
1953 return ZOOM_EVENT_NONE;
1954 return cs->last_event;
1958 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
1962 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
1963 /* timeout and this connection was waiting */
1964 ZOOM_set_error(c, ZOOM_ERROR_TIMEOUT, 0);
1965 ZOOM_connection_close(c);
1966 ZOOM_connection_put_event(c, event);
1972 ZOOM_connection_process(ZOOM_connection c)
1978 event = ZOOM_connection_get_event(c);
1981 ZOOM_Event_destroy(event);
1984 ZOOM_connection_exec_task(c);
1985 event = ZOOM_connection_get_event(c);
1988 ZOOM_Event_destroy(event);
1995 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
1999 yaz_log(log_details0, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
2001 for (i = 0; i<no; i++)
2003 ZOOM_connection c = cs[i];
2005 if (c && ZOOM_connection_process(c))
2011 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
2013 if (c->mask && mask)
2014 ZOOM_connection_do_io(c, mask);
2018 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
2021 return cs_fileno(c->cs);
2025 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
2033 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
2040 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
2042 return ZOOM_options_get_int(c->options, "timeout", 30);
2045 ZOOM_API(void) ZOOM_connection_close(ZOOM_connection c)
2050 ZOOM_connection_set_mask(c, 0);
2051 c->state = STATE_IDLE;
2057 * c-file-style: "Stroustrup"
2058 * indent-tabs-mode: nil
2060 * vim: shiftwidth=4 tabstop=8 expandtab