2dfac4dab0d3a3111d96b80928428d76b66f7b1c
[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 #include <stdio.h>
32 #include <assert.h>
33
34 #if HAVE_CONFIG_H
35 #include <cconfig.h>
36 #endif
37
38 #ifdef WIN32
39 #include <winsock.h>
40 #else
41 #include <unistd.h>
42 #endif
43 #include <sys/time.h>
44 #include <stdlib.h>
45 #include <errno.h>
46 #include <string.h>
47
48 #include <yaz/yconfig.h>
49 #include <yaz/log.h>
50 #include <yaz/comstack.h>
51 #include <yaz/xmalloc.h>
52 #include "eventl.h"
53 #include <yaz/statserv.h>
54
55 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
56 {
57     IOCHAN new_iochan;
58
59     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
60         return 0;
61     new_iochan->destroyed = 0;
62     new_iochan->fd = fd;
63     new_iochan->flags = flags;
64     new_iochan->fun = cb;
65     new_iochan->force_event = 0;
66     new_iochan->last_event = new_iochan->max_idle = 0;
67     new_iochan->next = NULL;
68     return new_iochan;
69 }
70
71 int event_loop(IOCHAN *iochans)
72 {
73     do /* loop as long as there are active associations to process */
74     {
75         IOCHAN p, nextp;
76         fd_set in, out, except;
77         int res, max;
78         static struct timeval nullto = {0, 0}, to;
79         struct timeval *timeout;
80
81         FD_ZERO(&in);
82         FD_ZERO(&out);
83         FD_ZERO(&except);
84         timeout = &to; /* hang on select */
85         to.tv_sec = 15;
86         to.tv_usec = 0;
87         max = 0;
88         for (p = *iochans; p; p = p->next)
89         {
90             if (p->fd < 0)
91                 continue;
92             if (p->force_event)
93                 timeout = &nullto;        /* polling select */
94             if (p->flags & EVENT_INPUT)
95                 FD_SET(p->fd, &in);
96             if (p->flags & EVENT_OUTPUT)
97                 FD_SET(p->fd, &out);
98             if (p->flags & EVENT_EXCEPT)
99                 FD_SET(p->fd, &except);
100             if (p->fd > max)
101                 max = p->fd;
102             if (p->max_idle && p->max_idle < to.tv_sec)
103                 to.tv_sec = p->max_idle;
104         }
105         if ((res = select(max + 1, &in, &out, &except, timeout)) < 0)
106         {
107             if (errno == EINTR)
108                 continue;
109             else
110                 abort();
111         }
112         for (p = *iochans; p; p = p->next)
113         {
114             int force_event = p->force_event;
115             time_t now = time(0);
116
117             p->force_event = 0;
118             if (!p->destroyed && ((p->max_idle && now - p->last_event >
119                 p->max_idle) || force_event == EVENT_TIMEOUT))
120             {
121                 p->last_event = now;
122                 (*p->fun)(p, EVENT_TIMEOUT);
123             }
124             if (p->fd < 0)
125                 continue;
126             if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
127                 force_event == EVENT_INPUT))
128             {
129                 p->last_event = now;
130                 (*p->fun)(p, EVENT_INPUT);
131             }
132             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
133                 force_event == EVENT_OUTPUT))
134             {
135                 p->last_event = now;
136                 (*p->fun)(p, EVENT_OUTPUT);
137             }
138             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
139                 force_event == EVENT_EXCEPT))
140             {
141                 p->last_event = now;
142                 (*p->fun)(p, EVENT_EXCEPT);
143             }
144         }
145         for (p = *iochans; p; p = nextp)
146         {
147             nextp = p->next;
148
149             if (p->destroyed)
150             {
151                 IOCHAN tmp = p, pr;
152
153                 /* Now reset the pointers */
154                 if (p == *iochans)
155                     *iochans = p->next;
156                 else
157                 {
158                     for (pr = *iochans; pr; pr = pr->next)
159                         if (pr->next == p)
160                             break;
161                     assert(pr); /* grave error if it weren't there */
162                     pr->next = p->next;
163                 }
164                 if (nextp == p)
165                     nextp = p->next;
166                 xfree(tmp);
167             }
168         }
169     }
170     while (*iochans);
171     return 0;
172 }
173
174 /*
175  * Local variables:
176  * c-basic-offset: 4
177  * indent-tabs-mode: nil
178  * End:
179  * vim: shiftwidth=4 tabstop=8 expandtab
180  */