2 * Copyright (c) 1998-2000, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-socket-manager.cpp,v $
6 * Revision 1.12 2000-10-24 12:29:57 adam
7 * Fixed bug in proxy where a Yaz_ProxyClient could be owned by
8 * two Yaz_Proxy's (fatal).
10 * Revision 1.11 2000/10/11 11:58:17 adam
11 * Moved header files to include/yaz++. Switched to libtool and automake.
12 * Configure script creates yaz++-config script.
14 * Revision 1.10 2000/09/08 10:23:42 adam
15 * Added skeleton of yaz-z-server.
17 * Revision 1.9 2000/08/07 14:19:59 adam
18 * Fixed serious bug regarding timeouts. Improved logging for proxy.
20 * Revision 1.8 1999/12/06 13:52:45 adam
21 * Modified for new location of YAZ header files. Experimental threaded
24 * Revision 1.7 1999/04/28 13:02:08 adam
25 * Added include of string.h.
27 * Revision 1.6 1999/04/21 12:09:01 adam
28 * Many improvements. Modified to proxy server to work with "sessions"
31 * Revision 1.5 1999/04/09 11:46:57 adam
32 * Added object Yaz_Z_Assoc. Much more functional client.
34 * Revision 1.4 1999/03/23 14:17:57 adam
35 * More work on timeout handling. Work on yaz-client.
37 * Revision 1.3 1999/02/02 14:01:23 adam
38 * First WIN32 port of YAZ++.
40 * Revision 1.2 1999/01/28 13:08:48 adam
41 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
42 * yaz-socket-manager.cc.
44 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
45 * First implementation of YAZ++.
53 #include <sys/types.h>
60 #include <yaz++/yaz-socket-manager.h>
62 Yaz_SocketManager::YazSocketEntry **Yaz_SocketManager::lookupObserver(
63 IYazSocketObserver *observer)
67 for (se = &m_observers; *se; se = &(*se)->next)
68 if ((*se)->observer == observer)
73 void Yaz_SocketManager::addObserver(int fd, IYazSocketObserver *observer)
77 se = *lookupObserver(observer);
80 se = new YazSocketEntry;
81 se->next= m_observers;
83 se->observer = observer;
87 se->last_activity = 0;
91 void Yaz_SocketManager::deleteObserver(IYazSocketObserver *observer)
93 YazSocketEntry **se = lookupObserver(observer);
96 removeEvent (observer);
97 YazSocketEntry *se_tmp = *se;
103 void Yaz_SocketManager::deleteObservers()
105 YazSocketEntry *se = m_observers;
109 YazSocketEntry *se_next = se->next;
116 void Yaz_SocketManager::maskObserver(IYazSocketObserver *observer, int mask)
120 se = *lookupObserver(observer);
125 void Yaz_SocketManager::timeoutObserver(IYazSocketObserver *observer,
130 se = *lookupObserver(observer);
132 se->timeout = timeout;
135 int Yaz_SocketManager::processEvent()
138 YazSocketEvent *event = getEvent();
139 unsigned timeout = 0;
140 logf (m_log, "processEvent");
143 event->observer->socketNotify(event->event);
148 fd_set in, out, except;
157 time_t now = time(0);
158 for (p = m_observers; p; p = p->next)
163 if (p->mask & YAZ_SOCKET_OBSERVE_READ)
165 if (p->mask & YAZ_SOCKET_OBSERVE_WRITE)
167 if (p->mask & YAZ_SOCKET_OBSERVE_EXCEPT)
173 unsigned timeout_this;
174 timeout_this = p->timeout;
175 if (p->last_activity)
176 timeout_this -= now - p->last_activity;
177 if (timeout_this < 1)
179 if (!timeout || timeout_this < timeout)
180 timeout = timeout_this;
185 logf (m_log, "no pending events return 0");
187 logf (m_log, "no observers");
195 logf (m_log, "select pending=%d timeout=%d", no, timeout);
196 while ((res = select(max + 1, &in, &out, &except, timeout ? &to : 0)) < 0)
200 for (p = m_observers; p; p = p->next)
204 if (FD_ISSET(fd, &in))
205 mask |= YAZ_SOCKET_OBSERVE_READ;
207 if (FD_ISSET(fd, &out))
208 mask |= YAZ_SOCKET_OBSERVE_WRITE;
210 if (FD_ISSET(fd, &except))
211 mask |= YAZ_SOCKET_OBSERVE_EXCEPT;
215 YazSocketEvent *event = new YazSocketEvent;
216 p->last_activity = now;
217 event->observer = p->observer;
221 else if (p->timeout && p->last_activity &&
222 now >= p->last_activity + (int) (p->timeout))
224 YazSocketEvent *event = new YazSocketEvent;
225 logf (LOG_LOG, "timeout now = %ld last_activity=%ld timeout=%d",
226 now, p->last_activity, p->timeout);
227 p->last_activity = now;
228 event->observer = p->observer;
229 event->event = YAZ_SOCKET_OBSERVE_TIMEOUT;
233 if ((event = getEvent()))
235 event->observer->socketNotify(event->event);
243 // n p n p ...... n p n p
246 void Yaz_SocketManager::putEvent(YazSocketEvent *event)
248 // put in back of queue
251 m_queue_back->prev = event;
252 assert (m_queue_front);
256 assert (!m_queue_front);
257 m_queue_front = event;
259 event->next = m_queue_back;
261 m_queue_back = event;
264 Yaz_SocketManager::YazSocketEvent *Yaz_SocketManager::getEvent()
266 // get from front of queue
267 YazSocketEvent *event = m_queue_front;
270 assert (m_queue_back);
271 m_queue_front = event->prev;
274 assert (m_queue_back);
275 m_queue_front->next = 0;
282 void Yaz_SocketManager::removeEvent(IYazSocketObserver *observer)
284 YazSocketEvent *ev = m_queue_back;
287 YazSocketEvent *ev_next = ev->next;
288 if (observer == ev->observer)
291 ev->prev->next = ev->next;
293 m_queue_back = ev->next;
295 ev->next->prev = ev->prev;
297 m_queue_front = ev->prev;
304 Yaz_SocketManager::Yaz_SocketManager()
312 Yaz_SocketManager::~Yaz_SocketManager()