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 || event == ZOOM_EVENT_END)
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;
97 yaz_mutex_enter(con->host->mutex);
102 *conp = (*conp)->next;
105 conp = &(*conp)->next;
107 yaz_mutex_leave(con->host->mutex);
110 // Close connection and recycle structure
111 void connection_destroy(struct connection *co)
115 ZOOM_connection_destroy(co->link);
116 iochan_destroy(co->iochan);
118 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
122 client_disconnect(co->client);
125 remove_connection_from_host(co);
130 // Creates a new connection for client, associated with the host of
132 static struct connection *connection_create(struct client *cl,
133 int operation_timeout,
135 iochan_man_t iochan_man)
137 struct connection *new;
138 struct host *host = client_get_host(cl);
140 new = xmalloc(sizeof(*new));
143 yaz_mutex_enter(host->mutex);
144 new->next = new->host->connections;
145 new->host->connections = new;
146 yaz_mutex_leave(host->mutex);
150 client_set_connection(cl, new);
152 new->state = Conn_Resolving;
153 new->operation_timeout = operation_timeout;
154 new->session_timeout = session_timeout;
156 connection_connect(new, iochan_man);
160 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);
177 yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
183 const char *error, *addinfo;
185 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
187 yaz_log(YLOG_LOG, "Error %s from %s",
188 error, client_get_url(cl));
190 iochan_settimeout(iochan, co->session_timeout);
191 client_set_diagnostic(cl, err);
192 client_set_state(cl, Client_Idle);
195 case ZOOM_EVENT_SEND_DATA:
197 case ZOOM_EVENT_RECV_DATA:
199 case ZOOM_EVENT_UNKNOWN:
201 case ZOOM_EVENT_SEND_APDU:
202 client_set_state(co->client, Client_Working);
203 iochan_settimeout(iochan, co->operation_timeout);
205 case ZOOM_EVENT_RECV_APDU:
207 case ZOOM_EVENT_CONNECT:
208 yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
209 co->state = Conn_Open;
211 case ZOOM_EVENT_RECV_SEARCH:
212 client_search_response(cl);
214 case ZOOM_EVENT_RECV_RECORD:
215 client_record_response(cl);
218 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
219 ev, client_get_url(cl));
225 void connection_continue(struct connection *co)
227 non_block_events(co);
230 static void connection_handler(IOCHAN iochan, int event)
232 struct connection *co = iochan_getdata(iochan);
233 struct client *cl = co->client;
237 /* no client associated with it.. We are probably getting
238 a closed connection from the target.. Or, perhaps, an unexpected
239 package.. We will just close the connection */
240 connection_destroy(co);
243 if (event & EVENT_TIMEOUT)
245 if (co->state == Conn_Connecting)
247 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
252 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
253 connection_destroy(co);
258 non_block_events(co);
260 ZOOM_connection_fire_event_socket(co->link, event);
262 non_block_events(co);
267 // Disassociate connection from client
268 void connection_release(struct connection *co)
270 struct client *cl = co->client;
274 client_set_connection(cl, 0);
278 void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
280 struct connection *con;
283 yaz_mutex_enter(host->mutex);
284 con = host->connections;
287 if (con->state == Conn_Resolving)
289 if (!host->ipport) /* unresolved */
291 yaz_mutex_leave(host->mutex);
292 connection_destroy(con);
294 /* start all over .. at some point it will be NULL */
296 else if (!con->client)
298 yaz_mutex_leave(host->mutex);
299 connection_destroy(con);
300 /* start all over .. at some point it will be NULL */
305 yaz_mutex_leave(host->mutex);
306 connection_connect(con, iochan_man);
307 client_start_search(con->client);
313 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
317 yaz_mutex_leave(host->mutex);
320 static struct host *connection_get_host(struct connection *con)
325 // Callback for use by event loop
326 // We do this because ZOOM connections don't always have (the same) sockets
327 static int socketfun(IOCHAN c)
329 struct connection *co = iochan_getdata(c);
332 return ZOOM_connection_get_socket(co->link);
335 // Because ZOOM always knows what events it is interested in; we may not
336 static int maskfun(IOCHAN c)
338 struct connection *co = iochan_getdata(c);
342 // This is cheating a little, and assuming that eventl mask IDs are always
343 // the same as ZOOM-C's.
344 return ZOOM_connection_get_mask(co->link);
347 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
349 ZOOM_connection link = 0;
350 struct host *host = connection_get_host(con);
351 ZOOM_options zoptions = ZOOM_options_create();
355 const char *sru_version = 0;
357 struct session_database *sdb = client_get_database(con->client);
358 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
359 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
361 assert(host->ipport);
364 ZOOM_options_set(zoptions, "async", "1");
365 ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
366 ZOOM_options_set(zoptions, "implementationVersion", VERSION);
368 if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
369 ZOOM_options_set(zoptions, "charset", charset);
371 if (zproxy && *zproxy)
373 con->zproxy = xstrdup(zproxy);
374 ZOOM_options_set(zoptions, "proxy", zproxy);
376 if (apdulog && *apdulog)
377 ZOOM_options_set(zoptions, "apdulog", apdulog);
379 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
380 ZOOM_options_set(zoptions, "user", auth);
381 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
382 ZOOM_options_set(zoptions, "sru", sru);
383 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
385 ZOOM_options_set(zoptions, "sru_version", sru_version);
387 if (!(link = ZOOM_connection_create(zoptions)))
389 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
390 ZOOM_options_destroy(zoptions);
396 char http_hostport[512];
397 strcpy(http_hostport, "http://");
398 strcat(http_hostport, host->hostport);
399 ZOOM_connection_connect(link, http_hostport, 0);
402 ZOOM_connection_connect(link, host->ipport, 0);
405 con->iochan = iochan_create(0, connection_handler, 0);
406 con->state = Conn_Connecting;
407 iochan_settimeout(con->iochan, con->operation_timeout);
408 iochan_setdata(con->iochan, con);
409 iochan_setsocketfun(con->iochan, socketfun);
410 iochan_setmaskfun(con->iochan, maskfun);
411 iochan_add(iochan_man, con->iochan);
413 /* this fragment is bad DRY: from client_prep_connection */
414 client_set_state(con->client, Client_Connecting);
415 ZOOM_options_destroy(zoptions);
419 const char *connection_get_url(struct connection *co)
421 return client_get_url(co->client);
424 // Ensure that client has a connection associated
425 int client_prep_connection(struct client *cl,
426 int operation_timeout, int session_timeout,
427 iochan_man_t iochan_man)
429 struct connection *co;
430 struct host *host = client_get_host(cl);
431 struct session_database *sdb = client_get_database(cl);
432 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
434 if (zproxy && zproxy[0] == '\0')
440 co = client_get_connection(cl);
442 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
446 // See if someone else has an idle connection
447 // We should look at timestamps here to select the longest-idle connection
448 yaz_mutex_enter(host->mutex);
449 for (co = host->connections; co; co = co->next)
450 if (connection_is_idle(co) &&
451 (!co->client || client_get_state(co->client) == Client_Idle) &&
452 !strcmp(ZOOM_connection_option_get(co->link, "user"),
453 session_setting_oneval(client_get_database(cl),
456 if (zproxy == 0 && co->zproxy == 0)
458 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
463 connection_release(co);
464 client_set_connection(cl, co);
466 yaz_mutex_leave(host->mutex);
467 co->operation_timeout = operation_timeout;
468 co->session_timeout = session_timeout;
469 /* tells ZOOM to reconnect if necessary. Disabled becuase
470 the ZOOM_connection_connect flushes the task queue */
471 ZOOM_connection_connect(co->link, 0, 0);
475 yaz_mutex_leave(host->mutex);
476 co = connection_create(cl, operation_timeout, session_timeout,
490 * c-file-style: "Stroustrup"
491 * indent-tabs-mode: nil
493 * vim: shiftwidth=4 tabstop=8 expandtab