2 * Copyright (c) 1998-2000, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-socket-manager.cpp,v $
6 * Revision 1.11 2000-10-11 11:58:17 adam
7 * Moved header files to include/yaz++. Switched to libtool and automake.
8 * Configure script creates yaz++-config script.
10 * Revision 1.10 2000/09/08 10:23:42 adam
11 * Added skeleton of yaz-z-server.
13 * Revision 1.9 2000/08/07 14:19:59 adam
14 * Fixed serious bug regarding timeouts. Improved logging for proxy.
16 * Revision 1.8 1999/12/06 13:52:45 adam
17 * Modified for new location of YAZ header files. Experimental threaded
20 * Revision 1.7 1999/04/28 13:02:08 adam
21 * Added include of string.h.
23 * Revision 1.6 1999/04/21 12:09:01 adam
24 * Many improvements. Modified to proxy server to work with "sessions"
27 * Revision 1.5 1999/04/09 11:46:57 adam
28 * Added object Yaz_Z_Assoc. Much more functional client.
30 * Revision 1.4 1999/03/23 14:17:57 adam
31 * More work on timeout handling. Work on yaz-client.
33 * Revision 1.3 1999/02/02 14:01:23 adam
34 * First WIN32 port of YAZ++.
36 * Revision 1.2 1999/01/28 13:08:48 adam
37 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
38 * yaz-socket-manager.cc.
40 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
41 * First implementation of YAZ++.
49 #include <sys/types.h>
56 #include <yaz++/yaz-socket-manager.h>
58 Yaz_SocketManager::YazSocketEntry **Yaz_SocketManager::lookupObserver(
59 IYazSocketObserver *observer)
63 for (se = &m_observers; *se; se = &(*se)->next)
64 if ((*se)->observer == observer)
69 void Yaz_SocketManager::addObserver(int fd, IYazSocketObserver *observer)
73 se = *lookupObserver(observer);
76 se = new YazSocketEntry;
77 se->next= m_observers;
79 se->observer = observer;
83 se->last_activity = 0;
87 void Yaz_SocketManager::deleteObserver(IYazSocketObserver *observer)
89 YazSocketEntry **se = lookupObserver(observer);
92 removeEvent (observer);
93 YazSocketEntry *se_tmp = *se;
99 void Yaz_SocketManager::deleteObservers()
101 YazSocketEntry *se = m_observers;
105 YazSocketEntry *se_next = se->next;
112 void Yaz_SocketManager::maskObserver(IYazSocketObserver *observer, int mask)
116 se = *lookupObserver(observer);
121 void Yaz_SocketManager::timeoutObserver(IYazSocketObserver *observer,
126 se = *lookupObserver(observer);
128 se->timeout = timeout;
131 int Yaz_SocketManager::processEvent()
134 YazSocketEvent *event = getEvent();
135 unsigned timeout = 0;
136 logf (m_log, "processEvent");
139 event->observer->socketNotify(event->event);
144 fd_set in, out, except;
153 time_t now = time(0);
154 for (p = m_observers; p; p = p->next)
159 if (p->mask & YAZ_SOCKET_OBSERVE_READ)
161 if (p->mask & YAZ_SOCKET_OBSERVE_WRITE)
163 if (p->mask & YAZ_SOCKET_OBSERVE_EXCEPT)
169 unsigned timeout_this;
170 timeout_this = p->timeout;
171 if (p->last_activity)
172 timeout_this -= now - p->last_activity;
173 if (timeout_this < 1)
175 if (!timeout || timeout_this < timeout)
176 timeout = timeout_this;
181 logf (m_log, "no pending events return 0");
183 logf (m_log, "no observers");
191 logf (m_log, "select pending=%d timeout=%d", no, timeout);
192 while ((res = select(max + 1, &in, &out, &except, timeout ? &to : 0)) < 0)
196 for (p = m_observers; p; p = p->next)
200 if (FD_ISSET(fd, &in))
201 mask |= YAZ_SOCKET_OBSERVE_READ;
203 if (FD_ISSET(fd, &out))
204 mask |= YAZ_SOCKET_OBSERVE_WRITE;
206 if (FD_ISSET(fd, &except))
207 mask |= YAZ_SOCKET_OBSERVE_EXCEPT;
211 YazSocketEvent *event = new YazSocketEvent;
212 p->last_activity = now;
213 event->observer = p->observer;
217 else if (p->timeout && p->last_activity &&
218 now >= p->last_activity + (int) (p->timeout))
220 YazSocketEvent *event = new YazSocketEvent;
221 logf (LOG_LOG, "timeout now = %ld last_activity=%ld timeout=%d",
222 now, p->last_activity, p->timeout);
223 p->last_activity = now;
224 event->observer = p->observer;
225 event->event = YAZ_SOCKET_OBSERVE_TIMEOUT;
229 if ((event = getEvent()))
231 event->observer->socketNotify(event->event);
238 void Yaz_SocketManager::putEvent(YazSocketEvent *event)
240 // put in back of queue
243 m_queue_back->prev = event;
244 assert (m_queue_front);
248 assert (!m_queue_front);
249 m_queue_front = event;
251 event->next = m_queue_back;
253 m_queue_back = event;
256 Yaz_SocketManager::YazSocketEvent *Yaz_SocketManager::getEvent()
258 // get from front of queue
259 YazSocketEvent *event = m_queue_front;
262 assert (m_queue_back);
263 m_queue_front = event->prev;
266 assert (m_queue_back);
267 m_queue_front->next = 0;
274 void Yaz_SocketManager::removeEvent(IYazSocketObserver *observer)
276 YazSocketEvent *ev = m_queue_back;
279 YazSocketEvent *ev_next = ev->next;
280 if (observer == ev->observer)
283 ev->prev->next = ev->next;
285 m_queue_back = ev->next;
287 ev->next->prev = ev->prev;
289 m_queue_front = ev->prev;
296 Yaz_SocketManager::Yaz_SocketManager()
304 Yaz_SocketManager::~Yaz_SocketManager()