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"
51 /** \brief Represents a physical, reusable connection to a remote Z39.50 host
57 struct client *client;
64 int operation_timeout;
66 struct connection *next; // next for same host or next in free list
69 static int connection_connect(struct connection *con, iochan_man_t iochan_man);
71 static int connection_is_idle(struct connection *co)
73 ZOOM_connection link = co->link;
76 if (co->state != Conn_Open || !link)
79 if (!ZOOM_connection_is_idle(link))
81 event = ZOOM_connection_peek_event(link);
82 if (event == ZOOM_EVENT_NONE || event == ZOOM_EVENT_END)
88 ZOOM_connection connection_get_link(struct connection *co)
93 static void remove_connection_from_host(struct connection *con)
95 struct connection **conp = &con->host->connections;
97 yaz_mutex_enter(con->host->mutex);
102 *conp = (*conp)->next;
105 conp = &(*conp)->next;
107 yaz_mutex_leave(con->host->mutex);
110 // Close connection and recycle structure
111 void connection_destroy(struct connection *co)
115 ZOOM_connection_destroy(co->link);
116 iochan_destroy(co->iochan);
118 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
120 remove_connection_from_host(co);
123 client_disconnect(co->client);
129 // Creates a new connection for client, associated with the host of
131 static struct connection *connection_create(struct client *cl,
132 int operation_timeout,
134 iochan_man_t iochan_man)
136 struct connection *new;
137 struct host *host = client_get_host(cl);
139 new = xmalloc(sizeof(*new));
142 yaz_mutex_enter(host->mutex);
143 new->next = new->host->connections;
144 new->host->connections = new;
145 yaz_mutex_leave(host->mutex);
149 client_set_connection(cl, new);
151 new->state = Conn_Resolving;
152 new->operation_timeout = operation_timeout;
153 new->session_timeout = session_timeout;
155 connection_connect(new, iochan_man);
159 static void non_block_events(struct connection *co)
161 struct client *cl = co->client;
162 IOCHAN iochan = co->iochan;
163 ZOOM_connection link = co->link;
167 int r = ZOOM_event_nonblock(1, &link);
170 ev = ZOOM_connection_last_event(link);
172 yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
178 const char *error, *addinfo;
180 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
182 yaz_log(YLOG_LOG, "Error %s from %s",
183 error, client_get_url(cl));
185 iochan_settimeout(iochan, co->session_timeout);
186 client_set_diagnostic(cl, err);
187 client_set_state(cl, Client_Idle);
190 case ZOOM_EVENT_SEND_DATA:
192 case ZOOM_EVENT_RECV_DATA:
194 case ZOOM_EVENT_UNKNOWN:
196 case ZOOM_EVENT_SEND_APDU:
197 client_set_state(co->client, Client_Working);
198 iochan_settimeout(iochan, co->operation_timeout);
200 case ZOOM_EVENT_RECV_APDU:
202 case ZOOM_EVENT_CONNECT:
203 yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
204 co->state = Conn_Open;
206 case ZOOM_EVENT_RECV_SEARCH:
207 client_search_response(cl);
209 case ZOOM_EVENT_RECV_RECORD:
210 client_record_response(cl);
213 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
214 ev, client_get_url(cl));
219 void connection_continue(struct connection *co)
221 non_block_events(co);
224 static void connection_handler(IOCHAN iochan, int event)
226 struct connection *co = iochan_getdata(iochan);
227 struct client *cl = co->client;
231 /* no client associated with it.. We are probably getting
232 a closed connection from the target.. Or, perhaps, an unexpected
233 package.. We will just close the connection */
234 connection_destroy(co);
237 if (event & EVENT_TIMEOUT)
239 if (co->state == Conn_Connecting)
241 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
246 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
247 connection_destroy(co);
252 non_block_events(co);
254 ZOOM_connection_fire_event_socket(co->link, event);
256 non_block_events(co);
261 // Disassociate connection from client
262 void connection_release(struct connection *co)
264 struct client *cl = co->client;
268 client_set_connection(cl, 0);
272 void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
274 struct connection *con;
277 yaz_mutex_enter(host->mutex);
278 con = host->connections;
281 if (con->state == Conn_Resolving)
283 if (!host->ipport) /* unresolved */
285 yaz_mutex_leave(host->mutex);
286 connection_destroy(con);
288 /* start all over .. at some point it will be NULL */
290 else if (!con->client)
292 yaz_mutex_leave(host->mutex);
293 connection_destroy(con);
294 /* start all over .. at some point it will be NULL */
299 yaz_mutex_leave(host->mutex);
300 connection_connect(con, iochan_man);
301 client_start_search(con->client);
307 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
311 yaz_mutex_leave(host->mutex);
314 static struct host *connection_get_host(struct connection *con)
319 // Callback for use by event loop
320 // We do this because ZOOM connections don't always have (the same) sockets
321 static int socketfun(IOCHAN c)
323 struct connection *co = iochan_getdata(c);
326 return ZOOM_connection_get_socket(co->link);
329 // Because ZOOM always knows what events it is interested in; we may not
330 static int maskfun(IOCHAN c)
332 struct connection *co = iochan_getdata(c);
336 // This is cheating a little, and assuming that eventl mask IDs are always
337 // the same as ZOOM-C's.
338 return ZOOM_connection_get_mask(co->link);
341 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
343 ZOOM_connection link = 0;
344 struct host *host = connection_get_host(con);
345 ZOOM_options zoptions = ZOOM_options_create();
349 const char *sru_version = 0;
351 struct session_database *sdb = client_get_database(con->client);
352 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
353 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
355 assert(host->ipport);
358 ZOOM_options_set(zoptions, "async", "1");
359 ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
360 ZOOM_options_set(zoptions, "implementationVersion", VERSION);
362 if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
363 ZOOM_options_set(zoptions, "charset", charset);
365 if (zproxy && *zproxy)
367 con->zproxy = xstrdup(zproxy);
368 ZOOM_options_set(zoptions, "proxy", zproxy);
370 if (apdulog && *apdulog)
371 ZOOM_options_set(zoptions, "apdulog", apdulog);
373 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
374 ZOOM_options_set(zoptions, "user", auth);
375 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
376 ZOOM_options_set(zoptions, "sru", sru);
377 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
379 ZOOM_options_set(zoptions, "sru_version", sru_version);
381 if (!(link = ZOOM_connection_create(zoptions)))
383 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
384 ZOOM_options_destroy(zoptions);
390 char http_hostport[512];
391 strcpy(http_hostport, "http://");
392 strcat(http_hostport, host->hostport);
393 ZOOM_connection_connect(link, http_hostport, 0);
396 ZOOM_connection_connect(link, host->ipport, 0);
399 con->iochan = iochan_create(0, connection_handler, 0);
400 con->state = Conn_Connecting;
401 iochan_settimeout(con->iochan, con->operation_timeout);
402 iochan_setdata(con->iochan, con);
403 iochan_setsocketfun(con->iochan, socketfun);
404 iochan_setmaskfun(con->iochan, maskfun);
405 iochan_add(iochan_man, con->iochan);
407 /* this fragment is bad DRY: from client_prep_connection */
408 client_set_state(con->client, Client_Connecting);
409 ZOOM_options_destroy(zoptions);
413 const char *connection_get_url(struct connection *co)
415 return client_get_url(co->client);
418 // Ensure that client has a connection associated
419 int client_prep_connection(struct client *cl,
420 int operation_timeout, int session_timeout,
421 iochan_man_t iochan_man)
423 struct connection *co;
424 struct host *host = client_get_host(cl);
425 struct session_database *sdb = client_get_database(cl);
426 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
428 if (zproxy && zproxy[0] == '\0')
434 co = client_get_connection(cl);
436 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
440 // See if someone else has an idle connection
441 // We should look at timestamps here to select the longest-idle connection
442 yaz_mutex_enter(host->mutex);
443 for (co = host->connections; co; co = co->next)
444 if (connection_is_idle(co) &&
445 (!co->client || client_get_state(co->client) == Client_Idle) &&
446 !strcmp(ZOOM_connection_option_get(co->link, "user"),
447 session_setting_oneval(client_get_database(cl),
450 if (zproxy == 0 && co->zproxy == 0)
452 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
457 connection_release(co);
458 client_set_connection(cl, co);
460 yaz_mutex_leave(host->mutex);
461 co->operation_timeout = operation_timeout;
462 co->session_timeout = session_timeout;
463 /* tells ZOOM to reconnect if necessary. Disabled becuase
464 the ZOOM_connection_connect flushes the task queue */
465 ZOOM_connection_connect(co->link, 0, 0);
469 yaz_mutex_leave(host->mutex);
470 co = connection_create(cl, operation_timeout, session_timeout,
484 * c-file-style: "Stroustrup"
485 * indent-tabs-mode: nil
487 * vim: shiftwidth=4 tabstop=8 expandtab