1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2011 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"
50 /* connection counting (1) , disable connection counting (0) */
52 static YAZ_MUTEX g_mutex = 0;
53 static int no_connections = 0;
54 static int total_no_connections = 0;
56 static int connection_use(int delta)
60 yaz_mutex_create(&g_mutex);
61 yaz_mutex_enter(g_mutex);
62 no_connections += delta;
63 result = no_connections;
65 total_no_connections += delta;
66 yaz_mutex_leave(g_mutex);
69 yaz_log(YLOG_LOG, "%s connections=%d", delta > 0 ? "INC" : "DEC",
74 int connections_count(void)
76 return connection_use(0);
81 #define connection_use(x)
82 #define connections_count(x) 0
83 #define connections_count_total(x) 0
87 /** \brief Represents a physical, reusable connection to a remote Z39.50 host
93 struct client *client;
100 int operation_timeout;
102 struct connection *next; // next for same host or next in free list
105 static int connection_connect(struct connection *con, iochan_man_t iochan_man);
107 static int connection_is_idle(struct connection *co)
109 ZOOM_connection link = co->link;
112 if (co->state != Conn_Open || !link)
115 if (!ZOOM_connection_is_idle(link))
117 event = ZOOM_connection_peek_event(link);
118 if (event == ZOOM_EVENT_NONE)
124 ZOOM_connection connection_get_link(struct connection *co)
129 static void remove_connection_from_host(struct connection *con)
131 struct connection **conp = &con->host->connections;
137 *conp = (*conp)->next;
140 conp = &(*conp)->next;
142 yaz_cond_broadcast(con->host->cond_ready);
145 // Close connection and recycle structure
146 static void connection_destroy(struct connection *co)
150 ZOOM_connection_destroy(co->link);
151 iochan_destroy(co->iochan);
153 yaz_log(YLOG_DEBUG, "%p Connection destroy %s", co, co->host->hostport);
157 client_disconnect(co->client);
165 // Creates a new connection for client, associated with the host of
167 static struct connection *connection_create(struct client *cl,
169 int operation_timeout,
171 iochan_man_t iochan_man)
173 struct connection *co;
175 co = xmalloc(sizeof(*co));
180 client_set_connection(cl, co);
182 co->state = Conn_Closed;
183 co->operation_timeout = operation_timeout;
184 co->session_timeout = session_timeout;
186 connection_connect(co, iochan_man);
188 yaz_mutex_enter(host->mutex);
189 co->next = co->host->connections;
190 co->host->connections = co;
191 yaz_mutex_leave(host->mutex);
197 static void non_block_events(struct connection *co)
200 IOCHAN iochan = co->iochan;
201 ZOOM_connection link = co->link;
204 struct client *cl = co->client;
206 int r = ZOOM_event_nonblock(1, &link);
211 ev = ZOOM_connection_last_event(link);
214 yaz_log(YLOG_DEBUG, "%p Connection ZOOM_EVENT_%s", co, ZOOM_get_event_str(ev));
220 const char *error, *addinfo;
222 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
224 yaz_log(YLOG_LOG, "Error %s from %s",
225 error, client_get_id(cl));
226 client_set_diagnostic(cl, err);
227 client_set_state(cl, Client_Error);
231 iochan_settimeout(iochan, co->session_timeout);
232 client_set_state(cl, Client_Idle);
234 yaz_cond_broadcast(co->host->cond_ready);
237 case ZOOM_EVENT_SEND_DATA:
239 case ZOOM_EVENT_RECV_DATA:
241 case ZOOM_EVENT_UNKNOWN:
243 case ZOOM_EVENT_SEND_APDU:
244 client_set_state(co->client, Client_Working);
245 iochan_settimeout(iochan, co->operation_timeout);
247 case ZOOM_EVENT_RECV_APDU:
249 case ZOOM_EVENT_CONNECT:
250 yaz_log(YLOG_LOG, "Connected to %s", client_get_id(cl));
251 co->state = Conn_Open;
253 case ZOOM_EVENT_RECV_SEARCH:
254 client_search_response(cl);
256 case ZOOM_EVENT_RECV_RECORD:
257 client_record_response(cl);
261 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
262 ev, client_get_id(cl));
267 struct client *cl = co->client;
270 client_check_preferred_watch(cl);
271 client_got_records(cl);
276 void connection_continue(struct connection *co)
278 int r = ZOOM_connection_exec_task(co->link);
281 const char *error, *addinfo;
283 if ((err = ZOOM_connection_error(co->link, &error, &addinfo)))
287 yaz_log(YLOG_LOG, "Error %s from %s",
288 error, client_get_id(co->client));
289 client_set_diagnostic(co->client, err);
290 client_set_state_nb(co->client, Client_Error);
296 iochan_setflags(co->iochan, ZOOM_connection_get_mask(co->link));
297 iochan_setfd(co->iochan, ZOOM_connection_get_socket(co->link));
301 static void connection_handler(IOCHAN iochan, int event)
303 struct connection *co = iochan_getdata(iochan);
305 struct host *host = co->host;
307 yaz_mutex_enter(host->mutex);
311 /* no client associated with it.. We are probably getting
312 a closed connection from the target.. Or, perhaps, an unexpected
313 package.. We will just close the connection */
314 yaz_log(YLOG_LOG, "timeout connection %p event=%d", co, event);
315 remove_connection_from_host(co);
316 yaz_mutex_leave(host->mutex);
317 connection_destroy(co);
319 else if (event & EVENT_TIMEOUT)
321 if (co->state == Conn_Connecting)
323 yaz_log(YLOG_WARN, "%p connect timeout %s", co, client_get_id(cl));
325 client_set_state(cl, Client_Error);
326 remove_connection_from_host(co);
327 yaz_mutex_leave(host->mutex);
328 connection_destroy(co);
332 yaz_log(YLOG_LOG, "%p Connection idle timeout %s", co, client_get_id(cl));
333 remove_connection_from_host(co);
334 yaz_mutex_leave(host->mutex);
335 connection_destroy(co);
340 yaz_mutex_leave(host->mutex);
343 non_block_events(co);
345 ZOOM_connection_fire_event_socket(co->link, event);
347 non_block_events(co);
352 iochan_setflags(iochan, ZOOM_connection_get_mask(co->link));
353 iochan_setfd(iochan, ZOOM_connection_get_socket(co->link));
359 // Disassociate connection from client
360 static void connection_release(struct connection *co)
362 struct client *cl = co->client;
366 client_set_connection(cl, 0);
370 static struct host *connection_get_host(struct connection *con)
375 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
377 struct host *host = connection_get_host(con);
378 ZOOM_options zoptions = ZOOM_options_create();
382 const char *sru_version = 0;
384 struct session_database *sdb = client_get_database(con->client);
385 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
386 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
390 ZOOM_options_set(zoptions, "async", "1");
391 ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
392 ZOOM_options_set(zoptions, "implementationVersion", VERSION);
394 if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
395 ZOOM_options_set(zoptions, "charset", charset);
397 if (zproxy && *zproxy)
399 con->zproxy = xstrdup(zproxy);
400 ZOOM_options_set(zoptions, "proxy", zproxy);
402 if (apdulog && *apdulog)
403 ZOOM_options_set(zoptions, "apdulog", apdulog);
405 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
406 ZOOM_options_set(zoptions, "user", auth);
407 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
408 ZOOM_options_set(zoptions, "sru", sru);
409 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
411 ZOOM_options_set(zoptions, "sru_version", sru_version);
412 if (!(con->link = ZOOM_connection_create(zoptions)))
414 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
415 ZOOM_options_destroy(zoptions);
421 char http_hostport[512];
422 strcpy(http_hostport, "http://");
423 strcat(http_hostport, host->hostport);
424 ZOOM_connection_connect(con->link, http_hostport, 0);
428 ZOOM_connection_connect(con->link, host->hostport, 0);
431 con->iochan = iochan_create(-1, connection_handler, 0, "connection_socket");
432 con->state = Conn_Connecting;
433 iochan_settimeout(con->iochan, con->operation_timeout);
434 iochan_setdata(con->iochan, con);
435 iochan_add(iochan_man, con->iochan);
437 client_set_state(con->client, Client_Connecting);
438 ZOOM_options_destroy(zoptions);
442 // Ensure that client has a connection associated
443 int client_prep_connection(struct client *cl,
444 int operation_timeout, int session_timeout,
445 iochan_man_t iochan_man,
446 const struct timeval *abstime)
448 struct connection *co;
449 struct session_database *sdb = client_get_database(cl);
450 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
451 const char *url = session_setting_oneval(sdb, PZ_URL);
452 struct host *host = 0;
454 if (zproxy && zproxy[0] == '\0')
458 url = sdb->database->id;
460 host = find_host(client_get_session(cl)->service->server->database_hosts,
463 yaz_log(YLOG_DEBUG, "client_prep_connection: target=%s url=%s",
464 client_get_id(cl), url);
466 co = client_get_connection(cl);
471 if (co->host == host)
472 ; /* reusing connection. It's ours! */
476 connection_release(co);
482 int max_connections = 0;
483 int reuse_connections = 1;
484 const char *v = session_setting_oneval(client_get_database(cl),
487 max_connections = atoi(v);
489 v = session_setting_oneval(client_get_database(cl),
490 PZ_REUSE_CONNECTIONS);
492 reuse_connections = atoi(v);
494 // See if someone else has an idle connection
495 // We should look at timestamps here to select the longest-idle connection
496 yaz_mutex_enter(host->mutex);
499 int num_connections = 0;
500 for (co = host->connections; co; co = co->next)
502 if (reuse_connections)
504 for (co = host->connections; co; co = co->next)
506 if (connection_is_idle(co) &&
507 (!co->client || client_get_state(co->client) == Client_Idle) &&
508 !strcmp(ZOOM_connection_option_get(co->link, "user"),
509 session_setting_oneval(client_get_database(cl),
512 if (zproxy == 0 && co->zproxy == 0)
514 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
520 yaz_log(YLOG_LOG, "num_connections = %d (reusing)", num_connections);
524 if (max_connections <= 0 || num_connections < max_connections)
526 yaz_log(YLOG_LOG, "num_connections = %d (new); max = %d",
527 num_connections, max_connections);
530 yaz_log(YLOG_LOG, "num_connections = %d (waiting) max = %d",
531 num_connections, max_connections);
532 if (yaz_cond_wait(host->cond_ready, host->mutex, abstime))
534 yaz_log(YLOG_LOG, "out of connections %s", client_get_id(cl));
535 client_set_state(cl, Client_Error);
536 yaz_mutex_leave(host->mutex);
542 yaz_log(YLOG_LOG, "%p Connection reuse. state: %d", co, co->state);
543 connection_release(co);
544 client_set_connection(cl, co);
546 /* ensure that connection is only assigned to this client
547 by marking the client non Idle */
548 client_set_state(cl, Client_Working);
549 yaz_mutex_leave(host->mutex);
550 co->operation_timeout = operation_timeout;
551 co->session_timeout = session_timeout;
552 /* tells ZOOM to reconnect if necessary. Disabled becuase
553 the ZOOM_connection_connect flushes the task queue */
554 ZOOM_connection_connect(co->link, 0, 0);
558 yaz_mutex_leave(host->mutex);
559 co = connection_create(cl, host, operation_timeout, session_timeout,
574 * c-file-style: "Stroustrup"
575 * indent-tabs-mode: nil
577 * vim: shiftwidth=4 tabstop=8 expandtab