make our event loop compatible to my new twisted reactor core
[vuplus_dvbapp] / lib / base / ebase.cpp
1 #include <lib/base/ebase.h>
2
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <errno.h>
6
7 #include <lib/base/eerror.h>
8 #include <lib/base/elock.h>
9
10 eSocketNotifier::eSocketNotifier(eMainloop *context, int fd, int requested, bool startnow): context(*context), fd(fd), state(0), requested(requested)
11 {
12         if (startnow)   
13                 start();
14 }
15
16 eSocketNotifier::~eSocketNotifier()
17 {
18         stop();
19 }
20
21 void eSocketNotifier::start()
22 {
23         if (state)
24                 stop();
25
26         context.addSocketNotifier(this);
27         state=1;
28 }
29
30 void eSocketNotifier::stop()
31 {
32         if (state)
33                 context.removeSocketNotifier(this);
34
35         state=0;
36 }
37
38                                         // timer
39 void eTimer::start(long msek, bool singleShot)
40 {
41         if (bActive)
42                 stop();
43
44         bActive = true;
45         bSingleShot = singleShot;
46         interval = msek;
47         gettimeofday(&nextActivation, 0);
48 //      eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_usec, msek);
49         nextActivation += (msek<0 ? 0 : msek);
50 //      eDebug("next Activation sec = %d, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
51         context.addTimer(this);
52 }
53
54 void eTimer::startLongTimer( int seconds )
55 {
56         if (bActive)
57                 stop();
58
59         bActive = bSingleShot = true;
60         interval = 0;
61         gettimeofday(&nextActivation, 0);
62 //      eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d sec", this, nextActivation.tv_sec, nextActivation.tv_usec, seconds);
63         if ( seconds > 0 )
64                 nextActivation.tv_sec += seconds;
65 //      eDebug("next Activation sec = %d, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
66         context.addTimer(this);
67 }
68
69 void eTimer::stop()
70 {
71         if (bActive)
72         {
73                 bActive=false;
74                 context.removeTimer(this);
75         }
76 }
77
78 void eTimer::changeInterval(long msek)
79 {
80         if (bActive)  // Timer is running?
81         {
82                 context.removeTimer(this);       // then stop
83                 nextActivation -= interval;  // sub old interval
84         }
85         else
86                 bActive=true; // then activate Timer
87
88         interval = msek;                                                // set new Interval
89         nextActivation += interval;             // calc nextActivation
90
91         context.addTimer(this);                         // add Timer to context TimerList
92 }
93
94 void eTimer::activate()   // Internal Funktion... called from eApplication
95 {
96         context.removeTimer(this);
97
98         if (!bSingleShot)
99         {
100                 nextActivation += interval;
101                 context.addTimer(this);
102         }
103         else
104                 bActive=false;
105
106         /*emit*/ timeout();
107 }
108
109 void eTimer::addTimeOffset( int offset )
110 {
111         nextActivation.tv_sec += offset;
112 }
113
114 // mainloop
115 ePtrList<eMainloop> eMainloop::existing_loops;
116
117 void eMainloop::addSocketNotifier(eSocketNotifier *sn)
118 {
119         notifiers.insert(std::pair<int,eSocketNotifier*> (sn->getFD(), sn));
120 }
121
122 void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
123 {
124         for (std::multimap<int,eSocketNotifier*>::iterator i = notifiers.find(sn->getFD());
125                         i != notifiers.end();
126                         ++i)
127                 if (i->second == sn)
128                         return notifiers.erase(i);
129         eFatal("removed socket notifier which is not present");
130 }
131
132 int eMainloop::processOneEvent(unsigned int user_timeout)
133 {
134         int return_reason = 0;
135                 /* get current time */
136         timeval now;
137         gettimeofday(&now, 0);
138         m_now_is_invalid = 0;
139         
140         int poll_timeout = -1; /* infinite in case of empty timer list */
141         
142         if (m_timer_list)
143         {
144                 singleLock s(recalcLock);
145                 poll_timeout = timeval_to_usec(m_timer_list.begin()->getNextActivation() - now);
146                         /* if current timer already passed, don't delay infinite. */
147                 if (poll_timeout < 0)
148                         poll_timeout = 0;
149                         
150                         /* convert us to ms */
151                 poll_timeout /= 1000;
152         }
153         
154         if ((user_timeout > 0) && (poll_timeout > user_timeout))
155         {
156                 poll_timeout = user_timeout;
157                 return_reason = 1;
158         }
159         
160         int ret = 0;
161         
162         if (poll_timeout)
163         {
164
165                 std::multimap<int,eSocketNotifier*>::iterator it;
166                 std::map<int,int> fd_merged;
167                 std::map<int,int>::const_iterator fd_merged_it;
168                 
169                 for (it = notifiers.begin(); it != notifiers.end(); ++it)
170                         fd_merged[it->first] |= it->second->getRequested();
171                 
172                 fd_merged_it = fd_merged.begin();
173                 
174                 int fdcount = fd_merged.size();
175
176                         // build the poll aray
177                 pollfd* pfd = new pollfd[fdcount];  // make new pollfd array
178                 
179                 for (int i=0; i < fdcount; i++, fd_merged_it++)
180                 {
181                         pfd[i].fd = fd_merged_it->first;
182                         pfd[i].events = fd_merged_it->second;
183                 }
184
185                 ret = poll(pfd, fdcount, poll_timeout);
186                 
187                         /* ret > 0 means that there are some active poll entries. */
188                 if (ret > 0)
189                 {
190                         return_reason = 0;
191                         for (int i=0; i < fdcount ; i++)
192                         {
193                                 it = notifiers.begin();
194                                 
195                                 int handled = 0;
196                                 
197                                 std::multimap<int,eSocketNotifier*>::iterator 
198                                         l = notifiers.lower_bound(pfd[i].fd),
199                                         u = notifiers.upper_bound(pfd[i].fd);
200                                 
201                                 ePtrList<eSocketNotifier> n;
202                                 
203                                 for (; l != u; ++l)
204                                         n.push_back(l->second);
205                                 
206                                 for (ePtrList<eSocketNotifier>::iterator li(n.begin()); li != n.end(); ++li)
207                                 {
208                                         int req = li->getRequested();
209                                         
210                                         handled |= req;
211                                 
212                                         if (pfd[i].revents & req)
213                                                 (*li)->activate(pfd[i].revents);
214                                 }
215                                 if ((pfd[i].revents&~handled) & (POLLERR|POLLHUP|POLLNVAL))
216                                                 eFatal("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd, pfd[i].revents);
217                         }
218                         
219                         ret = 1; /* poll did not timeout. */
220                 } else if (ret < 0)
221                 {
222                                 /* when we got a signal, we get EINTR. */
223                         if (errno != EINTR)
224                                 eDebug("poll made error (%m)");
225                         else
226                         {
227                                 return_reason = 2;
228                                 ret = -1; /* don't assume the timeout has passed when we got a signal */
229                         }
230                 }
231                 delete [] pfd;
232         }
233         
234                 /* when we not processed anything, check timers. */
235         if (!ret)
236         {
237                         /* we know that this time has passed. */
238                 now += poll_timeout;
239                 
240                 singleLock s(recalcLock);
241
242                         /* this will never change while we have the recalcLock */
243                         /* we can savely return here, the timer will be re-checked soon. */
244                 if (m_now_is_invalid)
245                         return 0;
246
247                         /* process all timers which are ready. first remove them out of the list. */
248                 while ((!m_timer_list.empty()) && (m_timer_list.begin()->getNextActivation() <= now))
249                         m_timer_list.begin()->activate();
250         }
251         
252         return return_reason;
253 }
254
255 void eMainloop::addTimer(eTimer* e)
256 {
257         m_timer_list.insert_in_order(e);
258 }
259
260 void eMainloop::removeTimer(eTimer* e)
261 {
262         m_timer_list.remove(e);
263 }
264
265 int eMainloop::iterate(unsigned int user_timeout)
266 {
267         int ret = 0;
268         
269         do
270         { 
271                 if (app_quit_now) break; 
272                 ret = processOneEvent(user_timeout);
273         } while (ret == 0);
274         
275         return ret;
276 }
277
278 int eMainloop::runLoop()
279 {
280         while (!app_quit_now)
281                 iterate();
282         return retval;
283 }
284
285 void eMainloop::quit(int ret)
286 {
287         retval = ret;
288         app_quit_now = true;
289 }
290
291 void eMainloop::addTimeOffset(int offset)
292 {
293         for (ePtrList<eMainloop>::iterator it(eMainloop::existing_loops)
294                 ;it != eMainloop::existing_loops.end(); ++it)
295         {
296                 singleLock s(it->recalcLock);
297                 it->m_now_is_invalid = 1;
298                 for (ePtrList<eTimer>::iterator tit = it->m_timer_list.begin(); tit != it->m_timer_list.end(); ++tit )
299                         tit->addTimeOffset(offset);
300         }
301 }
302
303 eApplication* eApp = 0;