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;
55 static void connection_use(int delta)
59 yaz_mutex_create(&g_mutex);
60 yaz_mutex_enter(g_mutex);
61 no_connections += delta;
62 result = no_connection;
64 total_no_connections += delta;
65 yaz_mutex_leave(g_mutex);
68 yaz_log(YLOG_LOG, "%s connections=%d", delta > 0 ? "INC" : "DEC",
73 int connections_count() {
74 return connection_use(0);
79 #define connection_use(x)
80 #define connections_count(x) 0
81 #define connections_count_total(x) 0
85 /** \brief Represents a physical, reusable connection to a remote Z39.50 host
91 struct client *client;
99 int operation_timeout;
101 struct connection *next; // next for same host or next in free list
104 static int connection_connect(struct connection *con, iochan_man_t iochan_man);
106 static int connection_is_idle(struct connection *co)
108 ZOOM_connection link = co->link;
111 if (co->state != Conn_Open || !link)
114 if (!ZOOM_connection_is_idle(link))
116 event = ZOOM_connection_peek_event(link);
117 if (event == ZOOM_EVENT_NONE)
123 ZOOM_connection connection_get_link(struct connection *co)
128 static void remove_connection_from_host(struct connection *con)
130 struct connection **conp = &con->host->connections;
136 *conp = (*conp)->next;
139 conp = &(*conp)->next;
141 yaz_cond_broadcast(con->host->cond_ready);
144 // Close connection and recycle structure
145 static void connection_destroy(struct connection *co)
149 ZOOM_connection_destroy(co->link);
150 iochan_destroy(co->iochan);
152 yaz_log(YLOG_DEBUG, "%p Connection destroy %s", co, co->host->hostport);
156 client_disconnect(co->client);
164 // Creates a new connection for client, associated with the host of
166 static struct connection *connection_create(struct client *cl,
167 int operation_timeout,
169 iochan_man_t iochan_man)
171 struct connection *co;
172 struct host *host = client_get_host(cl);
174 co = xmalloc(sizeof(*co));
179 client_set_connection(cl, co);
181 co->state = Conn_Resolving;
182 co->operation_timeout = operation_timeout;
183 co->session_timeout = session_timeout;
185 connection_connect(co, iochan_man);
187 yaz_mutex_enter(host->mutex);
188 co->next = co->host->connections;
189 co->host->connections = co;
190 yaz_mutex_leave(host->mutex);
196 static void non_block_events(struct connection *co)
199 IOCHAN iochan = co->iochan;
200 ZOOM_connection link = co->link;
203 struct client *cl = co->client;
205 int r = ZOOM_event_nonblock(1, &link);
210 ev = ZOOM_connection_last_event(link);
213 yaz_log(YLOG_DEBUG, "%p Connection ZOOM_EVENT_%s", co, ZOOM_get_event_str(ev));
219 const char *error, *addinfo;
221 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
223 yaz_log(YLOG_LOG, "Error %s from %s",
224 error, client_get_url(cl));
225 client_set_diagnostic(cl, err);
226 client_set_state(cl, Client_Error);
230 iochan_settimeout(iochan, co->session_timeout);
231 client_set_state(cl, Client_Idle);
233 yaz_cond_broadcast(co->host->cond_ready);
236 case ZOOM_EVENT_SEND_DATA:
238 case ZOOM_EVENT_RECV_DATA:
240 case ZOOM_EVENT_UNKNOWN:
242 case ZOOM_EVENT_SEND_APDU:
243 client_set_state(co->client, Client_Working);
244 iochan_settimeout(iochan, co->operation_timeout);
246 case ZOOM_EVENT_RECV_APDU:
248 case ZOOM_EVENT_CONNECT:
249 yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
250 co->state = Conn_Open;
252 case ZOOM_EVENT_RECV_SEARCH:
253 client_search_response(cl);
255 case ZOOM_EVENT_RECV_RECORD:
256 client_record_response(cl);
260 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
261 ev, client_get_url(cl));
266 struct client *cl = co->client;
269 client_check_preferred_watch(cl);
270 client_got_records(cl);
275 void connection_continue(struct connection *co)
277 int r = ZOOM_connection_exec_task(co->link);
279 yaz_log(YLOG_WARN, "No task was executed for connection");
280 iochan_setflags(co->iochan, ZOOM_connection_get_mask(co->link));
281 iochan_setfd(co->iochan, ZOOM_connection_get_socket(co->link));
284 static void connection_handler(IOCHAN iochan, int event)
286 struct connection *co = iochan_getdata(iochan);
288 struct host *host = co->host;
290 yaz_mutex_enter(host->mutex);
294 /* no client associated with it.. We are probably getting
295 a closed connection from the target.. Or, perhaps, an unexpected
296 package.. We will just close the connection */
297 yaz_log(YLOG_LOG, "timeout connection %p event=%d", co, event);
298 remove_connection_from_host(co);
299 yaz_mutex_leave(host->mutex);
300 connection_destroy(co);
302 else if (event & EVENT_TIMEOUT)
304 if (co->state == Conn_Connecting)
306 yaz_log(YLOG_WARN, "%p connect timeout %s", co, client_get_url(cl));
308 client_set_state(cl, Client_Error);
309 remove_connection_from_host(co);
310 yaz_mutex_leave(host->mutex);
311 connection_destroy(co);
315 yaz_log(YLOG_LOG, "%p Connection idle timeout %s", co, client_get_url(cl));
316 remove_connection_from_host(co);
317 yaz_mutex_leave(host->mutex);
318 connection_destroy(co);
323 yaz_mutex_leave(host->mutex);
326 non_block_events(co);
328 ZOOM_connection_fire_event_socket(co->link, event);
330 non_block_events(co);
335 iochan_setflags(iochan, ZOOM_connection_get_mask(co->link));
336 iochan_setfd(iochan, ZOOM_connection_get_socket(co->link));
342 // Disassociate connection from client
343 static void connection_release(struct connection *co)
345 struct client *cl = co->client;
349 client_set_connection(cl, 0);
353 void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
355 struct connection *con;
358 yaz_mutex_enter(host->mutex);
359 con = host->connections;
362 if (con->state == Conn_Resolving)
364 if (!host->ipport) /* unresolved */
366 remove_connection_from_host(con);
367 yaz_mutex_leave(host->mutex);
368 connection_destroy(con);
370 /* start all over .. at some point it will be NULL */
372 else if (!con->client)
374 remove_connection_from_host(con);
375 yaz_mutex_leave(host->mutex);
376 connection_destroy(con);
377 /* start all over .. at some point it will be NULL */
382 yaz_mutex_leave(host->mutex);
383 connection_connect(con, iochan_man);
384 client_start_search(con->client);
390 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
394 yaz_mutex_leave(host->mutex);
397 static struct host *connection_get_host(struct connection *con)
402 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
404 ZOOM_connection link = 0;
405 struct host *host = connection_get_host(con);
406 ZOOM_options zoptions = ZOOM_options_create();
410 const char *sru_version = 0;
412 struct session_database *sdb = client_get_database(con->client);
413 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
414 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
416 assert(host->ipport);
419 ZOOM_options_set(zoptions, "async", "1");
420 ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
421 ZOOM_options_set(zoptions, "implementationVersion", VERSION);
423 if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
424 ZOOM_options_set(zoptions, "charset", charset);
426 if (zproxy && *zproxy)
428 con->zproxy = xstrdup(zproxy);
429 ZOOM_options_set(zoptions, "proxy", zproxy);
431 if (apdulog && *apdulog)
432 ZOOM_options_set(zoptions, "apdulog", apdulog);
434 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
435 ZOOM_options_set(zoptions, "user", auth);
436 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
437 ZOOM_options_set(zoptions, "sru", sru);
438 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
440 ZOOM_options_set(zoptions, "sru_version", sru_version);
441 if (!(link = ZOOM_connection_create(zoptions)))
443 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
444 ZOOM_options_destroy(zoptions);
450 char http_hostport[512];
451 strcpy(http_hostport, "http://");
452 strcat(http_hostport, host->hostport);
453 ZOOM_connection_connect(link, http_hostport, 0);
455 else if (zproxy && *zproxy)
456 ZOOM_connection_connect(link, host->hostport, 0);
458 ZOOM_connection_connect(link, host->ipport, 0);
461 con->iochan = iochan_create(-1, connection_handler, 0, "connection_socket");
462 con->state = Conn_Connecting;
463 iochan_settimeout(con->iochan, con->operation_timeout);
464 iochan_setdata(con->iochan, con);
465 iochan_add(iochan_man, con->iochan);
467 /* this fragment is bad DRY: from client_prep_connection */
468 client_set_state(con->client, Client_Connecting);
469 ZOOM_options_destroy(zoptions);
473 // Ensure that client has a connection associated
474 int client_prep_connection(struct client *cl,
475 int operation_timeout, int session_timeout,
476 iochan_man_t iochan_man,
477 const struct timeval *abstime)
479 struct connection *co;
480 struct host *host = client_get_host(cl);
481 struct session_database *sdb = client_get_database(cl);
482 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
484 if (zproxy && zproxy[0] == '\0')
490 co = client_get_connection(cl);
492 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
496 int max_connections = 0;
497 int reuse_connections = 1;
498 const char *v = session_setting_oneval(client_get_database(cl),
501 max_connections = atoi(v);
503 v = session_setting_oneval(client_get_database(cl),
504 PZ_REUSE_CONNECTIONS);
506 reuse_connections = atoi(v);
508 // See if someone else has an idle connection
509 // We should look at timestamps here to select the longest-idle connection
510 yaz_mutex_enter(host->mutex);
513 int num_connections = 0;
514 for (co = host->connections; co; co = co->next)
516 if (reuse_connections) {
517 for (co = host->connections; co; co = co->next)
519 if (connection_is_idle(co) &&
520 (!co->client || client_get_state(co->client) == Client_Idle) &&
521 !strcmp(ZOOM_connection_option_get(co->link, "user"),
522 session_setting_oneval(client_get_database(cl),
525 if (zproxy == 0 && co->zproxy == 0)
527 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
533 yaz_log(YLOG_LOG, "num_connections = %d (reusing)", num_connections);
537 if (max_connections <= 0 || num_connections < max_connections)
539 yaz_log(YLOG_LOG, "num_connections = %d (new); max = %d",
540 num_connections, max_connections);
543 yaz_log(YLOG_LOG, "num_connections = %d (waiting) max = %d",
544 num_connections, max_connections);
545 if (yaz_cond_wait(host->cond_ready, host->mutex, abstime))
547 yaz_log(YLOG_LOG, "out of connections %s", client_get_url(cl));
548 client_set_state(cl, Client_Error);
549 yaz_mutex_leave(host->mutex);
555 yaz_log(YLOG_LOG, "%p Connection reuse. state: %d", co, co->state);
556 connection_release(co);
557 client_set_connection(cl, co);
559 /* ensure that connection is only assigned to this client
560 by marking the client non Idle */
561 client_set_state(cl, Client_Working);
562 yaz_mutex_leave(host->mutex);
563 co->operation_timeout = operation_timeout;
564 co->session_timeout = session_timeout;
565 /* tells ZOOM to reconnect if necessary. Disabled becuase
566 the ZOOM_connection_connect flushes the task queue */
567 ZOOM_connection_connect(co->link, 0, 0);
571 yaz_mutex_leave(host->mutex);
572 co = connection_create(cl, operation_timeout, session_timeout,
586 * c-file-style: "Stroustrup"
587 * indent-tabs-mode: nil
589 * vim: shiftwidth=4 tabstop=8 expandtab