2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-socket-manager.cpp,v 1.25 2004-01-30 00:38:28 adam Exp $
12 #include <sys/types.h>
19 #include <yaz++/socket-manager.h>
21 Yaz_SocketManager::YazSocketEntry **Yaz_SocketManager::lookupObserver(
22 IYazSocketObserver *observer)
26 for (se = &m_observers; *se; se = &(*se)->next)
27 if ((*se)->observer == observer)
32 void Yaz_SocketManager::addObserver(int fd, IYazSocketObserver *observer)
36 se = *lookupObserver(observer);
39 se = new YazSocketEntry;
40 se->next= m_observers;
42 se->observer = observer;
46 se->last_activity = 0;
50 void Yaz_SocketManager::deleteObserver(IYazSocketObserver *observer)
52 YazSocketEntry **se = lookupObserver(observer);
55 removeEvent (observer);
56 YazSocketEntry *se_tmp = *se;
62 void Yaz_SocketManager::deleteObservers()
64 YazSocketEntry *se = m_observers;
68 YazSocketEntry *se_next = se->next;
75 void Yaz_SocketManager::maskObserver(IYazSocketObserver *observer, int mask)
79 yaz_log(m_log, "obs=%p read=%d write=%d except=%d", observer,
80 mask & YAZ_SOCKET_OBSERVE_READ,
81 mask & YAZ_SOCKET_OBSERVE_WRITE,
82 mask & YAZ_SOCKET_OBSERVE_EXCEPT);
84 se = *lookupObserver(observer);
89 void Yaz_SocketManager::timeoutObserver(IYazSocketObserver *observer,
94 se = *lookupObserver(observer);
96 se->timeout = timeout;
99 int Yaz_SocketManager::processEvent()
102 YazSocketEvent *event = getEvent();
104 yaz_log (m_log, "Yaz_SocketManager::processEvent manager=%p", this);
107 event->observer->socketNotify(event->event);
112 fd_set in, out, except;
121 time_t now = time(0);
122 for (p = m_observers; p; p = p->next)
127 if (p->mask & YAZ_SOCKET_OBSERVE_READ)
129 yaz_log (m_log, "Yaz_SocketManager::select fd=%d read", fd);
132 if (p->mask & YAZ_SOCKET_OBSERVE_WRITE)
134 yaz_log (m_log, "Yaz_SocketManager::select fd=%d write", fd);
137 if (p->mask & YAZ_SOCKET_OBSERVE_EXCEPT)
139 yaz_log (m_log, "Yaz_SocketManager::select fd=%d except", fd);
147 timeout_this = p->timeout;
148 if (p->last_activity)
149 timeout_this -= now - p->last_activity;
151 p->last_activity = now;
152 if (timeout_this < 0 || timeout_this > 2147483646)
154 if (timeout == -1 || timeout_this < timeout)
155 timeout = timeout_this;
156 p->timeout_this = timeout_this;
157 yaz_log (m_log, "Yaz_SocketManager::select timeout_this=%d",
163 yaz_log (m_log, "no pending events return 0");
165 yaz_log (m_log, "no observers");
173 yaz_log (m_log, "Yaz_SocketManager::select begin no=%d timeout=%d",
175 while ((res = select(max + 1, &in, &out, &except,
176 timeout== -1 ? 0 : &to)) < 0)
179 yaz_log (LOG_LOG|LOG_WARN, "select");
182 yaz_log(m_log, "select returned res=%d", res);
184 for (p = m_observers; p; p = p->next)
188 if (FD_ISSET(fd, &in))
189 mask |= YAZ_SOCKET_OBSERVE_READ;
191 if (FD_ISSET(fd, &out))
192 mask |= YAZ_SOCKET_OBSERVE_WRITE;
194 if (FD_ISSET(fd, &except))
195 mask |= YAZ_SOCKET_OBSERVE_EXCEPT;
199 YazSocketEvent *event = new YazSocketEvent;
200 p->last_activity = now;
201 event->observer = p->observer;
205 yaz_log (m_log, "putEvent I/O mask=%d", mask);
207 else if ((now - p->last_activity) >= p->timeout)
209 YazSocketEvent *event = new YazSocketEvent;
210 assert (p->last_activity);
211 yaz_log (m_log, "putEvent timeout, now = %ld last_activity=%ld timeout=%d",
212 now, p->last_activity, p->timeout);
213 p->last_activity = now;
214 event->observer = p->observer;
215 event->event = YAZ_SOCKET_OBSERVE_TIMEOUT;
219 if ((event = getEvent()))
221 event->observer->socketNotify(event->event);
225 yaz_log (LOG_WARN, "unhandled event in processEvent");
230 // n p n p ...... n p n p
233 void Yaz_SocketManager::putEvent(YazSocketEvent *event)
235 // put in back of queue
238 m_queue_back->prev = event;
239 assert (m_queue_front);
243 assert (!m_queue_front);
244 m_queue_front = event;
246 event->next = m_queue_back;
248 m_queue_back = event;
251 Yaz_SocketManager::YazSocketEvent *Yaz_SocketManager::getEvent()
253 // get from front of queue
254 YazSocketEvent *event = m_queue_front;
257 assert (m_queue_back);
258 m_queue_front = event->prev;
261 assert (m_queue_back);
262 m_queue_front->next = 0;
269 void Yaz_SocketManager::removeEvent(IYazSocketObserver *observer)
271 YazSocketEvent *ev = m_queue_back;
274 YazSocketEvent *ev_next = ev->next;
275 if (observer == ev->observer)
278 ev->prev->next = ev->next;
280 m_queue_back = ev->next;
282 ev->next->prev = ev->prev;
284 m_queue_front = ev->prev;
291 Yaz_SocketManager::Yaz_SocketManager()
299 Yaz_SocketManager::~Yaz_SocketManager()