2 * Copyright (c) 1998-2000, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-socket-manager.cpp,v $
6 * Revision 1.15 2001-03-26 14:43:49 adam
7 * New threaded PDU association.
9 * Revision 1.14 2000/11/20 14:17:36 adam
10 * Yet another WIN32 fix for connect notify.
12 * Revision 1.13 2000/11/20 11:27:33 adam
13 * Fixes for connect operation (timeout and notify fix).
15 * Revision 1.12 2000/10/24 12:29:57 adam
16 * Fixed bug in proxy where a Yaz_ProxyClient could be owned by
17 * two Yaz_Proxy's (fatal).
19 * Revision 1.11 2000/10/11 11:58:17 adam
20 * Moved header files to include/yaz++. Switched to libtool and automake.
21 * Configure script creates yaz++-config script.
23 * Revision 1.10 2000/09/08 10:23:42 adam
24 * Added skeleton of yaz-z-server.
26 * Revision 1.9 2000/08/07 14:19:59 adam
27 * Fixed serious bug regarding timeouts. Improved logging for proxy.
29 * Revision 1.8 1999/12/06 13:52:45 adam
30 * Modified for new location of YAZ header files. Experimental threaded
33 * Revision 1.7 1999/04/28 13:02:08 adam
34 * Added include of string.h.
36 * Revision 1.6 1999/04/21 12:09:01 adam
37 * Many improvements. Modified to proxy server to work with "sessions"
40 * Revision 1.5 1999/04/09 11:46:57 adam
41 * Added object Yaz_Z_Assoc. Much more functional client.
43 * Revision 1.4 1999/03/23 14:17:57 adam
44 * More work on timeout handling. Work on yaz-client.
46 * Revision 1.3 1999/02/02 14:01:23 adam
47 * First WIN32 port of YAZ++.
49 * Revision 1.2 1999/01/28 13:08:48 adam
50 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
51 * yaz-socket-manager.cc.
53 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
54 * First implementation of YAZ++.
62 #include <sys/types.h>
69 #include <yaz++/yaz-socket-manager.h>
71 Yaz_SocketManager::YazSocketEntry **Yaz_SocketManager::lookupObserver(
72 IYazSocketObserver *observer)
76 for (se = &m_observers; *se; se = &(*se)->next)
77 if ((*se)->observer == observer)
82 void Yaz_SocketManager::addObserver(int fd, IYazSocketObserver *observer)
86 se = *lookupObserver(observer);
89 se = new YazSocketEntry;
90 se->next= m_observers;
92 se->observer = observer;
96 se->last_activity = 0;
100 void Yaz_SocketManager::deleteObserver(IYazSocketObserver *observer)
102 YazSocketEntry **se = lookupObserver(observer);
105 removeEvent (observer);
106 YazSocketEntry *se_tmp = *se;
112 void Yaz_SocketManager::deleteObservers()
114 YazSocketEntry *se = m_observers;
118 YazSocketEntry *se_next = se->next;
125 void Yaz_SocketManager::maskObserver(IYazSocketObserver *observer, int mask)
129 se = *lookupObserver(observer);
134 void Yaz_SocketManager::timeoutObserver(IYazSocketObserver *observer,
139 se = *lookupObserver(observer);
141 se->timeout = timeout;
144 int Yaz_SocketManager::processEvent()
147 YazSocketEvent *event = getEvent();
148 unsigned timeout = 0;
149 logf (m_log, "Yaz_SocketManager::processEvent manager=%p", this);
152 event->observer->socketNotify(event->event);
157 fd_set in, out, except;
166 time_t now = time(0);
167 for (p = m_observers; p; p = p->next)
172 if (p->mask & YAZ_SOCKET_OBSERVE_READ)
174 yaz_log (m_log, "Yaz_SocketManager::select fd=%d read", fd);
177 if (p->mask & YAZ_SOCKET_OBSERVE_WRITE)
179 yaz_log (m_log, "Yaz_SocketManager::select fd=%d write", fd);
182 if (p->mask & YAZ_SOCKET_OBSERVE_EXCEPT)
184 yaz_log (m_log, "Yaz_SocketManager::select fd=%d except", fd);
191 unsigned timeout_this;
192 timeout_this = p->timeout;
193 if (p->last_activity)
194 timeout_this -= now - p->last_activity;
196 p->last_activity = now;
197 if (timeout_this < 1)
199 if (!timeout || timeout_this < timeout)
200 timeout = timeout_this;
205 logf (m_log, "no pending events return 0");
207 logf (m_log, "no observers");
215 logf (m_log, "Yaz_SocketManager::select no=%d timeout=%d", no, timeout);
216 while ((res = select(max + 1, &in, &out, &except, timeout ? &to : 0)) < 0)
220 for (p = m_observers; p; p = p->next)
224 if (FD_ISSET(fd, &in))
225 mask |= YAZ_SOCKET_OBSERVE_READ;
227 if (FD_ISSET(fd, &out))
228 mask |= YAZ_SOCKET_OBSERVE_WRITE;
230 if (FD_ISSET(fd, &except))
231 mask |= YAZ_SOCKET_OBSERVE_EXCEPT;
235 YazSocketEvent *event = new YazSocketEvent;
236 p->last_activity = now;
237 event->observer = p->observer;
241 else if (p->timeout && p->last_activity &&
242 now >= p->last_activity + (int) (p->timeout))
244 YazSocketEvent *event = new YazSocketEvent;
245 logf (m_log, "timeout, now = %ld last_activity=%ld timeout=%d",
246 now, p->last_activity, p->timeout);
247 p->last_activity = now;
248 event->observer = p->observer;
249 event->event = YAZ_SOCKET_OBSERVE_TIMEOUT;
253 if ((event = getEvent()))
255 event->observer->socketNotify(event->event);
263 // n p n p ...... n p n p
266 void Yaz_SocketManager::putEvent(YazSocketEvent *event)
268 // put in back of queue
271 m_queue_back->prev = event;
272 assert (m_queue_front);
276 assert (!m_queue_front);
277 m_queue_front = event;
279 event->next = m_queue_back;
281 m_queue_back = event;
284 Yaz_SocketManager::YazSocketEvent *Yaz_SocketManager::getEvent()
286 // get from front of queue
287 YazSocketEvent *event = m_queue_front;
290 assert (m_queue_back);
291 m_queue_front = event->prev;
294 assert (m_queue_back);
295 m_queue_front->next = 0;
302 void Yaz_SocketManager::removeEvent(IYazSocketObserver *observer)
304 YazSocketEvent *ev = m_queue_back;
307 YazSocketEvent *ev_next = ev->next;
308 if (observer == ev->observer)
311 ev->prev->next = ev->next;
313 m_queue_back = ev->next;
315 ev->next->prev = ev->prev;
317 m_queue_front = ev->prev;
324 Yaz_SocketManager::Yaz_SocketManager()
332 Yaz_SocketManager::~Yaz_SocketManager()