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);
510 if (c->sru_mode == zoom_sru_error)
512 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, val);
513 ZOOM_connection_remove_tasks(c);
517 yaz_log(c->log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
518 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
522 while (ZOOM_event(1, &c))
527 ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
529 #if ZOOM_RESULT_LISTS
533 /* remove ourselves from the resultsets in connection */
534 ZOOM_resultset *rp = &r->connection->resultsets;
537 assert(*rp); /* we must be in this list!! */
539 { /* OK, we're here - take us out of it */
551 ZOOM_connection_destroy(ZOOM_connection c)
553 #if ZOOM_RESULT_LISTS
554 ZOOM_resultsets list;
560 yaz_log(c->log_api, "%p ZOOM_connection_destroy", c);
564 #if ZOOM_RESULT_LISTS
565 /* Remove the connection's usage of resultsets */
566 list = c->resultsets;
568 ZOOM_resultsets removed = list;
569 ZOOM_resultset_destroy(list->resultset);
574 for (r = c->resultsets; r; r = r->next)
581 odr_destroy(c->odr_in);
582 odr_destroy(c->odr_out);
585 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
586 odr_destroy(c->odr_print);
588 ZOOM_options_destroy(c->options);
589 ZOOM_connection_remove_tasks(c);
590 ZOOM_connection_remove_events(c);
595 xfree(c->cookie_out);
601 xfree(c->sru_version);
605 void ZOOM_resultset_addref(ZOOM_resultset r)
609 yaz_mutex_enter(r->mutex);
611 yaz_log(log_details0, "%p ZOOM_resultset_addref count=%d",
613 yaz_mutex_leave(r->mutex);
617 static int g_resultsets = 0;
618 static YAZ_MUTEX g_resultset_mutex = 0;
620 /* TODO We need to initialize this before running threaded:
621 * call resultset_use(0) */
623 static int resultset_use(int delta) {
625 if (g_resultset_mutex == 0)
626 yaz_mutex_create(&g_resultset_mutex);
627 yaz_mutex_enter(g_resultset_mutex);
628 g_resultsets += delta;
629 resultset_count = g_resultsets;
630 yaz_mutex_leave(g_resultset_mutex);
631 return resultset_count;
634 int resultsets_count(void) {
635 return resultset_use(0);
638 ZOOM_resultset ZOOM_resultset_create(void)
641 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
645 yaz_log(log_details0, "%p ZOOM_resultset_create", r);
648 r->odr = odr_createmem(ODR_ENCODE);
653 for (i = 0; i<RECORD_HASH_SIZE; i++)
654 r->record_hash[i] = 0;
658 r->databaseNames = 0;
659 r->num_databaseNames = 0;
664 yaz_mutex_create(&r->mutex);
667 WRBUF w = wrbuf_alloc();
668 YAZ_SHPTR_INIT(r->record_wrbuf, w);
675 ZOOM_API(ZOOM_resultset)
676 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
679 ZOOM_query s = ZOOM_query_create();
681 ZOOM_query_prefix(s, q);
683 r = ZOOM_connection_search(c, s);
684 ZOOM_query_destroy(s);
688 ZOOM_API(ZOOM_resultset)
689 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
691 ZOOM_resultset r = ZOOM_resultset_create();
695 const char *syntax, *elementSetName;
696 #if ZOOM_RESULT_LISTS
700 yaz_log(c->log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
701 r->r_sort_spec = ZOOM_query_get_sortspec(q);
704 r->options = ZOOM_options_create_with_parent(c->options);
706 start = ZOOM_options_get_int(r->options, "start", 0);
707 count = ZOOM_options_get_int(r->options, "count", 0);
709 /* If "presentChunk" is defined use that; otherwise "step" */
710 const char *cp = ZOOM_options_get(r->options, "presentChunk");
711 r->step = ZOOM_options_get_int(r->options,
712 (cp != 0 ? "presentChunk": "step"), 0);
714 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
715 cp = ZOOM_options_get(r->options, "setname");
717 r->setname = xstrdup(cp);
718 cp = ZOOM_options_get(r->options, "schema");
720 r->schema = xstrdup(cp);
722 r->databaseNames = ZOOM_connection_get_databases(c, c->options, &r->num_databaseNames,
727 #if ZOOM_RESULT_LISTS
728 yaz_log(log_details, "%p ZOOM_connection_search: Adding new resultset (%p) to resultsets (%p) ", c, r, c->resultsets);
729 set = xmalloc(sizeof(*set));
730 ZOOM_resultset_addref(r);
732 set->next = c->resultsets;
735 r->next = c->resultsets;
738 if (c->host_port && c->proto == PROTO_HTTP)
742 yaz_log(c->log_details, "ZOOM_connection_search: no comstack");
743 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
747 yaz_log(c->log_details, "ZOOM_connection_search: reconnect");
752 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
753 task->u.search.resultset = r;
754 task->u.search.start = start;
755 task->u.search.count = count;
756 task->u.search.recv_search_fired = 0;
758 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
759 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
760 elementSetName = ZOOM_options_get(r->options, "elementSetName");
761 task->u.search.elementSetName = elementSetName
762 ? xstrdup(elementSetName) : 0;
764 ZOOM_resultset_addref(r);
766 ZOOM_query_addref(q);
770 while (ZOOM_event(1, &c))
777 ZOOM_resultset_sort(ZOOM_resultset r,
778 const char *sort_type, const char *sort_spec)
780 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
784 ZOOM_resultset_sort1(ZOOM_resultset r,
785 const char *sort_type, const char *sort_spec)
787 ZOOM_connection c = r->connection;
791 newq = ZOOM_query_create();
792 if (ZOOM_query_sortby(newq, sort_spec) < 0)
795 yaz_log(c->log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
796 r, r, sort_type, sort_spec);
800 if (c->host_port && c->proto == PROTO_HTTP)
804 yaz_log(c->log_details, "%p ZOOM_resultset_sort: no comstack", r);
805 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
809 yaz_log(c->log_details, "%p ZOOM_resultset_sort: prepare reconnect",
815 ZOOM_resultset_cache_reset(r);
816 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
817 task->u.sort.resultset = r;
818 task->u.sort.q = newq;
820 ZOOM_resultset_addref(r);
824 while (ZOOM_event(1, &c))
832 ZOOM_resultset_destroy(ZOOM_resultset r)
834 resultset_destroy(r);
837 static void resultset_destroy(ZOOM_resultset r)
841 yaz_mutex_enter(r->mutex);
843 yaz_log(log_details0, "%p ZOOM_resultset_destroy r=%p count=%d",
845 if (r->refcount == 0)
847 yaz_mutex_leave(r->mutex);
849 yaz_log(log_details0, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r);
850 ZOOM_resultset_cache_reset(r);
851 ZOOM_resultset_release(r);
852 ZOOM_query_destroy(r->query);
853 ZOOM_options_destroy(r->options);
857 yaz_mutex_destroy(&r->mutex);
859 YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
865 yaz_mutex_leave(r->mutex);
869 ZOOM_resultset_size(ZOOM_resultset r)
874 int ZOOM_test_reconnect(ZOOM_connection c)
878 if (!c->reconnect_ok)
880 ZOOM_connection_close(c);
882 c->tasks->running = 0;
883 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
885 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
886 ZOOM_connection_put_event(c, event);
891 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
892 int force_sync, int start, int count)
897 const char *syntax, *elementSetName;
901 yaz_log(log_details0, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
902 " count=%d", r, force_sync, start, count);
907 if (c->host_port && c->proto == PROTO_HTTP)
911 yaz_log(log_details0, "%p ZOOM_resultset_retrieve: no comstack", r);
912 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
916 yaz_log(log_details0, "%p ZOOM_resultset_retrieve: prepare "
921 task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
922 task->u.retrieve.resultset = r;
923 task->u.retrieve.start = start;
924 task->u.retrieve.count = count;
926 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
927 task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
928 elementSetName = ZOOM_options_get(r->options, "elementSetName");
929 task->u.retrieve.elementSetName = elementSetName
930 ? xstrdup(elementSetName) : 0;
932 cp = ZOOM_options_get(r->options, "schema");
935 if (!r->schema || strcmp(r->schema, cp))
938 r->schema = xstrdup(cp);
942 ZOOM_resultset_addref(r);
944 if (!r->connection->async || force_sync)
945 while (r->connection && ZOOM_event(1, &r->connection))
950 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
951 size_t start, size_t count)
953 int force_present = 0;
957 yaz_log(log_api0, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
958 r, r, (long) start, (long) count);
961 ZOOM_resultset_retrieve(r, force_present, start, count);
965 for (i = 0; i< count; i++)
966 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
971 ZOOM_resultset_facets_size(ZOOM_resultset r) {
972 return r->num_facets;
975 ZOOM_API(ZOOM_facet_field)
976 ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name) {
977 int num = r->num_facets;
978 ZOOM_facet_field *facets = r->facets;
980 for (index = 0; index < num; index++) {
981 if (!strcmp(facets[index]->facet_name, name)) {
982 return facets[index];
989 ZOOM_API(ZOOM_facet_field *)
990 ZOOM_resultset_facets(ZOOM_resultset r)
995 ZOOM_API(const char**)
996 ZOOM_resultset_facet_names(ZOOM_resultset r)
998 return (const char **) r->facets_names;
1001 ZOOM_API(const char*)
1002 ZOOM_facet_field_name(ZOOM_facet_field field)
1004 return field->facet_name;
1008 ZOOM_facet_field_term_count(ZOOM_facet_field field)
1010 return field->num_terms;
1013 ZOOM_API(const char*)
1014 ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq) {
1015 *freq = field->facet_terms[idx].frequency;
1016 return field->facet_terms[idx].term;
1020 static void get_cert(ZOOM_connection c)
1025 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1027 ZOOM_connection_option_setl(c, "sslPeerCert",
1028 cert_buf, cert_len);
1033 static zoom_ret do_connect_host(ZOOM_connection c,
1034 const char *logical_url);
1036 static zoom_ret do_connect(ZOOM_connection c)
1038 return do_connect_host(c, c->host_port);
1041 static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
1047 c->cs = cs_create_host_proxy(logical_url, 0, &add, c->proxy);
1049 if (c->cs && c->cs->protocol == PROTO_HTTP)
1052 c->proto = PROTO_HTTP;
1054 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1055 ZOOM_connection_close(c);
1056 return zoom_complete;
1061 int ret = cs_connect(c->cs, add);
1064 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1065 ZOOM_connection_put_event(c, event);
1067 if (c->proto == PROTO_Z3950)
1068 ZOOM_connection_Z3950_send_init(c);
1071 /* no init request for SRW .. */
1072 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1073 ZOOM_connection_remove_task(c);
1074 ZOOM_connection_set_mask(c, 0);
1075 ZOOM_connection_exec_task(c);
1077 c->state = STATE_ESTABLISHED;
1078 return zoom_pending;
1082 int mask = ZOOM_SELECT_EXCEPT;
1083 if (c->cs->io_pending & CS_WANT_WRITE)
1084 mask += ZOOM_SELECT_WRITE;
1085 if (c->cs->io_pending & CS_WANT_READ)
1086 mask += ZOOM_SELECT_READ;
1087 ZOOM_connection_set_mask(c, mask);
1088 c->state = STATE_CONNECTING;
1089 return zoom_pending;
1092 c->state = STATE_IDLE;
1093 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, logical_url);
1094 return zoom_complete;
1097 /* returns 1 if PDU was sent OK (still pending )
1098 0 if PDU was not sent OK (nothing to wait for)
1101 ZOOM_API(ZOOM_record)
1102 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1104 const char *syntax =
1105 ZOOM_options_get(s->options, "preferredRecordSyntax");
1106 const char *elementSetName =
1107 ZOOM_options_get(s->options, "elementSetName");
1109 return ZOOM_record_cache_lookup(s, pos, syntax, elementSetName);
1112 ZOOM_API(ZOOM_record)
1113 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1115 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1120 * MIKE: I think force_sync should always be zero, but I don't
1121 * want to make this change until I get the go-ahead from
1122 * Adam, in case something depends on the old synchronous
1126 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1127 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1128 rec = ZOOM_resultset_record_immediate(r, pos);
1133 ZOOM_API(ZOOM_scanset)
1134 ZOOM_connection_scan(ZOOM_connection c, const char *start)
1137 ZOOM_query q = ZOOM_query_create();
1139 ZOOM_query_prefix(q, start);
1141 s = ZOOM_connection_scan1(c, q);
1142 ZOOM_query_destroy(q);
1147 ZOOM_API(ZOOM_scanset)
1148 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
1150 ZOOM_scanset scan = 0;
1151 Z_Query *z_query = ZOOM_query_get_Z_Query(q);
1155 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
1156 scan->connection = c;
1157 scan->odr = odr_createmem(ODR_DECODE);
1158 scan->options = ZOOM_options_create_with_parent(c->options);
1160 scan->scan_response = 0;
1161 scan->srw_scan_response = 0;
1164 ZOOM_query_addref(q);
1165 scan->databaseNames = ZOOM_connection_get_databases(c, c->options,
1166 &scan->num_databaseNames,
1171 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
1172 task->u.scan.scan = scan;
1177 while (ZOOM_event(1, &c))
1185 ZOOM_scanset_destroy(ZOOM_scanset scan)
1190 if (scan->refcount == 0)
1192 ZOOM_query_destroy(scan->query);
1194 odr_destroy(scan->odr);
1196 ZOOM_options_destroy(scan->options);
1201 static zoom_ret send_package(ZOOM_connection c)
1205 yaz_log(c->log_details, "%p send_package", c);
1207 return zoom_complete;
1208 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1210 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1211 ZOOM_connection_put_event(c, event);
1213 c->buf_out = c->tasks->u.package->buf_out;
1214 c->len_out = c->tasks->u.package->len_out;
1216 return ZOOM_send_buf(c);
1220 ZOOM_scanset_size(ZOOM_scanset scan)
1225 if (scan->scan_response && scan->scan_response->entries)
1226 return scan->scan_response->entries->num_entries;
1227 else if (scan->srw_scan_response)
1228 return scan->srw_scan_response->num_terms;
1232 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
1234 const char **value_term, size_t *value_len,
1235 const char **disp_term, size_t *disp_len)
1237 size_t noent = ZOOM_scanset_size(scan);
1248 if (scan->scan_response)
1250 Z_ScanResponse *res = scan->scan_response;
1251 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1253 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1255 *value_term = (const char *) t->term->u.general->buf;
1256 *value_len = t->term->u.general->len;
1259 *disp_term = t->displayTerm;
1260 *disp_len = strlen(*disp_term);
1262 else if (t->term->which == Z_Term_general)
1264 *disp_term = (const char *) t->term->u.general->buf;
1265 *disp_len = t->term->u.general->len;
1267 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1270 if (scan->srw_scan_response)
1272 Z_SRW_scanResponse *res = scan->srw_scan_response;
1273 Z_SRW_scanTerm *t = res->terms + pos;
1276 *value_term = t->value;
1277 *value_len = strlen(*value_term);
1280 *disp_term = t->displayTerm;
1282 *disp_term = t->value;
1283 *disp_len = strlen(*disp_term);
1284 *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
1289 ZOOM_API(const char *)
1290 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
1291 size_t *occ, size_t *len)
1293 const char *value_term = 0;
1294 size_t value_len = 0;
1295 const char *disp_term = 0;
1296 size_t disp_len = 0;
1298 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1299 &disp_term, &disp_len);
1305 ZOOM_API(const char *)
1306 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
1307 size_t *occ, size_t *len)
1309 const char *value_term = 0;
1310 size_t value_len = 0;
1311 const char *disp_term = 0;
1312 size_t disp_len = 0;
1314 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1315 &disp_term, &disp_len);
1321 ZOOM_API(const char *)
1322 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
1324 return ZOOM_options_get(scan->options, key);
1328 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
1331 ZOOM_options_set(scan->options, key, val);
1335 ZOOM_API(ZOOM_package)
1336 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
1338 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
1341 p->odr_out = odr_createmem(ODR_ENCODE);
1342 p->options = ZOOM_options_create_with_parent2(options, c->options);
1350 ZOOM_package_destroy(ZOOM_package p)
1355 if (p->refcount == 0)
1357 odr_destroy(p->odr_out);
1360 ZOOM_options_destroy(p->options);
1365 ZOOM_API(const char *)
1366 ZOOM_package_option_get(ZOOM_package p, const char *key)
1368 return ZOOM_options_get(p->options, key);
1371 ZOOM_API(const char *)
1372 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
1374 return ZOOM_options_getl(p->options, key, lenp);
1378 ZOOM_package_option_set(ZOOM_package p, const char *key,
1381 ZOOM_options_set(p->options, key, val);
1385 ZOOM_package_option_setl(ZOOM_package p, const char *key,
1386 const char *val, int len)
1388 ZOOM_options_setl(p->options, key, val, len);
1392 ZOOM_connection_exec_task(ZOOM_connection c)
1394 ZOOM_task task = c->tasks;
1395 zoom_ret ret = zoom_complete;
1399 yaz_log(c->log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
1400 c, task->which, task->running);
1401 if (c->error != ZOOM_ERROR_NONE)
1403 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1404 "removing tasks because of error = %d", c, c->error);
1405 ZOOM_connection_remove_tasks(c);
1410 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1411 "task already running", c);
1415 ret = zoom_complete;
1416 if (c->cs || task->which == ZOOM_TASK_CONNECT)
1418 switch (task->which)
1420 case ZOOM_TASK_SEARCH:
1421 if (c->proto == PROTO_HTTP)
1422 ret = ZOOM_connection_srw_send_search(c);
1424 ret = ZOOM_connection_Z3950_send_search(c);
1426 case ZOOM_TASK_RETRIEVE:
1427 if (c->proto == PROTO_HTTP)
1428 ret = ZOOM_connection_srw_send_search(c);
1430 ret = send_Z3950_present(c);
1432 case ZOOM_TASK_CONNECT:
1433 ret = do_connect(c);
1435 case ZOOM_TASK_SCAN:
1436 if (c->proto == PROTO_HTTP)
1437 ret = ZOOM_connection_srw_send_scan(c);
1439 ret = ZOOM_connection_Z3950_send_scan(c);
1441 case ZOOM_TASK_PACKAGE:
1442 ret = send_package(c);
1444 case ZOOM_TASK_SORT:
1445 c->tasks->u.sort.resultset->r_sort_spec =
1446 ZOOM_query_get_sortspec(c->tasks->u.sort.q);
1447 ret = send_Z3950_sort(c, c->tasks->u.sort.resultset);
1453 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1454 "remove tasks because no connection exist", c);
1455 ZOOM_connection_remove_tasks(c);
1457 if (ret == zoom_complete)
1459 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1460 "task removed (complete)", c);
1461 ZOOM_connection_remove_task(c);
1464 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1471 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri,
1472 Z_HTTP_Response *cookie_hres)
1474 struct Z_HTTP_Header *h;
1475 char *combined_cookies = 0;
1476 int combined_cookies_len = 0;
1477 Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 0);
1479 gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
1480 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
1483 for (h = cookie_hres->headers; h; h = h->next)
1485 if (!strcmp(h->name, "Set-Cookie"))
1489 if (!(cp = strchr(h->value, ';')))
1490 cp = h->value + strlen(h->value);
1491 if (cp - h->value >= 1) {
1492 combined_cookies = xrealloc(combined_cookies, combined_cookies_len + cp - h->value + 3);
1493 memcpy(combined_cookies+combined_cookies_len, h->value, cp - h->value);
1494 combined_cookies[combined_cookies_len + cp - h->value] = '\0';
1495 strcat(combined_cookies,"; ");
1496 combined_cookies_len = strlen(combined_cookies);
1501 if (combined_cookies_len)
1503 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
1504 "Cookie", combined_cookies);
1505 xfree(combined_cookies);
1508 if (c->user && c->password)
1510 z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
1511 c->user, c->password);
1513 if (!z_GDU(c->odr_out, &gdu, 0, 0))
1514 return zoom_complete;
1516 z_GDU(c->odr_print, &gdu, 0, 0);
1517 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1519 odr_reset(c->odr_out);
1520 return ZOOM_send_buf(c);
1524 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
1525 const char *addinfo, const char *addinfo2)
1527 ZOOM_set_dset_error(c, error, "HTTP", addinfo, addinfo2);
1532 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
1534 zoom_ret cret = zoom_complete;
1536 const char *addinfo = 0;
1537 const char *connection_head = z_HTTP_header_lookup(hres->headers,
1539 const char *location;
1541 ZOOM_connection_set_mask(c, 0);
1542 yaz_log(c->log_details, "%p handle_http", c);
1544 if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
1545 && (location = z_HTTP_header_lookup(hres->headers, "Location")))
1548 if (c->no_redirects > 10)
1550 ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1551 c->no_redirects = 0;
1552 ZOOM_connection_close(c);
1556 /* since redirect may change host we just reconnect. A smarter
1557 implementation might check whether it's the same server */
1558 do_connect_host(c, location);
1559 send_HTTP_redirect(c, location, hres);
1560 /* we're OK for now. Operation is not really complete */
1566 ret = ZOOM_handle_sru(c, hres, &cret, &addinfo);
1569 if (c->no_redirects) /* end of redirect. change hosts again */
1570 ZOOM_connection_close(c);
1572 c->no_redirects = 0;
1576 if (hres->code != 200)
1577 ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1580 yaz_log(YLOG_LOG, "set error... addinfo=%s", addinfo ?
1582 ZOOM_set_error(c, ZOOM_ERROR_DECODE, addinfo);
1584 ZOOM_connection_close(c);
1586 if (cret == zoom_complete)
1588 yaz_log(c->log_details, "removing tasks in handle_http");
1589 ZOOM_connection_remove_task(c);
1593 if (!strcmp(hres->version, "1.0"))
1595 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
1596 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
1601 /* HTTP 1.1: only if no close we stay alive.. */
1602 if (connection_head && !strcmp(connection_head, "close"))
1607 ZOOM_connection_close(c);
1610 c->tasks->running = 0;
1611 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1612 c->reconnect_ok = 0;
1619 static int do_read(ZOOM_connection c)
1624 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
1625 ZOOM_connection_put_event(c, event);
1627 r = cs_get(c->cs, &c->buf_in, &c->len_in);
1628 more = cs_more(c->cs);
1629 yaz_log(c->log_details, "%p do_read len=%d more=%d", c, r, more);
1634 if (!ZOOM_test_reconnect(c))
1636 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1637 ZOOM_connection_close(c);
1645 odr_reset(c->odr_in);
1646 odr_setbuf(c->odr_in, c->buf_in, r, 0);
1647 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
1648 ZOOM_connection_put_event(c, event);
1650 if (!z_GDU(c->odr_in, &gdu, 0, 0))
1653 int err = odr_geterrorx(c->odr_in, &x);
1655 const char *element = odr_getelement(c->odr_in);
1656 yaz_snprintf(msg, sizeof(msg),
1657 "ODR code %d:%d element=%s offset=%d",
1658 err, x, element ? element : "<unknown>",
1659 odr_offset(c->odr_in));
1660 ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg);
1663 FILE *ber_file = yaz_log_file();
1665 odr_dumpBER(ber_file, c->buf_in, r);
1667 ZOOM_connection_close(c);
1672 z_GDU(c->odr_print, &gdu, 0, 0);
1673 if (gdu->which == Z_GDU_Z3950)
1674 ZOOM_handle_Z3950_apdu(c, gdu->u.z3950);
1675 else if (gdu->which == Z_GDU_HTTP_Response)
1678 handle_http(c, gdu->u.HTTP_Response);
1680 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1681 ZOOM_connection_close(c);
1685 c->reconnect_ok = 0;
1690 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
1695 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1696 ZOOM_connection_put_event(c, event);
1698 yaz_log(c->log_details, "%p do_write_ex len=%d", c, len_out);
1699 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
1701 yaz_log(c->log_details, "%p do_write_ex write failed", c);
1702 if (ZOOM_test_reconnect(c))
1704 return zoom_pending;
1706 if (c->state == STATE_CONNECTING)
1707 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1709 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1710 ZOOM_connection_close(c);
1711 return zoom_complete;
1715 int mask = ZOOM_SELECT_EXCEPT;
1716 if (c->cs->io_pending & CS_WANT_WRITE)
1717 mask += ZOOM_SELECT_WRITE;
1718 if (c->cs->io_pending & CS_WANT_READ)
1719 mask += ZOOM_SELECT_READ;
1720 ZOOM_connection_set_mask(c, mask);
1721 yaz_log(c->log_details, "%p do_write_ex write incomplete mask=%d",
1726 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
1727 yaz_log(c->log_details, "%p do_write_ex write complete mask=%d",
1730 return zoom_pending;
1733 zoom_ret ZOOM_send_buf(ZOOM_connection c)
1735 return do_write_ex(c, c->buf_out, c->len_out);
1739 ZOOM_API(const char *)
1740 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
1742 return ZOOM_options_get(c->options, key);
1745 ZOOM_API(const char *)
1746 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
1748 return ZOOM_options_getl(c->options, key, lenp);
1752 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
1755 ZOOM_options_set(c->options, key, val);
1759 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
1760 const char *val, int len)
1762 ZOOM_options_setl(c->options, key, val, len);
1765 ZOOM_API(const char *)
1766 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
1768 return ZOOM_options_get(r->options, key);
1772 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
1775 ZOOM_options_set(r->options, key, val);
1780 ZOOM_connection_errcode(ZOOM_connection c)
1782 return ZOOM_connection_error(c, 0, 0);
1785 ZOOM_API(const char *)
1786 ZOOM_connection_errmsg(ZOOM_connection c)
1789 ZOOM_connection_error(c, &msg, 0);
1793 ZOOM_API(const char *)
1794 ZOOM_connection_addinfo(ZOOM_connection c)
1796 const char *addinfo;
1797 ZOOM_connection_error(c, 0, &addinfo);
1801 ZOOM_API(const char *)
1802 ZOOM_connection_diagset(ZOOM_connection c)
1804 const char *diagset;
1805 ZOOM_connection_error_x(c, 0, 0, &diagset);
1809 ZOOM_API(const char *)
1810 ZOOM_diag_str(int error)
1814 case ZOOM_ERROR_NONE:
1816 case ZOOM_ERROR_CONNECT:
1817 return "Connect failed";
1818 case ZOOM_ERROR_MEMORY:
1819 return "Out of memory";
1820 case ZOOM_ERROR_ENCODE:
1821 return "Encoding failed";
1822 case ZOOM_ERROR_DECODE:
1823 return "Decoding failed";
1824 case ZOOM_ERROR_CONNECTION_LOST:
1825 return "Connection lost";
1826 case ZOOM_ERROR_INIT:
1827 return "Init rejected";
1828 case ZOOM_ERROR_INTERNAL:
1829 return "Internal failure";
1830 case ZOOM_ERROR_TIMEOUT:
1832 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
1833 return "Unsupported protocol";
1834 case ZOOM_ERROR_UNSUPPORTED_QUERY:
1835 return "Unsupported query type";
1836 case ZOOM_ERROR_INVALID_QUERY:
1837 return "Invalid query";
1838 case ZOOM_ERROR_CQL_PARSE:
1839 return "CQL parsing error";
1840 case ZOOM_ERROR_CQL_TRANSFORM:
1841 return "CQL transformation error";
1842 case ZOOM_ERROR_CCL_CONFIG:
1843 return "CCL configuration error";
1844 case ZOOM_ERROR_CCL_PARSE:
1845 return "CCL parsing error";
1846 case ZOOM_ERROR_ES_INVALID_ACTION:
1847 return "Extended Service. invalid action";
1848 case ZOOM_ERROR_ES_INVALID_VERSION:
1849 return "Extended Service. invalid version";
1850 case ZOOM_ERROR_ES_INVALID_SYNTAX:
1851 return "Extended Service. invalid syntax";
1853 return diagbib1_str(error);
1858 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
1859 const char **addinfo, const char **diagset)
1861 int error = c->error;
1864 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
1865 *cp = ZOOM_diag_str(error);
1866 else if (!strcmp(c->diagset, "HTTP"))
1867 *cp = z_HTTP_errmsg(c->error);
1868 else if (!strcmp(c->diagset, "Bib-1"))
1869 *cp = ZOOM_diag_str(error);
1870 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
1871 *cp = yaz_diag_srw_str(c->error);
1873 *cp = "Unknown error and diagnostic set";
1876 *addinfo = c->addinfo ? c->addinfo : "";
1878 *diagset = c->diagset ? c->diagset : "";
1883 ZOOM_connection_error(ZOOM_connection c, const char **cp,
1884 const char **addinfo)
1886 return ZOOM_connection_error_x(c, cp, addinfo, 0);
1889 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
1891 ZOOM_Event event = 0;
1892 int r = cs_look(c->cs);
1893 yaz_log(c->log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
1898 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1899 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1900 ZOOM_connection_close(c);
1901 ZOOM_connection_put_event(c, event);
1903 else if (r == CS_CONNECT)
1905 int ret = ret = cs_rcvconnect(c->cs);
1906 yaz_log(c->log_details, "%p ZOOM_connection_do_io "
1907 "cs_rcvconnect returned %d", c, ret);
1910 int mask = ZOOM_SELECT_EXCEPT;
1911 if (c->cs->io_pending & CS_WANT_WRITE)
1912 mask += ZOOM_SELECT_WRITE;
1913 if (c->cs->io_pending & CS_WANT_READ)
1914 mask += ZOOM_SELECT_READ;
1915 ZOOM_connection_set_mask(c, mask);
1916 event = ZOOM_Event_create(ZOOM_EVENT_NONE);
1917 ZOOM_connection_put_event(c, event);
1921 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1922 ZOOM_connection_put_event(c, event);
1924 if (c->proto == PROTO_Z3950)
1925 ZOOM_connection_Z3950_send_init(c);
1928 /* no init request for SRW .. */
1929 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1930 ZOOM_connection_remove_task(c);
1931 ZOOM_connection_set_mask(c, 0);
1932 ZOOM_connection_exec_task(c);
1934 c->state = STATE_ESTABLISHED;
1938 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1939 ZOOM_connection_close(c);
1944 if (mask & ZOOM_SELECT_EXCEPT)
1946 if (!ZOOM_test_reconnect(c))
1948 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1949 ZOOM_connection_close(c);
1953 if (mask & ZOOM_SELECT_READ)
1955 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1961 ZOOM_connection_last_event(ZOOM_connection cs)
1964 return ZOOM_EVENT_NONE;
1965 return cs->last_event;
1969 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
1973 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
1974 /* timeout and this connection was waiting */
1975 ZOOM_set_error(c, ZOOM_ERROR_TIMEOUT, 0);
1976 ZOOM_connection_close(c);
1977 ZOOM_connection_put_event(c, event);
1983 ZOOM_connection_process(ZOOM_connection c)
1989 event = ZOOM_connection_get_event(c);
1992 ZOOM_Event_destroy(event);
1995 ZOOM_connection_exec_task(c);
1996 event = ZOOM_connection_get_event(c);
1999 ZOOM_Event_destroy(event);
2006 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
2010 yaz_log(log_details0, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
2012 for (i = 0; i<no; i++)
2014 ZOOM_connection c = cs[i];
2016 if (c && ZOOM_connection_process(c))
2022 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
2024 if (c->mask && mask)
2025 ZOOM_connection_do_io(c, mask);
2029 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
2032 return cs_fileno(c->cs);
2036 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
2044 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
2051 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
2053 return ZOOM_options_get_int(c->options, "timeout", 30);
2056 ZOOM_API(void) ZOOM_connection_close(ZOOM_connection c)
2061 ZOOM_connection_set_mask(c, 0);
2062 c->state = STATE_IDLE;
2068 * c-file-style: "Stroustrup"
2069 * indent-tabs-mode: nil
2071 * vim: shiftwidth=4 tabstop=8 expandtab