1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 Index Data
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
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
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
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.
28 * Based on revision YAZ' server/eventl.c 1.29.
51 #include <yaz/yconfig.h>
53 #include <yaz/comstack.h>
54 #include <yaz/xmalloc.h>
61 iochan_man_t iochan_man_create(void)
63 iochan_man_t man = xmalloc(sizeof(*man));
64 man->channel_list = 0;
68 void iochan_man_destroy(iochan_man_t *mp)
77 void iochan_add(iochan_man_t man, IOCHAN chan)
79 chan->next = man->channel_list;
80 man->channel_list = chan;
83 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
87 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
89 new_iochan->destroyed = 0;
91 new_iochan->flags = flags;
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;
101 static int event_loop(IOCHAN *iochans)
103 do /* loop as long as there are active associations to process */
106 fd_set in, out, except;
108 static struct timeval nullto = {0, 0}, to;
109 struct timeval *timeout;
114 timeout = &to; /* hang on select */
118 for (p = *iochans; p; p = p->next)
121 p->flags = (*p->maskfun)(p);
123 p->fd = (*p->socketfun)(p);
127 timeout = &nullto; /* polling select */
128 if (p->flags & EVENT_INPUT)
130 if (p->flags & EVENT_OUTPUT)
132 if (p->flags & EVENT_EXCEPT)
133 FD_SET(p->fd, &except);
136 if (p->max_idle && p->max_idle < to.tv_sec)
137 to.tv_sec = p->max_idle;
139 res = select(max + 1, &in, &out, &except, timeout);
146 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
150 for (p = *iochans; p; p = p->next)
152 int force_event = p->force_event;
153 time_t now = time(0);
156 if (!p->destroyed && ((p->max_idle && now - p->last_event >
157 p->max_idle) || force_event == EVENT_TIMEOUT))
160 (*p->fun)(p, EVENT_TIMEOUT);
164 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
165 force_event == EVENT_INPUT))
168 yaz_log(YLOG_DEBUG, "Eventl input event");
169 (*p->fun)(p, EVENT_INPUT);
171 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
172 force_event == EVENT_OUTPUT))
175 yaz_log(YLOG_DEBUG, "Eventl output event");
176 (*p->fun)(p, EVENT_OUTPUT);
178 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
179 force_event == EVENT_EXCEPT))
182 (*p->fun)(p, EVENT_EXCEPT);
185 for (p = *iochans; p; p = nextp)
193 /* Now reset the pointers */
198 for (pr = *iochans; pr; pr = pr->next)
201 assert(pr); /* grave error if it weren't there */
214 void iochan_man_events(iochan_man_t man)
216 event_loop(&man->channel_list);
222 * c-file-style: "Stroustrup"
223 * indent-tabs-mode: nil
225 * vim: shiftwidth=4 tabstop=8 expandtab