2 * Copyright (c) 1998-2005, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-socket-manager.cpp,v 1.33 2005-06-08 13:28:06 adam Exp $
15 #include <sys/types.h>
26 #include <yaz++/socket-manager.h>
28 using namespace yazpp_1;
30 SocketManager::SocketEntry **SocketManager::lookupObserver(
31 ISocketObserver *observer)
35 for (se = &m_observers; *se; se = &(*se)->next)
36 if ((*se)->observer == observer)
41 void SocketManager::addObserver(int fd, ISocketObserver *observer)
45 se = *lookupObserver(observer);
49 se->next= m_observers;
51 se->observer = observer;
55 se->last_activity = 0;
59 void SocketManager::deleteObserver(ISocketObserver *observer)
61 SocketEntry **se = lookupObserver(observer);
64 removeEvent (observer);
65 SocketEntry *se_tmp = *se;
71 void SocketManager::deleteObservers()
73 SocketEntry *se = m_observers;
77 SocketEntry *se_next = se->next;
84 void SocketManager::maskObserver(ISocketObserver *observer, int mask)
88 yaz_log(m_log, "obs=%p read=%d write=%d except=%d", observer,
89 mask & SOCKET_OBSERVE_READ,
90 mask & SOCKET_OBSERVE_WRITE,
91 mask & SOCKET_OBSERVE_EXCEPT);
93 se = *lookupObserver(observer);
98 void SocketManager::timeoutObserver(ISocketObserver *observer,
103 se = *lookupObserver(observer);
105 se->timeout = timeout;
108 int SocketManager::processEvent()
111 SocketEvent *event = getEvent();
113 yaz_log (m_log, "SocketManager::processEvent manager=%p", this);
116 event->observer->socketNotify(event->event);
121 fd_set in, out, except;
130 time_t now = time(0);
131 for (p = m_observers; p; p = p->next)
136 if (p->mask & SOCKET_OBSERVE_READ)
138 yaz_log (m_log, "SocketManager::select fd=%d read", fd);
141 if (p->mask & SOCKET_OBSERVE_WRITE)
143 yaz_log (m_log, "SocketManager::select fd=%d write", fd);
146 if (p->mask & SOCKET_OBSERVE_EXCEPT)
148 yaz_log (m_log, "SocketManager::select fd=%d except", fd);
153 if (p->timeout > 0 ||
154 (p->timeout == 0 && (p->mask & SOCKET_OBSERVE_WRITE) == 0))
157 timeout_this = p->timeout;
158 if (p->last_activity)
159 timeout_this -= now - p->last_activity;
161 p->last_activity = now;
162 if (timeout_this < 0 || timeout_this > 2147483646)
164 if (timeout == -1 || timeout_this < timeout)
165 timeout = timeout_this;
166 p->timeout_this = timeout_this;
167 yaz_log (m_log, "SocketManager::select timeout_this=%d",
173 yaz_log (m_log, "no pending events return 0");
175 yaz_log (m_log, "no observers");
183 yaz_log (m_log, "SocketManager::select begin no=%d timeout=%d",
186 while ((res = select(max + 1, &in, &out, &except,
187 timeout== -1 ? 0 : &to)) < 0)
190 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
191 yaz_log(YLOG_WARN, "errno=%d max=%d timeout=%d",
192 errno, max, timeout);
196 yaz_log(m_log, "select returned res=%d", res);
198 for (p = m_observers; p; p = p->next)
202 if (FD_ISSET(fd, &in))
203 mask |= SOCKET_OBSERVE_READ;
205 if (FD_ISSET(fd, &out))
206 mask |= SOCKET_OBSERVE_WRITE;
208 if (FD_ISSET(fd, &except))
209 mask |= SOCKET_OBSERVE_EXCEPT;
213 SocketEvent *event = new SocketEvent;
214 p->last_activity = now;
215 event->observer = p->observer;
219 yaz_log (m_log, "putEvent I/O mask=%d", mask);
221 else if (res == 0 && p->timeout_this == timeout)
223 SocketEvent *event = new SocketEvent;
224 assert (p->last_activity);
225 yaz_log (m_log, "putEvent timeout fd=%d, now = %ld last_activity=%ld timeout=%d",
226 p->fd, now, p->last_activity, p->timeout);
227 p->last_activity = now;
228 event->observer = p->observer;
229 event->event = SOCKET_OBSERVE_TIMEOUT;
233 if ((event = getEvent()))
235 event->observer->socketNotify(event->event);
239 yaz_log(YLOG_WARN, "unhandled event in processEvent res=%d", res);
244 // n p n p ...... n p n p
247 void SocketManager::putEvent(SocketEvent *event)
249 // put in back of queue
252 m_queue_back->prev = event;
253 assert (m_queue_front);
257 assert (!m_queue_front);
258 m_queue_front = event;
260 event->next = m_queue_back;
262 m_queue_back = event;
265 SocketManager::SocketEvent *SocketManager::getEvent()
267 // get from front of queue
268 SocketEvent *event = m_queue_front;
271 assert (m_queue_back);
272 m_queue_front = event->prev;
275 assert (m_queue_back);
276 m_queue_front->next = 0;
283 void SocketManager::removeEvent(ISocketObserver *observer)
285 SocketEvent *ev = m_queue_back;
288 SocketEvent *ev_next = ev->next;
289 if (observer == ev->observer)
292 ev->prev->next = ev->next;
294 m_queue_back = ev->next;
296 ev->next->prev = ev->prev;
298 m_queue_front = ev->prev;
305 SocketManager::SocketManager()
313 SocketManager::~SocketManager()