Simplify use of iconv for MARC records
[yazpp-moved-to-github.git] / src / yaz-socket-manager.cpp
1 /*
2  * Copyright (c) 1998-2004, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-socket-manager.cpp,v 1.27 2004-02-26 23:42:27 adam Exp $
6  */
7 #include <assert.h>
8 #ifdef WIN32
9 #include <winsock.h>
10 #else
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14 #endif
15 #include <errno.h>
16 #include <string.h>
17
18 #include <yaz/log.h>
19 #include <yaz++/socket-manager.h>
20
21 Yaz_SocketManager::YazSocketEntry **Yaz_SocketManager::lookupObserver(
22     IYazSocketObserver *observer)
23 {
24     YazSocketEntry **se;
25     
26     for (se = &m_observers; *se; se = &(*se)->next)
27         if ((*se)->observer == observer)
28             break;
29     return se;
30 }
31
32 void Yaz_SocketManager::addObserver(int fd, IYazSocketObserver *observer)
33 {
34     YazSocketEntry *se;
35
36     se = *lookupObserver(observer);
37     if (!se)
38     {
39         se = new YazSocketEntry;
40         se->next= m_observers;
41         m_observers = se;
42         se->observer = observer;
43     }
44     se->fd = fd;
45     se->mask = 0;
46     se->last_activity = 0;
47     se->timeout = -1;
48 }
49
50 void Yaz_SocketManager::deleteObserver(IYazSocketObserver *observer)
51 {
52     YazSocketEntry **se = lookupObserver(observer);
53     if (*se)
54     {
55         removeEvent (observer);
56         YazSocketEntry *se_tmp = *se;
57         *se = (*se)->next;
58         delete se_tmp;
59     }
60 }
61
62 void Yaz_SocketManager::deleteObservers()
63 {
64     YazSocketEntry *se = m_observers;
65     
66     while (se)
67     {
68         YazSocketEntry *se_next = se->next;
69         delete se;
70         se = se_next;
71     }
72     m_observers = 0;
73 }
74
75 void Yaz_SocketManager::maskObserver(IYazSocketObserver *observer, int mask)
76 {
77     YazSocketEntry *se;
78
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);
83
84     se = *lookupObserver(observer);
85     if (se)
86         se->mask = mask;
87 }
88
89 void Yaz_SocketManager::timeoutObserver(IYazSocketObserver *observer,
90                                         int timeout)
91 {
92     YazSocketEntry *se;
93
94     se = *lookupObserver(observer);
95     if (se)
96         se->timeout = timeout;
97 }
98
99 int Yaz_SocketManager::processEvent()
100 {
101     YazSocketEntry *p;
102     YazSocketEvent *event = getEvent();
103     int timeout = -1;
104     yaz_log (m_log, "Yaz_SocketManager::processEvent manager=%p", this);
105     if (event)
106     {
107         event->observer->socketNotify(event->event);
108         delete event;
109         return 1;
110     }
111
112     fd_set in, out, except;
113     int res;
114     int max = 0;
115     int no = 0;
116
117     FD_ZERO(&in);
118     FD_ZERO(&out);
119     FD_ZERO(&except);
120
121     time_t now = time(0);
122     for (p = m_observers; p; p = p->next)
123     {
124         int fd = p->fd;
125         if (p->mask)
126             no++;
127         if (p->mask & YAZ_SOCKET_OBSERVE_READ)
128         {
129             yaz_log (m_log, "Yaz_SocketManager::select fd=%d read", fd);
130             FD_SET(fd, &in);
131         }
132         if (p->mask & YAZ_SOCKET_OBSERVE_WRITE)
133         {
134             yaz_log (m_log, "Yaz_SocketManager::select fd=%d write", fd);
135             FD_SET(fd, &out);
136         }
137         if (p->mask & YAZ_SOCKET_OBSERVE_EXCEPT)
138         {
139             yaz_log (m_log, "Yaz_SocketManager::select fd=%d except", fd);
140             FD_SET(fd, &except);
141         }
142         if (fd > max)
143             max = fd;
144         if (p->timeout > 0 ||
145             (p->timeout == 0 && (p->mask & YAZ_SOCKET_OBSERVE_WRITE) == 0))
146         {
147             int timeout_this;
148             timeout_this = p->timeout;
149             if (p->last_activity)
150                 timeout_this -= now - p->last_activity;
151             else
152                 p->last_activity = now;
153             if (timeout_this < 0 || timeout_this > 2147483646)
154                 timeout_this = 0;
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", 
159                      p->timeout_this);
160         }
161     }
162     if (!no)
163     {
164         yaz_log (m_log, "no pending events return 0");
165         if (!m_observers)
166             yaz_log (m_log, "no observers");
167         return 0;
168     }
169
170     struct timeval to;
171     to.tv_sec = timeout;
172     to.tv_usec = 0;
173     
174     yaz_log (m_log, "Yaz_SocketManager::select begin no=%d timeout=%d",
175              no, timeout);
176     int pass = 0;
177     while ((res = select(max + 1, &in, &out, &except,
178                          timeout== -1 ? 0 : &to)) < 0)
179         if (errno != EINTR)
180         {
181             yaz_log(LOG_ERRNO|LOG_WARN, "select");
182             yaz_log(LOG_WARN, "errno=%d max=%d timeout=%d",
183                              errno, max, timeout);
184             if (++pass > 10)
185                 return -1;
186         }
187     yaz_log(m_log, "select returned res=%d", res);
188     now = time(0);
189     for (p = m_observers; p; p = p->next)
190     {
191         int fd = p->fd;
192         int mask = 0;
193         if (FD_ISSET(fd, &in))
194             mask |= YAZ_SOCKET_OBSERVE_READ;
195
196         if (FD_ISSET(fd, &out))
197             mask |= YAZ_SOCKET_OBSERVE_WRITE;
198
199         if (FD_ISSET(fd, &except))
200             mask |= YAZ_SOCKET_OBSERVE_EXCEPT;
201         
202         if (mask)
203         {
204             YazSocketEvent *event = new YazSocketEvent;
205             p->last_activity = now;
206             event->observer = p->observer;
207             event->event = mask;
208             putEvent (event);
209
210             yaz_log (m_log, "putEvent I/O mask=%d", mask);
211         }
212         else if (
213             (p->timeout > 0 ||
214              (p->timeout == 0 && (p->mask & YAZ_SOCKET_OBSERVE_WRITE) == 0))
215             &&
216             (now - p->last_activity) >= p->timeout)
217         {
218             YazSocketEvent *event = new YazSocketEvent;
219             assert (p->last_activity);
220             yaz_log (m_log, "putEvent timeout, now = %ld last_activity=%ld timeout=%d",
221                      now, p->last_activity, p->timeout);
222             p->last_activity = now;
223             event->observer = p->observer;
224             event->event = YAZ_SOCKET_OBSERVE_TIMEOUT;
225             putEvent (event);
226         }
227     }
228     if ((event = getEvent()))
229     {
230         event->observer->socketNotify(event->event);
231         delete event;
232         return 1;
233     }
234     yaz_log (LOG_WARN, "unhandled event in processEvent");
235     return 1;
236 }
237
238
239 //    n p    n p  ......   n p    n p
240 //   front                        back
241
242 void Yaz_SocketManager::putEvent(YazSocketEvent *event)
243 {
244     // put in back of queue
245     if (m_queue_back)
246     {
247         m_queue_back->prev = event;
248         assert (m_queue_front);
249     }
250     else
251     {
252         assert (!m_queue_front);
253         m_queue_front = event;
254     }
255     event->next = m_queue_back;
256     event->prev = 0;
257     m_queue_back = event;
258 }
259
260 Yaz_SocketManager::YazSocketEvent *Yaz_SocketManager::getEvent()
261 {
262     // get from front of queue
263     YazSocketEvent *event = m_queue_front;
264     if (!event)
265         return 0;
266     assert (m_queue_back);
267     m_queue_front = event->prev;
268     if (m_queue_front)
269     {
270         assert (m_queue_back);
271         m_queue_front->next = 0;
272     }
273     else
274         m_queue_back = 0;
275     return event;
276 }
277
278 void Yaz_SocketManager::removeEvent(IYazSocketObserver *observer)
279 {
280     YazSocketEvent *ev = m_queue_back;
281     while (ev)
282     {
283         YazSocketEvent *ev_next = ev->next;
284         if (observer == ev->observer)
285         {
286             if (ev->prev)
287                 ev->prev->next = ev->next;
288             else
289                 m_queue_back = ev->next;
290             if (ev->next)
291                 ev->next->prev = ev->prev;
292             else
293                 m_queue_front = ev->prev;
294             delete ev;
295         }
296         ev = ev_next;
297     }
298 }
299
300 Yaz_SocketManager::Yaz_SocketManager()
301 {
302     m_observers = 0;
303     m_queue_front = 0;
304     m_queue_back = 0;
305     m_log = LOG_DEBUG;
306 }
307
308 Yaz_SocketManager::~Yaz_SocketManager()
309 {
310     deleteObservers();
311 }