Example #1
0
class Operator:
    OPTYPE = 'Base'
    OPERATIONS_LIST = []

    @classmethod
    def update_operations_list(cls, operations_list):
        base_list = copy.copy(cls.OPERATIONS_LIST)
        cls.OPERATIONS_LIST = []
        cls.OPERATIONS_LIST = base_list + operations_list

    def __init__(self, self_address, home_dir='/tmp/', key_storage=None, \
                    is_init_node=False, node_name='unknown-node', config={}):
        config_file = os.path.join(home_dir, 'node_config')
        Config.load(config_file)
        self.update_config(config, self.OPTYPE)
        self.msg_container = MessageContainer(MC_SIZE)

        self.__lock = threading.RLock()
        self.self_address = self_address
        self.home_dir = home_dir
        self.node_name = node_name

        self.__neighbours = {NT_SUPERIOR: {}, NT_UPPER: {}}

        self.__upper_keep_alives = {}
        self.__superior_keep_alives = {}

        if key_storage:
            cert = key_storage.get_node_cert()
            ckey = key_storage.get_node_cert_key()
        else:
            cert = ckey = None
        self.fri_client = FriClient(bool(cert), cert, ckey)

        self.__fri_agents_manager = WorkersManager(FriAgent, server_name=node_name, \
                    init_params=(self,))
        self.__fri_agents_manager.start_carefully()
        self.__async_requests = self.__fri_agents_manager.get_queue()

        self.__check_neighbours_thread = CheckNeighboursThread(self)
        self.__check_neighbours_thread.setName('%s-CheckNeighbours'%(node_name,))
        self.__check_neighbours_thread.start()

        self.start_datetime = datetime.now()
        self.is_init_node = is_init_node
        self.__session_manager = SessionsManager(home_dir)
        self.__discovery = NeigboursDiscoveryRoutines(self)
        self.__api_workers_mgr = None
        self.__stat = Statistic()
        init_oper_stat = {}
        for opclass in self.OPERATIONS_LIST:
            init_oper_stat[opclass.get_name()] = 0
        self.__null_oper_stat = init_oper_stat

        self.stopped = threading.Event()

    def get_start_datetime(self):
        return self.start_datetime

    def is_stopped(self):
        return self.stopped.is_set()

    def stop(self):
        try:
            logger.info('stopping operator...')
            self.stopped.set()
            self.__check_neighbours_thread.stop()

            uppers = self.get_neighbours(NT_UPPER)
            superiors = self.get_neighbours(NT_SUPERIOR)

            self.__unbind_neighbours(uppers, NT_SUPERIOR)
            self.__unbind_neighbours(superiors, NT_UPPER)
        except Exception, err:
            logger.error('Operator stopping failed. Details: %s'%err)
        finally:
Example #2
0
class Operator:
    OPTYPE = None
    OPERATIONS_LIST = []

    @classmethod
    def update_operations_list(cls, operations_list):
        base_list = copy.copy(cls.OPERATIONS_LIST)
        cls.OPERATIONS_LIST = []
        cls.OPERATIONS_LIST = base_list + operations_list

    def __init__(self, self_address, home_dir='/tmp/', key_storage=None, \
                    is_init_node=False, node_name='unknown-node', config={}):
        self.update_config(config)
        self.msg_container = MessageContainer(MC_SIZE)

        self.__lock = threading.RLock()
        self.__discovery_lock = threading.RLock()
        self.self_address = self_address
        self.home_dir = home_dir
        self.node_name = node_name
        self.__node_version = PluginsManager.get_version(self.OPTYPE)

        self.__neighbours = {NT_SUPERIOR: {}, NT_UPPER: {}}

        self.__upper_keep_alives = {}
        self.__superior_keep_alives = {}

        self.start_datetime = datetime.now()
        self.is_init_node = is_init_node
        self.__discovery = NeigboursDiscoveryRoutines(self)
        self.__api_workers_mgr = None
        self.__stat = Statistic()
        self.__allow_check_neighbours = threading.Event()

        self.__auth_key = self.generate_auth_key()

        self.fri_client = FriClient(key_storage)

        self.__fri_agents_manager = WorkersManager(FriAgent, server_name=node_name, \
                    init_params=(self,))
        self.__fri_agents_manager.start_carefully()
        self.__async_requests = self.__fri_agents_manager.get_queue()

        self.__check_neighbours_thread = CheckNeighboursThread(self)
        self.__check_neighbours_thread.setName('%s-CheckNeighbours'%(node_name,))
        self.__check_neighbours_thread.start()

        init_oper_stat = {}
        for opclass in self.OPERATIONS_LIST:
            init_oper_stat[opclass.get_name()] = 0
        self.__null_oper_stat = init_oper_stat

        self.stopped = threading.Event()

    def get_auth_key(self):
        self.__lock.acquire()
        try:
            return self.__auth_key
        finally:
            self.__lock.release()

    def set_auth_key(self, auth_key):
        self.__lock.acquire()
        try:
            self.__auth_key = auth_key
        finally:
            self.__lock.release()

    def generate_auth_key(self): 
        return hashlib.sha256(str(uuid.uuid4())).hexdigest()

    def get_start_datetime(self):
        return self.start_datetime

    def is_stopped(self):
        return self.stopped.is_set()

    def stop(self):
        try:
            logger.info('stopping operator...')
            self.stopped.set()
            self.__check_neighbours_thread.stop()

            uppers = self.get_neighbours(NT_UPPER)
            superiors = self.get_neighbours(NT_SUPERIOR)

            self.__unbind_neighbours(uppers, NT_SUPERIOR)
            self.__unbind_neighbours(superiors, NT_UPPER)
        except Exception, err:
            logger.error('Operator stopping failed. Details: %s'%err)
        finally: