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;
66 int operation_timeout;
68 struct connection *next; // next for same host or next in free list
71 static struct connection *connection_freelist = 0;
73 static int connection_connect(struct connection *con);
75 static int connection_is_idle(struct connection *co)
77 ZOOM_connection link = co->link;
80 if (co->state != Conn_Open || !link)
83 if (!ZOOM_connection_is_idle(link))
85 event = ZOOM_connection_peek_event(link);
86 if (event == ZOOM_EVENT_NONE || event == ZOOM_EVENT_END)
92 ZOOM_connection connection_get_link(struct connection *co)
97 static void remove_connection_from_host(struct connection *con)
99 struct connection **conp = &con->host->connections;
105 *conp = (*conp)->next;
108 conp = &(*conp)->next;
113 // Close connection and recycle structure
114 void connection_destroy(struct connection *co)
118 ZOOM_connection_destroy(co->link);
119 iochan_destroy(co->iochan);
121 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
123 remove_connection_from_host(co);
126 client_disconnect(co->client);
130 co->next = connection_freelist;
131 connection_freelist = co;
134 // Creates a new connection for client, associated with the host of
136 static struct connection *connection_create(struct client *cl,
137 int operation_timeout,
140 struct connection *new;
141 struct host *host = client_get_host(cl);
143 if ((new = connection_freelist))
144 connection_freelist = new->next;
147 new = xmalloc(sizeof (struct connection));
152 new->next = new->host->connections;
153 new->host->connections = new;
156 client_set_connection(cl, new);
158 new->state = Conn_Resolving;
159 new->operation_timeout = operation_timeout;
160 new->session_timeout = session_timeout;
162 connection_connect(new);
166 static void non_block_events(struct connection *co)
168 struct client *cl = co->client;
169 IOCHAN iochan = co->iochan;
170 ZOOM_connection link = co->link;
174 int r = ZOOM_event_nonblock(1, &link);
177 ev = ZOOM_connection_last_event(link);
179 yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
185 const char *error, *addinfo;
187 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
189 yaz_log(YLOG_LOG, "Error %s from %s",
190 error, client_get_url(cl));
192 iochan_settimeout(iochan, co->session_timeout);
193 client_set_diagnostic(cl, err);
194 client_set_state(cl, Client_Idle);
197 case ZOOM_EVENT_SEND_DATA:
199 case ZOOM_EVENT_RECV_DATA:
201 case ZOOM_EVENT_UNKNOWN:
203 case ZOOM_EVENT_SEND_APDU:
204 client_set_state(co->client, Client_Working);
205 iochan_settimeout(iochan, co->operation_timeout);
207 case ZOOM_EVENT_RECV_APDU:
209 case ZOOM_EVENT_CONNECT:
210 yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
211 co->state = Conn_Open;
213 case ZOOM_EVENT_RECV_SEARCH:
214 client_search_response(cl);
216 case ZOOM_EVENT_RECV_RECORD:
217 client_record_response(cl);
220 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
221 ev, client_get_url(cl));
226 void connection_continue(struct connection *co)
228 non_block_events(co);
231 static void connection_handler(IOCHAN iochan, int event)
233 struct connection *co = iochan_getdata(iochan);
234 struct client *cl = co->client;
235 struct session *se = 0;
238 se = client_get_session(cl);
241 connection_destroy(co);
245 if (event & EVENT_TIMEOUT)
247 if (co->state == Conn_Connecting)
249 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
254 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
255 connection_destroy(co);
260 non_block_events(co);
262 ZOOM_connection_fire_event_socket(co->link, event);
264 non_block_events(co);
269 // Disassociate connection from client
270 void connection_release(struct connection *co)
272 struct client *cl = co->client;
274 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
277 client_set_connection(cl, 0);
281 void connect_resolver_host(struct host *host)
283 struct connection *con = host->connections;
286 if (con->state == Conn_Resolving)
288 if (!host->ipport) /* unresolved */
290 connection_destroy(con);
291 /* start all over .. at some point it will be NULL */
292 con = host->connections;
295 else if (!con->client)
297 connection_destroy(con);
298 /* start all over .. at some point it will be NULL */
299 con = host->connections;
304 connection_connect(con);
305 client_start_search(con->client);
310 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
316 static struct host *connection_get_host(struct connection *con)
321 // Callback for use by event loop
322 // We do this because ZOOM connections don't always have (the same) sockets
323 static int socketfun(IOCHAN c)
325 struct connection *co = iochan_getdata(c);
328 return ZOOM_connection_get_socket(co->link);
331 // Because ZOOM always knows what events it is interested in; we may not
332 static int maskfun(IOCHAN c)
334 struct connection *co = iochan_getdata(c);
338 // This is cheating a little, and assuming that eventl mask IDs are always
339 // the same as ZOOM-C's.
340 return ZOOM_connection_get_mask(co->link);
343 static int connection_connect(struct connection *con)
345 ZOOM_connection link = 0;
346 struct host *host = connection_get_host(con);
347 ZOOM_options zoptions = ZOOM_options_create();
350 const char *sru_version = 0;
352 struct session_database *sdb = client_get_database(con->client);
353 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
354 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
356 assert(host->ipport);
359 ZOOM_options_set(zoptions, "async", "1");
360 ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
361 ZOOM_options_set(zoptions, "implementationVersion", VERSION);
362 if (zproxy && *zproxy)
364 con->zproxy = xstrdup(zproxy);
365 ZOOM_options_set(zoptions, "proxy", zproxy);
367 if (apdulog && *apdulog)
368 ZOOM_options_set(zoptions, "apdulog", apdulog);
370 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
371 ZOOM_options_set(zoptions, "user", auth);
372 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
373 ZOOM_options_set(zoptions, "sru", sru);
374 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
376 ZOOM_options_set(zoptions, "sru_version", sru_version);
378 if (!(link = ZOOM_connection_create(zoptions)))
380 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
381 ZOOM_options_destroy(zoptions);
387 char http_hostport[512];
388 strcpy(http_hostport, "http://");
389 strcat(http_hostport, host->hostport);
390 ZOOM_connection_connect(link, http_hostport, 0);
393 ZOOM_connection_connect(link, host->ipport, 0);
396 con->iochan = iochan_create(0, connection_handler, 0);
397 con->state = Conn_Connecting;
398 iochan_settimeout(con->iochan, con->operation_timeout);
399 iochan_setdata(con->iochan, con);
400 iochan_setsocketfun(con->iochan, socketfun);
401 iochan_setmaskfun(con->iochan, maskfun);
402 pazpar2_add_channel(con->iochan);
404 /* this fragment is bad DRY: from client_prep_connection */
405 client_set_state(con->client, Client_Connecting);
406 ZOOM_options_destroy(zoptions);
410 const char *connection_get_url(struct connection *co)
412 return client_get_url(co->client);
415 // Ensure that client has a connection associated
416 int client_prep_connection(struct client *cl,
417 int operation_timeout, int session_timeout)
419 struct connection *co;
420 struct session *se = client_get_session(cl);
421 struct host *host = client_get_host(cl);
422 struct session_database *sdb = client_get_database(cl);
423 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
425 if (zproxy && zproxy[0] == '\0')
431 co = client_get_connection(cl);
433 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
437 // See if someone else has an idle connection
438 // We should look at timestamps here to select the longest-idle connection
439 for (co = host->connections; co; co = co->next)
440 if (connection_is_idle(co) &&
441 (!co->client || client_get_session(co->client) != se) &&
442 !strcmp(ZOOM_connection_option_get(co->link, "user"),
443 session_setting_oneval(client_get_database(cl),
446 if (zproxy == 0 && co->zproxy == 0)
448 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
453 connection_release(co);
454 client_set_connection(cl, co);
456 co->operation_timeout = operation_timeout;
457 co->session_timeout = session_timeout;
458 /* tells ZOOM to reconnect if necessary. Disabled becuase
459 the ZOOM_connection_connect flushes the task queue */
460 ZOOM_connection_connect(co->link, 0, 0);
464 co = connection_create(cl, operation_timeout, session_timeout);
477 * c-file-style: "Stroustrup"
478 * indent-tabs-mode: nil
480 * vim: shiftwidth=4 tabstop=8 expandtab