1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 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)
38 #include <sys/socket.h>
42 typedef int socklen_t;
53 #include <yaz/comstack.h>
54 #include <yaz/tcpip.h>
55 #include "connection.h"
61 #include "parameters.h"
64 /** \brief Represents a physical, reusable connection to a remote Z39.50 host
69 ZOOM_resultset resultset;
71 struct client *client;
80 struct connection *next; // next for same host or next in free list
83 static struct connection *connection_freelist = 0;
85 static int connection_connect(struct connection *con);
87 static int connection_is_idle(struct connection *co)
89 ZOOM_connection link = co->link;
92 if (co->state != Conn_Open || !link)
95 event = ZOOM_connection_peek_event(link);
96 if (event == ZOOM_EVENT_NONE ||
97 event == ZOOM_EVENT_END)
103 ZOOM_connection connection_get_link(struct connection *co)
108 ZOOM_resultset connection_get_resultset(struct connection *co)
110 return co->resultset;
113 void connection_set_resultset(struct connection *co, ZOOM_resultset rs)
116 ZOOM_resultset_destroy(co->resultset);
120 static void remove_connection_from_host(struct connection *con)
122 struct connection **conp = &con->host->connections;
128 *conp = (*conp)->next;
131 conp = &(*conp)->next;
136 void connection_continue(struct connection *co)
138 yaz_log(YLOG_LOG, "connection_continue");
139 iochan_setevent(co->iochan, EVENT_OUTPUT);
142 // Close connection and recycle structure
143 void connection_destroy(struct connection *co)
147 ZOOM_connection_destroy(co->link);
148 iochan_destroy(co->iochan);
151 ZOOM_resultset_destroy(co->resultset);
153 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
155 remove_connection_from_host(co);
158 client_disconnect(co->client);
162 co->next = connection_freelist;
163 connection_freelist = co;
166 // Creates a new connection for client, associated with the host of
168 static struct connection *connection_create(struct client *cl)
170 struct connection *new;
171 struct host *host = client_get_host(cl);
173 if ((new = connection_freelist))
174 connection_freelist = new->next;
177 new = xmalloc(sizeof (struct connection));
182 new->next = new->host->connections;
183 new->host->connections = new;
186 client_set_connection(cl, new);
189 new->state = Conn_Resolving;
191 connection_connect(new);
195 static void connection_handler(IOCHAN i, int event)
197 struct connection *co = iochan_getdata(i);
198 struct client *cl = co->client;
199 struct session *se = 0;
202 se = client_get_session(cl);
205 connection_destroy(co);
209 if (event & EVENT_TIMEOUT)
211 if (co->state == Conn_Connecting)
213 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
218 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
219 connection_destroy(co);
225 ZOOM_connection link = co->link;
227 if (ZOOM_event(1, &link))
231 int event = ZOOM_connection_last_event(link);
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);
245 case ZOOM_EVENT_RECV_APDU:
246 client_set_state(co->client, Client_Idle);
248 case ZOOM_EVENT_CONNECT:
249 yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
250 co->state = Conn_Open;
251 client_set_state(co->client, Client_Connected);
252 iochan_settimeout(i, global_parameters.z3950_session_timeout);
254 case ZOOM_EVENT_RECV_SEARCH:
255 yaz_log(YLOG_LOG, "Search response from %s", client_get_url(cl));
256 client_search_response(cl);
258 case ZOOM_EVENT_RECV_RECORD:
259 yaz_log(YLOG_LOG, "Record from %s", client_get_url(cl));
260 client_record_response(cl);
263 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
264 event, client_get_url(cl));
267 while (ZOOM_event_nonblock(1, &link));
273 // Disassociate connection from client
274 void connection_release(struct connection *co)
276 struct client *cl = co->client;
278 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
281 client_set_connection(cl, 0);
285 void connect_resolver_host(struct host *host)
287 struct connection *con = host->connections;
290 if (con->state == Conn_Resolving)
292 if (!host->ipport) /* unresolved */
294 connection_destroy(con);
295 /* start all over .. at some point it will be NULL */
296 con = host->connections;
299 else if (!con->client)
301 connection_destroy(con);
302 /* start all over .. at some point it will be NULL */
303 con = host->connections;
308 connection_connect(con);
309 client_start_search(con->client);
314 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
320 static struct host *connection_get_host(struct connection *con)
325 // Callback for use by event loop
326 // We do this because ZOOM connections don't always have (the same) sockets
327 static int socketfun(IOCHAN c)
329 struct connection *co = iochan_getdata(c);
332 return ZOOM_connection_get_socket(co->link);
335 // Because ZOOM always knows what events it is interested in; we may not
336 static int maskfun(IOCHAN c)
338 struct connection *co = iochan_getdata(c);
342 // This is cheating a little, and assuming that eventl mask IDs are always
343 // the same as ZOOM-C's.
344 return ZOOM_connection_get_mask(co->link);
347 static int connection_connect(struct connection *con)
349 ZOOM_connection link = 0;
350 struct host *host = connection_get_host(con);
351 ZOOM_options zoptions = ZOOM_options_create();
354 const char *sru_version = 0;
355 char ipport[512] = "";
357 struct session_database *sdb = client_get_database(con->client);
358 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
359 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
361 assert(host->ipport);
364 ZOOM_options_set(zoptions, "async", "1");
365 ZOOM_options_set(zoptions, "implementationName",
366 global_parameters.implementationName);
367 ZOOM_options_set(zoptions, "implementationVersion",
368 global_parameters.implementationVersion);
369 if (zproxy && *zproxy)
371 con->zproxy = xstrdup(zproxy);
372 ZOOM_options_set(zoptions, "proxy", zproxy);
374 if (apdulog && *apdulog)
375 ZOOM_options_set(zoptions, "apdulog", apdulog);
377 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
378 ZOOM_options_set(zoptions, "user", auth);
379 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
380 ZOOM_options_set(zoptions, "sru", sru);
381 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
383 ZOOM_options_set(zoptions, "sru_version", sru_version);
385 if (!(link = ZOOM_connection_create(zoptions)))
387 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
388 ZOOM_options_destroy(zoptions);
393 strcpy(ipport, "http://");
394 strcat(ipport, host->ipport);
395 /* deal with SRU path here because databaseName option is not read in
399 if (*sdb->database->databases[0])
402 strcat(ipport, sdb->database->databases[0]);
405 ZOOM_connection_connect(link, ipport, 0);
408 con->iochan = iochan_create(0, connection_handler, 0);
409 con->state = Conn_Connecting;
410 iochan_settimeout(con->iochan, global_parameters.z3950_connect_timeout);
411 iochan_setdata(con->iochan, con);
412 iochan_setsocketfun(con->iochan, socketfun);
413 iochan_setmaskfun(con->iochan, maskfun);
414 pazpar2_add_channel(con->iochan);
416 /* this fragment is bad DRY: from client_prep_connection */
417 client_set_state(con->client, Client_Connecting);
418 ZOOM_options_destroy(zoptions);
419 // This creates the connection
420 ZOOM_connection_process(link);
424 const char *connection_get_url(struct connection *co)
426 return client_get_url(co->client);
429 // Ensure that client has a connection associated
430 int client_prep_connection(struct client *cl)
432 struct connection *co;
433 struct session *se = client_get_session(cl);
434 struct host *host = client_get_host(cl);
435 struct session_database *sdb = client_get_database(cl);
436 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
438 if (zproxy && zproxy[0] == '\0')
441 co = client_get_connection(cl);
443 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
447 // See if someone else has an idle connection
448 // We should look at timestamps here to select the longest-idle connection
449 for (co = host->connections; co; co = co->next)
450 if (connection_is_idle(co) &&
451 (!co->client || client_get_session(co->client) != se) &&
452 !strcmp(ZOOM_connection_option_get(co->link, "user"),
453 session_setting_oneval(client_get_database(cl),
456 if (zproxy == 0 && co->zproxy == 0)
458 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
463 connection_release(co);
464 client_set_connection(cl, co);
468 co = connection_create(cl);
480 * indent-tabs-mode: nil
482 * vim: shiftwidth=4 tabstop=8 expandtab