d90ae17319dd90f3359d34577b15077faae86212
[yaz-moved-to-github.git] / server / eventl.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: eventl.c,v $
7  * Revision 1.20  1996-02-21 12:52:55  quinn
8  * Test
9  *
10  * Revision 1.19  1995/12/05  11:17:30  quinn
11  * Moved some paranthesises around. Sigh.
12  *
13  * Revision 1.18  1995/11/13  09:27:41  quinn
14  * Fiddling with the variant stuff.
15  *
16  * Revision 1.17  1995/11/07  12:37:44  quinn
17  * Added support for forcing TIMEOUT event.
18  *
19  * Revision 1.16  1995/11/01  13:54:56  quinn
20  * Minor adjustments
21  *
22  * Revision 1.15  1995/09/15  14:44:15  quinn
23  * *** empty log message ***
24  *
25  * Revision 1.14  1995/08/29  14:44:50  quinn
26  * Reset timeouts.
27  *
28  * Revision 1.13  1995/08/29  11:17:56  quinn
29  * Added code to receive close
30  *
31  * Revision 1.12  1995/08/29  10:41:18  quinn
32  * Small.
33  *
34  * Revision 1.11  1995/06/19  12:39:09  quinn
35  * Fixed bug in timeout code. Added BER dumper.
36  *
37  * Revision 1.10  1995/06/16  10:31:33  quinn
38  * Added session timeout.
39  *
40  * Revision 1.9  1995/06/05  10:53:31  quinn
41  * Added a better SCAN.
42  *
43  * Revision 1.8  1995/05/16  08:51:01  quinn
44  * License, documentation, and memory fixes
45  *
46  * Revision 1.7  1995/03/27  15:02:01  quinn
47  * Added some includes for better portability
48  *
49  * Revision 1.6  1995/03/27  08:34:21  quinn
50  * Added dynamic server functionality.
51  * Released bindings to session.c (is now redundant)
52  *
53  * Revision 1.5  1995/03/15  08:37:41  quinn
54  * Now we're pretty much set for nonblocking I/O.
55  *
56  * Revision 1.4  1995/03/14  16:59:48  quinn
57  * Bug-fixes
58  *
59  * Revision 1.3  1995/03/14  11:30:14  quinn
60  * Works better now.
61  *
62  * Revision 1.2  1995/03/14  10:27:59  quinn
63  * More work on demo server.
64  *
65  * Revision 1.1  1995/03/10  18:22:44  quinn
66  * The rudiments of an asynchronous server.
67  *
68  */
69
70 #include <stdio.h>
71 #include <assert.h>
72 #include <sys/time.h>
73 #include <sys/types.h>
74 #include <unistd.h>
75 #include <stdlib.h>
76 #include <errno.h>
77 #include <string.h>
78 #ifdef _AIX
79 #include <sys/select.h>
80 #endif
81
82 #include <eventl.h>
83
84 #include <xmalloc.h>
85
86 static IOCHAN iochans = 0;
87
88 IOCHAN iochan_getchan(void)
89 {
90     return iochans;
91 }
92
93 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
94 {
95     IOCHAN new;
96
97     if (!(new = xmalloc(sizeof(*new))))
98         return 0;
99     new->destroyed = 0;
100     new->fd = fd;
101     new->flags = flags;
102     new->fun = cb;
103     new->next = iochans;
104     new->force_event = 0;
105     new->last_event = new->max_idle = 0;
106     iochans = new;
107     return new;
108 }
109
110 int event_loop()
111 {
112     do /* loop as long as there are active associations to process */
113     {
114         IOCHAN p, nextp;
115         fd_set in, out, except;
116         int res, max;
117         static struct timeval nullto = {0, 0}, to;
118         struct timeval *timeout;
119
120         FD_ZERO(&in);
121         FD_ZERO(&out);
122         FD_ZERO(&except);
123         timeout = &to; /* hang on select */
124         to.tv_sec = 5*60;
125         to.tv_usec = 0;
126         max = 0;
127         for (p = iochans; p; p = p->next)
128         {
129             if (p->force_event)
130                 timeout = &nullto;        /* polling select */
131             if (p->flags & EVENT_INPUT)
132                 FD_SET(p->fd, &in);
133             if (p->flags & EVENT_OUTPUT)
134                 FD_SET(p->fd, &out);
135             if (p->flags & EVENT_EXCEPT)
136                 FD_SET(p->fd, &except);
137             if (p->fd > max)
138                 max = p->fd;
139         }
140         if ((res = select(max + 1, &in, &out, &except, timeout)) < 0)
141         {
142             if (errno == EINTR)
143                 continue;
144             return 1;
145         }
146         for (p = iochans; p; p = p->next)
147         {
148             int force_event = p->force_event;
149             time_t now = time(0);
150
151             p->force_event = 0;
152             if (!p->destroyed && FD_ISSET(p->fd, &in) || force_event ==
153                 EVENT_INPUT)
154             {
155                 p->last_event = now;
156                 (*p->fun)(p, EVENT_INPUT);
157             }
158             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
159                 force_event == EVENT_OUTPUT))
160             {
161                 p->last_event = now;
162                 (*p->fun)(p, EVENT_OUTPUT);
163             }
164             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
165                 force_event == EVENT_EXCEPT))
166             {
167                 p->last_event = now;
168                 (*p->fun)(p, EVENT_EXCEPT);
169             }
170             if (!p->destroyed && ((p->max_idle && now - p->last_event >
171                 p->max_idle) || force_event == EVENT_TIMEOUT))
172             {
173                 p->last_event = now;
174                 (*p->fun)(p, EVENT_TIMEOUT);
175             }
176         }
177         for (p = iochans; p; p = nextp)
178         {
179             nextp = p->next;
180
181             if (p->destroyed)
182             {
183                 IOCHAN tmp = p, pr;
184
185                 if (p == iochans)
186                     iochans = p->next;
187                 else
188                 {
189                     for (pr = iochans; pr; pr = pr->next)
190                         if (pr->next == p)
191                             break;
192                     assert(pr); /* grave error if it weren't there */
193                     pr->next = p->next;
194                 }
195                 if (nextp == p)
196                     nextp = p->next;
197                 xfree(tmp);
198             }
199         }
200     }
201     while (iochans);
202     return 0;
203 }