2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-socket-manager.cpp,v 1.26 2004-02-02 11:17:45 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);
144 if (p->timeout > 0 ||
145 (p->timeout == 0 && (p->mask & YAZ_SOCKET_OBSERVE_WRITE) == 0))
148 timeout_this = p->timeout;
149 if (p->last_activity)
150 timeout_this -= now - p->last_activity;
152 p->last_activity = now;
153 if (timeout_this < 0 || timeout_this > 2147483646)
155 if (timeout == -1 || timeout_this < timeout)
156 timeout = timeout_this;
157 p->timeout_this = timeout_this;
158 yaz_log (m_log, "Yaz_SocketManager::select timeout_this=%d",
164 yaz_log (m_log, "no pending events return 0");
166 yaz_log (m_log, "no observers");
174 yaz_log (m_log, "Yaz_SocketManager::select begin no=%d timeout=%d",
176 while ((res = select(max + 1, &in, &out, &except,
177 timeout== -1 ? 0 : &to)) < 0)
180 yaz_log (LOG_LOG|LOG_WARN, "select");
183 yaz_log(m_log, "select returned res=%d", res);
185 for (p = m_observers; p; p = p->next)
189 if (FD_ISSET(fd, &in))
190 mask |= YAZ_SOCKET_OBSERVE_READ;
192 if (FD_ISSET(fd, &out))
193 mask |= YAZ_SOCKET_OBSERVE_WRITE;
195 if (FD_ISSET(fd, &except))
196 mask |= YAZ_SOCKET_OBSERVE_EXCEPT;
200 YazSocketEvent *event = new YazSocketEvent;
201 p->last_activity = now;
202 event->observer = p->observer;
206 yaz_log (m_log, "putEvent I/O mask=%d", mask);
210 (p->timeout == 0 && (p->mask & YAZ_SOCKET_OBSERVE_WRITE) == 0))
212 (now - p->last_activity) >= p->timeout)
214 YazSocketEvent *event = new YazSocketEvent;
215 assert (p->last_activity);
216 yaz_log (m_log, "putEvent timeout, now = %ld last_activity=%ld timeout=%d",
217 now, p->last_activity, p->timeout);
218 p->last_activity = now;
219 event->observer = p->observer;
220 event->event = YAZ_SOCKET_OBSERVE_TIMEOUT;
224 if ((event = getEvent()))
226 event->observer->socketNotify(event->event);
230 yaz_log (LOG_WARN, "unhandled event in processEvent");
235 // n p n p ...... n p n p
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()