Remove two unused members from connection
[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 "pazpar2.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 struct connection *connection_freelist = 0; /* thread pr */
70
71 static int connection_connect(struct connection *con, iochan_man_t iochan_man);
72
73 static int connection_is_idle(struct connection *co)
74 {
75     ZOOM_connection link = co->link;
76     int event;
77
78     if (co->state != Conn_Open || !link)
79         return 0;
80
81     if (!ZOOM_connection_is_idle(link))
82         return 0;
83     event = ZOOM_connection_peek_event(link);
84     if (event == ZOOM_EVENT_NONE || event == ZOOM_EVENT_END)
85         return 1;
86     else
87         return 0;
88 }
89
90 ZOOM_connection connection_get_link(struct connection *co)
91 {
92     return co->link;
93 }
94
95 static void remove_connection_from_host(struct connection *con)
96 {
97     struct connection **conp = &con->host->connections;
98     assert(con);
99     while (*conp)
100     {
101         if (*conp == con)
102         {
103             *conp = (*conp)->next;
104             return;
105         }
106         conp = &(*conp)->next;
107     }
108     assert(*conp == 0);
109 }
110
111 // Close connection and recycle structure
112 void connection_destroy(struct connection *co)
113 {
114     if (co->link)
115     {
116         ZOOM_connection_destroy(co->link);
117         iochan_destroy(co->iochan);
118     }
119     yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
120
121     remove_connection_from_host(co);
122     if (co->client)
123     {
124         client_disconnect(co->client);
125     }
126     xfree(co->zproxy);
127     co->zproxy = 0;
128     co->next = connection_freelist;
129     connection_freelist = co;
130 }
131
132 // Creates a new connection for client, associated with the host of 
133 // client's database
134 static struct connection *connection_create(struct client *cl,
135                                             int operation_timeout,
136                                             int session_timeout,
137                                             iochan_man_t iochan_man)
138 {
139     struct connection *new;
140     struct host *host = client_get_host(cl);
141
142     if ((new = connection_freelist))
143         connection_freelist = new->next;
144     else
145     {
146         new = xmalloc(sizeof (struct connection));
147     }
148     new->host = host;
149     new->next = new->host->connections;
150     new->host->connections = new;
151     new->client = cl;
152     new->zproxy = 0;
153     client_set_connection(cl, new);
154     new->link = 0;
155     new->state = Conn_Resolving;
156     new->operation_timeout = operation_timeout;
157     new->session_timeout = session_timeout;
158     if (host->ipport)
159         connection_connect(new, iochan_man);
160     return new;
161 }
162
163 static void non_block_events(struct connection *co)
164 {
165     struct client *cl = co->client;
166     IOCHAN iochan = co->iochan;
167     ZOOM_connection link = co->link;
168     while (1)
169     {
170         int ev;
171         int r = ZOOM_event_nonblock(1, &link);
172         if (!r)
173             break;
174         ev = ZOOM_connection_last_event(link);
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             break;
216         default:
217             yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
218                     ev, client_get_url(cl));
219         }
220     }
221 }
222
223 void connection_continue(struct connection *co)
224 {
225     non_block_events(co);
226 }
227
228 static void connection_handler(IOCHAN iochan, int event)
229 {
230     struct connection *co = iochan_getdata(iochan);
231     struct client *cl = co->client;
232     struct session *se = 0;
233
234     if (cl)
235         se = client_get_session(cl);
236     else
237     {
238         connection_destroy(co);
239         return;
240     }
241
242     if (event & EVENT_TIMEOUT)
243     {
244         if (co->state == Conn_Connecting)
245         {
246             yaz_log(YLOG_WARN,  "connect timeout %s", client_get_url(cl));
247             client_fatal(cl);
248         }
249         else
250         {
251             yaz_log(YLOG_LOG,  "idle timeout %s", client_get_url(cl));
252             connection_destroy(co);
253         }
254     }
255     else
256     {
257         non_block_events(co);
258
259         ZOOM_connection_fire_event_socket(co->link, event);
260         
261         non_block_events(co);
262     }
263 }
264
265
266 // Disassociate connection from client
267 void connection_release(struct connection *co)
268 {
269     struct client *cl = co->client;
270
271     yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
272     if (!cl)
273         return;
274     client_set_connection(cl, 0);
275     co->client = 0;
276 }
277
278 void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
279 {
280     struct connection *con = host->connections;
281     while (con)
282     {
283         if (con->state == Conn_Resolving)
284         {
285             if (!host->ipport) /* unresolved */
286             {
287                 connection_destroy(con);
288                 /* start all over .. at some point it will be NULL */
289                 con = host->connections;
290                 continue;
291             }
292             else if (!con->client)
293             {
294                 connection_destroy(con);
295                 /* start all over .. at some point it will be NULL */
296                 con = host->connections;
297                 continue;
298             }
299             else
300             {
301                 connection_connect(con, iochan_man);
302                 client_start_search(con->client);
303             }
304         }
305         else
306         {
307             yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
308         }
309         con = con->next;
310     }
311 }
312
313 static struct host *connection_get_host(struct connection *con)
314 {
315     return con->host;
316 }
317
318 // Callback for use by event loop
319 // We do this because ZOOM connections don't always have (the same) sockets
320 static int socketfun(IOCHAN c)
321 {
322     struct connection *co = iochan_getdata(c);
323     if (!co->link)
324         return -1;
325     return ZOOM_connection_get_socket(co->link);
326 }
327
328 // Because ZOOM always knows what events it is interested in; we may not
329 static int maskfun(IOCHAN c)
330 {
331     struct connection *co = iochan_getdata(c);
332     if (!co->link)
333         return 0;
334
335     // This is cheating a little, and assuming that eventl mask IDs are always
336     // the same as ZOOM-C's.
337     return ZOOM_connection_get_mask(co->link);
338 }
339
340 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
341 {
342     ZOOM_connection link = 0;
343     struct host *host = connection_get_host(con);
344     ZOOM_options zoptions = ZOOM_options_create();
345     const char *auth;
346     const char *charset;
347     const char *sru;
348     const char *sru_version = 0;
349
350     struct session_database *sdb = client_get_database(con->client);
351     const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
352     const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
353
354     assert(host->ipport);
355     assert(con);
356
357     ZOOM_options_set(zoptions, "async", "1");
358     ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
359     ZOOM_options_set(zoptions, "implementationVersion", VERSION);
360         
361     if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
362         ZOOM_options_set(zoptions, "charset", charset);
363     
364     if (zproxy && *zproxy)
365     {
366         con->zproxy = xstrdup(zproxy);
367         ZOOM_options_set(zoptions, "proxy", zproxy);
368     }
369     if (apdulog && *apdulog)
370         ZOOM_options_set(zoptions, "apdulog", apdulog);
371
372     if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
373         ZOOM_options_set(zoptions, "user", auth);
374     if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
375         ZOOM_options_set(zoptions, "sru", sru);
376     if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION)) 
377         && *sru_version)
378         ZOOM_options_set(zoptions, "sru_version", sru_version);
379
380     if (!(link = ZOOM_connection_create(zoptions)))
381     {
382         yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
383         ZOOM_options_destroy(zoptions);
384         return -1;
385     }
386
387     if (sru && *sru)
388     {
389         char http_hostport[512];
390         strcpy(http_hostport, "http://");
391         strcat(http_hostport, host->hostport);
392         ZOOM_connection_connect(link, http_hostport, 0);
393     }
394     else
395         ZOOM_connection_connect(link, host->ipport, 0);
396     
397     con->link = link;
398     con->iochan = iochan_create(0, connection_handler, 0);
399     con->state = Conn_Connecting;
400     iochan_settimeout(con->iochan, con->operation_timeout);
401     iochan_setdata(con->iochan, con);
402     iochan_setsocketfun(con->iochan, socketfun);
403     iochan_setmaskfun(con->iochan, maskfun);
404     iochan_add(iochan_man, con->iochan);
405
406     /* this fragment is bad DRY: from client_prep_connection */
407     client_set_state(con->client, Client_Connecting);
408     ZOOM_options_destroy(zoptions);
409     return 0;
410 }
411
412 const char *connection_get_url(struct connection *co)
413 {
414     return client_get_url(co->client);
415 }
416
417 // Ensure that client has a connection associated
418 int client_prep_connection(struct client *cl,
419                            int operation_timeout, int session_timeout,
420                            iochan_man_t iochan_man)
421 {
422     struct connection *co;
423     struct session *se = client_get_session(cl);
424     struct host *host = client_get_host(cl);
425     struct session_database *sdb = client_get_database(cl);
426     const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
427
428     if (zproxy && zproxy[0] == '\0')
429         zproxy = 0;
430
431     if (!host)
432         return 0;
433
434     co = client_get_connection(cl);
435
436     yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
437
438     if (!co)
439     {
440         // See if someone else has an idle connection
441         // We should look at timestamps here to select the longest-idle connection
442         for (co = host->connections; co; co = co->next)
443             if (connection_is_idle(co) &&
444                 (!co->client || client_get_session(co->client) != se) &&
445                 !strcmp(ZOOM_connection_option_get(co->link, "user"),
446                         session_setting_oneval(client_get_database(cl),
447                                                PZ_AUTHENTICATION)))
448             {
449                 if (zproxy == 0 && co->zproxy == 0)
450                     break;
451                 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
452                     break;
453             }
454         if (co)
455         {
456             connection_release(co);
457             client_set_connection(cl, co);
458             co->client = cl;
459             co->operation_timeout = operation_timeout;
460             co->session_timeout = session_timeout;
461             /* tells ZOOM to reconnect if necessary. Disabled becuase
462                the ZOOM_connection_connect flushes the task queue */
463             ZOOM_connection_connect(co->link, 0, 0);
464         }
465         else
466         {
467             co = connection_create(cl, operation_timeout, session_timeout,
468                                    iochan_man);
469         }
470     }
471
472     if (co && co->link)
473         return 1;
474     else
475         return 0;
476 }
477
478 /*
479  * Local variables:
480  * c-basic-offset: 4
481  * c-file-style: "Stroustrup"
482  * indent-tabs-mode: nil
483  * End:
484  * vim: shiftwidth=4 tabstop=8 expandtab
485  */
486