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;
127 // Close connection and recycle structure
128 static void connection_destroy(struct connection *co)
132 ZOOM_connection_destroy(co->link);
133 iochan_destroy(co->iochan);
135 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
139 client_disconnect(co->client);
142 remove_connection_from_host(co);
148 // Creates a new connection for client, associated with the host of
150 static struct connection *connection_create(struct client *cl,
151 int operation_timeout,
153 iochan_man_t iochan_man)
155 struct connection *co;
156 struct host *host = client_get_host(cl);
158 co = xmalloc(sizeof(*co));
163 client_set_connection(cl, co);
165 co->state = Conn_Resolving;
166 co->operation_timeout = operation_timeout;
167 co->session_timeout = session_timeout;
169 connection_connect(co, iochan_man);
171 yaz_mutex_enter(host->mutex);
172 co->next = co->host->connections;
173 co->host->connections = co;
174 yaz_mutex_leave(host->mutex);
180 static void non_block_events(struct connection *co)
183 IOCHAN iochan = co->iochan;
184 ZOOM_connection link = co->link;
187 struct client *cl = co->client;
189 int r = ZOOM_event_nonblock(1, &link);
194 ev = ZOOM_connection_last_event(link);
197 yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
203 const char *error, *addinfo;
205 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
207 yaz_log(YLOG_LOG, "Error %s from %s",
208 error, client_get_url(cl));
210 iochan_settimeout(iochan, co->session_timeout);
211 client_set_diagnostic(cl, err);
212 client_set_state(cl, Client_Idle);
215 case ZOOM_EVENT_SEND_DATA:
217 case ZOOM_EVENT_RECV_DATA:
219 case ZOOM_EVENT_UNKNOWN:
221 case ZOOM_EVENT_SEND_APDU:
222 client_set_state(co->client, Client_Working);
223 iochan_settimeout(iochan, co->operation_timeout);
225 case ZOOM_EVENT_RECV_APDU:
227 case ZOOM_EVENT_CONNECT:
228 yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
229 co->state = Conn_Open;
231 case ZOOM_EVENT_RECV_SEARCH:
232 client_search_response(cl);
234 case ZOOM_EVENT_RECV_RECORD:
235 client_record_response(cl);
239 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
240 ev, client_get_url(cl));
245 struct client *cl = co->client;
248 client_got_records(cl);
253 void connection_continue(struct connection *co)
255 int r = ZOOM_connection_exec_task(co->link);
257 yaz_log(YLOG_WARN, "No task was executed for connection");
258 iochan_setflags(co->iochan, ZOOM_connection_get_mask(co->link));
259 iochan_setfd(co->iochan, ZOOM_connection_get_socket(co->link));
262 static void connection_handler(IOCHAN iochan, int event)
264 struct connection *co = iochan_getdata(iochan);
266 struct host *host = co->host;
268 yaz_mutex_enter(host->mutex);
272 /* no client associated with it.. We are probably getting
273 a closed connection from the target.. Or, perhaps, an unexpected
274 package.. We will just close the connection */
275 yaz_log(YLOG_LOG, "timeout connection %p event=%d", co, event);
276 connection_destroy(co);
277 yaz_mutex_leave(host->mutex);
279 else if (event & EVENT_TIMEOUT)
281 if (co->state == Conn_Connecting)
283 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
285 client_set_state(cl, Client_Error);
286 connection_destroy(co);
288 else if (client_get_state(co->client) == Client_Idle)
290 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
291 connection_destroy(co);
295 yaz_log(YLOG_LOG, "ignore timeout %s", client_get_url(cl));
297 yaz_mutex_leave(host->mutex);
301 yaz_mutex_leave(host->mutex);
304 non_block_events(co);
306 ZOOM_connection_fire_event_socket(co->link, event);
308 non_block_events(co);
313 iochan_setflags(iochan, ZOOM_connection_get_mask(co->link));
314 iochan_setfd(iochan, ZOOM_connection_get_socket(co->link));
320 // Disassociate connection from client
321 static void connection_release(struct connection *co)
323 struct client *cl = co->client;
327 client_set_connection(cl, 0);
331 void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
333 struct connection *con;
336 yaz_mutex_enter(host->mutex);
337 con = host->connections;
340 if (con->state == Conn_Resolving)
342 if (!host->ipport) /* unresolved */
344 yaz_mutex_leave(host->mutex);
345 connection_destroy(con);
347 /* start all over .. at some point it will be NULL */
349 else if (!con->client)
351 yaz_mutex_leave(host->mutex);
352 connection_destroy(con);
353 /* start all over .. at some point it will be NULL */
358 yaz_mutex_leave(host->mutex);
359 connection_connect(con, iochan_man);
360 client_start_search(con->client);
366 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
370 yaz_mutex_leave(host->mutex);
373 static struct host *connection_get_host(struct connection *con)
378 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
380 ZOOM_connection link = 0;
381 struct host *host = connection_get_host(con);
382 ZOOM_options zoptions = ZOOM_options_create();
386 const char *sru_version = 0;
388 struct session_database *sdb = client_get_database(con->client);
389 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
390 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
392 assert(host->ipport);
395 ZOOM_options_set(zoptions, "async", "1");
396 ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
397 ZOOM_options_set(zoptions, "implementationVersion", VERSION);
399 if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
400 ZOOM_options_set(zoptions, "charset", charset);
402 if (zproxy && *zproxy)
404 con->zproxy = xstrdup(zproxy);
405 ZOOM_options_set(zoptions, "proxy", zproxy);
407 if (apdulog && *apdulog)
408 ZOOM_options_set(zoptions, "apdulog", apdulog);
410 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
411 ZOOM_options_set(zoptions, "user", auth);
412 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
413 ZOOM_options_set(zoptions, "sru", sru);
414 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
416 ZOOM_options_set(zoptions, "sru_version", sru_version);
418 if (!(link = ZOOM_connection_create(zoptions)))
420 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
421 ZOOM_options_destroy(zoptions);
427 char http_hostport[512];
428 strcpy(http_hostport, "http://");
429 strcat(http_hostport, host->hostport);
430 ZOOM_connection_connect(link, http_hostport, 0);
433 ZOOM_connection_connect(link, host->ipport, 0);
436 con->iochan = iochan_create(0, connection_handler, 0, "connection_socket");
437 con->state = Conn_Connecting;
438 iochan_settimeout(con->iochan, con->operation_timeout);
439 iochan_setdata(con->iochan, con);
440 iochan_add(iochan_man, con->iochan);
442 /* this fragment is bad DRY: from client_prep_connection */
443 client_set_state(con->client, Client_Connecting);
444 ZOOM_options_destroy(zoptions);
448 // Ensure that client has a connection associated
449 int client_prep_connection(struct client *cl,
450 int operation_timeout, int session_timeout,
451 iochan_man_t iochan_man)
453 struct connection *co;
454 struct host *host = client_get_host(cl);
455 struct session_database *sdb = client_get_database(cl);
456 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
458 if (zproxy && zproxy[0] == '\0')
464 co = client_get_connection(cl);
466 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
470 // See if someone else has an idle connection
471 // We should look at timestamps here to select the longest-idle connection
472 yaz_mutex_enter(host->mutex);
473 for (co = host->connections; co; co = co->next)
474 if (connection_is_idle(co) &&
475 (!co->client || client_get_state(co->client) == Client_Idle) &&
476 !strcmp(ZOOM_connection_option_get(co->link, "user"),
477 session_setting_oneval(client_get_database(cl),
480 if (zproxy == 0 && co->zproxy == 0)
482 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
487 connection_release(co);
488 client_set_connection(cl, co);
490 /* ensure that connection is only assigned to this client
491 by marking the client non Idle */
492 client_set_state(cl, Client_Working);
493 yaz_mutex_leave(host->mutex);
494 co->operation_timeout = operation_timeout;
495 co->session_timeout = session_timeout;
496 /* tells ZOOM to reconnect if necessary. Disabled becuase
497 the ZOOM_connection_connect flushes the task queue */
498 ZOOM_connection_connect(co->link, 0, 0);
502 yaz_mutex_leave(host->mutex);
503 co = connection_create(cl, operation_timeout, session_timeout,
517 * c-file-style: "Stroustrup"
518 * indent-tabs-mode: nil
520 * vim: shiftwidth=4 tabstop=8 expandtab