1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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) + 3);
74 strcpy(c->addinfo, addinfo);
75 strcat(c->addinfo, ": ");
76 strcat(c->addinfo, addinfo2);
79 c->addinfo = xstrdup(addinfo);
80 if (error != ZOOM_ERROR_NONE)
82 yaz_log(c->log_api, "%p set_dset_error %s %s:%d %s %s",
83 c, c->host_port ? c->host_port : "<>", dset, error,
84 addinfo ? addinfo : "",
85 addinfo2 ? addinfo2 : "");
86 ZOOM_connection_remove_tasks(c);
90 int ZOOM_uri_to_code(const char *uri)
94 if ((cp = strrchr(uri, '/')))
99 void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo)
101 ZOOM_set_dset_error(c, error, "ZOOM", addinfo, 0);
104 static void clear_error(ZOOM_connection c)
107 * If an error is tied to an operation then it's ok to clear: for
108 * example, a diagnostic returned from a search is cleared by a
109 * subsequent search. However, problems such as Connection Lost
110 * or Init Refused are not cleared, because they are not
111 * recoverable: doing another search doesn't help.
114 ZOOM_connection_remove_events(c);
117 case ZOOM_ERROR_CONNECT:
118 case ZOOM_ERROR_MEMORY:
119 case ZOOM_ERROR_DECODE:
120 case ZOOM_ERROR_CONNECTION_LOST:
121 case ZOOM_ERROR_INIT:
122 case ZOOM_ERROR_INTERNAL:
123 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
126 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
130 void ZOOM_connection_show_task(ZOOM_task task)
134 case ZOOM_TASK_SEARCH:
135 yaz_log(YLOG_LOG, "search p=%p", task);
137 case ZOOM_TASK_CONNECT:
138 yaz_log(YLOG_LOG, "connect p=%p", task);
141 yaz_log(YLOG_LOG, "scan p=%p", task);
146 void ZOOM_connection_show_tasks(ZOOM_connection c)
149 yaz_log(YLOG_LOG, "connection p=%p tasks", c);
150 for (task = c->tasks; task; task = task->next)
151 ZOOM_connection_show_task(task);
154 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
156 ZOOM_task *taskp = &c->tasks;
158 taskp = &(*taskp)->next;
159 *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
160 (*taskp)->running = 0;
161 (*taskp)->which = which;
167 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
169 return c->tasks ? 0 : 1;
172 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
174 ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
176 task->next = c->tasks;
184 void ZOOM_connection_remove_task(ZOOM_connection c)
186 ZOOM_task task = c->tasks;
190 c->tasks = task->next;
193 case ZOOM_TASK_SEARCH:
194 resultset_destroy(task->u.search.resultset);
195 xfree(task->u.search.syntax);
196 xfree(task->u.search.elementSetName);
197 xfree(task->u.search.schema);
199 case ZOOM_TASK_CONNECT:
202 ZOOM_scanset_destroy(task->u.scan.scan);
204 case ZOOM_TASK_PACKAGE:
205 ZOOM_package_destroy(task->u.package);
208 resultset_destroy(task->u.sort.resultset);
209 ZOOM_query_destroy(task->u.sort.q);
218 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
219 ZOOM_connection_put_event(c, event);
224 void ZOOM_connection_remove_tasks(ZOOM_connection c)
227 ZOOM_connection_remove_task(c);
230 static void odr_wrbuf_write(ODR o, void *handle, int type,
231 const char *buf, int len)
233 WRBUF w = (WRBUF) handle;
234 wrbuf_write(w, buf, len);
237 ZOOM_API(ZOOM_connection)
238 ZOOM_connection_create(ZOOM_options options)
240 ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
244 c->log_api = log_api0;
245 c->log_details = log_details0;
247 yaz_log(c->log_api, "%p ZOOM_connection_create", c);
249 c->proto = PROTO_Z3950;
251 ZOOM_connection_set_mask(c, 0);
253 c->state = STATE_IDLE;
256 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
263 c->options = ZOOM_options_create_with_parent(options);
269 c->charset = c->lang = 0;
279 c->url_authentication = 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);
290 c->support_named_resultsets = 0;
291 c->last_event = ZOOM_EVENT_NONE;
293 c->m_queue_front = 0;
299 c->saveAPDU_wrbuf = 0;
301 ZOOM_memcached_init(c);
305 ZOOM_API(void) ZOOM_connection_save_apdu_wrbuf(ZOOM_connection c, WRBUF w)
309 odr_destroy(c->odr_save);
314 c->odr_save = odr_createmem(ODR_PRINT);
315 odr_set_stream(c->odr_save, w, odr_wrbuf_write, 0);
319 /* set database names. Take local databases (if set); otherwise
320 take databases given in ZURL (if set); otherwise use Default */
321 char **ZOOM_connection_get_databases(ZOOM_connection con, ZOOM_options options,
324 char **databaseNames;
325 const char *cp = ZOOM_options_get(options, "databaseName");
327 if ((!cp || !*cp) && con->host_port)
328 cs_get_host_args(con->host_port, &cp);
331 nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
332 return databaseNames;
335 ZOOM_API(ZOOM_connection)
336 ZOOM_connection_new(const char *host, int portnum)
338 ZOOM_connection c = ZOOM_connection_create(0);
340 ZOOM_connection_connect(c, host, portnum);
344 static zoom_sru_mode get_sru_mode_from_string(const char *s)
347 return zoom_sru_soap;
348 if (!yaz_matchstr(s, "soap"))
349 return zoom_sru_soap;
350 else if (!yaz_matchstr(s, "get"))
352 else if (!yaz_matchstr(s, "post"))
353 return zoom_sru_post;
354 else if (!yaz_matchstr(s, "solr"))
355 return zoom_sru_solr;
356 return zoom_sru_error;
360 ZOOM_connection_connect(ZOOM_connection c,
361 const char *host, int portnum)
364 const char *http_lead;
368 yaz_log(c->log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
369 c, host ? host : "null", portnum);
371 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
372 ZOOM_connection_remove_tasks(c);
376 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
377 odr_destroy(c->odr_print);
379 if (ZOOM_options_get_bool(c->options, "apdulog", 0))
381 c->odr_print = odr_createmem(ODR_PRINT);
382 odr_setprint(c->odr_print, yaz_log_file());
389 yaz_log(c->log_details, "%p ZOOM_connection_connect reconnect ok", c);
393 yaz_log(c->log_details, "%p ZOOM_connection_connect connect", c);
396 val = ZOOM_options_get(c->options, "proxy");
399 yaz_log(c->log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
400 c->proxy = xstrdup(val);
405 val = ZOOM_options_get(c->options, "tproxy");
408 yaz_log(c->log_details, "%p ZOOM_connection_connect tproxy=%s", c, val);
409 c->tproxy = xstrdup(val);
414 val = ZOOM_options_get(c->options, "charset");
417 yaz_log(c->log_details, "%p ZOOM_connection_connect charset=%s", c, val);
418 c->charset = xstrdup(val);
422 val = ZOOM_options_get(c->options, "lang");
425 yaz_log(c->log_details, "%p ZOOM_connection_connect lang=%s", c, val);
426 c->lang = xstrdup(val);
431 val = ZOOM_options_get(c->options, "sru");
432 if (val && *val && !strstr(host, "://"))
433 http_lead = "http://";
436 c->sru_mode = get_sru_mode_from_string(val);
444 sprintf(hostn, "%.80s:%d", host, portnum);
447 c->host_port = xmalloc(strlen(host) + strlen(http_lead) + 1);
448 strcpy(c->host_port, http_lead);
449 strcat(c->host_port, host);
454 * If the "<scheme>:" part of the host string is preceded by one
455 * or more comma-separated <name>=<value> pairs, these are taken
456 * to be options to be set on the connection object. Among other
457 * applications, this facility can be used to embed authentication
459 * user=admin,password=secret,tcp:localhost:9999
461 char *remainder = c->host_port;
462 char *pcolon = strchr(remainder, ':');
465 while ((pcomma = strchr(remainder, ',')) != 0 &&
466 (pcolon == 0 || pcomma < pcolon))
469 if ((pequals = strchr(remainder, '=')) != 0)
472 ZOOM_connection_option_set(c, remainder, pequals+1);
474 remainder = pcomma+1;
477 if (remainder != c->host_port)
479 remainder = xstrdup(remainder);
481 c->host_port = remainder;
485 xfree(c->sru_version);
486 val = ZOOM_options_get(c->options, "sru_version");
487 c->sru_version = xstrdup(val ? val : "1.2");
489 ZOOM_options_set(c->options, "host", c->host_port);
491 xfree(c->cookie_out);
493 val = ZOOM_options_get(c->options, "cookie");
496 yaz_log(c->log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
497 c->cookie_out = xstrdup(val);
502 val = ZOOM_options_get(c->options, "clientIP");
505 yaz_log(c->log_details, "%p ZOOM_connection_connect clientIP=%s",
507 c->client_IP = xstrdup(val);
512 val = ZOOM_options_get(c->options, "group");
514 c->group = xstrdup(val);
518 val = ZOOM_options_get(c->options, "user");
520 c->user = xstrdup(val);
524 val = ZOOM_options_get(c->options, "password");
526 val = ZOOM_options_get(c->options, "pass");
528 c->password = xstrdup(val);
530 val = ZOOM_options_get(c->options, "authenticationMode");
531 if (val && !strcmp(val, "url"))
532 c->url_authentication = 1;
534 c->url_authentication = 0;
536 c->maximum_record_size =
537 ZOOM_options_get_int(c->options, "maximumRecordSize", 64*1024*1024);
538 c->preferred_message_size =
539 ZOOM_options_get_int(c->options, "preferredMessageSize", 64*1024*1024);
541 c->async = ZOOM_options_get_bool(c->options, "async", 0);
543 yaz_cookies_destroy(c->cookies);
544 c->cookies = yaz_cookies_create();
546 if (ZOOM_memcached_configure(c))
548 ZOOM_connection_remove_tasks(c);
551 if (c->sru_mode == zoom_sru_error)
553 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, val);
554 ZOOM_connection_remove_tasks(c);
558 yaz_log(c->log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
559 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
563 while (ZOOM_event(1, &c))
568 ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
572 /* remove ourselves from the resultsets in connection */
573 ZOOM_resultset *rp = &r->connection->resultsets;
576 assert(*rp); /* we must be in this list!! */
578 { /* OK, we're here - take us out of it */
589 ZOOM_connection_destroy(ZOOM_connection c)
594 yaz_log(c->log_api, "%p ZOOM_connection_destroy", c);
596 ZOOM_memcached_destroy(c);
600 for (r = c->resultsets; r; r = r->next)
606 odr_destroy(c->odr_in);
607 odr_destroy(c->odr_out);
609 odr_destroy(c->odr_save);
612 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
613 odr_destroy(c->odr_print);
615 ZOOM_options_destroy(c->options);
616 ZOOM_connection_remove_tasks(c);
617 ZOOM_connection_remove_events(c);
623 xfree(c->cookie_out);
629 xfree(c->sru_version);
630 yaz_cookies_destroy(c->cookies);
631 wrbuf_destroy(c->saveAPDU_wrbuf);
635 void ZOOM_resultset_addref(ZOOM_resultset r)
639 yaz_mutex_enter(r->mutex);
641 yaz_log(log_details0, "%p ZOOM_resultset_addref count=%d",
643 yaz_mutex_leave(r->mutex);
647 static int g_resultsets = 0;
648 static YAZ_MUTEX g_resultset_mutex = 0;
650 /* TODO We need to initialize this before running threaded:
651 * call resultset_use(0) */
653 static int resultset_use(int delta) {
655 if (g_resultset_mutex == 0)
656 yaz_mutex_create(&g_resultset_mutex);
657 yaz_mutex_enter(g_resultset_mutex);
658 g_resultsets += delta;
659 resultset_count = g_resultsets;
660 yaz_mutex_leave(g_resultset_mutex);
661 return resultset_count;
664 int resultsets_count(void) {
665 return resultset_use(0);
668 ZOOM_resultset ZOOM_resultset_create(void)
671 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
675 yaz_log(log_details0, "%p ZOOM_resultset_create", r);
678 r->odr = odr_createmem(ODR_DECODE);
682 for (i = 0; i<RECORD_HASH_SIZE; i++)
683 r->record_hash[i] = 0;
687 r->databaseNames = 0;
688 r->num_databaseNames = 0;
690 r->num_res_facets = 0;
693 yaz_mutex_create(&r->mutex);
696 WRBUF w = wrbuf_alloc();
697 YAZ_SHPTR_INIT(r->record_wrbuf, w);
706 ZOOM_API(ZOOM_resultset)
707 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
710 ZOOM_query s = ZOOM_query_create();
712 ZOOM_query_prefix(s, q);
714 r = ZOOM_connection_search(c, s);
715 ZOOM_query_destroy(s);
719 ZOOM_API(ZOOM_resultset)
720 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
722 ZOOM_resultset r = ZOOM_resultset_create();
726 const char *syntax, *elementSetName, *schema;
727 yaz_log(c->log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
728 r->r_sort_spec = ZOOM_query_get_sortspec(q);
730 ZOOM_query_addref(q);
732 r->options = ZOOM_options_create_with_parent(c->options);
734 start = ZOOM_options_get_int(r->options, "start", 0);
735 count = ZOOM_options_get_int(r->options, "count", 0);
737 /* If "presentChunk" is defined use that; otherwise "step" */
738 const char *cp = ZOOM_options_get(r->options, "presentChunk");
739 r->step = ZOOM_options_get_int(r->options,
740 (cp != 0 ? "presentChunk": "step"), 0);
742 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
743 cp = ZOOM_options_get(r->options, "setname");
745 r->setname = xstrdup(cp);
747 r->databaseNames = ZOOM_connection_get_databases(c, c->options, &r->num_databaseNames,
751 r->next = c->resultsets;
754 ZOOM_memcached_resultset(r, q);
756 if (c->host_port && c->proto == PROTO_HTTP)
760 yaz_log(c->log_details, "ZOOM_connection_search: no comstack");
761 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
765 yaz_log(c->log_details, "ZOOM_connection_search: reconnect");
770 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
771 task->u.search.resultset = r;
772 task->u.search.start = start;
773 task->u.search.count = count;
775 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
776 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
777 elementSetName = ZOOM_options_get(r->options, "elementSetName");
778 task->u.search.elementSetName = elementSetName ?
779 xstrdup(elementSetName) : 0;
780 schema = ZOOM_options_get(r->options, "schema");
781 task->u.search.schema = schema ? xstrdup(schema) : 0;
783 ZOOM_resultset_addref(r);
787 while (ZOOM_event(1, &c))
794 ZOOM_resultset_sort(ZOOM_resultset r,
795 const char *sort_type, const char *sort_spec)
797 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
801 ZOOM_resultset_sort1(ZOOM_resultset r,
802 const char *sort_type, const char *sort_spec)
804 ZOOM_connection c = r->connection;
808 newq = ZOOM_query_create();
809 if (ZOOM_query_sortby(newq, sort_spec) < 0)
812 yaz_log(c->log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
813 r, r, sort_type, sort_spec);
817 if (c->host_port && c->proto == PROTO_HTTP)
821 yaz_log(c->log_details, "%p ZOOM_resultset_sort: no comstack", r);
822 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
826 yaz_log(c->log_details, "%p ZOOM_resultset_sort: prepare reconnect",
832 ZOOM_resultset_cache_reset(r);
833 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
834 task->u.sort.resultset = r;
835 task->u.sort.q = newq;
837 ZOOM_resultset_addref(r);
841 while (ZOOM_event(1, &c))
849 ZOOM_resultset_destroy(ZOOM_resultset r)
851 resultset_destroy(r);
854 static void resultset_destroy(ZOOM_resultset r)
858 yaz_mutex_enter(r->mutex);
860 yaz_log(log_details0, "%p ZOOM_resultset_destroy r=%p count=%d",
862 if (r->refcount == 0)
864 yaz_mutex_leave(r->mutex);
866 yaz_log(log_details0, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r);
867 ZOOM_resultset_cache_reset(r);
868 ZOOM_resultset_release(r);
869 ZOOM_query_destroy(r->query);
870 ZOOM_options_destroy(r->options);
873 yaz_mutex_destroy(&r->mutex);
875 YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
877 wrbuf_destroy(r->mc_key);
882 yaz_mutex_leave(r->mutex);
886 ZOOM_resultset_size(ZOOM_resultset r)
891 int ZOOM_test_reconnect(ZOOM_connection c)
895 if (!c->reconnect_ok)
897 ZOOM_connection_close(c);
899 c->tasks->running = 0;
900 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
902 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
903 ZOOM_connection_put_event(c, event);
908 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
909 int force_sync, int start, int count)
914 const char *syntax, *elementSetName;
918 yaz_log(log_details0, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
919 " count=%d", r, force_sync, start, count);
924 if (c->host_port && c->proto == PROTO_HTTP)
928 yaz_log(log_details0, "%p ZOOM_resultset_retrieve: no comstack", r);
929 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
933 yaz_log(log_details0, "%p ZOOM_resultset_retrieve: prepare "
938 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
939 task->u.search.resultset = r;
940 task->u.search.start = start;
941 task->u.search.count = count;
943 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
944 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
945 elementSetName = ZOOM_options_get(r->options, "elementSetName");
946 task->u.search.elementSetName = elementSetName
947 ? xstrdup(elementSetName) : 0;
949 cp = ZOOM_options_get(r->options, "schema");
950 task->u.search.schema = cp ? xstrdup(cp) : 0;
952 ZOOM_resultset_addref(r);
954 if (!r->connection->async || force_sync)
955 while (r->connection && ZOOM_event(1, &r->connection))
960 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
961 size_t start, size_t count)
963 int force_present = 0;
967 yaz_log(log_api0, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
968 r, r, (long) start, (long) count);
971 ZOOM_resultset_retrieve(r, force_present, start, count);
975 for (i = 0; i< count; i++)
976 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
981 ZOOM_resultset_facets_size(ZOOM_resultset r)
983 return r->num_res_facets;
986 ZOOM_API(ZOOM_facet_field)
987 ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name)
989 int num = r->num_res_facets;
990 ZOOM_facet_field *facets = r->res_facets;
992 for (i = 0; i < num; i++)
993 if (!strcmp(facets[i]->facet_name, name))
998 ZOOM_API(ZOOM_facet_field)
999 ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int idx)
1001 int num = r->num_res_facets;
1002 ZOOM_facet_field *facets = r->res_facets;
1003 if (idx >= 0 && idx < num)
1008 ZOOM_API(ZOOM_facet_field *)
1009 ZOOM_resultset_facets(ZOOM_resultset r)
1011 return r->res_facets;
1014 ZOOM_API(const char**)
1015 ZOOM_resultset_facets_names(ZOOM_resultset r)
1017 return (const char **) r->facets_names;
1020 ZOOM_API(const char*)
1021 ZOOM_facet_field_name(ZOOM_facet_field field)
1023 return field->facet_name;
1027 ZOOM_facet_field_term_count(ZOOM_facet_field field)
1029 return field->num_terms;
1032 ZOOM_API(const char*)
1033 ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq)
1035 *freq = field->facet_terms[idx].frequency;
1036 return field->facet_terms[idx].term;
1040 static void get_cert(ZOOM_connection c)
1045 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1047 ZOOM_connection_option_setl(c, "sslPeerCert",
1048 cert_buf, cert_len);
1053 static zoom_ret do_connect_host(ZOOM_connection c,
1054 const char *logical_url);
1056 static zoom_ret do_connect(ZOOM_connection c)
1058 return do_connect_host(c, c->host_port);
1061 static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
1067 c->cs = cs_create_host_proxy(logical_url, 0, &add,
1068 c->tproxy ? c->tproxy : c->proxy);
1070 if (c->cs && c->cs->protocol == PROTO_HTTP)
1073 c->proto = PROTO_HTTP;
1075 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1076 ZOOM_connection_close(c);
1077 return zoom_complete;
1082 int ret = cs_connect(c->cs, add);
1085 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1086 ZOOM_connection_put_event(c, event);
1088 if (c->proto == PROTO_Z3950)
1089 ZOOM_connection_Z3950_send_init(c);
1092 /* no init request for SRW .. */
1093 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1094 ZOOM_connection_remove_task(c);
1095 ZOOM_connection_set_mask(c, 0);
1096 ZOOM_connection_exec_task(c);
1098 c->state = STATE_ESTABLISHED;
1099 return zoom_pending;
1103 int mask = ZOOM_SELECT_EXCEPT;
1104 if (c->cs->io_pending & CS_WANT_WRITE)
1105 mask += ZOOM_SELECT_WRITE;
1106 if (c->cs->io_pending & CS_WANT_READ)
1107 mask += ZOOM_SELECT_READ;
1108 ZOOM_connection_set_mask(c, mask);
1109 c->state = STATE_CONNECTING;
1110 return zoom_pending;
1113 c->state = STATE_IDLE;
1114 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, logical_url);
1115 return zoom_complete;
1118 /* returns 1 if PDU was sent OK (still pending )
1119 0 if PDU was not sent OK (nothing to wait for)
1122 ZOOM_API(ZOOM_record)
1123 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1125 const char *syntax =
1126 ZOOM_options_get(s->options, "preferredRecordSyntax");
1127 const char *elementSetName =
1128 ZOOM_options_get(s->options, "elementSetName");
1129 const char *schema =
1130 ZOOM_options_get(s->options, "schema");
1132 return ZOOM_record_cache_lookup(s, pos, syntax, elementSetName, schema);
1135 ZOOM_API(ZOOM_record)
1136 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1138 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1143 * MIKE: I think force_sync should always be zero, but I don't
1144 * want to make this change until I get the go-ahead from
1145 * Adam, in case something depends on the old synchronous
1149 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1150 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1151 rec = ZOOM_resultset_record_immediate(r, pos);
1156 ZOOM_API(ZOOM_scanset)
1157 ZOOM_connection_scan(ZOOM_connection c, const char *start)
1160 ZOOM_query q = ZOOM_query_create();
1162 ZOOM_query_prefix(q, start);
1164 s = ZOOM_connection_scan1(c, q);
1165 ZOOM_query_destroy(q);
1170 ZOOM_API(ZOOM_scanset)
1171 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
1173 ZOOM_scanset scan = 0;
1174 Z_Query *z_query = ZOOM_query_get_Z_Query(q);
1178 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
1179 scan->connection = c;
1180 scan->odr = odr_createmem(ODR_DECODE);
1181 scan->options = ZOOM_options_create_with_parent(c->options);
1183 scan->scan_response = 0;
1184 scan->srw_scan_response = 0;
1187 ZOOM_query_addref(q);
1188 scan->databaseNames = ZOOM_connection_get_databases(c, c->options,
1189 &scan->num_databaseNames,
1194 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
1195 task->u.scan.scan = scan;
1200 while (ZOOM_event(1, &c))
1208 ZOOM_scanset_destroy(ZOOM_scanset scan)
1213 if (scan->refcount == 0)
1215 ZOOM_query_destroy(scan->query);
1217 odr_destroy(scan->odr);
1219 ZOOM_options_destroy(scan->options);
1224 static zoom_ret send_package(ZOOM_connection c)
1228 yaz_log(c->log_details, "%p send_package", c);
1230 return zoom_complete;
1231 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1233 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1234 ZOOM_connection_put_event(c, event);
1236 c->buf_out = c->tasks->u.package->buf_out;
1237 c->len_out = c->tasks->u.package->len_out;
1239 return ZOOM_send_buf(c);
1243 ZOOM_scanset_size(ZOOM_scanset scan)
1248 if (scan->scan_response && scan->scan_response->entries)
1249 return scan->scan_response->entries->num_entries;
1250 else if (scan->srw_scan_response)
1251 return scan->srw_scan_response->num_terms;
1255 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
1257 const char **value_term, size_t *value_len,
1258 const char **disp_term, size_t *disp_len)
1260 size_t noent = ZOOM_scanset_size(scan);
1271 if (scan->scan_response)
1273 Z_ScanResponse *res = scan->scan_response;
1274 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1276 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1278 *value_term = (const char *) t->term->u.general->buf;
1279 *value_len = t->term->u.general->len;
1282 *disp_term = t->displayTerm;
1283 *disp_len = strlen(*disp_term);
1285 else if (t->term->which == Z_Term_general)
1287 *disp_term = (const char *) t->term->u.general->buf;
1288 *disp_len = t->term->u.general->len;
1290 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1293 if (scan->srw_scan_response)
1295 Z_SRW_scanResponse *res = scan->srw_scan_response;
1296 Z_SRW_scanTerm *t = res->terms + pos;
1299 *value_term = t->value;
1300 *value_len = strlen(*value_term);
1303 *disp_term = t->displayTerm;
1305 *disp_term = t->value;
1306 *disp_len = strlen(*disp_term);
1307 *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
1312 ZOOM_API(const char *)
1313 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
1314 size_t *occ, size_t *len)
1316 const char *value_term = 0;
1317 size_t value_len = 0;
1318 const char *disp_term = 0;
1319 size_t disp_len = 0;
1321 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1322 &disp_term, &disp_len);
1328 ZOOM_API(const char *)
1329 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
1330 size_t *occ, size_t *len)
1332 const char *value_term = 0;
1333 size_t value_len = 0;
1334 const char *disp_term = 0;
1335 size_t disp_len = 0;
1337 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1338 &disp_term, &disp_len);
1344 ZOOM_API(const char *)
1345 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
1347 return ZOOM_options_get(scan->options, key);
1351 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
1354 ZOOM_options_set(scan->options, key, val);
1358 ZOOM_API(ZOOM_package)
1359 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
1361 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
1364 p->odr_out = odr_createmem(ODR_ENCODE);
1365 p->options = ZOOM_options_create_with_parent2(options, c->options);
1373 ZOOM_package_destroy(ZOOM_package p)
1378 if (p->refcount == 0)
1380 odr_destroy(p->odr_out);
1383 ZOOM_options_destroy(p->options);
1388 ZOOM_API(const char *)
1389 ZOOM_package_option_get(ZOOM_package p, const char *key)
1391 return ZOOM_options_get(p->options, key);
1394 ZOOM_API(const char *)
1395 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
1397 return ZOOM_options_getl(p->options, key, lenp);
1401 ZOOM_package_option_set(ZOOM_package p, const char *key,
1404 ZOOM_options_set(p->options, key, val);
1408 ZOOM_package_option_setl(ZOOM_package p, const char *key,
1409 const char *val, int len)
1411 ZOOM_options_setl(p->options, key, val, len);
1415 ZOOM_connection_exec_task(ZOOM_connection c)
1417 ZOOM_task task = c->tasks;
1418 zoom_ret ret = zoom_complete;
1422 yaz_log(c->log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
1423 c, task->which, task->running);
1424 if (c->error != ZOOM_ERROR_NONE)
1426 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1427 "removing tasks because of error = %d", c, c->error);
1428 ZOOM_connection_remove_tasks(c);
1433 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1434 "task already running", c);
1438 ret = zoom_complete;
1439 if (c->cs || task->which == ZOOM_TASK_CONNECT)
1441 switch (task->which)
1443 case ZOOM_TASK_SEARCH:
1444 if (c->proto == PROTO_HTTP)
1445 ret = ZOOM_connection_srw_send_search(c);
1447 ret = ZOOM_connection_Z3950_search(c);
1449 case ZOOM_TASK_CONNECT:
1450 ret = do_connect(c);
1452 case ZOOM_TASK_SCAN:
1453 if (c->proto == PROTO_HTTP)
1454 ret = ZOOM_connection_srw_send_scan(c);
1456 ret = ZOOM_connection_Z3950_send_scan(c);
1458 case ZOOM_TASK_PACKAGE:
1459 ret = send_package(c);
1461 case ZOOM_TASK_SORT:
1462 c->tasks->u.sort.resultset->r_sort_spec =
1463 ZOOM_query_get_sortspec(c->tasks->u.sort.q);
1464 ret = send_Z3950_sort(c, c->tasks->u.sort.resultset);
1470 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1471 "remove tasks because no connection exist", c);
1472 ZOOM_connection_remove_tasks(c);
1474 if (ret == zoom_complete)
1476 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1477 "task removed (complete)", c);
1478 ZOOM_connection_remove_task(c);
1481 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1488 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri)
1490 Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 0);
1492 gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
1493 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
1495 yaz_cookies_request(c->cookies, c->odr_out, gdu->u.HTTP_Request);
1496 if (c->user && c->password)
1498 z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
1499 c->user, c->password);
1501 return ZOOM_send_GDU(c, gdu);
1504 zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu)
1508 int r = z_GDU(c->odr_out, &gdu, 0, 0);
1510 return zoom_complete;
1512 z_GDU(c->odr_print, &gdu, 0, 0);
1514 z_GDU(c->odr_save, &gdu, 0, 0);
1515 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1516 odr_reset(c->odr_out);
1518 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1519 ZOOM_connection_put_event(c, event);
1521 return ZOOM_send_buf(c);
1525 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
1526 const char *addinfo, const char *addinfo2)
1528 ZOOM_set_dset_error(c, error, "HTTP", addinfo, addinfo2);
1533 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
1535 zoom_ret cret = zoom_complete;
1538 const char *connection_head = z_HTTP_header_lookup(hres->headers,
1540 const char *location;
1542 ZOOM_connection_set_mask(c, 0);
1543 yaz_log(c->log_details, "%p handle_http", c);
1545 yaz_cookies_response(c->cookies, hres);
1546 if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
1547 && (location = z_HTTP_header_lookup(hres->headers, "Location")))
1550 if (c->no_redirects > 10)
1552 ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1553 c->no_redirects = 0;
1554 ZOOM_connection_close(c);
1558 /* since redirect may change host we just reconnect. A smarter
1559 implementation might check whether it's the same server */
1561 int host_change = 0;
1562 location = yaz_check_location(c->odr_in, c->host_port,
1563 location, &host_change);
1564 if (do_connect_host(c, location) == zoom_complete)
1565 return; /* connect failed.. */
1566 send_HTTP_redirect(c, location);
1572 ret = ZOOM_handle_sru(c, hres, &cret, &addinfo);
1575 if (c->no_redirects) /* end of redirect. change hosts again */
1576 ZOOM_connection_close(c);
1578 c->no_redirects = 0;
1582 if (hres->code != 200)
1583 ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1586 yaz_log(YLOG_LOG, "set error... addinfo=%s", addinfo ?
1588 ZOOM_set_error(c, ZOOM_ERROR_DECODE, addinfo);
1590 ZOOM_connection_close(c);
1592 if (cret == zoom_complete)
1594 yaz_log(c->log_details, "removing tasks in handle_http");
1595 ZOOM_connection_remove_task(c);
1599 if (!strcmp(hres->version, "1.0"))
1601 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
1602 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
1607 /* HTTP 1.1: only if no close we stay alive.. */
1608 if (connection_head && !strcmp(connection_head, "close"))
1613 ZOOM_connection_close(c);
1616 c->tasks->running = 0;
1617 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1618 c->reconnect_ok = 0;
1622 c->reconnect_ok = 1; /* if the server closes anyway */
1627 static int do_read(ZOOM_connection c)
1632 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
1633 ZOOM_connection_put_event(c, event);
1635 r = cs_get(c->cs, &c->buf_in, &c->len_in);
1636 more = cs_more(c->cs);
1637 yaz_log(c->log_details, "%p do_read len=%d more=%d", c, r, more);
1642 if (!ZOOM_test_reconnect(c))
1644 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1645 ZOOM_connection_close(c);
1653 odr_reset(c->odr_in);
1654 odr_setbuf(c->odr_in, c->buf_in, r, 0);
1655 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
1656 ZOOM_connection_put_event(c, event);
1658 if (!z_GDU(c->odr_in, &gdu, 0, 0))
1661 int err = odr_geterrorx(c->odr_in, &x);
1663 const char *element = odr_getelement(c->odr_in);
1664 yaz_snprintf(msg, sizeof(msg),
1665 "ODR code %d:%d element=%s offset=%d",
1666 err, x, element ? element : "<unknown>",
1667 odr_offset(c->odr_in));
1668 ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg);
1671 FILE *ber_file = yaz_log_file();
1673 odr_dumpBER(ber_file, c->buf_in, r);
1675 ZOOM_connection_close(c);
1680 z_GDU(c->odr_print, &gdu, 0, 0);
1682 z_GDU(c->odr_save, &gdu, 0, 0);
1683 if (gdu->which == Z_GDU_Z3950)
1684 ZOOM_handle_Z3950_apdu(c, gdu->u.z3950);
1685 else if (gdu->which == Z_GDU_HTTP_Response)
1688 handle_http(c, gdu->u.HTTP_Response);
1690 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1691 ZOOM_connection_close(c);
1699 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
1704 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1705 ZOOM_connection_put_event(c, event);
1707 yaz_log(c->log_details, "%p do_write_ex len=%d", c, len_out);
1708 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
1710 yaz_log(c->log_details, "%p do_write_ex write failed", c);
1711 if (ZOOM_test_reconnect(c))
1713 return zoom_pending;
1715 if (c->state == STATE_CONNECTING)
1716 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1718 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1719 ZOOM_connection_close(c);
1720 return zoom_complete;
1724 int mask = ZOOM_SELECT_EXCEPT;
1725 if (c->cs->io_pending & CS_WANT_WRITE)
1726 mask += ZOOM_SELECT_WRITE;
1727 if (c->cs->io_pending & CS_WANT_READ)
1728 mask += ZOOM_SELECT_READ;
1729 ZOOM_connection_set_mask(c, mask);
1730 yaz_log(c->log_details, "%p do_write_ex write incomplete mask=%d",
1735 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
1736 yaz_log(c->log_details, "%p do_write_ex write complete mask=%d",
1739 return zoom_pending;
1742 zoom_ret ZOOM_send_buf(ZOOM_connection c)
1744 return do_write_ex(c, c->buf_out, c->len_out);
1748 ZOOM_API(const char *)
1749 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
1751 if (!strcmp(key, "APDU"))
1753 return c->saveAPDU_wrbuf ? wrbuf_cstr(c->saveAPDU_wrbuf) : "";
1756 return ZOOM_options_get(c->options, key);
1759 ZOOM_API(const char *)
1760 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
1762 if (!strcmp(key, "APDU"))
1764 if (c->saveAPDU_wrbuf)
1766 *lenp = wrbuf_len(c->saveAPDU_wrbuf);
1767 return wrbuf_cstr(c->saveAPDU_wrbuf);
1776 return ZOOM_options_getl(c->options, key, lenp);
1780 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
1783 if (!strcmp(key, "saveAPDU"))
1785 if (val && strcmp(val, "0"))
1787 if (!c->saveAPDU_wrbuf)
1788 c->saveAPDU_wrbuf = wrbuf_alloc();
1790 wrbuf_rewind(c->saveAPDU_wrbuf);
1794 wrbuf_destroy(c->saveAPDU_wrbuf);
1795 c->saveAPDU_wrbuf = 0;
1797 ZOOM_connection_save_apdu_wrbuf(c, c->saveAPDU_wrbuf);
1800 ZOOM_options_set(c->options, key, val);
1804 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
1805 const char *val, int len)
1807 ZOOM_options_setl(c->options, key, val, len);
1810 ZOOM_API(const char *)
1811 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
1813 return ZOOM_options_get(r->options, key);
1817 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
1820 ZOOM_options_set(r->options, key, val);
1825 ZOOM_connection_errcode(ZOOM_connection c)
1827 return ZOOM_connection_error(c, 0, 0);
1830 ZOOM_API(const char *)
1831 ZOOM_connection_errmsg(ZOOM_connection c)
1834 ZOOM_connection_error(c, &msg, 0);
1838 ZOOM_API(const char *)
1839 ZOOM_connection_addinfo(ZOOM_connection c)
1841 const char *addinfo;
1842 ZOOM_connection_error(c, 0, &addinfo);
1846 ZOOM_API(const char *)
1847 ZOOM_connection_diagset(ZOOM_connection c)
1849 const char *diagset;
1850 ZOOM_connection_error_x(c, 0, 0, &diagset);
1854 ZOOM_API(const char *)
1855 ZOOM_diag_str(int error)
1859 case ZOOM_ERROR_NONE:
1861 case ZOOM_ERROR_CONNECT:
1862 return "Connect failed";
1863 case ZOOM_ERROR_MEMORY:
1864 return "Out of memory";
1865 case ZOOM_ERROR_ENCODE:
1866 return "Encoding failed";
1867 case ZOOM_ERROR_DECODE:
1868 return "Decoding failed";
1869 case ZOOM_ERROR_CONNECTION_LOST:
1870 return "Connection lost";
1871 case ZOOM_ERROR_INIT:
1872 return "Init rejected";
1873 case ZOOM_ERROR_INTERNAL:
1874 return "Internal failure";
1875 case ZOOM_ERROR_TIMEOUT:
1877 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
1878 return "Unsupported protocol";
1879 case ZOOM_ERROR_UNSUPPORTED_QUERY:
1880 return "Unsupported query type";
1881 case ZOOM_ERROR_INVALID_QUERY:
1882 return "Invalid query";
1883 case ZOOM_ERROR_CQL_PARSE:
1884 return "CQL parsing error";
1885 case ZOOM_ERROR_CQL_TRANSFORM:
1886 return "CQL transformation error";
1887 case ZOOM_ERROR_CCL_CONFIG:
1888 return "CCL configuration error";
1889 case ZOOM_ERROR_CCL_PARSE:
1890 return "CCL parsing error";
1891 case ZOOM_ERROR_ES_INVALID_ACTION:
1892 return "Extended Service. invalid action";
1893 case ZOOM_ERROR_ES_INVALID_VERSION:
1894 return "Extended Service. invalid version";
1895 case ZOOM_ERROR_ES_INVALID_SYNTAX:
1896 return "Extended Service. invalid syntax";
1897 case ZOOM_ERROR_MEMCACHED:
1900 return diagbib1_str(error);
1905 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
1906 const char **addinfo, const char **diagset)
1908 int error = c->error;
1911 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
1912 *cp = ZOOM_diag_str(error);
1913 else if (!strcmp(c->diagset, "HTTP"))
1914 *cp = z_HTTP_errmsg(c->error);
1915 else if (!strcmp(c->diagset, "Bib-1"))
1916 *cp = ZOOM_diag_str(error);
1917 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
1918 *cp = yaz_diag_srw_str(c->error);
1920 *cp = "Unknown error and diagnostic set";
1923 *addinfo = c->addinfo ? c->addinfo : "";
1925 *diagset = c->diagset ? c->diagset : "";
1930 ZOOM_connection_error(ZOOM_connection c, const char **cp,
1931 const char **addinfo)
1933 return ZOOM_connection_error_x(c, cp, addinfo, 0);
1936 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
1938 ZOOM_Event event = 0;
1939 int r = cs_look(c->cs);
1940 yaz_log(c->log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
1945 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1946 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1947 ZOOM_connection_close(c);
1948 ZOOM_connection_put_event(c, event);
1950 else if (r == CS_CONNECT)
1952 int ret = ret = cs_rcvconnect(c->cs);
1953 yaz_log(c->log_details, "%p ZOOM_connection_do_io "
1954 "cs_rcvconnect returned %d", c, ret);
1957 int mask = ZOOM_SELECT_EXCEPT;
1958 if (c->cs->io_pending & CS_WANT_WRITE)
1959 mask += ZOOM_SELECT_WRITE;
1960 if (c->cs->io_pending & CS_WANT_READ)
1961 mask += ZOOM_SELECT_READ;
1962 ZOOM_connection_set_mask(c, mask);
1963 event = ZOOM_Event_create(ZOOM_EVENT_NONE);
1964 ZOOM_connection_put_event(c, event);
1968 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1969 ZOOM_connection_put_event(c, event);
1971 if (c->proto == PROTO_Z3950)
1972 ZOOM_connection_Z3950_send_init(c);
1975 /* no init request for SRW .. */
1976 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1977 ZOOM_connection_remove_task(c);
1978 ZOOM_connection_set_mask(c, 0);
1979 ZOOM_connection_exec_task(c);
1981 c->state = STATE_ESTABLISHED;
1985 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1986 ZOOM_connection_close(c);
1991 if (mask & ZOOM_SELECT_EXCEPT)
1993 if (!ZOOM_test_reconnect(c))
1995 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1996 ZOOM_connection_close(c);
2000 if (mask & ZOOM_SELECT_READ)
2002 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2008 ZOOM_connection_last_event(ZOOM_connection cs)
2011 return ZOOM_EVENT_NONE;
2012 return cs->last_event;
2016 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
2020 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2021 /* timeout and this connection was waiting */
2022 ZOOM_set_error(c, ZOOM_ERROR_TIMEOUT, 0);
2023 ZOOM_connection_close(c);
2024 ZOOM_connection_put_event(c, event);
2030 ZOOM_connection_process(ZOOM_connection c)
2036 event = ZOOM_connection_get_event(c);
2039 ZOOM_Event_destroy(event);
2042 ZOOM_connection_exec_task(c);
2043 event = ZOOM_connection_get_event(c);
2046 ZOOM_Event_destroy(event);
2053 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
2057 yaz_log(log_details0, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
2059 for (i = 0; i<no; i++)
2061 ZOOM_connection c = cs[i];
2063 if (c && ZOOM_connection_process(c))
2069 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
2071 if (c->mask && mask)
2072 ZOOM_connection_do_io(c, mask);
2076 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
2079 return cs_fileno(c->cs);
2083 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
2091 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
2098 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
2100 return ZOOM_options_get_int(c->options, "timeout", 30);
2103 ZOOM_API(void) ZOOM_connection_close(ZOOM_connection c)
2108 ZOOM_connection_set_mask(c, 0);
2109 c->state = STATE_IDLE;
2115 * c-file-style: "Stroustrup"
2116 * indent-tabs-mode: nil
2118 * vim: shiftwidth=4 tabstop=8 expandtab