2 * ParaZ - a simple tool for harvesting performance data for parallel
3 * operations using Z39.50.
4 * Copyright (c) 2000-2004 Index Data ApS
5 * See LICENSE file for details.
9 * $Id: eventl.c,v 1.3 2007-03-28 12:05:18 marc Exp $
10 * Based on revision YAZ' server/eventl.c 1.29.
25 #include <yaz/yconfig.h>
27 #include <yaz/comstack.h>
28 #include <yaz/xmalloc.h>
29 #include <yaz/statserv.h>
34 IOCHAN iochan_create(int fd, struct sockaddr_in *addr_in,
35 IOC_CALLBACK cb, int flags)
39 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
41 new_iochan->destroyed = 0;
45 new_iochan->addr_in.sin_family = addr_in->sin_family;
46 new_iochan->addr_in.sin_port = addr_in->sin_port;
47 new_iochan->addr_in.sin_addr = addr_in->sin_addr;
48 strncpy(new_iochan->addr_str, inet_ntoa(addr_in->sin_addr), 64);
51 new_iochan->flags = flags;
53 new_iochan->force_event = 0;
54 new_iochan->last_event = new_iochan->max_idle = 0;
55 new_iochan->next = NULL;
59 int event_loop(IOCHAN *iochans)
61 do /* loop as long as there are active associations to process */
64 fd_set in, out, except;
66 static struct timeval nullto = {0, 0}, to;
67 struct timeval *timeout;
72 timeout = &to; /* hang on select */
76 for (p = *iochans; p; p = p->next)
81 timeout = &nullto; /* polling select */
82 if (p->flags & EVENT_INPUT)
84 if (p->flags & EVENT_OUTPUT)
86 if (p->flags & EVENT_EXCEPT)
87 FD_SET(p->fd, &except);
91 if ((res = select(max + 1, &in, &out, &except, timeout)) < 0)
98 for (p = *iochans; p; p = p->next)
100 int force_event = p->force_event;
101 time_t now = time(0);
104 if (!p->destroyed && ((p->max_idle && now - p->last_event >
105 p->max_idle) || force_event == EVENT_TIMEOUT))
108 (*p->fun)(p, EVENT_TIMEOUT);
112 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
113 force_event == EVENT_INPUT))
116 (*p->fun)(p, EVENT_INPUT);
118 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
119 force_event == EVENT_OUTPUT))
122 (*p->fun)(p, EVENT_OUTPUT);
124 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
125 force_event == EVENT_EXCEPT))
128 (*p->fun)(p, EVENT_EXCEPT);
131 for (p = *iochans; p; p = nextp)
139 /* Now reset the pointers */
144 for (pr = *iochans; pr; pr = pr->next)
147 assert(pr); /* grave error if it weren't there */
163 * indent-tabs-mode: nil
165 * vim: shiftwidth=4 tabstop=8 expandtab