Small IOCHAN refactor. Introduce iochan_man_t
[pazpar2-moved-to-github.git] / src / eventl.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 /*
21  * Based on  ParaZ - a simple tool for harvesting performance data for
22  * parallel operations using Z39.50.
23  * Copyright (C) 2006-2010 Index Data ApS
24  * See LICENSE file for details.
25  */
26
27 /*
28  * Based on revision YAZ' server/eventl.c 1.29.
29  */
30
31 #if HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <stdio.h>
36 #include <assert.h>
37
38 #ifdef WIN32
39 #include <winsock.h>
40 #else
41 #include <unistd.h>
42 #endif
43 #if HAVE_SYS_TIME_H
44 #include <sys/time.h>
45 #endif
46
47 #include <stdlib.h>
48 #include <errno.h>
49 #include <string.h>
50
51 #include <yaz/yconfig.h>
52 #include <yaz/log.h>
53 #include <yaz/comstack.h>
54 #include <yaz/xmalloc.h>
55 #include "eventl.h"
56
57 struct iochan_man_s {
58     IOCHAN channel_list;
59 };
60
61 iochan_man_t iochan_man_create(void)
62 {
63     iochan_man_t man = xmalloc(sizeof(*man));
64     man->channel_list = 0;
65     return man;
66 }
67
68 void iochan_man_destroy(iochan_man_t *mp)
69 {
70     if (*mp)
71     {
72         xfree(*mp);
73         *mp = 0;
74     }
75 }
76
77 void iochan_add(iochan_man_t man, IOCHAN chan)
78 {
79     chan->next = man->channel_list;
80     man->channel_list = chan;
81 }
82
83 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
84 {
85     IOCHAN new_iochan;
86
87     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
88         return 0;
89     new_iochan->destroyed = 0;
90     new_iochan->fd = fd;
91     new_iochan->flags = flags;
92     new_iochan->fun = cb;
93     new_iochan->socketfun = NULL;
94     new_iochan->maskfun = NULL;
95     new_iochan->force_event = 0;
96     new_iochan->last_event = new_iochan->max_idle = 0;
97     new_iochan->next = NULL;
98     return new_iochan;
99 }
100
101 static int event_loop(IOCHAN *iochans)
102 {
103     do /* loop as long as there are active associations to process */
104     {
105         IOCHAN p, nextp;
106         fd_set in, out, except;
107         int res, max;
108         static struct timeval nullto = {0, 0}, to;
109         struct timeval *timeout;
110
111         FD_ZERO(&in);
112         FD_ZERO(&out);
113         FD_ZERO(&except);
114         timeout = &to; /* hang on select */
115         to.tv_sec = 15;
116         to.tv_usec = 0;
117         max = 0;
118         for (p = *iochans; p; p = p->next)
119         {
120             if (p->maskfun)
121                 p->flags = (*p->maskfun)(p);
122             if (p->socketfun)
123                 p->fd = (*p->socketfun)(p);
124             if (p->fd < 0)
125                 continue;
126             if (p->force_event)
127                 timeout = &nullto;        /* polling select */
128             if (p->flags & EVENT_INPUT)
129                 FD_SET(p->fd, &in);
130             if (p->flags & EVENT_OUTPUT)
131                 FD_SET(p->fd, &out);
132             if (p->flags & EVENT_EXCEPT)
133                 FD_SET(p->fd, &except);
134             if (p->fd > max)
135                 max = p->fd;
136             if (p->max_idle && p->max_idle < to.tv_sec)
137                 to.tv_sec = p->max_idle;
138         }
139         res = select(max + 1, &in, &out, &except, timeout);        
140         if (res < 0)
141         {
142             if (errno == EINTR)
143                 continue;
144             else
145             {
146                 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
147                 return 0;
148             }
149         }
150         for (p = *iochans; p; p = p->next)
151         {
152             int force_event = p->force_event;
153             time_t now = time(0);
154
155             p->force_event = 0;
156             if (!p->destroyed && ((p->max_idle && now - p->last_event >
157                 p->max_idle) || force_event == EVENT_TIMEOUT))
158             {
159                 p->last_event = now;
160                 (*p->fun)(p, EVENT_TIMEOUT);
161             }
162             if (p->fd < 0)
163                 continue;
164             if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
165                 force_event == EVENT_INPUT))
166             {
167                 p->last_event = now;
168                 yaz_log(YLOG_DEBUG, "Eventl input event");
169                 (*p->fun)(p, EVENT_INPUT);
170             }
171             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
172                 force_event == EVENT_OUTPUT))
173             {
174                 p->last_event = now;
175                 yaz_log(YLOG_DEBUG, "Eventl output event");
176                 (*p->fun)(p, EVENT_OUTPUT);
177             }
178             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
179                 force_event == EVENT_EXCEPT))
180             {
181                 p->last_event = now;
182                 (*p->fun)(p, EVENT_EXCEPT);
183             }
184         }
185         for (p = *iochans; p; p = nextp)
186         {
187             nextp = p->next;
188
189             if (p->destroyed)
190             {
191                 IOCHAN tmp = p, pr;
192
193                 /* Now reset the pointers */
194                 if (p == *iochans)
195                     *iochans = p->next;
196                 else
197                 {
198                     for (pr = *iochans; pr; pr = pr->next)
199                         if (pr->next == p)
200                             break;
201                     assert(pr); /* grave error if it weren't there */
202                     pr->next = p->next;
203                 }
204                 if (nextp == p)
205                     nextp = p->next;
206                 xfree(tmp);
207             }
208         }
209     }
210     while (*iochans);
211     return 0;
212 }
213
214 void iochan_man_events(iochan_man_t man)
215 {
216     event_loop(&man->channel_list);
217 }
218
219 /*
220  * Local variables:
221  * c-basic-offset: 4
222  * c-file-style: "Stroustrup"
223  * indent-tabs-mode: nil
224  * End:
225  * vim: shiftwidth=4 tabstop=8 expandtab
226  */
227