2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-socket-manager.cpp,v 1.24 2004-01-07 13:40:06 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");
183 for (p = m_observers; p; p = p->next)
187 if (FD_ISSET(fd, &in))
188 mask |= YAZ_SOCKET_OBSERVE_READ;
190 if (FD_ISSET(fd, &out))
191 mask |= YAZ_SOCKET_OBSERVE_WRITE;
193 if (FD_ISSET(fd, &except))
194 mask |= YAZ_SOCKET_OBSERVE_EXCEPT;
198 YazSocketEvent *event = new YazSocketEvent;
199 p->last_activity = now;
200 event->observer = p->observer;
204 yaz_log (m_log, "putEvent I/O mask=%d", mask);
206 else if (p->timeout && (now - p->last_activity) >= p->timeout)
208 YazSocketEvent *event = new YazSocketEvent;
209 assert (p->last_activity);
210 yaz_log (m_log, "putEvent timeout, now = %ld last_activity=%ld timeout=%d",
211 now, p->last_activity, p->timeout);
212 p->last_activity = now;
213 event->observer = p->observer;
214 event->event = YAZ_SOCKET_OBSERVE_TIMEOUT;
218 if ((event = getEvent()))
220 event->observer->socketNotify(event->event);
224 yaz_log (LOG_WARN, "unhandled event in processEvent");
229 // n p n p ...... n p n p
232 void Yaz_SocketManager::putEvent(YazSocketEvent *event)
234 // put in back of queue
237 m_queue_back->prev = event;
238 assert (m_queue_front);
242 assert (!m_queue_front);
243 m_queue_front = event;
245 event->next = m_queue_back;
247 m_queue_back = event;
250 Yaz_SocketManager::YazSocketEvent *Yaz_SocketManager::getEvent()
252 // get from front of queue
253 YazSocketEvent *event = m_queue_front;
256 assert (m_queue_back);
257 m_queue_front = event->prev;
260 assert (m_queue_back);
261 m_queue_front->next = 0;
268 void Yaz_SocketManager::removeEvent(IYazSocketObserver *observer)
270 YazSocketEvent *ev = m_queue_back;
273 YazSocketEvent *ev_next = ev->next;
274 if (observer == ev->observer)
277 ev->prev->next = ev->next;
279 m_queue_back = ev->next;
281 ev->next->prev = ev->prev;
283 m_queue_front = ev->prev;
290 Yaz_SocketManager::Yaz_SocketManager()
298 Yaz_SocketManager::~Yaz_SocketManager()