Ejemplo n.º 1
0
    def __init_state_machine(self):
        '''
        Build the state machine
        '''
        states = [
            {
                'name': 'transition_main'
            },
            {
                'name': 'transition_diverging'
            },
            {
                'name': 'diverging'
            },
            {
                'name': 'main'
            },
        ]

        transitions = [
            {
                'trigger': '_set_main',
                'source': 'diverging',
                'dest': 'transition_main'
            },
            {
                'trigger': '_set_diverging',
                'source': 'main',
                'dest': 'transition_diverging'
            },
            {
                'trigger': '_motion_complete_main',
                'source': 'transition_main',
                'dest': 'main'
            },
            {
                'trigger': '_motion_complete_diverging',
                'source': 'transition_diverging',
                'dest': 'diverging'
            },
        ]

        Machine.__init__(self,
                         states=states,
                         transitions=transitions,
                         initial='transition_main',
                         auto_transitions=False,
                         after_state_change='_update_state',
                         ignore_invalid_triggers=True)
Ejemplo n.º 2
0
    def __init_state_machine(self, randomize):
        '''
        '''
        if randomize:
            self.discovery_timeout = random.uniform(
                (self.discovery_timeout *
                 (1 - NetworkManager.DISCOVERY_TIMEOUT_RAND_FACTOR)),
                (self.discovery_timeout *
                 (1 + NetworkManager.DISCOVERY_TIMEOUT_RAND_FACTOR)))

        self.__STATES = [
            {
                'name': 'initialized'
            },
            {
                'name': 'searching',
                'timeout': self.discovery_timeout,
                'on_timeout': '_start_server',
                'on_enter': '_start_client'
            },
            {
                'name': 'connected'
            },
            {
                'name': 'disconnecting',
                'on_enter': '_stop'
            },
            {
                'name': 'stopping',
                'on_enter': '_stop'
            },
        ]

        self.__TRANSITIONS = [{
            'trigger': 'start',
            'source': 'initialized',
            'dest': 'searching'
        }, {
            'trigger': '_connected',
            'source': 'searching',
            'dest': 'connected'
        }, {
            'trigger': '_disconnected',
            'source': 'connected',
            'dest': 'disconnecting'
        }, {
            'trigger': 'stop',
            'source': ['searching', 'connected'],
            'dest': 'stopping'
        }, {
            'trigger': '_stopped',
            'source': 'stopping',
            'dest': 'initialized'
        }, {
            'trigger': '_stopped',
            'source': 'disconnecting',
            'dest': 'searching'
        }]

        Machine.__init__(self,
                         states=self.__STATES,
                         transitions=self.__TRANSITIONS,
                         initial='initialized',
                         auto_transitions=False,
                         after_state_change='_update_connection_state',
                         ignore_invalid_triggers=True)
    def __init__(self,
                 endpoints,
                 client,
                 server=None,
                 discovery_timeout=DISCOVERY_TIMEOUT_S,
                 randomize_timeout=True):
        """Create a network manager"""
        self.__logger = logging.getLogger('network_manager')

        if randomize_timeout:
            self.discovery_timeout = random.randint(
                int(discovery_timeout *
                    (1 - NetworkManager.DISCOVERY_TIMEOUT_RAND_FACTOR)),
                int(discovery_timeout *
                    (1 + NetworkManager.DISCOVERY_TIMEOUT_RAND_FACTOR)))
        else:
            self.discovery_timeout = discovery_timeout

        self.__STATES = [
            {
                'name': 'initializing',
                'on_enter': '_stop'
            },
            {
                'name': 'searching',
                'timeout': self.discovery_timeout,
                'on_timeout': '_start_server',
                'on_enter': '_start_client'
            },
            {
                'name': 'connected'
            },
        ]

        self.__TRANSITIONS = [{
            'trigger': 'search',
            'source': 'initializing',
            'dest': 'searching'
        }, {
            'trigger': 'connected',
            'source': 'searching',
            'dest': 'connected'
        }, {
            'trigger': 'disconnected',
            'source': 'connected',
            'dest': 'searching'
        }, {
            'trigger': 'shutdown',
            'source': '*',
            'dest': 'initializing'
        }]

        Machine.__init__(self,
                         states=self.__STATES,
                         transitions=self.__TRANSITIONS,
                         initial='initializing',
                         auto_transitions=False)

        # Required to provide a client
        if None == client:
            raise AttributeError('Client must be specified')

        self.__roles = {'client': client, 'server': server}

        self.__role = 'client'

        if type(endpoints) is not list:
            self.__endpoints = [endpoints]
        else:
            self.__endpoints = endpoints

        self.__endpoints = [
            endp for endp in self.__endpoints if endp is not None
        ]

        if not len(self.__endpoints):
            raise AttributeError('No endpoints specified')