1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 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 #include "parameters.h"
53 /** \brief Represents a physical, reusable connection to a remote Z39.50 host
58 ZOOM_resultset resultset;
60 struct client *client;
69 struct connection *next; // next for same host or next in free list
72 static struct connection *connection_freelist = 0;
74 static int connection_connect(struct connection *con);
76 static int connection_is_idle(struct connection *co)
78 ZOOM_connection link = co->link;
81 if (co->state != Conn_Open || !link)
84 if (!ZOOM_connection_is_idle(link))
86 event = ZOOM_connection_peek_event(link);
87 if (event == ZOOM_EVENT_NONE || event == ZOOM_EVENT_END)
93 ZOOM_connection connection_get_link(struct connection *co)
98 ZOOM_resultset connection_get_resultset(struct connection *co)
100 return co->resultset;
103 void connection_set_resultset(struct connection *co, ZOOM_resultset rs)
106 ZOOM_resultset_destroy(co->resultset);
110 static void remove_connection_from_host(struct connection *con)
112 struct connection **conp = &con->host->connections;
118 *conp = (*conp)->next;
121 conp = &(*conp)->next;
126 // Close connection and recycle structure
127 void connection_destroy(struct connection *co)
131 ZOOM_connection_destroy(co->link);
132 iochan_destroy(co->iochan);
135 ZOOM_resultset_destroy(co->resultset);
137 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
139 remove_connection_from_host(co);
142 client_disconnect(co->client);
146 co->next = connection_freelist;
147 connection_freelist = co;
150 // Creates a new connection for client, associated with the host of
152 static struct connection *connection_create(struct client *cl)
154 struct connection *new;
155 struct host *host = client_get_host(cl);
157 if ((new = connection_freelist))
158 connection_freelist = new->next;
161 new = xmalloc(sizeof (struct connection));
166 new->next = new->host->connections;
167 new->host->connections = new;
170 client_set_connection(cl, new);
173 new->state = Conn_Resolving;
175 connection_connect(new);
179 static void non_block_events(struct connection *co)
181 struct client *cl = co->client;
182 IOCHAN iochan = co->iochan;
183 ZOOM_connection link = co->link;
187 int r = ZOOM_event_nonblock(1, &link);
190 ev = ZOOM_connection_last_event(link);
192 yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
198 const char *error, *addinfo;
199 if (ZOOM_connection_error(link, &error, &addinfo))
201 yaz_log(YLOG_LOG, "Error %s from %s",
202 error, client_get_url(cl));
204 client_set_state(cl, Client_Idle);
207 case ZOOM_EVENT_SEND_DATA:
209 case ZOOM_EVENT_RECV_DATA:
211 case ZOOM_EVENT_UNKNOWN:
213 case ZOOM_EVENT_SEND_APDU:
214 client_set_state(co->client, Client_Working);
216 case ZOOM_EVENT_RECV_APDU:
218 case ZOOM_EVENT_CONNECT:
219 yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
220 co->state = Conn_Open;
221 iochan_settimeout(iochan, global_parameters.z3950_session_timeout);
223 case ZOOM_EVENT_RECV_SEARCH:
224 client_search_response(cl);
226 case ZOOM_EVENT_RECV_RECORD:
227 client_record_response(cl);
230 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
231 ev, client_get_url(cl));
236 void connection_continue(struct connection *co)
238 non_block_events(co);
241 static void connection_handler(IOCHAN iochan, int event)
243 struct connection *co = iochan_getdata(iochan);
244 struct client *cl = co->client;
245 struct session *se = 0;
248 se = client_get_session(cl);
251 connection_destroy(co);
255 if (event & EVENT_TIMEOUT)
257 if (co->state == Conn_Connecting)
259 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
264 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
265 connection_destroy(co);
270 non_block_events(co);
272 ZOOM_connection_fire_event_socket(co->link, event);
274 non_block_events(co);
279 // Disassociate connection from client
280 void connection_release(struct connection *co)
282 struct client *cl = co->client;
284 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
287 client_set_connection(cl, 0);
291 void connect_resolver_host(struct host *host)
293 struct connection *con = host->connections;
296 if (con->state == Conn_Resolving)
298 if (!host->ipport) /* unresolved */
300 connection_destroy(con);
301 /* start all over .. at some point it will be NULL */
302 con = host->connections;
305 else if (!con->client)
307 connection_destroy(con);
308 /* start all over .. at some point it will be NULL */
309 con = host->connections;
314 connection_connect(con);
315 client_start_search(con->client);
320 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
326 static struct host *connection_get_host(struct connection *con)
331 // Callback for use by event loop
332 // We do this because ZOOM connections don't always have (the same) sockets
333 static int socketfun(IOCHAN c)
335 struct connection *co = iochan_getdata(c);
338 return ZOOM_connection_get_socket(co->link);
341 // Because ZOOM always knows what events it is interested in; we may not
342 static int maskfun(IOCHAN c)
344 struct connection *co = iochan_getdata(c);
348 // This is cheating a little, and assuming that eventl mask IDs are always
349 // the same as ZOOM-C's.
350 return ZOOM_connection_get_mask(co->link);
353 static int connection_connect(struct connection *con)
355 ZOOM_connection link = 0;
356 struct host *host = connection_get_host(con);
357 ZOOM_options zoptions = ZOOM_options_create();
360 const char *sru_version = 0;
361 char ipport[512] = "";
363 struct session_database *sdb = client_get_database(con->client);
364 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
365 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
367 assert(host->ipport);
370 ZOOM_options_set(zoptions, "async", "1");
371 ZOOM_options_set(zoptions, "implementationName",
372 global_parameters.implementationName);
373 ZOOM_options_set(zoptions, "implementationVersion",
374 global_parameters.implementationVersion);
375 if (zproxy && *zproxy)
377 con->zproxy = xstrdup(zproxy);
378 ZOOM_options_set(zoptions, "proxy", zproxy);
380 if (apdulog && *apdulog)
381 ZOOM_options_set(zoptions, "apdulog", apdulog);
383 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
384 ZOOM_options_set(zoptions, "user", auth);
385 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
386 ZOOM_options_set(zoptions, "sru", sru);
387 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
389 ZOOM_options_set(zoptions, "sru_version", sru_version);
391 if (!(link = ZOOM_connection_create(zoptions)))
393 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
394 ZOOM_options_destroy(zoptions);
399 strcpy(ipport, "http://");
400 strcat(ipport, host->ipport);
402 ZOOM_connection_connect(link, ipport, 0);
405 con->iochan = iochan_create(0, connection_handler, 0);
406 con->state = Conn_Connecting;
407 iochan_settimeout(con->iochan, global_parameters.z3950_connect_timeout);
408 iochan_setdata(con->iochan, con);
409 iochan_setsocketfun(con->iochan, socketfun);
410 iochan_setmaskfun(con->iochan, maskfun);
411 pazpar2_add_channel(con->iochan);
413 /* this fragment is bad DRY: from client_prep_connection */
414 client_set_state(con->client, Client_Connecting);
415 ZOOM_options_destroy(zoptions);
416 // This creates the connection
417 ZOOM_connection_process(link);
421 const char *connection_get_url(struct connection *co)
423 return client_get_url(co->client);
426 // Ensure that client has a connection associated
427 int client_prep_connection(struct client *cl)
429 struct connection *co;
430 struct session *se = client_get_session(cl);
431 struct host *host = client_get_host(cl);
432 struct session_database *sdb = client_get_database(cl);
433 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
435 if (zproxy && zproxy[0] == '\0')
438 co = client_get_connection(cl);
440 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
444 // See if someone else has an idle connection
445 // We should look at timestamps here to select the longest-idle connection
446 for (co = host->connections; co; co = co->next)
447 if (connection_is_idle(co) &&
448 (!co->client || client_get_session(co->client) != se) &&
449 !strcmp(ZOOM_connection_option_get(co->link, "user"),
450 session_setting_oneval(client_get_database(cl),
453 if (zproxy == 0 && co->zproxy == 0)
455 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
460 connection_release(co);
461 client_set_connection(cl, co);
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 co = connection_create(cl);
482 * c-file-style: "Stroustrup"
483 * indent-tabs-mode: nil
485 * vim: shiftwidth=4 tabstop=8 expandtab