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;
82 struct connection *next; // next for same host or next in free list
85 static struct connection *connection_freelist = 0;
87 static int connection_connect(struct connection *con);
89 static int connection_is_idle(struct connection *co)
91 ZOOM_connection link = co->link;
94 if (co->state != Conn_Open || !link)
97 event = ZOOM_connection_peek_event(link);
98 if (event == ZOOM_EVENT_NONE ||
99 event == ZOOM_EVENT_END)
105 ZOOM_connection connection_get_link(struct connection *co)
110 ZOOM_resultset connection_get_resultset(struct connection *co)
112 return co->resultset;
115 void connection_set_resultset(struct connection *co, ZOOM_resultset rs)
118 ZOOM_resultset_destroy(co->resultset);
119 co->set_id_request++;
123 static void remove_connection_from_host(struct connection *con)
125 struct connection **conp = &con->host->connections;
131 *conp = (*conp)->next;
134 conp = &(*conp)->next;
139 // Close connection and recycle structure
140 void connection_destroy(struct connection *co)
144 ZOOM_connection_destroy(co->link);
145 iochan_destroy(co->iochan);
148 ZOOM_resultset_destroy(co->resultset);
150 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
152 remove_connection_from_host(co);
155 client_disconnect(co->client);
159 co->next = connection_freelist;
160 connection_freelist = co;
163 // Creates a new connection for client, associated with the host of
165 static struct connection *connection_create(struct client *cl)
167 struct connection *new;
168 struct host *host = client_get_host(cl);
170 if ((new = connection_freelist))
171 connection_freelist = new->next;
174 new = xmalloc(sizeof (struct connection));
179 new->next = new->host->connections;
180 new->host->connections = new;
183 client_set_connection(cl, new);
186 new->state = Conn_Resolving;
187 new->set_id_request = 0;
188 new->set_id_response = 0;
190 connection_connect(new);
194 static void non_block_events(struct connection *co)
196 struct client *cl = co->client;
197 IOCHAN iochan = co->iochan;
198 ZOOM_connection link = co->link;
202 int r = ZOOM_event_nonblock(1, &link);
205 ev = ZOOM_connection_last_event(link);
209 if (co->set_id_request == co->set_id_response)
210 client_set_state(co->client, Client_Idle);
212 case ZOOM_EVENT_SEND_DATA:
214 case ZOOM_EVENT_RECV_DATA:
216 case ZOOM_EVENT_UNKNOWN:
218 case ZOOM_EVENT_SEND_APDU:
219 client_set_state(co->client, Client_Working);
221 case ZOOM_EVENT_RECV_APDU:
223 case ZOOM_EVENT_CONNECT:
224 yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
225 co->state = Conn_Open;
226 client_set_state(co->client, Client_Connected);
227 iochan_settimeout(iochan, global_parameters.z3950_session_timeout);
229 case ZOOM_EVENT_RECV_SEARCH:
230 co->set_id_response++;
231 if (co->set_id_request == co->set_id_response)
232 client_search_response(cl);
234 case ZOOM_EVENT_RECV_RECORD:
235 if (co->set_id_request == co->set_id_response)
236 client_record_response(cl);
239 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
240 ev, client_get_url(cl));
245 void connection_continue(struct connection *co)
247 non_block_events(co);
250 static void connection_handler(IOCHAN iochan, int event)
252 struct connection *co = iochan_getdata(iochan);
253 struct client *cl = co->client;
254 struct session *se = 0;
257 se = client_get_session(cl);
260 connection_destroy(co);
264 if (event & EVENT_TIMEOUT)
266 if (co->state == Conn_Connecting)
268 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
273 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
274 connection_destroy(co);
279 non_block_events(co);
281 ZOOM_connection_fire_event_socket(co->link, event);
283 non_block_events(co);
288 // Disassociate connection from client
289 void connection_release(struct connection *co)
291 struct client *cl = co->client;
293 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
296 client_set_connection(cl, 0);
300 void connect_resolver_host(struct host *host)
302 struct connection *con = host->connections;
305 if (con->state == Conn_Resolving)
307 if (!host->ipport) /* unresolved */
309 connection_destroy(con);
310 /* start all over .. at some point it will be NULL */
311 con = host->connections;
314 else if (!con->client)
316 connection_destroy(con);
317 /* start all over .. at some point it will be NULL */
318 con = host->connections;
323 connection_connect(con);
324 client_start_search(con->client);
329 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
335 static struct host *connection_get_host(struct connection *con)
340 // Callback for use by event loop
341 // We do this because ZOOM connections don't always have (the same) sockets
342 static int socketfun(IOCHAN c)
344 struct connection *co = iochan_getdata(c);
347 return ZOOM_connection_get_socket(co->link);
350 // Because ZOOM always knows what events it is interested in; we may not
351 static int maskfun(IOCHAN c)
353 struct connection *co = iochan_getdata(c);
357 // This is cheating a little, and assuming that eventl mask IDs are always
358 // the same as ZOOM-C's.
359 return ZOOM_connection_get_mask(co->link);
362 static int connection_connect(struct connection *con)
364 ZOOM_connection link = 0;
365 struct host *host = connection_get_host(con);
366 ZOOM_options zoptions = ZOOM_options_create();
369 const char *sru_version = 0;
370 char ipport[512] = "";
372 struct session_database *sdb = client_get_database(con->client);
373 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
374 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
376 assert(host->ipport);
379 ZOOM_options_set(zoptions, "async", "1");
380 ZOOM_options_set(zoptions, "implementationName",
381 global_parameters.implementationName);
382 ZOOM_options_set(zoptions, "implementationVersion",
383 global_parameters.implementationVersion);
384 if (zproxy && *zproxy)
386 con->zproxy = xstrdup(zproxy);
387 ZOOM_options_set(zoptions, "proxy", zproxy);
389 if (apdulog && *apdulog)
390 ZOOM_options_set(zoptions, "apdulog", apdulog);
392 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
393 ZOOM_options_set(zoptions, "user", auth);
394 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
395 ZOOM_options_set(zoptions, "sru", sru);
396 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
398 ZOOM_options_set(zoptions, "sru_version", sru_version);
400 if (!(link = ZOOM_connection_create(zoptions)))
402 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
403 ZOOM_options_destroy(zoptions);
408 strcpy(ipport, "http://");
409 strcat(ipport, host->ipport);
410 /* deal with SRU path here because databaseName option is not read in
414 if (*sdb->database->databases[0])
417 strcat(ipport, sdb->database->databases[0]);
420 ZOOM_connection_connect(link, ipport, 0);
423 con->iochan = iochan_create(0, connection_handler, 0);
424 con->state = Conn_Connecting;
425 iochan_settimeout(con->iochan, global_parameters.z3950_connect_timeout);
426 iochan_setdata(con->iochan, con);
427 iochan_setsocketfun(con->iochan, socketfun);
428 iochan_setmaskfun(con->iochan, maskfun);
429 pazpar2_add_channel(con->iochan);
431 /* this fragment is bad DRY: from client_prep_connection */
432 client_set_state(con->client, Client_Connecting);
433 ZOOM_options_destroy(zoptions);
434 // This creates the connection
435 ZOOM_connection_process(link);
439 const char *connection_get_url(struct connection *co)
441 return client_get_url(co->client);
444 // Ensure that client has a connection associated
445 int client_prep_connection(struct client *cl)
447 struct connection *co;
448 struct session *se = client_get_session(cl);
449 struct host *host = client_get_host(cl);
450 struct session_database *sdb = client_get_database(cl);
451 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
453 if (zproxy && zproxy[0] == '\0')
456 co = client_get_connection(cl);
458 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
462 // See if someone else has an idle connection
463 // We should look at timestamps here to select the longest-idle connection
464 for (co = host->connections; co; co = co->next)
465 if (connection_is_idle(co) &&
466 (!co->client || client_get_session(co->client) != se) &&
467 !strcmp(ZOOM_connection_option_get(co->link, "user"),
468 session_setting_oneval(client_get_database(cl),
471 if (zproxy == 0 && co->zproxy == 0)
473 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
478 connection_release(co);
479 client_set_connection(cl, co);
482 /* tells ZOOM to reconnect if necessary. Disabled becuase
483 the ZOOM_connection_connect flushes the task queue */
484 ZOOM_connection_connect(co->link, 0, 0);
488 co = connection_create(cl);
500 * indent-tabs-mode: nil
502 * vim: shiftwidth=4 tabstop=8 expandtab