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;
70 struct connection *next; // next for same host or next in free list
73 static struct connection *connection_freelist = 0;
75 static int connection_connect(struct connection *con);
77 static int connection_is_idle(struct connection *co)
79 ZOOM_connection link = co->link;
82 if (co->state != Conn_Open || !link)
85 if (!ZOOM_connection_is_idle(link))
87 event = ZOOM_connection_peek_event(link);
88 if (event == ZOOM_EVENT_NONE || event == ZOOM_EVENT_END)
94 ZOOM_connection connection_get_link(struct connection *co)
99 static void remove_connection_from_host(struct connection *con)
101 struct connection **conp = &con->host->connections;
107 *conp = (*conp)->next;
110 conp = &(*conp)->next;
115 // Close connection and recycle structure
116 void connection_destroy(struct connection *co)
120 ZOOM_connection_destroy(co->link);
121 iochan_destroy(co->iochan);
123 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
125 remove_connection_from_host(co);
128 client_disconnect(co->client);
132 co->next = connection_freelist;
133 connection_freelist = co;
136 // Creates a new connection for client, associated with the host of
138 static struct connection *connection_create(struct client *cl,
142 struct connection *new;
143 struct host *host = client_get_host(cl);
145 if ((new = connection_freelist))
146 connection_freelist = new->next;
149 new = xmalloc(sizeof (struct connection));
154 new->next = new->host->connections;
155 new->host->connections = new;
158 client_set_connection(cl, new);
160 new->state = Conn_Resolving;
161 new->connect_timeout = connect_timeout;
162 new->session_timeout = session_timeout;
164 connection_connect(new);
168 static void non_block_events(struct connection *co)
170 struct client *cl = co->client;
171 IOCHAN iochan = co->iochan;
172 ZOOM_connection link = co->link;
176 int r = ZOOM_event_nonblock(1, &link);
179 ev = ZOOM_connection_last_event(link);
181 yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
187 const char *error, *addinfo;
188 if (ZOOM_connection_error(link, &error, &addinfo))
190 yaz_log(YLOG_LOG, "Error %s from %s",
191 error, client_get_url(cl));
193 client_set_state(cl, Client_Idle);
196 case ZOOM_EVENT_SEND_DATA:
198 case ZOOM_EVENT_RECV_DATA:
200 case ZOOM_EVENT_UNKNOWN:
202 case ZOOM_EVENT_SEND_APDU:
203 client_set_state(co->client, Client_Working);
205 case ZOOM_EVENT_RECV_APDU:
207 case ZOOM_EVENT_CONNECT:
208 yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
209 co->state = Conn_Open;
210 iochan_settimeout(iochan, co->session_timeout);
212 case ZOOM_EVENT_RECV_SEARCH:
213 client_search_response(cl);
215 case ZOOM_EVENT_RECV_RECORD:
216 client_record_response(cl);
219 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
220 ev, client_get_url(cl));
225 void connection_continue(struct connection *co)
227 non_block_events(co);
230 static void connection_handler(IOCHAN iochan, int event)
232 struct connection *co = iochan_getdata(iochan);
233 struct client *cl = co->client;
234 struct session *se = 0;
237 se = client_get_session(cl);
240 connection_destroy(co);
244 if (event & EVENT_TIMEOUT)
246 if (co->state == Conn_Connecting)
248 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
253 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
254 connection_destroy(co);
259 non_block_events(co);
261 ZOOM_connection_fire_event_socket(co->link, event);
263 non_block_events(co);
268 // Disassociate connection from client
269 void connection_release(struct connection *co)
271 struct client *cl = co->client;
273 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
276 client_set_connection(cl, 0);
280 void connect_resolver_host(struct host *host)
282 struct connection *con = host->connections;
285 if (con->state == Conn_Resolving)
287 if (!host->ipport) /* unresolved */
289 connection_destroy(con);
290 /* start all over .. at some point it will be NULL */
291 con = host->connections;
294 else if (!con->client)
296 connection_destroy(con);
297 /* start all over .. at some point it will be NULL */
298 con = host->connections;
303 connection_connect(con);
304 client_start_search(con->client);
309 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
315 static struct host *connection_get_host(struct connection *con)
320 // Callback for use by event loop
321 // We do this because ZOOM connections don't always have (the same) sockets
322 static int socketfun(IOCHAN c)
324 struct connection *co = iochan_getdata(c);
327 return ZOOM_connection_get_socket(co->link);
330 // Because ZOOM always knows what events it is interested in; we may not
331 static int maskfun(IOCHAN c)
333 struct connection *co = iochan_getdata(c);
337 // This is cheating a little, and assuming that eventl mask IDs are always
338 // the same as ZOOM-C's.
339 return ZOOM_connection_get_mask(co->link);
342 static int connection_connect(struct connection *con)
344 ZOOM_connection link = 0;
345 struct host *host = connection_get_host(con);
346 ZOOM_options zoptions = ZOOM_options_create();
349 const char *sru_version = 0;
350 char ipport[512] = "";
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);
386 strcpy(ipport, "http://");
387 strcat(ipport, host->ipport);
389 ZOOM_connection_connect(link, ipport, 0);
392 con->iochan = iochan_create(0, connection_handler, 0);
393 con->state = Conn_Connecting;
394 iochan_settimeout(con->iochan, con->connect_timeout);
395 iochan_setdata(con->iochan, con);
396 iochan_setsocketfun(con->iochan, socketfun);
397 iochan_setmaskfun(con->iochan, maskfun);
398 pazpar2_add_channel(con->iochan);
400 /* this fragment is bad DRY: from client_prep_connection */
401 client_set_state(con->client, Client_Connecting);
402 ZOOM_options_destroy(zoptions);
403 // This creates the connection
404 ZOOM_connection_process(link);
408 const char *connection_get_url(struct connection *co)
410 return client_get_url(co->client);
413 // Ensure that client has a connection associated
414 int client_prep_connection(struct client *cl,
415 int connect_timeout, int session_timeout)
417 struct connection *co;
418 struct session *se = client_get_session(cl);
419 struct host *host = client_get_host(cl);
420 struct session_database *sdb = client_get_database(cl);
421 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
423 if (zproxy && zproxy[0] == '\0')
426 co = client_get_connection(cl);
428 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
432 // See if someone else has an idle connection
433 // We should look at timestamps here to select the longest-idle connection
434 for (co = host->connections; co; co = co->next)
435 if (connection_is_idle(co) &&
436 (!co->client || client_get_session(co->client) != se) &&
437 !strcmp(ZOOM_connection_option_get(co->link, "user"),
438 session_setting_oneval(client_get_database(cl),
441 if (zproxy == 0 && co->zproxy == 0)
443 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
448 connection_release(co);
449 client_set_connection(cl, co);
451 /* tells ZOOM to reconnect if necessary. Disabled becuase
452 the ZOOM_connection_connect flushes the task queue */
453 co->connect_timeout = connect_timeout;
454 co->session_timeout = session_timeout;
455 ZOOM_connection_connect(co->link, 0, 0);
459 co = connection_create(cl, connect_timeout, session_timeout);
472 * c-file-style: "Stroustrup"
473 * indent-tabs-mode: nil
475 * vim: shiftwidth=4 tabstop=8 expandtab