2 * Copyright (c) 1998-2000, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-socket-manager.cpp,v $
6 * Revision 1.13 2000-11-20 11:27:33 adam
7 * Fixes for connect operation (timeout and notify fix).
9 * Revision 1.12 2000/10/24 12:29:57 adam
10 * Fixed bug in proxy where a Yaz_ProxyClient could be owned by
11 * two Yaz_Proxy's (fatal).
13 * Revision 1.11 2000/10/11 11:58:17 adam
14 * Moved header files to include/yaz++. Switched to libtool and automake.
15 * Configure script creates yaz++-config script.
17 * Revision 1.10 2000/09/08 10:23:42 adam
18 * Added skeleton of yaz-z-server.
20 * Revision 1.9 2000/08/07 14:19:59 adam
21 * Fixed serious bug regarding timeouts. Improved logging for proxy.
23 * Revision 1.8 1999/12/06 13:52:45 adam
24 * Modified for new location of YAZ header files. Experimental threaded
27 * Revision 1.7 1999/04/28 13:02:08 adam
28 * Added include of string.h.
30 * Revision 1.6 1999/04/21 12:09:01 adam
31 * Many improvements. Modified to proxy server to work with "sessions"
34 * Revision 1.5 1999/04/09 11:46:57 adam
35 * Added object Yaz_Z_Assoc. Much more functional client.
37 * Revision 1.4 1999/03/23 14:17:57 adam
38 * More work on timeout handling. Work on yaz-client.
40 * Revision 1.3 1999/02/02 14:01:23 adam
41 * First WIN32 port of YAZ++.
43 * Revision 1.2 1999/01/28 13:08:48 adam
44 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
45 * yaz-socket-manager.cc.
47 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
48 * First implementation of YAZ++.
56 #include <sys/types.h>
63 #include <yaz++/yaz-socket-manager.h>
65 Yaz_SocketManager::YazSocketEntry **Yaz_SocketManager::lookupObserver(
66 IYazSocketObserver *observer)
70 for (se = &m_observers; *se; se = &(*se)->next)
71 if ((*se)->observer == observer)
76 void Yaz_SocketManager::addObserver(int fd, IYazSocketObserver *observer)
80 se = *lookupObserver(observer);
83 se = new YazSocketEntry;
84 se->next= m_observers;
86 se->observer = observer;
90 se->last_activity = 0;
94 void Yaz_SocketManager::deleteObserver(IYazSocketObserver *observer)
96 YazSocketEntry **se = lookupObserver(observer);
99 removeEvent (observer);
100 YazSocketEntry *se_tmp = *se;
106 void Yaz_SocketManager::deleteObservers()
108 YazSocketEntry *se = m_observers;
112 YazSocketEntry *se_next = se->next;
119 void Yaz_SocketManager::maskObserver(IYazSocketObserver *observer, int mask)
123 se = *lookupObserver(observer);
128 void Yaz_SocketManager::timeoutObserver(IYazSocketObserver *observer,
133 se = *lookupObserver(observer);
135 se->timeout = timeout;
138 int Yaz_SocketManager::processEvent()
141 YazSocketEvent *event = getEvent();
142 unsigned timeout = 0;
143 logf (m_log, "processEvent");
146 event->observer->socketNotify(event->event);
151 fd_set in, out, except;
160 time_t now = time(0);
161 for (p = m_observers; p; p = p->next)
166 if (p->mask & YAZ_SOCKET_OBSERVE_READ)
168 if (p->mask & YAZ_SOCKET_OBSERVE_WRITE)
170 if (p->mask & YAZ_SOCKET_OBSERVE_EXCEPT)
176 unsigned timeout_this;
177 timeout_this = p->timeout;
178 if (p->last_activity)
179 timeout_this -= now - p->last_activity;
181 p->last_activity = now;
182 if (timeout_this < 1)
184 if (!timeout || timeout_this < timeout)
185 timeout = timeout_this;
190 logf (m_log, "no pending events return 0");
192 logf (m_log, "no observers");
200 logf (m_log, "select pending=%d timeout=%d", no, timeout);
201 while ((res = select(max + 1, &in, &out, &except, timeout ? &to : 0)) < 0)
205 for (p = m_observers; p; p = p->next)
209 if (FD_ISSET(fd, &in))
210 mask |= YAZ_SOCKET_OBSERVE_READ;
212 if (FD_ISSET(fd, &out))
213 mask |= YAZ_SOCKET_OBSERVE_WRITE;
215 if (FD_ISSET(fd, &except))
216 mask |= YAZ_SOCKET_OBSERVE_EXCEPT;
220 YazSocketEvent *event = new YazSocketEvent;
221 p->last_activity = now;
222 event->observer = p->observer;
226 else if (p->timeout && p->last_activity &&
227 now >= p->last_activity + (int) (p->timeout))
229 YazSocketEvent *event = new YazSocketEvent;
230 logf (m_log, "timeout, now = %ld last_activity=%ld timeout=%d",
231 now, p->last_activity, p->timeout);
232 p->last_activity = now;
233 event->observer = p->observer;
234 event->event = YAZ_SOCKET_OBSERVE_TIMEOUT;
238 if ((event = getEvent()))
240 event->observer->socketNotify(event->event);
248 // n p n p ...... n p n p
251 void Yaz_SocketManager::putEvent(YazSocketEvent *event)
253 // put in back of queue
256 m_queue_back->prev = event;
257 assert (m_queue_front);
261 assert (!m_queue_front);
262 m_queue_front = event;
264 event->next = m_queue_back;
266 m_queue_back = event;
269 Yaz_SocketManager::YazSocketEvent *Yaz_SocketManager::getEvent()
271 // get from front of queue
272 YazSocketEvent *event = m_queue_front;
275 assert (m_queue_back);
276 m_queue_front = event->prev;
279 assert (m_queue_back);
280 m_queue_front->next = 0;
287 void Yaz_SocketManager::removeEvent(IYazSocketObserver *observer)
289 YazSocketEvent *ev = m_queue_back;
292 YazSocketEvent *ev_next = ev->next;
293 if (observer == ev->observer)
296 ev->prev->next = ev->next;
298 m_queue_back = ev->next;
300 ev->next->prev = ev->prev;
302 m_queue_front = ev->prev;
309 Yaz_SocketManager::Yaz_SocketManager()
317 Yaz_SocketManager::~Yaz_SocketManager()