Exemple #1
0
    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')
Exemple #2
0
    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
Exemple #4
0
    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
Exemple #5
0
    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
Exemple #6
0
    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
Exemple #8
0
    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
Exemple #9
0
 def initialization(self):
     """
     Initialize the output database
     """
     try:
         self.state.database.connect()
     except DBError as error:
         self.state.actor.send_control(ErrorMessage(error.msg))
Exemple #10
0
    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()
Exemple #11
0
    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
Exemple #12
0
 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))))
Exemple #13
0
    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
Exemple #14
0
    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
Exemple #15
0
 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
Exemple #16
0
    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()
Exemple #17
0
    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())
Exemple #18
0
    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
Exemple #19
0
    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
Exemple #20
0
    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
Exemple #21
0
 def receive_control(self, timeout=None):
     return ErrorMessage('error')