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);
174 int SocketManager::processEvent()
177 SocketEvent *event = getEvent();
179 yaz_log (m_log, "SocketManager::processEvent manager=%p", this);
182 event->observer->socketNotify(event->event);
188 time_t now = time(0);
191 for (p = m_observers; p; p = p->next)
196 struct yaz_poll_fd *fds = new yaz_poll_fd [no_fds];
197 for (i = 0, p = m_observers; p; p = p->next, i++)
201 if (p->mask & SOCKET_OBSERVE_READ)
202 input_mask += yaz_poll_read;
203 if (p->mask & SOCKET_OBSERVE_WRITE)
204 input_mask += yaz_poll_write;
205 if (p->mask & SOCKET_OBSERVE_EXCEPT)
206 input_mask += yaz_poll_except;
207 if (p->timeout > 0 ||
208 (p->timeout == 0 && (p->mask & SOCKET_OBSERVE_WRITE) == 0))
211 timeout_this = p->timeout;
212 if (p->last_activity)
213 timeout_this -= now - p->last_activity;
215 p->last_activity = now;
216 if (timeout_this < 0 || timeout_this > 2147483646)
218 if (timeout == -1 || timeout_this < timeout)
219 timeout = timeout_this;
220 p->timeout_this = timeout_this;
221 yaz_log (m_log, "SocketManager::select timeout_this=%d",
225 p->timeout_this = -1;
226 fds[i].input_mask = (enum yaz_poll_mask) input_mask;
230 while ((res = yaz_poll(fds, no_fds, timeout, 0)) < 0)
234 yaz_log(YLOG_ERRNO|YLOG_WARN, "yaz_poll");
235 yaz_log(YLOG_WARN, "errno=%d timeout=%d", errno, timeout);
241 inspect_poll_result(res, fds, no_fds, timeout);
248 // n p n p ...... n p n p
251 void SocketManager::putEvent(SocketEvent *event)
253 // put in back of queue
256 m_queue_back->prev = event;
257 assert (m_queue_front);
261 assert (!m_queue_front);
262 m_queue_front = event;
264 event->next = m_queue_back;
266 m_queue_back = event;
269 SocketManager::SocketEvent *SocketManager::getEvent()
271 // get from front of queue
272 SocketEvent *event = m_queue_front;
275 assert (m_queue_back);
276 m_queue_front = event->prev;
279 assert (m_queue_back);
280 m_queue_front->next = 0;
287 void SocketManager::removeEvent(ISocketObserver *observer)
289 SocketEvent *ev = m_queue_back;
292 SocketEvent *ev_next = ev->next;
293 if (observer == ev->observer)
296 ev->prev->next = ev->next;
298 m_queue_back = ev->next;
300 ev->next->prev = ev->prev;
302 m_queue_front = ev->prev;
309 SocketManager::SocketManager()
317 SocketManager::~SocketManager()
324 * c-file-style: "Stroustrup"
325 * indent-tabs-mode: nil
327 * vim: shiftwidth=4 tabstop=8 expandtab