Package brisa :: Package core :: Module ireactor
[hide private]
[frames] | no frames]

Source Code for Module brisa.core.ireactor

  1  # Licensed under the MIT license 
  2  # http://opensource.org/licenses/mit-license.php or see LICENSE file. 
  3  # Copyright 2007-2008 Brisa Team <brisa-develop@garage.maemo.org> 
  4   
  5  """ Defines a interface to be implemented by reactors. Also defines constants to 
  6  be used by reactors. 
  7  """ 
  8   
  9   
 10  __all__ = ('ReactorInterface', 'REACTOR_STATE_STOPPED', 
 11             'REACTOR_STATE_RUNNING', 'EVENT_TYPE_READ', 'EVENT_TYPE_WRITE', 
 12             'EVENT_TYPE_EXCEPTION', 'ReactorAlreadyRunningException') 
 13   
 14  from brisa.core.singleton import Singleton 
 15   
 16   
 17  # Reactor states 
 18   
 19  REACTOR_STATE_STOPPED, REACTOR_STATE_RUNNING = range(2) 
 20   
 21  # Event flags may be combined with the OR operator 
 22   
 23  EVENT_TYPE_READ, EVENT_TYPE_WRITE, EVENT_TYPE_EXCEPTION = (1, 2, 4) 
 24   
 25   
 26  # Reactor Exceptions 
 27   
 28   
29 -class ReactorAlreadyRunningException(Exception):
30 pass
31 32
33 -class ReactorInterface(Singleton):
34 """ Basic reactor interface capable of handling fds and timers. 35 """ 36 37 state = REACTOR_STATE_STOPPED 38
39 - def __init__(self):
40 import brisa.core 41 import sys 42 43 if 'brisa.core.reactor' in sys.modules: 44 from brisa.core import log 45 log.warning('reactor already installed') 46 47 brisa.core.reactor = self 48 sys.modules['brisa.core.reactor'] = self
49
50 - def add_timer(self, interval, callback, threshold):
51 """ Add timer. 52 53 @note: should return an ID assigned to the timer, so that it can be 54 removed by rem_timer(). 55 """ 56 raise NotImplementedError('ReactorInterface.add_timer method not'\ 57 ' implemented.')
58
59 - def rem_timer(self, timer_id):
60 """ Removes a timer. 61 """ 62 raise NotImplementedError('ReactorInterface.rem_timer method not'\ 63 ' implemented.')
64
65 - def add_fd(self, fd, event_callback, event_type):
66 """ Adds a fd for watch. 67 """ 68 raise NotImplementedError('ReactorInterface.add_fd method not'\ 69 ' implemented.')
70
71 - def rem_fd(self, fd):
72 """ Removes a fd from being watched. 73 """ 74 raise NotImplementedError('ReactorInterface.rem_fd method not'\ 75 ' implemented.')
76
77 - def add_after_stop_func(self, func):
78 """ Registers a function to be called before entering the STOPPED 79 state. 80 """ 81 raise NotImplementedError('ReactorInterface.add_after_stop_func'\ 82 ' method not implemented.')
83
84 - def rem_after_stop_func(self, func):
85 """ Removes a registered function. 86 """ 87 raise NotImplementedError('ReactorInterface.rem_after_stop_func'\ 88 ' method not implemented.')
89
90 - def add_before_start_func(self, func):
91 """ Registers a function to be called before entering the RUNNING 92 state. 93 """ 94 raise NotImplementedError('ReactorInterface.add_before_start_func'\ 95 ' method not implemented.')
96
97 - def rem_before_start_func(self, func):
98 """ Removes a registered function. 99 """ 100 raise NotImplementedError('ReactorInterface.rem_before_start_func'\ 101 ' method not implemented.')
102
103 - def main_loop_iterate(self):
104 """ Runs a single iteration of the main loop. Reactor enters the 105 RUNNING state while this method executes. 106 """ 107 raise NotImplementedError('ReactorInterface.main_loop_iterate'\ 108 ' method not implemented.')
109
110 - def main(self):
111 """ Enters the RUNNING state by running the main loop until 112 main_quit() is called. 113 """ 114 raise NotImplementedError('ReactorInterface.main'\ 115 ' method not implemented.')
116
117 - def main_quit(self):
118 """ Terminates the main loop. 119 """ 120 raise NotImplementedError('ReactorInterface.main_quit'\ 121 ' method not implemented.')
122
123 - def is_running(self):
124 """ Returns True if the main loop is running. Otherwise returns False. 125 """ 126 raise NotImplementedError('ReactorInterface.is_running'\ 127 ' method not implemented.')
128