1
2
3
4
5 """ Ecore based reactor.
6 """
7
8 from brisa.core import log
9 from brisa.core.ireactor import *
10
11
12 try:
13 import ecore
14 __all__ = ('EcoreReactor', )
15 except ImportError:
16 __all__ = ()
17
18 log = log.getLogger('reactor.ecore')
19
20
22
23 _stop_funcs = []
24 _start_funcs = []
25
26 - def add_timer(self, interval, callback, threshold=0):
27 """ Add timer.
28
29 @note: should return an ID assigned to the timer, so that it can be
30 removed by rem_timer().
31 """
32 return ecore.Timer(interval, callback)
33
35 """ Removes a timer.
36 """
37 return timer.delete()
38
39 - def add_fd(self, fd, event_callback, event_type):
40 """ Adds a fd for watch.
41 """
42 condition = None
43 if event_type & EVENT_TYPE_READ:
44 condition = ecore.ECORE_FD_READ
45 if event_type & EVENT_TYPE_WRITE:
46 if not condition:
47 condition = ecore.ECORE_FD_WRITE
48 else:
49 condition = condition | ecore.ECORE_FD_WRITE
50 if event_type & EVENT_TYPE_EXCEPTION:
51 if not condition:
52 condition = ecore.ECORE_FD_ERROR
53 else:
54 condition = condition | ecore.ECORE_FD_ERROR
55
56 return ecore.fd_handler_add(fd, condition, event_callback)
57
59 """ Removes a fd from being watched.
60 """
61 return fd_handler.delete()
62
64 """ Registers a function to be called before entering the STOPPED
65 state.
66
67 @param func: function
68 @type func: callable
69 """
70 if func not in self._stop_funcs:
71 self._stop_funcs.append(func)
72
74 """ Removes a registered function.
75
76 @param func: function
77 @type func: callable
78 """
79 if func in self._stop_funcs:
80 self._stop_funcs.remove(func)
81
83 """ Registers a function to be called before starting the main loop.
84
85 @param func: function
86 @type func: callable
87 """
88 if func not in self._start_funcs:
89 self._start_funcs.append(func)
90
92 """ Removes a registered function.
93
94 @param func: function
95 @type func: callable
96 """
97 if func in self._start_funcs:
98 self._start_funcs.remove(func)
99
101 """ Runs a single iteration of the main loop. Reactor enters the
102 RUNNING state while this method executes.
103 """
104 ecore.main_loop_iterate()
105
107 """ Enters the RUNNING state by running the main loop until
108 main_quit() is called.
109 """
110 self._main_call_before_start_funcs()
111 self.state = REACTOR_STATE_RUNNING
112 ecore.main_loop_begin()
113 self.state = REACTOR_STATE_STOPPED
114 self._main_call_after_stop_funcs()
115
116 - def main_quit(self):
117 """ Terminates the main loop.
118 """
119 ecore.main_loop_quit()
120
122 """ Returns True if the main loop is running
123 """
124 return True if self.state else False
125
127 """ Calls registered functions to be called after the main loop is
128 stopped.
129 """
130 for cb in self._stop_funcs:
131 cb()
132
134 """ Call registered functions to be called before starting the main
135 loop.
136 """
137 for cb in self._start_funcs:
138 cb()
139