1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 /** \file connection.c
21 \brief Z39.50 connection (low-level client)
42 #include <yaz/comstack.h>
43 #include <yaz/tcpip.h>
44 #include "connection.h"
51 /** \brief Represents a physical, reusable connection to a remote Z39.50 host
57 struct client *client;
64 int operation_timeout;
66 struct connection *next; // next for same host or next in free list
69 static int connection_connect(struct connection *con, iochan_man_t iochan_man);
71 static int connection_is_idle(struct connection *co)
73 ZOOM_connection link = co->link;
76 if (co->state != Conn_Open || !link)
79 if (!ZOOM_connection_is_idle(link))
81 event = ZOOM_connection_peek_event(link);
82 if (event == ZOOM_EVENT_NONE)
88 ZOOM_connection connection_get_link(struct connection *co)
93 static void remove_connection_from_host(struct connection *con)
95 struct connection **conp = &con->host->connections;
101 *conp = (*conp)->next;
104 conp = &(*conp)->next;
108 // Close connection and recycle structure
109 static void connection_destroy(struct connection *co)
113 ZOOM_connection_destroy(co->link);
114 iochan_destroy(co->iochan);
116 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
120 client_disconnect(co->client);
123 remove_connection_from_host(co);
128 // Creates a new connection for client, associated with the host of
130 static struct connection *connection_create(struct client *cl,
131 int operation_timeout,
133 iochan_man_t iochan_man)
135 struct connection *co;
136 struct host *host = client_get_host(cl);
138 co = xmalloc(sizeof(*co));
143 client_set_connection(cl, co);
145 co->state = Conn_Resolving;
146 co->operation_timeout = operation_timeout;
147 co->session_timeout = session_timeout;
149 connection_connect(co, iochan_man);
151 yaz_mutex_enter(host->mutex);
152 co->next = co->host->connections;
153 co->host->connections = co;
154 yaz_mutex_leave(host->mutex);
159 static void non_block_events(struct connection *co)
162 IOCHAN iochan = co->iochan;
163 ZOOM_connection link = co->link;
166 struct client *cl = co->client;
168 int r = ZOOM_event_nonblock(1, &link);
173 ev = ZOOM_connection_last_event(link);
176 yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
182 const char *error, *addinfo;
184 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
186 yaz_log(YLOG_LOG, "Error %s from %s",
187 error, client_get_url(cl));
189 iochan_settimeout(iochan, co->session_timeout);
190 client_set_diagnostic(cl, err);
191 client_set_state(cl, Client_Idle);
194 case ZOOM_EVENT_SEND_DATA:
196 case ZOOM_EVENT_RECV_DATA:
198 case ZOOM_EVENT_UNKNOWN:
200 case ZOOM_EVENT_SEND_APDU:
201 client_set_state(co->client, Client_Working);
202 iochan_settimeout(iochan, co->operation_timeout);
204 case ZOOM_EVENT_RECV_APDU:
206 case ZOOM_EVENT_CONNECT:
207 yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
208 co->state = Conn_Open;
210 case ZOOM_EVENT_RECV_SEARCH:
211 client_search_response(cl);
213 case ZOOM_EVENT_RECV_RECORD:
214 client_record_response(cl);
218 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
219 ev, client_get_url(cl));
224 struct client *cl = co->client;
227 client_got_records(cl);
232 void connection_continue(struct connection *co)
234 int r = ZOOM_connection_exec_task(co->link);
236 yaz_log(YLOG_WARN, "No task was executed for connection");
239 static void connection_handler(IOCHAN iochan, int event)
241 struct connection *co = iochan_getdata(iochan);
243 struct host *host = co->host;
245 yaz_mutex_enter(host->mutex);
249 /* no client associated with it.. We are probably getting
250 a closed connection from the target.. Or, perhaps, an unexpected
251 package.. We will just close the connection */
252 yaz_log(YLOG_LOG, "timeout connection %p event=%d", co, event);
253 connection_destroy(co);
254 yaz_mutex_leave(host->mutex);
256 else if (event & EVENT_TIMEOUT)
258 if (co->state == Conn_Connecting)
260 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
262 client_set_state(cl, Client_Error);
263 connection_destroy(co);
265 else if (client_get_state(co->client) == Client_Idle)
267 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
268 connection_destroy(co);
272 yaz_log(YLOG_LOG, "ignore timeout %s", client_get_url(cl));
274 yaz_mutex_leave(host->mutex);
278 yaz_mutex_leave(host->mutex);
281 non_block_events(co);
283 ZOOM_connection_fire_event_socket(co->link, event);
285 non_block_events(co);
291 // Disassociate connection from client
292 static void connection_release(struct connection *co)
294 struct client *cl = co->client;
298 client_set_connection(cl, 0);
302 void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
304 struct connection *con;
307 yaz_mutex_enter(host->mutex);
308 con = host->connections;
311 if (con->state == Conn_Resolving)
313 if (!host->ipport) /* unresolved */
315 yaz_mutex_leave(host->mutex);
316 connection_destroy(con);
318 /* start all over .. at some point it will be NULL */
320 else if (!con->client)
322 yaz_mutex_leave(host->mutex);
323 connection_destroy(con);
324 /* start all over .. at some point it will be NULL */
329 yaz_mutex_leave(host->mutex);
330 connection_connect(con, iochan_man);
331 client_start_search(con->client);
337 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
341 yaz_mutex_leave(host->mutex);
344 static struct host *connection_get_host(struct connection *con)
349 // Callback for use by event loop
350 // We do this because ZOOM connections don't always have (the same) sockets
351 static int socketfun(IOCHAN c)
353 struct connection *co = iochan_getdata(c);
356 return ZOOM_connection_get_socket(co->link);
359 // Because ZOOM always knows what events it is interested in; we may not
360 static int maskfun(IOCHAN c)
362 struct connection *co = iochan_getdata(c);
366 // This is cheating a little, and assuming that eventl mask IDs are always
367 // the same as ZOOM-C's.
368 return ZOOM_connection_get_mask(co->link);
371 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
373 ZOOM_connection link = 0;
374 struct host *host = connection_get_host(con);
375 ZOOM_options zoptions = ZOOM_options_create();
379 const char *sru_version = 0;
381 struct session_database *sdb = client_get_database(con->client);
382 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
383 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
385 assert(host->ipport);
388 ZOOM_options_set(zoptions, "async", "1");
389 ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
390 ZOOM_options_set(zoptions, "implementationVersion", VERSION);
392 if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
393 ZOOM_options_set(zoptions, "charset", charset);
395 if (zproxy && *zproxy)
397 con->zproxy = xstrdup(zproxy);
398 ZOOM_options_set(zoptions, "proxy", zproxy);
400 if (apdulog && *apdulog)
401 ZOOM_options_set(zoptions, "apdulog", apdulog);
403 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
404 ZOOM_options_set(zoptions, "user", auth);
405 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
406 ZOOM_options_set(zoptions, "sru", sru);
407 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
409 ZOOM_options_set(zoptions, "sru_version", sru_version);
411 if (!(link = ZOOM_connection_create(zoptions)))
413 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
414 ZOOM_options_destroy(zoptions);
420 char http_hostport[512];
421 strcpy(http_hostport, "http://");
422 strcat(http_hostport, host->hostport);
423 ZOOM_connection_connect(link, http_hostport, 0);
426 ZOOM_connection_connect(link, host->ipport, 0);
429 con->iochan = iochan_create(0, connection_handler, 0, "connection_socket");
430 con->state = Conn_Connecting;
431 iochan_settimeout(con->iochan, con->operation_timeout);
432 iochan_setdata(con->iochan, con);
433 iochan_setsocketfun(con->iochan, socketfun);
434 iochan_setmaskfun(con->iochan, maskfun);
435 iochan_add(iochan_man, con->iochan);
437 /* this fragment is bad DRY: from client_prep_connection */
438 client_set_state(con->client, Client_Connecting);
439 ZOOM_options_destroy(zoptions);
443 // Ensure that client has a connection associated
444 int client_prep_connection(struct client *cl,
445 int operation_timeout, int session_timeout,
446 iochan_man_t iochan_man)
448 struct connection *co;
449 struct host *host = client_get_host(cl);
450 struct session_database *sdb = client_get_database(cl);
451 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
453 if (zproxy && zproxy[0] == '\0')
459 co = client_get_connection(cl);
461 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
465 // See if someone else has an idle connection
466 // We should look at timestamps here to select the longest-idle connection
467 yaz_mutex_enter(host->mutex);
468 for (co = host->connections; co; co = co->next)
469 if (connection_is_idle(co) &&
470 (!co->client || client_get_state(co->client) == Client_Idle) &&
471 !strcmp(ZOOM_connection_option_get(co->link, "user"),
472 session_setting_oneval(client_get_database(cl),
475 if (zproxy == 0 && co->zproxy == 0)
477 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
482 connection_release(co);
483 client_set_connection(cl, co);
485 /* ensure that connection is only assigned to this client
486 by marking the client non Idle */
487 client_set_state(cl, Client_Working);
488 yaz_mutex_leave(host->mutex);
489 co->operation_timeout = operation_timeout;
490 co->session_timeout = session_timeout;
491 /* tells ZOOM to reconnect if necessary. Disabled becuase
492 the ZOOM_connection_connect flushes the task queue */
493 ZOOM_connection_connect(co->link, 0, 0);
497 yaz_mutex_leave(host->mutex);
498 co = connection_create(cl, operation_timeout, session_timeout,
512 * c-file-style: "Stroustrup"
513 * indent-tabs-mode: nil
515 * vim: shiftwidth=4 tabstop=8 expandtab