def receiveMsg_StartMessage(self, message: StartMessage, sender: ActorAddress): """ When receiving a StartMessage : - if the actor is already initialized, answer with a ErrorMessage - otherwise initialize the actor with the abstract method _initialization and answer with an OKMessage """ self.name = message.name self.log_debug('received message ' + str(message)) self.parent = sender if self.initialized: self.send(sender, ErrorMessage(self.name, 'Actor already initialized')) return if not isinstance(message, self.start_message_cls): self.send( sender, ErrorMessage( self.name, 'use ' + self.start_message_cls.__name__ + ' instead of StartMessage')) return try: self._initialization(message) except InitializationException as exn: self.send(sender, ErrorMessage(self.name, exn.msg)) self.send(self.myAddress, ActorExitRequest()) return self.initialized = True self.send(sender, OKMessage(self.name)) self.log_info(self.name + ' started')
def run(self): """ Read data from Database and send it to the dispatchers. If there is no more data, send a kill message to every dispatcher. If stream mode is disable, kill the actor. :param None msg: None. """ while self.state.alive: try: report = self._pull_database() dispatchers = self._get_dispatchers(report) for dispatcher in dispatchers: dispatcher.send_data(report) except NoReportExtractedException: time.sleep(self.state.timeout_puller / 1000) if not self.state.stream_mode: return except FilterUselessError: self.state.actor.send_control( ErrorMessage("FilterUselessError")) return
def test_dispatcher_start_handler(self): """ Test the StartHandler of DispatcherActor """ # Define DispatcherState fake_socket_interface = get_fake_socket_interface() dispatcher_state = DispatcherState(Actor._initial_behaviour, fake_socket_interface, Mock(), None, None) assert dispatcher_state.initialized is False # Define StartHandler start_handler = StartHandler() # Test Random message when state is not initialized to_send = [ OKMessage(), ErrorMessage("Error"), HWPCReport("test", "test", "test", {}) ] for msg in to_send: start_handler.handle(msg, dispatcher_state) assert fake_socket_interface.method_calls == [] assert dispatcher_state.initialized is False # Try to initialize the state start_handler.handle(StartMessage(), dispatcher_state) assert dispatcher_state.initialized is True
def test_pusher_start_handler(self): """ Test the StartHandler of PusherActor """ # Define PusherState fake_database = get_fake_db() fake_socket_interface = get_fake_socket_interface() pusher_state = PusherState(Actor._initial_behaviour, fake_socket_interface, fake_database, mock.Mock()) assert pusher_state.initialized is False # Define StartHandler start_handler = PusherStartHandler() # Test Random message when state is not initialized to_send = [OKMessage(), ErrorMessage("Error"), create_report_root({})] for msg in to_send: start_handler.handle(msg, pusher_state) assert fake_database.method_calls == [] assert fake_socket_interface.method_calls == [] assert pusher_state.initialized is False # Try to initialize the state start_handler.handle(StartMessage(), pusher_state) assert pusher_state.initialized is True
def test_dispatcher_start_handler(self): """ Test the StartHandler of DispatcherActor """ # Define DispatcherState dispatcher_state = DispatcherState(Mock(), None, None) assert dispatcher_state.initialized is False # Define StartHandler start_handler = StartHandler(dispatcher_state) # Test Random message when state is not initialized to_send = [ OKMessage(), ErrorMessage("Error"), HWPCReport("test", "test", "test", {}) ] for msg in to_send: start_handler.handle(msg) assert dispatcher_state.initialized is False # Try to initialize the state start_handler.handle(StartMessage()) assert dispatcher_state.initialized is True
def handle(self, msg): """ Read data from Database and send it to the dispatchers. If there is no more data, send a kill message to every dispatcher. If stream mode is disable, kill the actor. :param None msg: None. """ try: (report, dispatchers) = self._get_report_dispatcher() # for sleeping mode self.state.actor.socket_interface.timeout = self.state.timeout_basic except NoReportExtractedException: if not self.state.stream_mode: self.state.alive = False # for sleeping mode if self.state.counter < 10: self.state.counter += 1 else: self.state.actor.socket_interface.timeout = self.state.timeout_sleeping return except FilterUselessError: self.state.alive = False self.state.actor.send_control(ErrorMessage("FilterUselessError")) return # Send to the dispatcher if it's not None for dispatcher in dispatchers: if dispatcher is not None: dispatcher.send_data(report)
def test_puller_start_handler(self): """ Test the StartHandler of PullerActor """ # Define PullerState fake_database = get_fake_db() fake_socket_interface = get_fake_socket_interface() fake_filter = get_fake_filter() puller_state = PullerState(Mock(), fake_database, fake_filter, HWPCModel(), False, 100) puller_state.actor.receive_control = Mock(return_value=None) assert puller_state.initialized is False # Define StartHandler start_handler = PullerStartHandler(puller_state, 0.1) # Test Random message when state is not initialized to_send = [OKMessage(), ErrorMessage("Error"), create_report_root({})] for msg in to_send: start_handler.handle(msg) assert fake_database.method_calls == [] assert fake_socket_interface.method_calls == [] assert fake_filter.method_calls == [] assert puller_state.initialized is False # Try to initialize the state start_handler.handle(StartMessage()) assert puller_state.initialized is True
def _database_connection(self): try: self.state.database.connect() self.state.database_it = self.state.database.iter(self.state.report_model, self.state.stream_mode) except DBError as error: self.state.actor.send_control(ErrorMessage(error.msg)) self.state.alive = False
def initialization(self): """ Initialize the output database """ try: self.state.database.connect() except DBError as error: self.state.actor.send_control(ErrorMessage(error.msg))
def handle(self, msg): if self.state.initialized: self.state.actor.send_control( ErrorMessage('Actor already initialized')) return if not isinstance(msg, StartMessage): return try: self.initialization() except InitiatizationException as e: self.state.actor.send_control(ErrorMessage(e.msg)) if self.state.alive: self.state.initialized = True self.state.actor.send_control(OKMessage()) self.pull_db()
def _connect(self): try: #self.state.database.connect() self.loop.run_until_complete(self.state.database.connect()) self.state.database_it = self.state.database.iter( self.state.report_model, self.state.stream_mode) except DBError as error: self.state.actor.send_control(ErrorMessage(error.msg)) self.state.alive = False
def receiveUnrecognizedMessage(self, message: Any, sender: ActorAddress): """ When receiving a message with a type that can't be handle, the actor answer with an ErrorMessage """ self.log_warning('received unrecognized message : ' + str(message)) self.send( sender, ErrorMessage( self.name, "did not recognize the message type : " + str(type(message))))
def initialization(self, state): """ Initialize the output database :param powerapi.State state: State of the actor. :rtype powerapi.State: the new state of the actor """ try: state.database.connect() except DBError as error: state.socket_interface.send_control(ErrorMessage(error.msg)) return state return state
def test_pusher_power_handler(self): """ Test the PowerHandler of PusherActor """ # Define PusherState fake_database = get_fake_db() fake_socket_interface = get_fake_socket_interface() pusher_state = PusherState(Actor._initial_behaviour, fake_socket_interface, fake_database, mock.Mock()) assert pusher_state.initialized is False # Define PowerHandler power_handler = PowerHandler() # Test Random message when state is not initialized to_send = [OKMessage(), ErrorMessage("Error"), create_report_root({})] for msg in to_send: power_handler.handle(msg, pusher_state) assert pusher_state.database.method_calls == [] assert pusher_state.initialized is False pusher_state.initialized = True # Test Random message when state is initialized to_send = [OKMessage(), ErrorMessage("Error"), create_report_root({})] for msg in to_send: power_handler.handle(msg, pusher_state) assert pusher_state.database.method_calls == [] # Test with a PowerMessage for _ in range(101): power_handler.handle( PowerReport("10", "test", "test", "test", "test"), pusher_state) assert len(pusher_state.buffer) == 101
def initialization(self): """ Initialize the output database """ try: if (self.state.asynchrone): loop = asyncio.get_event_loop() loop.run_until_complete(self.state.database.connect()) AsyncLoop(loop) else: self.state.database.connect() except DBError as error: self.state.actor.send_control(ErrorMessage(error.msg)) self.state.alive = False return
def initialization(self): """ Initialize the database and connect all dispatcher to the socket_interface """ try: self.state.database.connect() self.state.database_it = self.state.database.iter( self.state.report_model, self.state.stream_mode) except DBError as error: self.state.actor.send_control(ErrorMessage(error.msg)) self.state.alive = False # Connect to all dispatcher for _, dispatcher in self.state.report_filter.filters: dispatcher.connect_data()
def handle(self, msg): """ Allow to initialize the state of the actor, then reply to the control socket. :param powerapi.StartMessage msg: Message that initialize the actor """ if self.state.initialized: self.state.actor.send_control( ErrorMessage('Actor already initialized')) return if not isinstance(msg, StartMessage): return self.initialization() self.state.initialized = True self.state.actor.send_control(OKMessage())
def initialization(self, state): """ Initialize the database and connect all dispatcher to the socket_interface :param State state: State of the actor. :rtype powerapi.State: the new state of the actor """ try: state.database.connect() state.database_it = iter(state.database) except DBError as error: state.socket_interface.send_control(ErrorMessage(error.msg)) state.alive = False return state # Connect to all dispatcher for _, dispatcher in state.report_filter.filters: dispatcher.connect_data() return state
def test_pusher_power_handler(self): """ Test the ReportHandler of PusherActor """ # Define PusherState fake_database = get_fake_db() fake_socket_interface = get_fake_socket_interface() pusher_state = PusherState(Mock(), fake_database, HWPCModel()) pusher_state.actor.socket_interface = fake_socket_interface assert pusher_state.initialized is False # Define PowerHandler power_handler = ReportHandler(pusher_state) # Test Random message when state is not initialized to_send = [OKMessage(), ErrorMessage("Error"), create_report_root({})] for msg in to_send: power_handler.handle_message(msg) assert pusher_state.database.method_calls == [] assert pusher_state.initialized is False pusher_state.initialized = True
def handle(self, msg, state): """ Allow to initialize the state of the actor, then reply to the control socket. :param powerapi.StartMessage msg: Message that initialize the actor :param powerapi.State state: State of the actor :rtype powerapi.State: the new state of the actor """ if state.initialized: state.socket_interface.send_control( ErrorMessage('Actor already initialized')) return state if not isinstance(msg, StartMessage): return state state = self.initialization(state) state.initialized = True state.socket_interface.send_control(OKMessage()) return state
def receive_control(self, timeout=None): return ErrorMessage('error')