1 /* This file is part of the yazpp toolkit.
2 * Copyright (C) 1998-2012 Index Data and Mike Taylor
3 * See the file LICENSE for details.
13 #include <sys/types.h>
26 #include <yazpp/socket-manager.h>
29 using namespace yazpp_1;
31 SocketManager::SocketEntry **SocketManager::lookupObserver(
32 ISocketObserver *observer)
36 for (se = &m_observers; *se; se = &(*se)->next)
37 if ((*se)->observer == observer)
42 void SocketManager::addObserver(int fd, ISocketObserver *observer)
46 se = *lookupObserver(observer);
50 se->next= m_observers;
52 se->observer = observer;
56 se->last_activity = 0;
60 void SocketManager::deleteObserver(ISocketObserver *observer)
62 SocketEntry **se = lookupObserver(observer);
65 removeEvent (observer);
66 SocketEntry *se_tmp = *se;
72 void SocketManager::deleteObservers()
74 SocketEntry *se = m_observers;
78 SocketEntry *se_next = se->next;
85 void SocketManager::maskObserver(ISocketObserver *observer, int mask)
89 yaz_log(m_log, "obs=%p read=%d write=%d except=%d", observer,
90 mask & SOCKET_OBSERVE_READ,
91 mask & SOCKET_OBSERVE_WRITE,
92 mask & SOCKET_OBSERVE_EXCEPT);
94 se = *lookupObserver(observer);
99 void SocketManager::timeoutObserver(ISocketObserver *observer,
104 se = *lookupObserver(observer);
106 se->timeout = timeout;
110 void SocketManager::inspect_poll_result(int res, struct yaz_poll_fd *fds,
111 int no_fds, int timeout)
114 yaz_log(m_log, "yaz_poll returned res=%d", res);
115 time_t now = time(0);
117 int no_put_events = 0;
120 for (i = 0, p = m_observers; p; p = p->next, i++)
122 enum yaz_poll_mask output_mask = fds[i].output_mask;
125 if (output_mask & yaz_poll_read)
126 mask |= SOCKET_OBSERVE_READ;
128 if (output_mask & yaz_poll_write)
129 mask |= SOCKET_OBSERVE_WRITE;
131 if (output_mask & yaz_poll_except)
132 mask |= SOCKET_OBSERVE_EXCEPT;
136 SocketEvent *event = new SocketEvent;
137 p->last_activity = now;
138 event->observer = p->observer;
142 yaz_log (m_log, "putEvent I/O mask=%d", mask);
144 else if (res == 0 && p->timeout_this == timeout)
146 SocketEvent *event = new SocketEvent;
147 assert (p->last_activity);
148 yaz_log (m_log, "putEvent timeout fd=%d, now = %ld last_activity=%ld timeout=%d",
149 p->fd, now, p->last_activity, p->timeout);
150 p->last_activity = now;
151 event->observer = p->observer;
152 event->event = SOCKET_OBSERVE_TIMEOUT;
158 SocketEvent *event = getEvent();
161 event->observer->socketNotify(event->event);
168 yaz_log(YLOG_WARN, "unhandled socket event. yaz_poll returned %d", res);
169 yaz_log(YLOG_WARN, "no_put_events=%d no_fds=%d i=%d timeout=%d",
170 no_put_events, no_fds, i, timeout);
175 int SocketManager::processEvent()
178 SocketEvent *event = getEvent();
180 yaz_log (m_log, "SocketManager::processEvent manager=%p", this);
183 event->observer->socketNotify(event->event);
189 time_t now = time(0);
192 for (p = m_observers; p; p = p->next)
197 struct yaz_poll_fd *fds = new yaz_poll_fd [no_fds];
198 for (i = 0, p = m_observers; p; p = p->next, i++)
202 if (p->mask & SOCKET_OBSERVE_READ)
203 input_mask += yaz_poll_read;
204 if (p->mask & SOCKET_OBSERVE_WRITE)
205 input_mask += yaz_poll_write;
206 if (p->mask & SOCKET_OBSERVE_EXCEPT)
207 input_mask += yaz_poll_except;
208 if (p->timeout > 0 ||
209 (p->timeout == 0 && (p->mask & SOCKET_OBSERVE_WRITE) == 0))
212 timeout_this = p->timeout;
213 if (p->last_activity)
214 timeout_this -= now - p->last_activity;
216 p->last_activity = now;
217 if (timeout_this < 0 || timeout_this > 2147483646)
219 if (timeout == -1 || timeout_this < timeout)
220 timeout = timeout_this;
221 p->timeout_this = timeout_this;
222 yaz_log (m_log, "SocketManager::select timeout_this=%d",
226 p->timeout_this = -1;
227 fds[i].input_mask = (enum yaz_poll_mask) input_mask;
231 while ((res = yaz_poll(fds, no_fds, timeout, 0)) < 0)
235 yaz_log(YLOG_ERRNO|YLOG_WARN, "yaz_poll");
236 yaz_log(YLOG_WARN, "errno=%d timeout=%d", errno, timeout);
242 inspect_poll_result(res, fds, no_fds, timeout);
249 // n p n p ...... n p n p
252 void SocketManager::putEvent(SocketEvent *event)
254 // put in back of queue
257 m_queue_back->prev = event;
258 assert (m_queue_front);
262 assert (!m_queue_front);
263 m_queue_front = event;
265 event->next = m_queue_back;
267 m_queue_back = event;
270 SocketManager::SocketEvent *SocketManager::getEvent()
272 // get from front of queue
273 SocketEvent *event = m_queue_front;
276 assert (m_queue_back);
277 m_queue_front = event->prev;
280 assert (m_queue_back);
281 m_queue_front->next = 0;
288 void SocketManager::removeEvent(ISocketObserver *observer)
290 SocketEvent *ev = m_queue_back;
293 SocketEvent *ev_next = ev->next;
294 if (observer == ev->observer)
297 ev->prev->next = ev->next;
299 m_queue_back = ev->next;
301 ev->next->prev = ev->prev;
303 m_queue_front = ev->prev;
310 SocketManager::SocketManager()
318 SocketManager::~SocketManager()
325 * c-file-style: "Stroustrup"
326 * indent-tabs-mode: nil
328 * vim: shiftwidth=4 tabstop=8 expandtab