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
59 struct client *client;
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)
138 struct connection *new;
139 struct host *host = client_get_host(cl);
141 if ((new = connection_freelist))
142 connection_freelist = new->next;
145 new = xmalloc(sizeof (struct connection));
150 new->next = new->host->connections;
151 new->host->connections = new;
154 client_set_connection(cl, new);
156 new->state = Conn_Resolving;
158 connection_connect(new);
162 static void non_block_events(struct connection *co)
164 struct client *cl = co->client;
165 IOCHAN iochan = co->iochan;
166 ZOOM_connection link = co->link;
170 int r = ZOOM_event_nonblock(1, &link);
173 ev = ZOOM_connection_last_event(link);
175 yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
181 const char *error, *addinfo;
182 if (ZOOM_connection_error(link, &error, &addinfo))
184 yaz_log(YLOG_LOG, "Error %s from %s",
185 error, client_get_url(cl));
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);
199 case ZOOM_EVENT_RECV_APDU:
201 case ZOOM_EVENT_CONNECT:
202 yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
203 co->state = Conn_Open;
204 iochan_settimeout(iochan, global_parameters.z3950_session_timeout);
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;
228 struct session *se = 0;
231 se = client_get_session(cl);
234 connection_destroy(co);
238 if (event & EVENT_TIMEOUT)
240 if (co->state == Conn_Connecting)
242 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
247 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
248 connection_destroy(co);
253 non_block_events(co);
255 ZOOM_connection_fire_event_socket(co->link, event);
257 non_block_events(co);
262 // Disassociate connection from client
263 void connection_release(struct connection *co)
265 struct client *cl = co->client;
267 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
270 client_set_connection(cl, 0);
274 void connect_resolver_host(struct host *host)
276 struct connection *con = host->connections;
279 if (con->state == Conn_Resolving)
281 if (!host->ipport) /* unresolved */
283 connection_destroy(con);
284 /* start all over .. at some point it will be NULL */
285 con = host->connections;
288 else if (!con->client)
290 connection_destroy(con);
291 /* start all over .. at some point it will be NULL */
292 con = host->connections;
297 connection_connect(con);
298 client_start_search(con->client);
303 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
309 static struct host *connection_get_host(struct connection *con)
314 // Callback for use by event loop
315 // We do this because ZOOM connections don't always have (the same) sockets
316 static int socketfun(IOCHAN c)
318 struct connection *co = iochan_getdata(c);
321 return ZOOM_connection_get_socket(co->link);
324 // Because ZOOM always knows what events it is interested in; we may not
325 static int maskfun(IOCHAN c)
327 struct connection *co = iochan_getdata(c);
331 // This is cheating a little, and assuming that eventl mask IDs are always
332 // the same as ZOOM-C's.
333 return ZOOM_connection_get_mask(co->link);
336 static int connection_connect(struct connection *con)
338 ZOOM_connection link = 0;
339 struct host *host = connection_get_host(con);
340 ZOOM_options zoptions = ZOOM_options_create();
343 const char *sru_version = 0;
344 char ipport[512] = "";
346 struct session_database *sdb = client_get_database(con->client);
347 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
348 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
350 assert(host->ipport);
353 ZOOM_options_set(zoptions, "async", "1");
354 ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
355 ZOOM_options_set(zoptions, "implementationVersion", VERSION);
356 if (zproxy && *zproxy)
358 con->zproxy = xstrdup(zproxy);
359 ZOOM_options_set(zoptions, "proxy", zproxy);
361 if (apdulog && *apdulog)
362 ZOOM_options_set(zoptions, "apdulog", apdulog);
364 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
365 ZOOM_options_set(zoptions, "user", auth);
366 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
367 ZOOM_options_set(zoptions, "sru", sru);
368 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
370 ZOOM_options_set(zoptions, "sru_version", sru_version);
372 if (!(link = ZOOM_connection_create(zoptions)))
374 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
375 ZOOM_options_destroy(zoptions);
380 strcpy(ipport, "http://");
381 strcat(ipport, host->ipport);
383 ZOOM_connection_connect(link, ipport, 0);
386 con->iochan = iochan_create(0, connection_handler, 0);
387 con->state = Conn_Connecting;
388 iochan_settimeout(con->iochan, global_parameters.z3950_connect_timeout);
389 iochan_setdata(con->iochan, con);
390 iochan_setsocketfun(con->iochan, socketfun);
391 iochan_setmaskfun(con->iochan, maskfun);
392 pazpar2_add_channel(con->iochan);
394 /* this fragment is bad DRY: from client_prep_connection */
395 client_set_state(con->client, Client_Connecting);
396 ZOOM_options_destroy(zoptions);
397 // This creates the connection
398 ZOOM_connection_process(link);
402 const char *connection_get_url(struct connection *co)
404 return client_get_url(co->client);
407 // Ensure that client has a connection associated
408 int client_prep_connection(struct client *cl)
410 struct connection *co;
411 struct session *se = client_get_session(cl);
412 struct host *host = client_get_host(cl);
413 struct session_database *sdb = client_get_database(cl);
414 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
416 if (zproxy && zproxy[0] == '\0')
419 co = client_get_connection(cl);
421 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
425 // See if someone else has an idle connection
426 // We should look at timestamps here to select the longest-idle connection
427 for (co = host->connections; co; co = co->next)
428 if (connection_is_idle(co) &&
429 (!co->client || client_get_session(co->client) != se) &&
430 !strcmp(ZOOM_connection_option_get(co->link, "user"),
431 session_setting_oneval(client_get_database(cl),
434 if (zproxy == 0 && co->zproxy == 0)
436 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
441 connection_release(co);
442 client_set_connection(cl, co);
444 /* tells ZOOM to reconnect if necessary. Disabled becuase
445 the ZOOM_connection_connect flushes the task queue */
446 ZOOM_connection_connect(co->link, 0, 0);
450 co = connection_create(cl);
463 * c-file-style: "Stroustrup"
464 * indent-tabs-mode: nil
466 * vim: shiftwidth=4 tabstop=8 expandtab