1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2013 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->url);
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;
187 connection_connect(co, iochan_man);
189 yaz_mutex_enter(host->mutex);
190 co->next = co->host->connections;
191 co->host->connections = co;
192 yaz_mutex_leave(host->mutex);
198 static void non_block_events(struct connection *co)
201 IOCHAN iochan = co->iochan;
202 ZOOM_connection link = co->link;
205 struct client *cl = co->client;
207 int r = ZOOM_event_nonblock(1, &link);
212 ev = ZOOM_connection_last_event(link);
215 yaz_log(YLOG_DEBUG, "%p Connection ZOOM_EVENT_%s", co, ZOOM_get_event_str(ev));
219 case ZOOM_EVENT_TIMEOUT:
223 const char *error, *addinfo;
225 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
227 yaz_log(YLOG_LOG, "Error %s from %s",
228 error, client_get_id(cl));
229 client_set_diagnostic(cl, err, error, addinfo);
230 client_set_state(cl, Client_Error);
234 iochan_settimeout(iochan, co->session_timeout);
235 client_set_state(cl, Client_Idle);
237 yaz_cond_broadcast(co->host->cond_ready);
240 case ZOOM_EVENT_SEND_DATA:
242 case ZOOM_EVENT_RECV_DATA:
244 case ZOOM_EVENT_UNKNOWN:
246 case ZOOM_EVENT_SEND_APDU:
247 client_set_state(co->client, Client_Working);
248 iochan_settimeout(iochan, co->operation_timeout);
250 case ZOOM_EVENT_RECV_APDU:
252 case ZOOM_EVENT_CONNECT:
253 yaz_log(YLOG_LOG, "Connected to %s", client_get_id(cl));
254 co->state = Conn_Open;
256 case ZOOM_EVENT_RECV_SEARCH:
257 client_search_response(cl);
259 case ZOOM_EVENT_RECV_RECORD:
260 client_record_response(cl, &got_records);
263 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
264 ev, client_get_id(cl));
270 struct client *cl = co->client;
273 client_check_preferred_watch(cl);
274 client_got_records(cl);
279 void connection_continue(struct connection *co)
281 int r = ZOOM_connection_exec_task(co->link);
283 non_block_events(co);
286 iochan_setflags(co->iochan, ZOOM_connection_get_mask(co->link));
287 iochan_setfd(co->iochan, ZOOM_connection_get_socket(co->link));
291 static void connection_handler(IOCHAN iochan, int event)
293 struct connection *co = iochan_getdata(iochan);
295 struct host *host = co->host;
297 yaz_mutex_enter(host->mutex);
301 /* no client associated with it.. We are probably getting
302 a closed connection from the target.. Or, perhaps, an unexpected
303 package.. We will just close the connection */
304 yaz_log(YLOG_LOG, "timeout connection %p event=%d", co, event);
305 remove_connection_from_host(co);
306 yaz_mutex_leave(host->mutex);
307 connection_destroy(co);
309 else if (event & EVENT_TIMEOUT)
311 ZOOM_connection_fire_event_timeout(co->link);
313 non_block_events(co);
316 remove_connection_from_host(co);
317 yaz_mutex_leave(host->mutex);
318 connection_destroy(co);
322 yaz_mutex_leave(host->mutex);
325 non_block_events(co);
327 ZOOM_connection_fire_event_socket(co->link, event);
329 non_block_events(co);
334 iochan_setflags(iochan, ZOOM_connection_get_mask(co->link));
335 iochan_setfd(iochan, ZOOM_connection_get_socket(co->link));
341 // Disassociate connection from client
342 static void connection_release(struct connection *co)
344 struct client *cl = co->client;
348 client_set_connection(cl, 0);
352 void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
354 struct connection *con;
356 yaz_mutex_enter(host->mutex);
357 con = host->connections;
360 if (con->state == Conn_Closed)
362 if (!host->ipport || !con->client) /* unresolved or no client */
364 remove_connection_from_host(con);
365 yaz_mutex_leave(host->mutex);
366 connection_destroy(con);
370 struct session_database *sdb = client_get_database(con->client);
373 yaz_mutex_leave(host->mutex);
374 connection_connect(con, iochan_man);
375 client_start_search(con->client);
379 remove_connection_from_host(con);
380 yaz_mutex_leave(host->mutex);
381 connection_destroy(con);
384 /* start all over .. at some point it will be NULL */
385 yaz_mutex_enter(host->mutex);
386 con = host->connections;
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 struct host *host = connection_get_host(con);
405 ZOOM_options zoptions = ZOOM_options_create();
409 const char *sru_version = 0;
411 struct session_database *sdb = client_get_database(con->client);
412 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
416 ZOOM_options_set(zoptions, "async", "1");
417 ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
418 ZOOM_options_set(zoptions, "implementationVersion", VERSION);
420 if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
421 ZOOM_options_set(zoptions, "charset", charset);
423 assert(host->ipport);
426 yaz_log(YLOG_LOG, "proxy=%s", host->ipport);
427 ZOOM_options_set(zoptions, "proxy", host->ipport);
431 assert(host->tproxy);
432 yaz_log(YLOG_LOG, "tproxy=%s", host->ipport);
433 ZOOM_options_set(zoptions, "tproxy", host->ipport);
436 if (apdulog && *apdulog)
437 ZOOM_options_set(zoptions, "apdulog", apdulog);
439 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
440 ZOOM_options_set(zoptions, "user", auth);
441 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
442 ZOOM_options_set(zoptions, "sru", sru);
443 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
445 ZOOM_options_set(zoptions, "sru_version", sru_version);
446 if (!(con->link = ZOOM_connection_create(zoptions)))
448 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
449 ZOOM_options_destroy(zoptions);
453 if (sru && *sru && !strstr(host->url, "://"))
455 WRBUF w = wrbuf_alloc();
456 wrbuf_puts(w, "http://");
457 wrbuf_puts(w, host->url);
458 ZOOM_connection_connect(con->link, wrbuf_cstr(w), 0);
463 ZOOM_connection_connect(con->link, host->url, 0);
465 con->iochan = iochan_create(-1, connection_handler, 0, "connection_socket");
466 con->state = Conn_Connecting;
467 iochan_settimeout(con->iochan, con->operation_timeout);
468 iochan_setdata(con->iochan, con);
469 iochan_add(iochan_man, con->iochan);
471 client_set_state(con->client, Client_Connecting);
472 ZOOM_options_destroy(zoptions);
476 // Ensure that client has a connection associated
477 int client_prep_connection(struct client *cl,
478 int operation_timeout, int session_timeout,
479 iochan_man_t iochan_man,
480 const struct timeval *abstime)
482 struct connection *co;
483 struct session_database *sdb = client_get_database(cl);
484 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
485 const char *url = session_setting_oneval(sdb, PZ_URL);
486 const char *sru = session_setting_oneval(sdb, PZ_SRU);
487 struct host *host = 0;
488 int default_port = *sru ? 80 : 210;
490 if (zproxy && zproxy[0] == '\0')
494 url = sdb->database->id;
496 host = find_host(client_get_session(cl)->service->server->database_hosts,
497 url, zproxy, default_port, iochan_man);
499 yaz_log(YLOG_DEBUG, "client_prep_connection: target=%s url=%s",
500 client_get_id(cl), url);
504 co = client_get_connection(cl);
508 if (co->host == host && client_get_state(cl) == Client_Idle)
512 connection_release(co);
517 int max_connections = 0;
518 int reuse_connections = 1;
519 const char *v = session_setting_oneval(client_get_database(cl),
522 max_connections = atoi(v);
524 v = session_setting_oneval(client_get_database(cl),
525 PZ_REUSE_CONNECTIONS);
527 reuse_connections = atoi(v);
529 // See if someone else has an idle connection
530 // We should look at timestamps here to select the longest-idle connection
531 yaz_mutex_enter(host->mutex);
534 int num_connections = 0;
535 for (co = host->connections; co; co = co->next)
537 if (reuse_connections)
539 for (co = host->connections; co; co = co->next)
541 if (connection_is_idle(co) &&
542 (!co->client || client_get_state(co->client) == Client_Idle) &&
543 !strcmp(ZOOM_connection_option_get(co->link, "user"),
544 session_setting_oneval(client_get_database(cl),
547 if (zproxy == 0 && co->zproxy == 0)
549 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
555 yaz_log(YLOG_LOG, "num_connections = %d (reusing)", num_connections);
559 if (max_connections <= 0 || num_connections < max_connections)
561 yaz_log(YLOG_LOG, "num_connections = %d (new); max = %d",
562 num_connections, max_connections);
565 yaz_log(YLOG_LOG, "num_connections = %d (waiting) max = %d",
566 num_connections, max_connections);
567 if (yaz_cond_wait(host->cond_ready, host->mutex, abstime))
569 yaz_log(YLOG_LOG, "out of connections %s", client_get_id(cl));
570 client_set_state(cl, Client_Error);
571 yaz_mutex_leave(host->mutex);
577 yaz_log(YLOG_LOG, "%p Connection reuse. state: %d", co, co->state);
578 connection_release(co);
579 client_set_connection(cl, co);
581 /* ensure that connection is only assigned to this client
582 by marking the client non Idle */
583 client_set_state(cl, Client_Working);
584 yaz_mutex_leave(host->mutex);
585 co->operation_timeout = operation_timeout;
586 co->session_timeout = session_timeout;
587 /* tells ZOOM to reconnect if necessary. Disabled becuase
588 the ZOOM_connection_connect flushes the task queue */
589 ZOOM_connection_connect(co->link, 0, 0);
593 yaz_mutex_leave(host->mutex);
594 co = connection_create(cl, host, operation_timeout, session_timeout,
609 * c-file-style: "Stroustrup"
610 * indent-tabs-mode: nil
612 * vim: shiftwidth=4 tabstop=8 expandtab