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