979f739711917d68ce7fcafa5fdcedf09182dc29
[pazpar2-moved-to-github.git] / src / connection.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2009 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19
20 /** \file connection.c
21     \brief Z39.50 connection (low-level client)
22 */
23
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #if HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include <signal.h>
39 #include <assert.h>
40
41 #include <yaz/log.h>
42 #include <yaz/comstack.h>
43 #include <yaz/tcpip.h>
44 #include "connection.h"
45 #include "eventl.h"
46 #include "pazpar2.h"
47 #include "host.h"
48 #include "client.h"
49 #include "settings.h"
50 #include "parameters.h"
51
52
53 /** \brief Represents a physical, reusable  connection to a remote Z39.50 host
54  */
55 struct connection {
56     IOCHAN iochan;
57     ZOOM_connection link;
58     struct host *host;
59     struct client *client;
60     char *ibuf;
61     int ibufsize;
62     char *zproxy;
63     enum {
64         Conn_Resolving,
65         Conn_Connecting,
66         Conn_Open
67     } state;
68     int connect_timeout;
69     int session_timeout;
70     struct connection *next; // next for same host or next in free list
71 };
72
73 static struct connection *connection_freelist = 0;
74
75 static int connection_connect(struct connection *con);
76
77 static int connection_is_idle(struct connection *co)
78 {
79     ZOOM_connection link = co->link;
80     int event;
81
82     if (co->state != Conn_Open || !link)
83         return 0;
84
85     if (!ZOOM_connection_is_idle(link))
86         return 0;
87     event = ZOOM_connection_peek_event(link);
88     if (event == ZOOM_EVENT_NONE || event == ZOOM_EVENT_END)
89         return 1;
90     else
91         return 0;
92 }
93
94 ZOOM_connection connection_get_link(struct connection *co)
95 {
96     return co->link;
97 }
98
99 static void remove_connection_from_host(struct connection *con)
100 {
101     struct connection **conp = &con->host->connections;
102     assert(con);
103     while (*conp)
104     {
105         if (*conp == con)
106         {
107             *conp = (*conp)->next;
108             return;
109         }
110         conp = &(*conp)->next;
111     }
112     assert(*conp == 0);
113 }
114
115 // Close connection and recycle structure
116 void connection_destroy(struct connection *co)
117 {
118     if (co->link)
119     {
120         ZOOM_connection_destroy(co->link);
121         iochan_destroy(co->iochan);
122     }
123     yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
124
125     remove_connection_from_host(co);
126     if (co->client)
127     {
128         client_disconnect(co->client);
129     }
130     xfree(co->zproxy);
131     co->zproxy = 0;
132     co->next = connection_freelist;
133     connection_freelist = co;
134 }
135
136 // Creates a new connection for client, associated with the host of 
137 // client's database
138 static struct connection *connection_create(struct client *cl,
139                                             int connect_timeout,
140                                             int session_timeout)
141 {
142     struct connection *new;
143     struct host *host = client_get_host(cl);
144
145     if ((new = connection_freelist))
146         connection_freelist = new->next;
147     else
148     {
149         new = xmalloc(sizeof (struct connection));
150         new->ibuf = 0;
151         new->ibufsize = 0;
152     }
153     new->host = host;
154     new->next = new->host->connections;
155     new->host->connections = new;
156     new->client = cl;
157     new->zproxy = 0;
158     client_set_connection(cl, new);
159     new->link = 0;
160     new->state = Conn_Resolving;
161     new->connect_timeout = connect_timeout;
162     new->session_timeout = session_timeout;
163     if (host->ipport)
164         connection_connect(new);
165     return new;
166 }
167
168 static void non_block_events(struct connection *co)
169 {
170     struct client *cl = co->client;
171     IOCHAN iochan = co->iochan;
172     ZOOM_connection link = co->link;
173     while (1)
174     {
175         int ev;
176         int r = ZOOM_event_nonblock(1, &link);
177         if (!r)
178             break;
179         ev = ZOOM_connection_last_event(link);
180 #if 0
181         yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
182 #endif
183         switch (ev) 
184         {
185         case ZOOM_EVENT_END:
186             {
187                 const char *error, *addinfo;
188                 if (ZOOM_connection_error(link, &error, &addinfo))
189                 {
190                     yaz_log(YLOG_LOG, "Error %s from %s",
191                             error, client_get_url(cl));
192                 }
193                 client_set_state(cl, Client_Idle);
194             }
195             break;
196         case ZOOM_EVENT_SEND_DATA:
197             break;
198         case ZOOM_EVENT_RECV_DATA:
199             break;
200         case ZOOM_EVENT_UNKNOWN:
201             break;
202         case ZOOM_EVENT_SEND_APDU:
203             client_set_state(co->client, Client_Working);
204             break;
205         case ZOOM_EVENT_RECV_APDU:
206             break;
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);
211             break;
212         case ZOOM_EVENT_RECV_SEARCH:
213             client_search_response(cl);
214             break;
215         case ZOOM_EVENT_RECV_RECORD:
216             client_record_response(cl);
217             break;
218         default:
219             yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
220                     ev, client_get_url(cl));
221         }
222     }
223 }
224
225 void connection_continue(struct connection *co)
226 {
227     non_block_events(co);
228 }
229
230 static void connection_handler(IOCHAN iochan, int event)
231 {
232     struct connection *co = iochan_getdata(iochan);
233     struct client *cl = co->client;
234     struct session *se = 0;
235
236     if (cl)
237         se = client_get_session(cl);
238     else
239     {
240         connection_destroy(co);
241         return;
242     }
243
244     if (event & EVENT_TIMEOUT)
245     {
246         if (co->state == Conn_Connecting)
247         {
248             yaz_log(YLOG_WARN,  "connect timeout %s", client_get_url(cl));
249             client_fatal(cl);
250         }
251         else
252         {
253             yaz_log(YLOG_LOG,  "idle timeout %s", client_get_url(cl));
254             connection_destroy(co);
255         }
256     }
257     else
258     {
259         non_block_events(co);
260
261         ZOOM_connection_fire_event_socket(co->link, event);
262         
263         non_block_events(co);
264     }
265 }
266
267
268 // Disassociate connection from client
269 void connection_release(struct connection *co)
270 {
271     struct client *cl = co->client;
272
273     yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
274     if (!cl)
275         return;
276     client_set_connection(cl, 0);
277     co->client = 0;
278 }
279
280 void connect_resolver_host(struct host *host)
281 {
282     struct connection *con = host->connections;
283     while (con)
284     {
285         if (con->state == Conn_Resolving)
286         {
287             if (!host->ipport) /* unresolved */
288             {
289                 connection_destroy(con);
290                 /* start all over .. at some point it will be NULL */
291                 con = host->connections;
292                 continue;
293             }
294             else if (!con->client)
295             {
296                 connection_destroy(con);
297                 /* start all over .. at some point it will be NULL */
298                 con = host->connections;
299                 continue;
300             }
301             else
302             {
303                 connection_connect(con);
304                 client_start_search(con->client);
305             }
306         }
307         else
308         {
309             yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
310         }
311         con = con->next;
312     }
313 }
314
315 static struct host *connection_get_host(struct connection *con)
316 {
317     return con->host;
318 }
319
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)
323 {
324     struct connection *co = iochan_getdata(c);
325     if (!co->link)
326         return -1;
327     return ZOOM_connection_get_socket(co->link);
328 }
329
330 // Because ZOOM always knows what events it is interested in; we may not
331 static int maskfun(IOCHAN c)
332 {
333     struct connection *co = iochan_getdata(c);
334     if (!co->link)
335         return 0;
336
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);
340 }
341
342 static int connection_connect(struct connection *con)
343 {
344     ZOOM_connection link = 0;
345     struct host *host = connection_get_host(con);
346     ZOOM_options zoptions = ZOOM_options_create();
347     const char *auth;
348     const char *sru;
349     const char *sru_version = 0;
350     char ipport[512] = "";
351
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);
355
356     assert(host->ipport);
357     assert(con);
358
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)
363     {
364         con->zproxy = xstrdup(zproxy);
365         ZOOM_options_set(zoptions, "proxy", zproxy);
366     }
367     if (apdulog && *apdulog)
368         ZOOM_options_set(zoptions, "apdulog", apdulog);
369
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)) 
375         && *sru_version)
376         ZOOM_options_set(zoptions, "sru_version", sru_version);
377
378     if (!(link = ZOOM_connection_create(zoptions)))
379     {
380         yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
381         ZOOM_options_destroy(zoptions);
382         return -1;
383     }
384
385     if (sru && *sru)
386         strcpy(ipport, "http://");
387     strcat(ipport, host->ipport);
388
389     ZOOM_connection_connect(link, ipport, 0);
390     
391     con->link = link;
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);
399
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);
405     return 0;
406 }
407
408 const char *connection_get_url(struct connection *co)
409 {
410     return client_get_url(co->client);
411 }
412
413 // Ensure that client has a connection associated
414 int client_prep_connection(struct client *cl,
415                            int connect_timeout, int session_timeout)
416 {
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);
422
423     if (zproxy && zproxy[0] == '\0')
424         zproxy = 0;
425
426     co = client_get_connection(cl);
427
428     yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
429
430     if (!co)
431     {
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),
439                                                PZ_AUTHENTICATION)))
440             {
441                 if (zproxy == 0 && co->zproxy == 0)
442                     break;
443                 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
444                     break;
445             }
446         if (co)
447         {
448             connection_release(co);
449             client_set_connection(cl, co);
450             co->client = cl;
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);
456         }
457         else
458         {
459             co = connection_create(cl, connect_timeout, session_timeout);
460         }
461     }
462
463     if (co && co->link)
464         return 1;
465     else
466         return 0;
467 }
468
469 /*
470  * Local variables:
471  * c-basic-offset: 4
472  * c-file-style: "Stroustrup"
473  * indent-tabs-mode: nil
474  * End:
475  * vim: shiftwidth=4 tabstop=8 expandtab
476  */
477