No lock on host->mutex for non-timeout events
[pazpar2-moved-to-github.git] / src / connection.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2010 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 "session.h"
46 #include "host.h"
47 #include "client.h"
48 #include "settings.h"
49
50
51 /** \brief Represents a physical, reusable  connection to a remote Z39.50 host
52  */
53 struct connection {
54     IOCHAN iochan;
55     ZOOM_connection link;
56     struct host *host;
57     struct client *client;
58     char *zproxy;
59     enum {
60         Conn_Resolving,
61         Conn_Connecting,
62         Conn_Open
63     } state;
64     int operation_timeout;
65     int session_timeout;
66     struct connection *next; // next for same host or next in free list
67 };
68
69 static int connection_connect(struct connection *con, iochan_man_t iochan_man);
70
71 static int connection_is_idle(struct connection *co)
72 {
73     ZOOM_connection link = co->link;
74     int event;
75
76     if (co->state != Conn_Open || !link)
77         return 0;
78
79     if (!ZOOM_connection_is_idle(link))
80         return 0;
81     event = ZOOM_connection_peek_event(link);
82     if (event == ZOOM_EVENT_NONE)
83         return 1;
84     else
85         return 0;
86 }
87
88 ZOOM_connection connection_get_link(struct connection *co)
89 {
90     return co->link;
91 }
92
93 static void remove_connection_from_host(struct connection *con)
94 {
95     struct connection **conp = &con->host->connections;
96     assert(con);
97     while (*conp)
98     {
99         if (*conp == con)
100         {
101             *conp = (*conp)->next;
102             break;
103         }
104         conp = &(*conp)->next;
105     }
106 }
107
108 // Close connection and recycle structure
109 static void connection_destroy(struct connection *co)
110 {
111     if (co->link)
112     {
113         ZOOM_connection_destroy(co->link);
114         iochan_destroy(co->iochan);
115     }
116     yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
117
118     if (co->client)
119     {
120         client_disconnect(co->client);
121     }
122
123     remove_connection_from_host(co);
124     xfree(co->zproxy);
125     xfree(co);
126 }
127
128 // Creates a new connection for client, associated with the host of 
129 // client's database
130 static struct connection *connection_create(struct client *cl,
131                                             int operation_timeout,
132                                             int session_timeout,
133                                             iochan_man_t iochan_man)
134 {
135     struct connection *new;
136     struct host *host = client_get_host(cl);
137
138     new = xmalloc(sizeof(*new));
139     new->host = host;
140
141     new->client = cl;
142     new->zproxy = 0;
143     client_set_connection(cl, new);
144     new->link = 0;
145     new->state = Conn_Resolving;
146     new->operation_timeout = operation_timeout;
147     new->session_timeout = session_timeout;
148     if (host->ipport)
149         connection_connect(new, iochan_man);
150
151     yaz_mutex_enter(host->mutex);
152     new->next = new->host->connections;
153     new->host->connections = new;
154     yaz_mutex_leave(host->mutex);
155
156     return new;
157 }
158
159 static void non_block_events(struct connection *co)
160 {
161     int got_records = 0;
162     IOCHAN iochan = co->iochan;
163     ZOOM_connection link = co->link;
164     while (1)
165     {
166         struct client *cl = co->client;
167         int ev;
168         int r = ZOOM_event_nonblock(1, &link);
169         if (!r)
170             break;
171         if (!cl)
172             continue;
173         ev = ZOOM_connection_last_event(link);
174         
175 #if 0
176         yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
177 #endif
178         switch (ev) 
179         {
180         case ZOOM_EVENT_END:
181             {
182                 const char *error, *addinfo;
183                 int err;
184                 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
185                 {
186                     yaz_log(YLOG_LOG, "Error %s from %s",
187                             error, client_get_url(cl));
188                 }
189                 iochan_settimeout(iochan, co->session_timeout);
190                 client_set_diagnostic(cl, err);
191                 client_set_state(cl, Client_Idle);
192             }
193             break;
194         case ZOOM_EVENT_SEND_DATA:
195             break;
196         case ZOOM_EVENT_RECV_DATA:
197             break;
198         case ZOOM_EVENT_UNKNOWN:
199             break;
200         case ZOOM_EVENT_SEND_APDU:
201             client_set_state(co->client, Client_Working);
202             iochan_settimeout(iochan, co->operation_timeout);
203             break;
204         case ZOOM_EVENT_RECV_APDU:
205             break;
206         case ZOOM_EVENT_CONNECT:
207             yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
208             co->state = Conn_Open;
209             break;
210         case ZOOM_EVENT_RECV_SEARCH:
211             client_search_response(cl);
212             break;
213         case ZOOM_EVENT_RECV_RECORD:
214             client_record_response(cl);
215             got_records = 1;
216             break;
217         default:
218             yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
219                     ev, client_get_url(cl));
220         }
221     }
222     if (got_records)
223     {
224         struct client *cl = co->client;
225         if (cl)
226         {
227             client_got_records(cl);
228         }
229     }
230 }
231
232 void connection_continue(struct connection *co)
233 {
234     int r = ZOOM_connection_exec_task(co->link);
235     if (!r)
236         yaz_log(YLOG_WARN, "No task was executed for connection");
237 }
238
239 static void connection_handler(IOCHAN iochan, int event)
240 {
241     struct connection *co = iochan_getdata(iochan);
242     struct client *cl = co->client;
243     struct host *host = co->host;
244
245     yaz_mutex_enter(host->mutex);
246     if (!cl) 
247     {
248         /* no client associated with it.. We are probably getting
249            a closed connection from the target.. Or, perhaps, an unexpected
250            package.. We will just close the connection */
251         yaz_log(YLOG_LOG, "timeout connection %p event=%d", co, event);
252         connection_destroy(co);
253         yaz_mutex_leave(host->mutex);
254     }
255     else if (event & EVENT_TIMEOUT)
256     {
257         if (co->state == Conn_Connecting)
258         {
259             yaz_log(YLOG_WARN,  "connect timeout %s", client_get_url(cl));
260
261             connection_destroy(co);
262             client_set_state(cl, Client_Error);
263         }
264         else if (client_get_state(co->client) == Client_Idle)
265         {
266             yaz_log(YLOG_LOG,  "idle timeout %s", client_get_url(cl));
267             connection_destroy(co);
268         }
269         else
270         {
271             yaz_log(YLOG_LOG,  "ignore timeout %s", client_get_url(cl));
272         }
273         yaz_mutex_leave(host->mutex);
274     }
275     else
276     {
277         yaz_mutex_leave(host->mutex);
278         non_block_events(co);
279
280         ZOOM_connection_fire_event_socket(co->link, event);
281         
282         non_block_events(co);
283     }
284 }
285
286
287 // Disassociate connection from client
288 void connection_release(struct connection *co)
289 {
290     struct client *cl = co->client;
291
292     if (!cl)
293         return;
294     client_set_connection(cl, 0);
295     co->client = 0;
296 }
297
298 void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
299 {
300     struct connection *con;
301
302 start:
303     yaz_mutex_enter(host->mutex);
304     con = host->connections;
305     while (con)
306     {
307         if (con->state == Conn_Resolving)
308         {
309             if (!host->ipport) /* unresolved */
310             {
311                 yaz_mutex_leave(host->mutex);
312                 connection_destroy(con);
313                 goto start;
314                 /* start all over .. at some point it will be NULL */
315             }
316             else if (!con->client)
317             {
318                 yaz_mutex_leave(host->mutex);
319                 connection_destroy(con);
320                 /* start all over .. at some point it will be NULL */
321                 goto start;
322             }
323             else
324             {
325                 yaz_mutex_leave(host->mutex);
326                 connection_connect(con, iochan_man);
327                 client_start_search(con->client);
328                 goto start;
329             }
330         }
331         else
332         {
333             yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
334             con = con->next;
335         }
336     }
337     yaz_mutex_leave(host->mutex);
338 }
339
340 static struct host *connection_get_host(struct connection *con)
341 {
342     return con->host;
343 }
344
345 // Callback for use by event loop
346 // We do this because ZOOM connections don't always have (the same) sockets
347 static int socketfun(IOCHAN c)
348 {
349     struct connection *co = iochan_getdata(c);
350     if (!co->link)
351         return -1;
352     return ZOOM_connection_get_socket(co->link);
353 }
354
355 // Because ZOOM always knows what events it is interested in; we may not
356 static int maskfun(IOCHAN c)
357 {
358     struct connection *co = iochan_getdata(c);
359     if (!co->link)
360         return 0;
361
362     // This is cheating a little, and assuming that eventl mask IDs are always
363     // the same as ZOOM-C's.
364     return ZOOM_connection_get_mask(co->link);
365 }
366
367 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
368 {
369     ZOOM_connection link = 0;
370     struct host *host = connection_get_host(con);
371     ZOOM_options zoptions = ZOOM_options_create();
372     const char *auth;
373     const char *charset;
374     const char *sru;
375     const char *sru_version = 0;
376
377     struct session_database *sdb = client_get_database(con->client);
378     const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
379     const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
380
381     assert(host->ipport);
382     assert(con);
383
384     ZOOM_options_set(zoptions, "async", "1");
385     ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
386     ZOOM_options_set(zoptions, "implementationVersion", VERSION);
387         
388     if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
389         ZOOM_options_set(zoptions, "charset", charset);
390     
391     if (zproxy && *zproxy)
392     {
393         con->zproxy = xstrdup(zproxy);
394         ZOOM_options_set(zoptions, "proxy", zproxy);
395     }
396     if (apdulog && *apdulog)
397         ZOOM_options_set(zoptions, "apdulog", apdulog);
398
399     if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
400         ZOOM_options_set(zoptions, "user", auth);
401     if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
402         ZOOM_options_set(zoptions, "sru", sru);
403     if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION)) 
404         && *sru_version)
405         ZOOM_options_set(zoptions, "sru_version", sru_version);
406
407     if (!(link = ZOOM_connection_create(zoptions)))
408     {
409         yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
410         ZOOM_options_destroy(zoptions);
411         return -1;
412     }
413
414     if (sru && *sru)
415     {
416         char http_hostport[512];
417         strcpy(http_hostport, "http://");
418         strcat(http_hostport, host->hostport);
419         ZOOM_connection_connect(link, http_hostport, 0);
420     }
421     else
422         ZOOM_connection_connect(link, host->ipport, 0);
423     
424     con->link = link;
425     con->iochan = iochan_create(0, connection_handler, 0, "connection_socket");
426     con->state = Conn_Connecting;
427     iochan_settimeout(con->iochan, con->operation_timeout);
428     iochan_setdata(con->iochan, con);
429     iochan_setsocketfun(con->iochan, socketfun);
430     iochan_setmaskfun(con->iochan, maskfun);
431     iochan_add(iochan_man, con->iochan);
432
433     /* this fragment is bad DRY: from client_prep_connection */
434     client_set_state(con->client, Client_Connecting);
435     ZOOM_options_destroy(zoptions);
436     return 0;
437 }
438
439 const char *connection_get_url(struct connection *co)
440 {
441     return client_get_url(co->client);
442 }
443
444 // Ensure that client has a connection associated
445 int client_prep_connection(struct client *cl,
446                            int operation_timeout, int session_timeout,
447                            iochan_man_t iochan_man)
448 {
449     struct connection *co;
450     struct host *host = client_get_host(cl);
451     struct session_database *sdb = client_get_database(cl);
452     const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
453
454     if (zproxy && zproxy[0] == '\0')
455         zproxy = 0;
456
457     if (!host)
458         return 0;
459
460     co = client_get_connection(cl);
461
462     yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
463
464     if (!co)
465     {
466         // See if someone else has an idle connection
467         // We should look at timestamps here to select the longest-idle connection
468         yaz_mutex_enter(host->mutex);
469         for (co = host->connections; co; co = co->next)
470             if (connection_is_idle(co) &&
471                 (!co->client || client_get_state(co->client) == Client_Idle) &&
472                 !strcmp(ZOOM_connection_option_get(co->link, "user"),
473                         session_setting_oneval(client_get_database(cl),
474                                                PZ_AUTHENTICATION)))
475             {
476                 if (zproxy == 0 && co->zproxy == 0)
477                     break;
478                 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
479                     break;
480             }
481         if (co)
482         {
483             connection_release(co);
484             client_set_connection(cl, co);
485             co->client = cl;
486             /* ensure that connection is only assigned to this client
487                by marking the client non Idle */
488             client_set_state(cl, Client_Working);
489             yaz_mutex_leave(host->mutex);
490             co->operation_timeout = operation_timeout;
491             co->session_timeout = session_timeout;
492             /* tells ZOOM to reconnect if necessary. Disabled becuase
493                the ZOOM_connection_connect flushes the task queue */
494             ZOOM_connection_connect(co->link, 0, 0);
495         }
496         else
497         {
498             yaz_mutex_leave(host->mutex);
499             co = connection_create(cl, operation_timeout, session_timeout,
500                                    iochan_man);
501         }
502     }
503
504     if (co && co->link)
505         return 1;
506     else
507         return 0;
508 }
509
510 /*
511  * Local variables:
512  * c-basic-offset: 4
513  * c-file-style: "Stroustrup"
514  * indent-tabs-mode: nil
515  * End:
516  * vim: shiftwidth=4 tabstop=8 expandtab
517  */
518