1 #include <lib/base/ebase.h>
7 #include <lib/base/eerror.h>
8 #include <lib/base/elock.h>
10 eSocketNotifier::eSocketNotifier(eMainloop *context, int fd, int requested, bool startnow): context(*context), fd(fd), state(0), requested(requested)
16 eSocketNotifier::~eSocketNotifier()
21 void eSocketNotifier::start()
26 context.addSocketNotifier(this);
27 state=2; // running but not in poll yet
30 void eSocketNotifier::stop()
33 context.removeSocketNotifier(this);
39 void eTimer::start(long msek, bool singleShot)
45 bSingleShot = singleShot;
47 gettimeofday(&nextActivation, 0);
48 nextActivation.tv_sec -= context.getTimeOffset();
49 // eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_usec, msek);
50 nextActivation += (msek<0 ? 0 : msek);
51 // eDebug("next Activation sec = %d, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
52 context.addTimer(this);
55 void eTimer::startLongTimer( int seconds )
60 bActive = bSingleShot = true;
62 gettimeofday(&nextActivation, 0);
63 nextActivation.tv_sec -= context.getTimeOffset();
64 // eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d sec", this, nextActivation.tv_sec, nextActivation.tv_usec, seconds);
66 nextActivation.tv_sec += seconds;
67 // eDebug("next Activation sec = %d, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
68 context.addTimer(this);
76 context.removeTimer(this);
80 void eTimer::changeInterval(long msek)
82 if (bActive) // Timer is running?
84 context.removeTimer(this); // then stop
85 nextActivation -= interval; // sub old interval
88 bActive=true; // then activate Timer
90 interval = msek; // set new Interval
91 nextActivation += interval; // calc nextActivation
93 context.addTimer(this); // add Timer to context TimerList
96 void eTimer::activate() // Internal Funktion... called from eApplication
98 context.removeTimer(this);
102 nextActivation += interval;
103 context.addTimer(this);
111 void eTimer::addTimeOffset( int offset )
113 nextActivation.tv_sec += offset;
117 ePtrList<eMainloop> eMainloop::existing_loops;
119 void eMainloop::addSocketNotifier(eSocketNotifier *sn)
121 int fd = sn->getFD();
122 ASSERT(notifiers.find(fd) == notifiers.end());
126 void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
128 int fd = sn->getFD();
129 std::map<int,eSocketNotifier*>::iterator i(notifiers.find(fd));
130 if (i != notifiers.end())
131 return notifiers.erase(i);
132 eFatal("removed socket notifier which is not present");
135 int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePyObject additional)
137 int return_reason = 0;
138 /* get current time */
140 if (additional && !PyDict_Check(additional))
141 eFatal("additional, but it's not dict");
143 if (additional && !res)
144 eFatal("additional, but no res");
146 long poll_timeout = -1; /* infinite in case of empty timer list */
148 if (!m_timer_list.empty() || twisted_timeout > 0)
151 if (!m_timer_list.empty())
153 /* process all timers which are ready. first remove them out of the list. */
154 while (!m_timer_list.empty() && (poll_timeout = timeout_usec( m_timer_list.begin()->getNextActivation() ) ) <= 0 )
156 m_timer_list.begin()->activate();
159 if (poll_timeout < 0)
161 else /* convert us to ms */
162 poll_timeout /= 1000;
166 if ((twisted_timeout > 0) && (poll_timeout > 0) && ((unsigned int)poll_timeout > twisted_timeout))
168 poll_timeout = twisted_timeout;
172 int nativecount=notifiers.size(),
177 fdcount += PyDict_Size(additional);
179 // build the poll aray
180 pollfd pfd[fdcount]; // make new pollfd array
181 std::map<int,eSocketNotifier*>::iterator it = notifiers.begin();
183 for (; i < nativecount; ++i, ++it)
185 it->second->state = 1; // running and in poll
186 pfd[i].fd = it->first;
187 pfd[i].events = it->second->getRequested();
194 while (PyDict_Next(additional, &pos, &key, &val)) {
195 pfd[i].fd = PyObject_AsFileDescriptor(key);
196 pfd[i++].events = PyInt_AsLong(val);
201 Py_BEGIN_ALLOW_THREADS
202 ret = ::poll(pfd, fdcount, poll_timeout);
205 ret = ::poll(pfd, fdcount, poll_timeout);
207 /* ret > 0 means that there are some active poll entries. */
212 for (; i < nativecount; ++i)
216 it = notifiers.find(pfd[i].fd);
217 if (it != notifiers.end()
218 && it->second->state == 1) // added and in poll
220 int req = it->second->getRequested();
221 if (pfd[i].revents & req)
222 it->second->activate(pfd[i].revents & req);
223 pfd[i].revents &= ~req;
225 if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
226 eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd, pfd[i].revents);
229 for (; i < fdcount; ++i)
234 *res = PyList_New(0);
235 ePyObject it = PyTuple_New(2);
236 PyTuple_SET_ITEM(it, 0, PyInt_FromLong(pfd[i].fd));
237 PyTuple_SET_ITEM(it, 1, PyInt_FromLong(pfd[i].revents));
238 PyList_Append(*res, it);
245 /* when we got a signal, we get EINTR. */
247 eDebug("poll made error (%m)");
249 return_reason = 2; /* don't assume the timeout has passed when we got a signal */
252 return return_reason;
255 void eMainloop::addTimer(eTimer* e)
257 m_timer_list.insert_in_order(e);
260 void eMainloop::removeTimer(eTimer* e)
262 m_timer_list.remove(e);
265 int eMainloop::iterate(unsigned int twisted_timeout, PyObject **res, ePyObject dict)
271 gettimeofday(&m_twisted_timer, 0);
272 m_twisted_timer += twisted_timeout;
275 /* TODO: this code just became ugly. fix that. */
278 if (m_interrupt_requested)
280 m_interrupt_requested = 0;
290 timeval now, timeout;
291 gettimeofday(&now, 0);
292 m_twisted_timer += time_offset; // apply pending offset
293 if (m_twisted_timer<=now) // timeout
295 timeout = m_twisted_timer - now;
296 to = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
297 // remove pending offset .. it is re-applied in next call of processOneEvent.. applyTimeOffset
298 m_twisted_timer -= time_offset;
300 ret = processOneEvent(to, res, dict);
301 } while ( !ret && !(res && *res) );
306 int eMainloop::runLoop()
308 while (!app_quit_now)
313 void eMainloop::reset()
318 PyObject *eMainloop::poll(ePyObject timeout, ePyObject dict)
325 int twisted_timeout = (timeout == Py_None) ? 0 : PyInt_AsLong(timeout);
327 iterate(twisted_timeout, &res, dict);
331 return PyList_New(0); /* return empty list on timeout */
334 void eMainloop::interruptPoll()
336 m_interrupt_requested = 1;
339 void eMainloop::quit(int ret)
345 void eMainloop::addTimeOffset(int offset)
347 for (ePtrList<eMainloop>::iterator it(existing_loops.begin()); it != existing_loops.end(); ++it )
348 it->addInstanceTimeOffset(offset);
351 void eMainloop::addInstanceTimeOffset(int offset)
353 singleLock s(recalcLock);
354 if (m_timer_list.empty())
359 eDebug("time_offset %d avail.. add new offset %d than new is %d",
360 time_offset, offset, time_offset+offset);
365 void eMainloop::applyTimeOffset()
367 singleLock s(recalcLock);
370 for (ePtrList<eTimer>::iterator it(m_timer_list.begin()); it != m_timer_list.end(); ++it )
371 it->addTimeOffset( time_offset );
372 m_twisted_timer += time_offset;
377 eApplication* eApp = 0;