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"
50 /* connection counting (1) , disable connection counting (0) */
52 static YAZ_MUTEX g_mutex = 0;
53 static int no_connections = 0;
55 static void connection_use(int delta)
58 yaz_mutex_create(&g_mutex);
59 yaz_mutex_enter(g_mutex);
60 no_connections += delta;
61 yaz_mutex_leave(g_mutex);
62 yaz_log(YLOG_LOG, "%s connections=%d", delta > 0 ? "INC" : "DEC",
66 #define connection_use(x)
70 /** \brief Represents a physical, reusable connection to a remote Z39.50 host
76 struct client *client;
83 int operation_timeout;
85 struct connection *next; // next for same host or next in free list
88 static int connection_connect(struct connection *con, iochan_man_t iochan_man);
90 static int connection_is_idle(struct connection *co)
92 ZOOM_connection link = co->link;
95 if (co->state != Conn_Open || !link)
98 if (!ZOOM_connection_is_idle(link))
100 event = ZOOM_connection_peek_event(link);
101 if (event == ZOOM_EVENT_NONE)
107 ZOOM_connection connection_get_link(struct connection *co)
112 static void remove_connection_from_host(struct connection *con)
114 struct connection **conp = &con->host->connections;
120 *conp = (*conp)->next;
123 conp = &(*conp)->next;
125 yaz_cond_broadcast(con->host->cond_ready);
128 // Close connection and recycle structure
129 static void connection_destroy(struct connection *co)
133 ZOOM_connection_destroy(co->link);
134 iochan_destroy(co->iochan);
136 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
140 client_disconnect(co->client);
143 remove_connection_from_host(co);
149 // Creates a new connection for client, associated with the host of
151 static struct connection *connection_create(struct client *cl,
152 int operation_timeout,
154 iochan_man_t iochan_man)
156 struct connection *co;
157 struct host *host = client_get_host(cl);
159 co = xmalloc(sizeof(*co));
164 client_set_connection(cl, co);
166 co->state = Conn_Resolving;
167 co->operation_timeout = operation_timeout;
168 co->session_timeout = session_timeout;
170 connection_connect(co, iochan_man);
172 yaz_mutex_enter(host->mutex);
173 co->next = co->host->connections;
174 co->host->connections = co;
175 yaz_mutex_leave(host->mutex);
181 static void non_block_events(struct connection *co)
184 IOCHAN iochan = co->iochan;
185 ZOOM_connection link = co->link;
188 struct client *cl = co->client;
190 int r = ZOOM_event_nonblock(1, &link);
195 ev = ZOOM_connection_last_event(link);
198 yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
204 const char *error, *addinfo;
206 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
208 yaz_log(YLOG_LOG, "Error %s from %s",
209 error, client_get_url(cl));
211 iochan_settimeout(iochan, co->session_timeout);
212 client_set_diagnostic(cl, err);
213 client_set_state(cl, Client_Idle);
214 yaz_cond_broadcast(co->host->cond_ready);
217 case ZOOM_EVENT_SEND_DATA:
219 case ZOOM_EVENT_RECV_DATA:
221 case ZOOM_EVENT_UNKNOWN:
223 case ZOOM_EVENT_SEND_APDU:
224 client_set_state(co->client, Client_Working);
225 iochan_settimeout(iochan, co->operation_timeout);
227 case ZOOM_EVENT_RECV_APDU:
229 case ZOOM_EVENT_CONNECT:
230 yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
231 co->state = Conn_Open;
233 case ZOOM_EVENT_RECV_SEARCH:
234 client_search_response(cl);
236 case ZOOM_EVENT_RECV_RECORD:
237 client_record_response(cl);
241 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
242 ev, client_get_url(cl));
247 struct client *cl = co->client;
250 client_got_records(cl);
255 void connection_continue(struct connection *co)
257 int r = ZOOM_connection_exec_task(co->link);
259 yaz_log(YLOG_WARN, "No task was executed for connection");
260 iochan_setflags(co->iochan, ZOOM_connection_get_mask(co->link));
261 iochan_setfd(co->iochan, ZOOM_connection_get_socket(co->link));
264 static void connection_handler(IOCHAN iochan, int event)
266 struct connection *co = iochan_getdata(iochan);
268 struct host *host = co->host;
270 yaz_mutex_enter(host->mutex);
274 /* no client associated with it.. We are probably getting
275 a closed connection from the target.. Or, perhaps, an unexpected
276 package.. We will just close the connection */
277 yaz_log(YLOG_LOG, "timeout connection %p event=%d", co, event);
278 connection_destroy(co);
279 yaz_mutex_leave(host->mutex);
281 else if (event & EVENT_TIMEOUT)
283 if (co->state == Conn_Connecting)
285 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
287 client_set_state(cl, Client_Error);
288 connection_destroy(co);
290 else if (client_get_state(co->client) == Client_Idle)
292 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
293 connection_destroy(co);
297 yaz_log(YLOG_LOG, "ignore timeout %s", client_get_url(cl));
299 yaz_mutex_leave(host->mutex);
303 yaz_mutex_leave(host->mutex);
306 non_block_events(co);
308 ZOOM_connection_fire_event_socket(co->link, event);
310 non_block_events(co);
315 iochan_setflags(iochan, ZOOM_connection_get_mask(co->link));
316 iochan_setfd(iochan, ZOOM_connection_get_socket(co->link));
322 // Disassociate connection from client
323 static void connection_release(struct connection *co)
325 struct client *cl = co->client;
329 client_set_connection(cl, 0);
333 void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
335 struct connection *con;
338 yaz_mutex_enter(host->mutex);
339 con = host->connections;
342 if (con->state == Conn_Resolving)
344 if (!host->ipport) /* unresolved */
346 yaz_mutex_leave(host->mutex);
347 connection_destroy(con);
349 /* start all over .. at some point it will be NULL */
351 else if (!con->client)
353 yaz_mutex_leave(host->mutex);
354 connection_destroy(con);
355 /* start all over .. at some point it will be NULL */
360 yaz_mutex_leave(host->mutex);
361 connection_connect(con, iochan_man);
362 client_start_search(con->client);
368 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
372 yaz_mutex_leave(host->mutex);
375 static struct host *connection_get_host(struct connection *con)
380 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
382 ZOOM_connection link = 0;
383 struct host *host = connection_get_host(con);
384 ZOOM_options zoptions = ZOOM_options_create();
388 const char *sru_version = 0;
390 struct session_database *sdb = client_get_database(con->client);
391 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
392 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
394 assert(host->ipport);
397 ZOOM_options_set(zoptions, "async", "1");
398 ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
399 ZOOM_options_set(zoptions, "implementationVersion", VERSION);
401 if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
402 ZOOM_options_set(zoptions, "charset", charset);
404 if (zproxy && *zproxy)
406 con->zproxy = xstrdup(zproxy);
407 ZOOM_options_set(zoptions, "proxy", zproxy);
409 if (apdulog && *apdulog)
410 ZOOM_options_set(zoptions, "apdulog", apdulog);
412 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
413 ZOOM_options_set(zoptions, "user", auth);
414 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
415 ZOOM_options_set(zoptions, "sru", sru);
416 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
418 ZOOM_options_set(zoptions, "sru_version", sru_version);
420 if (!(link = ZOOM_connection_create(zoptions)))
422 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
423 ZOOM_options_destroy(zoptions);
429 char http_hostport[512];
430 strcpy(http_hostport, "http://");
431 strcat(http_hostport, host->hostport);
432 ZOOM_connection_connect(link, http_hostport, 0);
435 ZOOM_connection_connect(link, host->ipport, 0);
438 con->iochan = iochan_create(0, connection_handler, 0, "connection_socket");
439 con->state = Conn_Connecting;
440 iochan_settimeout(con->iochan, con->operation_timeout);
441 iochan_setdata(con->iochan, con);
442 iochan_add(iochan_man, con->iochan);
444 /* this fragment is bad DRY: from client_prep_connection */
445 client_set_state(con->client, Client_Connecting);
446 ZOOM_options_destroy(zoptions);
450 // Ensure that client has a connection associated
451 int client_prep_connection(struct client *cl,
452 int operation_timeout, int session_timeout,
453 iochan_man_t iochan_man,
454 const struct timeval *abstime)
456 struct connection *co;
457 struct host *host = client_get_host(cl);
458 struct session_database *sdb = client_get_database(cl);
459 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
461 if (zproxy && zproxy[0] == '\0')
467 co = client_get_connection(cl);
469 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
473 int max_connections = 0;
474 const char *v = session_setting_oneval(client_get_database(cl),
477 max_connections = atoi(v);
479 // See if someone else has an idle connection
480 // We should look at timestamps here to select the longest-idle connection
481 yaz_mutex_enter(host->mutex);
484 int num_connections = 0;
485 for (co = host->connections; co; co = co->next)
487 for (co = host->connections; co; co = co->next)
489 if (connection_is_idle(co) &&
490 (!co->client || client_get_state(co->client) == Client_Idle) &&
491 !strcmp(ZOOM_connection_option_get(co->link, "user"),
492 session_setting_oneval(client_get_database(cl),
495 if (zproxy == 0 && co->zproxy == 0)
497 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
503 yaz_log(YLOG_LOG, "num_connections = %d (reusing)",
507 if (max_connections <= 0 || num_connections < max_connections)
509 yaz_log(YLOG_LOG, "num_connections = %d (new); max = %d",
510 num_connections, max_connections);
513 yaz_log(YLOG_LOG, "num_connections = %d (waiting) max = %d",
514 num_connections, max_connections);
515 if (yaz_cond_wait(host->cond_ready, host->mutex, abstime))
517 yaz_log(YLOG_LOG, "out of connections %s", client_get_url(cl));
518 client_set_state(cl, Client_Error);
519 yaz_mutex_leave(host->mutex);
525 connection_release(co);
526 client_set_connection(cl, co);
528 /* ensure that connection is only assigned to this client
529 by marking the client non Idle */
530 client_set_state(cl, Client_Working);
531 yaz_mutex_leave(host->mutex);
532 co->operation_timeout = operation_timeout;
533 co->session_timeout = session_timeout;
534 /* tells ZOOM to reconnect if necessary. Disabled becuase
535 the ZOOM_connection_connect flushes the task queue */
536 ZOOM_connection_connect(co->link, 0, 0);
540 yaz_mutex_leave(host->mutex);
541 co = connection_create(cl, operation_timeout, session_timeout,
555 * c-file-style: "Stroustrup"
556 * indent-tabs-mode: nil
558 * vim: shiftwidth=4 tabstop=8 expandtab