Renamed wrbuf_put to wrbuf_vputs and changed prototype.
[yaz-moved-to-github.git] / src / eventl.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file eventl.c
8  * \brief Implements event loop handling for GFS.
9  *
10  * This source implements the main event loop for the Generic Frontend
11  * Server.
12  */
13
14 #include <assert.h>
15 #include <errno.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #if HAVE_SYS_TYPES_H
21 #include <sys/types.h>
22 #endif
23 #if HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #endif
26
27 #include <yaz/poll.h>
28
29 #include <yaz/log.h>
30 #include <yaz/comstack.h>
31 #include <yaz/xmalloc.h>
32 #include "eventl.h"
33 #include "session.h"
34 #include <yaz/statserv.h>
35
36 static int log_level=0;
37 static int log_level_initialized=0;
38
39 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, int chan_id)
40 {
41     IOCHAN new_iochan;
42
43     if (!log_level_initialized)
44     {
45         log_level=yaz_log_module_level("eventl");
46         log_level_initialized=1;
47     }
48
49     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
50         return 0;
51     new_iochan->destroyed = 0;
52     new_iochan->fd = fd;
53     new_iochan->flags = flags;
54     new_iochan->fun = cb;
55     new_iochan->force_event = 0;
56     new_iochan->last_event = new_iochan->max_idle = 0;
57     new_iochan->next = NULL;
58     new_iochan->chan_id = chan_id;
59     return new_iochan;
60 }
61
62
63 int iochan_is_alive(IOCHAN chan)
64 {
65     struct yaz_poll_fd fds;
66     int res;
67
68     fds.fd = chan->fd;
69     fds.input_mask = yaz_poll_read;
70     res = yaz_poll(&fds, 1, 0, 0);
71     if (res == 0)
72         return 1;
73     if (!ir_read(chan, EVENT_INPUT))
74         return 0;
75     return 1;
76 }
77
78 int iochan_event_loop(IOCHAN *iochans)
79 {
80     do /* loop as long as there are active associations to process */
81     {
82         IOCHAN p, nextp;
83         int i;
84         int tv_sec = 3600;
85         int no_fds = 0;
86         struct yaz_poll_fd *fds = 0;
87         int res;
88         time_t now = time(0);
89
90         if (statserv_must_terminate())
91         {
92             for (p = *iochans; p; p = p->next)
93                 p->force_event = EVENT_TIMEOUT;
94         }
95         for (p = *iochans; p; p = p->next)
96             no_fds++;
97         fds = (struct yaz_poll_fd *) xmalloc(no_fds * sizeof(*fds));
98         for (i = 0, p = *iochans; p; p = p->next, i++)
99         {
100             time_t w, ftime;
101             enum yaz_poll_mask input_mask = yaz_poll_none;
102             yaz_log(log_level, "fd=%d flags=%d force_event=%d",
103                     p->fd, p->flags, p->force_event);
104             if (p->force_event)
105                 tv_sec = 0;          /* polling select */
106             if (p->flags & EVENT_INPUT)
107                 yaz_poll_add(input_mask, yaz_poll_read);
108             if (p->flags & EVENT_OUTPUT)
109                 yaz_poll_add(input_mask, yaz_poll_write);
110             if (p->flags & EVENT_EXCEPT)
111                 yaz_poll_add(input_mask, yaz_poll_except);
112             if (p->max_idle && p->last_event)
113             {
114                 ftime = p->last_event + p->max_idle;
115                 if (ftime < now)
116                     w = p->max_idle;
117                 else
118                     w = ftime - now;
119                 if (w < tv_sec)
120                     tv_sec = w;
121             }
122             fds[i].fd = p->fd;
123             fds[i].input_mask = input_mask;
124         }
125         res = yaz_poll(fds, no_fds, tv_sec, 0);
126         if (res < 0)
127         {
128             if (yaz_errno() == EINTR)
129             {
130                 if (statserv_must_terminate())
131                 {
132                     for (p = *iochans; p; p = p->next)
133                         p->force_event = EVENT_TIMEOUT;
134                 }
135                 xfree(fds);
136                 continue;
137             }
138             else
139             {
140                 /* Destroy the first member in the chain, and try again */
141                 association *assoc = (association *)iochan_getdata(*iochans);
142                 COMSTACK conn = assoc->client_link;
143
144                 cs_close(conn);
145                 destroy_association(assoc);
146                 iochan_destroy(*iochans);
147                 yaz_log(log_level, "error select, destroying iochan %p",
148                         *iochans);
149             }
150         }
151         now = time(0);
152         for (i = 0, p = *iochans; p; p = p->next, i++)
153         {
154             int force_event = p->force_event;
155             enum yaz_poll_mask output_mask = fds[i].output_mask;
156
157             p->force_event = 0;
158             if (!p->destroyed && ((output_mask & yaz_poll_read) ||
159                                   force_event == EVENT_INPUT))
160             {
161                 p->last_event = now;
162                 (*p->fun)(p, EVENT_INPUT);
163             }
164             if (!p->destroyed && ((output_mask & yaz_poll_write) ||
165                                   force_event == EVENT_OUTPUT))
166             {
167                 p->last_event = now;
168                 (*p->fun)(p, EVENT_OUTPUT);
169             }
170             if (!p->destroyed && ((output_mask & yaz_poll_except) ||
171                 force_event == EVENT_EXCEPT))
172             {
173                 p->last_event = now;
174                 (*p->fun)(p, EVENT_EXCEPT);
175             }
176             if (!p->destroyed && ((p->max_idle && now - p->last_event >=
177                 p->max_idle) || force_event == EVENT_TIMEOUT))
178             {
179                 p->last_event = now;
180                 (*p->fun)(p, EVENT_TIMEOUT);
181             }
182         }
183         xfree(fds);
184         for (p = *iochans; p; p = nextp)
185         {
186             nextp = p->next;
187
188             if (p->destroyed)
189             {
190                 IOCHAN tmp = p, pr;
191
192                 /* We need to inform the threadlist that this channel has been destroyed */
193                 statserv_remove(p);
194
195                 /* Now reset the pointers */
196                 if (p == *iochans)
197                     *iochans = p->next;
198                 else
199                 {
200                     for (pr = *iochans; pr; pr = pr->next)
201                         if (pr->next == p)
202                             break;
203                     assert(pr); /* grave error if it weren't there */
204                     pr->next = p->next;
205                 }
206                 if (nextp == p)
207                     nextp = p->next;
208                 xfree(tmp);
209             }
210         }
211     }
212     while (*iochans);
213     return 0;
214 }
215 /*
216  * Local variables:
217  * c-basic-offset: 4
218  * indent-tabs-mode: nil
219  * End:
220  * vim: shiftwidth=4 tabstop=8 expandtab
221  */
222