Incorporate Windos services (sc).
[pazpar2-moved-to-github.git] / src / eventl.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2008 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) 2000-2004 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 #include <yaz/statserv.h>
57
58 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
59 {
60     IOCHAN new_iochan;
61
62     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
63         return 0;
64     new_iochan->destroyed = 0;
65     new_iochan->fd = fd;
66     new_iochan->flags = flags;
67     new_iochan->fun = cb;
68     new_iochan->force_event = 0;
69     new_iochan->last_event = new_iochan->max_idle = 0;
70     new_iochan->next = NULL;
71     return new_iochan;
72 }
73
74 int event_loop(IOCHAN *iochans)
75 {
76     do /* loop as long as there are active associations to process */
77     {
78         IOCHAN p, nextp;
79         fd_set in, out, except;
80         int res, max;
81         static struct timeval nullto = {0, 0}, to;
82         struct timeval *timeout;
83
84         FD_ZERO(&in);
85         FD_ZERO(&out);
86         FD_ZERO(&except);
87         timeout = &to; /* hang on select */
88         to.tv_sec = 15;
89         to.tv_usec = 0;
90         max = 0;
91         for (p = *iochans; p; p = p->next)
92         {
93             if (p->fd < 0)
94                 continue;
95             if (p->force_event)
96                 timeout = &nullto;        /* polling select */
97             if (p->flags & EVENT_INPUT)
98                 FD_SET(p->fd, &in);
99             if (p->flags & EVENT_OUTPUT)
100                 FD_SET(p->fd, &out);
101             if (p->flags & EVENT_EXCEPT)
102                 FD_SET(p->fd, &except);
103             if (p->fd > max)
104                 max = p->fd;
105             if (p->max_idle && p->max_idle < to.tv_sec)
106                 to.tv_sec = p->max_idle;
107         }
108         if ((res = select(max + 1, &in, &out, &except, timeout)) < 0)
109         {
110             if (errno == EINTR)
111                 continue;
112             else
113             {
114                 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
115                 return 0;
116             }
117         }
118         for (p = *iochans; p; p = p->next)
119         {
120             int force_event = p->force_event;
121             time_t now = time(0);
122
123             p->force_event = 0;
124             if (!p->destroyed && ((p->max_idle && now - p->last_event >
125                 p->max_idle) || force_event == EVENT_TIMEOUT))
126             {
127                 p->last_event = now;
128                 (*p->fun)(p, EVENT_TIMEOUT);
129             }
130             if (p->fd < 0)
131                 continue;
132             if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
133                 force_event == EVENT_INPUT))
134             {
135                 p->last_event = now;
136                 (*p->fun)(p, EVENT_INPUT);
137             }
138             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
139                 force_event == EVENT_OUTPUT))
140             {
141                 p->last_event = now;
142                 (*p->fun)(p, EVENT_OUTPUT);
143             }
144             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
145                 force_event == EVENT_EXCEPT))
146             {
147                 p->last_event = now;
148                 (*p->fun)(p, EVENT_EXCEPT);
149             }
150         }
151         for (p = *iochans; p; p = nextp)
152         {
153             nextp = p->next;
154
155             if (p->destroyed)
156             {
157                 IOCHAN tmp = p, pr;
158
159                 /* Now reset the pointers */
160                 if (p == *iochans)
161                     *iochans = p->next;
162                 else
163                 {
164                     for (pr = *iochans; pr; pr = pr->next)
165                         if (pr->next == p)
166                             break;
167                     assert(pr); /* grave error if it weren't there */
168                     pr->next = p->next;
169                 }
170                 if (nextp == p)
171                     nextp = p->next;
172                 xfree(tmp);
173             }
174         }
175     }
176     while (*iochans);
177     return 0;
178 }
179
180 /*
181  * Local variables:
182  * c-basic-offset: 4
183  * indent-tabs-mode: nil
184  * End:
185  * vim: shiftwidth=4 tabstop=8 expandtab
186  */