예제 #1
0
파일: ALPSCTP.py 프로젝트: peterwyj/WSDT
 def __init__(self,
              ctpc_received_callback,
              ctpc_send_callback,
              node_updated_inform_callback):
     self.__inform_ctpc_received = ctpc_received_callback
     self.__send_ctpc = ctpc_send_callback
     self.__node_update_inform_callback = node_updated_inform_callback
     self.__wait_response_nodes = {}
     self.__wait_lock = ALPSThread.allocate_lock()
     self.__ocm_table = OCMNetworkTable(self)
     self.__CTPC_MAP = {
            CTPCommandParser.COMMANDSTR_REPORTNODE:  self.__PROCESS_REPORTNODE,
            CTPCommandParser.COMMANDSTR_UPDATENODE:  self.__PROCESS_UPDATENODE,
            CTPCommandParser.COMMANDSTR_REGISTER:    self.__PROCESS_REGISTER,
            CTPCommandParser.COMMANDSTR_UNREGISTER:  self.__PROCESS_UNREGISTER,
            CTPCommandParser.COMMANDSTR_START:       self.__PROCESS_CONTROL,
            CTPCommandParser.COMMANDSTR_STOP:        self.__PROCESS_CONTROL,
            CTPCommandParser.COMMANDSTR_COMMAND:     self.__PROCESS_CONTROL,
            CTPCommandParser.COMMANDSTR_POST:        self.__PROCESS_POST,
            CTPCommandParser.COMMANDSTR_RESPONSE:    self.__PROCESS_RESPONSE,
            CTPCommandParser.COMMANDSTR_EVENT:       self.__PROCESS_EVENT, }
     self.__node_manager = NodeDiscover(self.__do_update_node)
     self.__node_manager.StartDiscover()
예제 #2
0
파일: ALPSCTP.py 프로젝트: peterwyj/WSDT
class ALPSCTP:
    NODE_SPLIT = ";"
    CTP_TIMEOUT = 120
    WAIT_CONTROLLER_TIMEOUT = 10
    def __init__(self,
                 ctpc_received_callback,
                 ctpc_send_callback,
                 node_updated_inform_callback):
        self.__inform_ctpc_received = ctpc_received_callback
        self.__send_ctpc = ctpc_send_callback
        self.__node_update_inform_callback = node_updated_inform_callback
        self.__wait_response_nodes = {}
        self.__wait_lock = ALPSThread.allocate_lock()
        self.__ocm_table = OCMNetworkTable(self)
        self.__CTPC_MAP = {
               CTPCommandParser.COMMANDSTR_REPORTNODE:  self.__PROCESS_REPORTNODE,
               CTPCommandParser.COMMANDSTR_UPDATENODE:  self.__PROCESS_UPDATENODE,
               CTPCommandParser.COMMANDSTR_REGISTER:    self.__PROCESS_REGISTER,
               CTPCommandParser.COMMANDSTR_UNREGISTER:  self.__PROCESS_UNREGISTER,
               CTPCommandParser.COMMANDSTR_START:       self.__PROCESS_CONTROL,
               CTPCommandParser.COMMANDSTR_STOP:        self.__PROCESS_CONTROL,
               CTPCommandParser.COMMANDSTR_COMMAND:     self.__PROCESS_CONTROL,
               CTPCommandParser.COMMANDSTR_POST:        self.__PROCESS_POST,
               CTPCommandParser.COMMANDSTR_RESPONSE:    self.__PROCESS_RESPONSE,
               CTPCommandParser.COMMANDSTR_EVENT:       self.__PROCESS_EVENT, }
        self.__node_manager = NodeDiscover(self.__do_update_node)
        self.__node_manager.StartDiscover()

    def reset_ocm_table(self):
        # clear all node
        self.__report_node()
        # get self node now
        self.__node_manager.do_node_discover()
        # update other nodes
        ctpcmd_updatenode = CTPCommandParser.generate_updatenode()
        self.__broadcast(ctpcmd_updatenode)

    def receive(self, peer, ctp_command):
        success, result = CTPCommandParser.process_command_args(ctp_command)
        if not success:
            MessageOutput.CTPCOMMAND_ERROR(result)
            return
        ctp_command_name, argv = result
        MessageOutput.CTPCOMMAND_RECV(ctp_command_name, argv)

        process_func = self.__CTPC_MAP[ctp_command_name]
        process_func(peer, ctp_command, ctp_command_name, argv)

    def append_nodes(self, nodes):
        if type(nodes) is list:
            self.__node_manager.extend(nodes)
        else:
            self.__node_manager.append(nodes)

    def remove_nodes(self, nodes):
        if type(nodes) is list:
            for node in nodes:
                self.__node_manager.remove(node)
        else:
            self.__node_manager.remove(nodes)

    def __send(self, peer, ctp_command):
        self.__send_ctpc(peer, ctp_command)

    def __broadcast(self, ctp_command):
        self.__send_ctpc(None, ctp_command)

    def __report_node(self):
        nodes = self.NODE_SPLIT.join(self.__ocm_table.get_nodes())
        ctpcmd_reportnode = CTPCommandParser.generate_reportnode(nodes)
        self.__broadcast(ctpcmd_reportnode)

    def __response(self, node, peer, return_code):
        ctpcmd_response = CTPCommandParser.generate_response(node, peer,
                                                             ReturnCode.gen_str(return_code))
        self.receive(peer, ctpcmd_response)

    def __do_update_node(self, nodes):
        if self.__ocm_table.update_owner(nodes, self):
            self.__report_node()
            self.__node_update_inform_callback(self.__ocm_table.get_nodes())

    def __inform_ctpc_response(self, node, returncode):
        self.__wait_lock.acquire()
        # If no one need this response, just ignore it.
        if self.__wait_response_nodes.has_key(node):
            wait_respondor = self.__wait_response_nodes[node]
            wait_respondor.set_response(returncode)
            wait_respondor.set()
        self.__wait_lock.release()

    def __wait_ctpc_response(self, node, ctpcmd):
        if self.__wait_response_nodes.has_key(node):
            return ReturnCode.gen(ReturnCode.CTPC_IN_PROGRESS)
        wait_respondor = ALPSRespondor(node)
        self.__wait_lock.acquire()
        self.__wait_response_nodes[node] = wait_respondor
        self.__wait_lock.release()
        self.receive(self, ctpcmd)
        timeout = not wait_respondor.wait(self.CTP_TIMEOUT)
        self.__wait_lock.acquire()
        self.__wait_response_nodes.pop(node)
        self.__wait_lock.release()
        if timeout:
            MessageOutput.CTPCOMMAND_TIMEOUT(node)
            return ReturnCode.gen(ReturnCode.CTPC_TIMEOUT)
        else:
            returncode = wait_respondor.get_response()
            return returncode

    def CTPC_REGISTER(self, node, register_type):
        if not self.__ocm_table.exist_owner(node):
            MessageOutput.NODE_UNFOUND(node)
            return ReturnCode.gen(ReturnCode.CTPC_NODE_UNFOUND)

        if register_type == CTPCommandParser.COMMANDPARA_CONTROLLER:
            if self.__ocm_table.exist_controller(node):
                MessageOutput.CONTROLLER_ALREADY_ASSIGNED(node, "self")
                return ReturnCode.gen(ReturnCode.CTPC_ALREADY_ASSIGNED)
            controller_node = "controller[%s]" % node
            if self.__node_manager.hasnode(controller_node):
                MessageOutput.CONTROLLER_ALREADY_ASSIGNED(node, "self")
                return ReturnCode.gen(ReturnCode.CTPC_ALREADY_ASSIGNED)
            ctpcmd_register = CTPCommandParser.generate_register(node,
                                                             controller=controller_node)

            # Add Virtual Controll Node
            self.append_nodes(controller_node)

            # Wait Response
            returncode = self.__wait_ctpc_response(node, ctpcmd_register)
            if ReturnCode.code(returncode) == ReturnCode.CTPC_SUCCESS:
                if not self.__ocm_table.exist_controller(node):
                    self.__ocm_table.set_controller(node, controller_node)
                MessageOutput.NODE_REGISTERED(node, register_type, controller_node)
            else:
                # Register Failed and Delete controller node
                self.remove_nodes(controller_node)
                if ReturnCode.code(returncode) == ReturnCode.CTPC_ALREADY_ASSIGNED:
                    MessageOutput.CONTROLLER_ALREADY_ASSIGNED(node, controller_node)
            return returncode

        elif register_type == CTPCommandParser.COMMANDPARA_MONITOR:
            if self.__ocm_table.get_monitors(node):
                return ReturnCode.gen(ReturnCode.CTPC_ALREADY_ASSIGNED)
            for i in range(1, 1000):
                monitor_node = "monitor[%s]_%s" % (node, i)
                if not self.__node_manager.hasnode(monitor_node):
                    break
            else:
                MessageOutput.CTPCOMMAND_ERROR("The number of Monitor[%s] is too large." % node)
                return ReturnCode.gen(ReturnCode.CTPC_ERROR)
            ctpcmd_register = CTPCommandParser.generate_register(node,
                                                                 monitor=monitor_node)

            # Add Virtual Monitor Node
            self.append_nodes(monitor_node)

            # Wait Response
            returncode = self.__wait_ctpc_response(node, ctpcmd_register)
            if ReturnCode.code(returncode) == ReturnCode.CTPC_SUCCESS:
                if not self.__ocm_table.get_monitors(node):
                    self.__ocm_table.add_monitor(node, monitor_node)
                MessageOutput.NODE_REGISTERED(node, register_type, monitor_node)
            else:
                # Register Failed and Delete monitor node
                self.remove_nodes(monitor_node)
            return returncode

        else:
            MessageOutput.CTPCOMMAND_ERROR("Unsupported Register type: " + register_type)
            return ReturnCode.gen(ReturnCode.CTPC_ERROR)
    def CTPC_UNREGISTER(self, node, unregister_type):
        if not self.__ocm_table.exist_owner(node):
            MessageOutput.NODE_UNFOUND(node)
            return ReturnCode.gen(ReturnCode.CTPC_NODE_UNFOUND)

        if unregister_type == CTPCommandParser.COMMANDPARA_CONTROLLER:
            if not self.__ocm_table.exist_controller(node):
                MessageOutput.NODE_NOT_ASSIGNED(node, unregister_type, 'self')
                return ReturnCode.gen(ReturnCode.CTPC_NOT_ASSIGNED)

            controller_node = self.__ocm_table.get_controller(node)
            ctpcmd_unregister = CTPCommandParser.generate_unregister(node,
                                                             controller=controller_node)
            # Wait Response
            returncode = self.__wait_ctpc_response(node, ctpcmd_unregister)
            if ReturnCode.code(returncode) == ReturnCode.CTPC_SUCCESS:
                if self.__ocm_table.exist_controller(node):
                    self.__ocm_table.remove_controller(node)
                self.remove_nodes(controller_node)
                MessageOutput.NODE_UNREGISTERED(node, unregister_type, controller_node)
            elif ReturnCode.code(returncode) == ReturnCode.CTPC_NOT_ASSIGNED:
                MessageOutput.NODE_NOT_ASSIGNED(node, unregister_type, controller_node)
            elif ReturnCode.code(returncode) == ReturnCode.CTPC_ALREADY_ASSIGNED:
                MessageOutput.CONTROLLER_ALREADY_ASSIGNED(node, controller_node)
            return returncode

        elif unregister_type == CTPCommandParser.COMMANDPARA_MONITOR:
            if not self.__ocm_table.get_monitors(node):
                return ReturnCode.gen(ReturnCode.CTPC_NOT_ASSIGNED)

            monitor_node = self.__ocm_table.get_monitors(node)[0]
            ctpcmd_unregister = CTPCommandParser.generate_unregister(node,
                                                             monitor=monitor_node)
            # Wait Response
            returncode = self.__wait_ctpc_response(node, ctpcmd_unregister)
            if ReturnCode.code(returncode) == ReturnCode.CTPC_SUCCESS:
                if self.__ocm_table.get_monitors(node):
                    self.__ocm_table.remove_monitor(node, monitor_node)
                self.remove_nodes(monitor_node)
                MessageOutput.NODE_UNREGISTERED(node, unregister_type, monitor_node)
            elif ReturnCode.code(returncode) == ReturnCode.CTPC_NOT_ASSIGNED:
                MessageOutput.NODE_NOT_ASSIGNED(node, unregister_type, monitor_node)
            return returncode

        else:
            MessageOutput.CTPCOMMAND_ERROR("Unsupported Unregister type: " + unregister_type)
            return ReturnCode.gen(ReturnCode.CTPC_ERROR)
    def CTPC_START(self, node, app, optionals=None):
        if not self.__ocm_table.exist_owner(node):
            returncode = ReturnCode.gen(ReturnCode.CTPC_NODE_UNFOUND)

        elif not self.__ocm_table.exist_controller(node):
            returncode = ReturnCode.gen(ReturnCode.CTPC_NOT_ASSIGNED)

        else:
            controller = self.__ocm_table.get_controller(node)
            if optionals:
                ctpcmd_start = CTPCommandParser.generate_start(node, controller, app,
                                                               optionals=repr(optionals))
            else:
                ctpcmd_start = CTPCommandParser.generate_start(node, controller, app)
            # Wait Response
            returncode = self.__wait_ctpc_response(node, ctpcmd_start)

        if ReturnCode.code(returncode) == ReturnCode.CTPC_NODE_UNFOUND:
            MessageOutput.NODE_UNFOUND(node)
        elif ReturnCode.code(returncode) == ReturnCode.CTPC_NOT_ASSIGNED:
            MessageOutput.NODE_NOT_ASSIGNED(node, CTPCommandParser.COMMANDPARA_CONTROLLER, 'self')
        elif ReturnCode.code(returncode) == ReturnCode.CTPC_REJECTED:
            MessageOutput.CONTROLLER_ALREADY_ASSIGNED(node, 'self')
        return returncode
    def CTPC_STOP(self, node, app, optionals=None):
        if not self.__ocm_table.exist_owner(node):
            returncode = ReturnCode.gen(ReturnCode.CTPC_NODE_UNFOUND)

        elif not self.__ocm_table.exist_controller(node):
            returncode = ReturnCode.gen(ReturnCode.CTPC_NOT_ASSIGNED)

        else:
            controller = self.__ocm_table.get_controller(node)
            if optionals:
                ctpcmd_stop = CTPCommandParser.generate_stop(node, controller, app,
                                                             optionals=repr(optionals))
            else:
                ctpcmd_stop = CTPCommandParser.generate_stop(node, controller, app)
            # Wait Response
            returncode = self.__wait_ctpc_response(node, ctpcmd_stop)
        if ReturnCode.code(returncode) == ReturnCode.CTPC_NODE_UNFOUND:
            MessageOutput.NODE_UNFOUND(node)
        elif ReturnCode.code(returncode) == ReturnCode.CTPC_NOT_ASSIGNED:
            MessageOutput.NODE_NOT_ASSIGNED(node, CTPCommandParser.COMMANDPARA_CONTROLLER, 'self')
        elif ReturnCode.code(returncode) == ReturnCode.CTPC_REJECTED:
            MessageOutput.CONTROLLER_ALREADY_ASSIGNED(node, 'self')
        return returncode
    def CTPC_COMMAND(self, node, app, content):
        if not self.__ocm_table.exist_owner(node):
            returncode = ReturnCode.gen(ReturnCode.CTPC_NODE_UNFOUND)

        elif not self.__ocm_table.exist_controller(node):
            returncode = ReturnCode.gen(ReturnCode.CTPC_NOT_ASSIGNED)

        else:
            controller = self.__ocm_table.get_controller(node)
            ctpcmd_command = CTPCommandParser.generate_command(node, controller, app, repr(content))

            # Wait Response
            returncode = self.__wait_ctpc_response(node, ctpcmd_command)
        if ReturnCode.code(returncode) == ReturnCode.CTPC_NODE_UNFOUND:
            MessageOutput.NODE_UNFOUND(node)
        elif ReturnCode.code(returncode) == ReturnCode.CTPC_NOT_ASSIGNED:
            MessageOutput.NODE_NOT_ASSIGNED(node, CTPCommandParser.COMMANDPARA_CONTROLLER, 'self')
        elif ReturnCode.code(returncode) == ReturnCode.CTPC_REJECTED:
            MessageOutput.CONTROLLER_ALREADY_ASSIGNED(node, 'self')
        return returncode
    def CTPC_RESPONSE(self, node, content):
        if not self.__ocm_table.exist_owner(node):
            MessageOutput.NODE_UNFOUND(node)
            return ReturnCode.gen(ReturnCode.CTPC_NODE_UNFOUND)

        if self.__ocm_table.get_owner(node) != self:
            MessageOutput.CONTROLLER_ALREADY_ASSIGNED(node, 'self')
            return ReturnCode.gen(ReturnCode.CTPC_REJECTED)

        if not self.__ocm_table.exist_controller(node):
            MessageOutput.NODE_NOT_ASSIGNED(node, CTPCommandParser.COMMANDPARA_CONTROLLER, 'self')
            return ReturnCode.gen(ReturnCode.CTPC_NOT_ASSIGNED)

        controller = self.__ocm_table.get_controller(node)
        ctpcmd_response = CTPCommandParser.generate_response(node, controller,
                                                             repr(ReturnCode.gen(argv=content)))
        self.receive(self, ctpcmd_response)
        return ReturnCode.gen(ReturnCode.CTPC_SUCCESS)
    def CTPC_POST(self, node, content):
        if not self.__ocm_table.exist_owner(node):
            MessageOutput.NODE_UNFOUND(node)
            return ReturnCode.gen(ReturnCode.CTPC_NODE_UNFOUND)
        ctpcmd_post = CTPCommandParser.generate_post(node, repr(content))
        self.receive(self, ctpcmd_post)
        return ReturnCode.gen(ReturnCode.CTPC_SUCCESS)
    def CTPC_EVENT(self, node, content):
        if not self.__ocm_table.exist_owner(node):
            MessageOutput.NODE_UNFOUND(node)
            return ReturnCode.gen(ReturnCode.CTPC_NODE_UNFOUND)

        if self.__ocm_table.get_owner(node) != self:
            MessageOutput.CONTROLLER_ALREADY_ASSIGNED(node, 'self')
            return ReturnCode.gen(ReturnCode.CTPC_REJECTED)

        if not self.__ocm_table.exist_controller(node):
            MessageOutput.NODE_NOT_ASSIGNED(node, CTPCommandParser.COMMANDPARA_CONTROLLER, 'self')
            return ReturnCode.gen(ReturnCode.CTPC_NOT_ASSIGNED)

        monitors = self.__ocm_table.get_monitors(node)
        monitors.insert(0, self.__ocm_table.get_controller(node))
        for monitor in monitors:
            ctpcmd_event = CTPCommandParser.generate_event(node, monitor, repr(content))
            self.receive(self, ctpcmd_event)
        return ReturnCode.gen(ReturnCode.CTPC_SUCCESS)

    def __PROCESS_REPORTNODE(self, peer, ctp_command, ctp_command_name, argv):
        nodes = argv[CTPCommandParser.COMMANDPARA_NODE].split(self.NODE_SPLIT)
        updated = self.__ocm_table.update_owner(nodes, peer)
        if updated:
            self.__report_node()
            self.__node_update_inform_callback(self.__ocm_table.get_nodes())
    def __PROCESS_UPDATENODE(self, peer, ctp_command, ctp_command_name, argv):
        nodes = self.NODE_SPLIT.join(self.__ocm_table.get_nodes())
        ctpcmd_reportnode = CTPCommandParser.generate_reportnode(nodes)
        # just send to whom needed
        self.__send(peer, ctpcmd_reportnode)
    def __PROCESS_REGISTER(self, peer, ctp_command, ctp_command_name, argv):
        node = argv[CTPCommandParser.COMMANDPARA_NODE].strip()
        if not self.__ocm_table.exist_owner(node):
            # we don't know where the node is, just ignore it.
            MessageOutput.NODE_UNFOUND(node)
            return

        owner = self.__ocm_table.get_owner(node)
        if owner != self:
            self.__send(owner, ctp_command)
            if peer == self:
                MessageOutput.CTPCOMMAND_SEND(node, ctp_command_name)
            else:
                MessageOutput.CTPCOMMAND_PASS(node, ctp_command_name)
            return

        if CTPCommandParser.COMMANDPARA_CONTROLLER in argv.keys():
            register_type = CTPCommandParser.COMMANDPARA_CONTROLLER
            controller_monitor_node = argv[CTPCommandParser.COMMANDPARA_CONTROLLER].strip()
        elif CTPCommandParser.COMMANDPARA_MONITOR in argv.keys():
            register_type = CTPCommandParser.COMMANDPARA_MONITOR
            controller_monitor_node = argv[CTPCommandParser.COMMANDPARA_MONITOR].strip()

        time_count = 1
        while (time_count < self.WAIT_CONTROLLER_TIMEOUT and
               not self.__ocm_table.exist_owner(controller_monitor_node)):
            time_count += 1
            time.sleep(1)

        if not self.__ocm_table.exist_owner(controller_monitor_node):
            # we don't know who the controller or monitor is, just ignore it.
            MessageOutput.CONTROLLER_UNFOUND(controller_monitor_node)
            return

        if register_type == CTPCommandParser.COMMANDPARA_CONTROLLER:
            if (self.__ocm_table.exist_controller(node) and
                self.__ocm_table.get_controller(node) != controller_monitor_node):
                self.__response(node, controller_monitor_node, ReturnCode.CTPC_ALREADY_ASSIGNED)
                MessageOutput.CONTROLLER_ALREADY_ASSIGNED(node, controller_monitor_node)
                return
            else:
                self.__ocm_table.set_controller(node, controller_monitor_node)
                self.__response(node, controller_monitor_node, ReturnCode.CTPC_SUCCESS)
                MessageOutput.NODE_REGISTERED(node, register_type, controller_monitor_node)
                return

        elif register_type == CTPCommandParser.COMMANDPARA_MONITOR:
            if not self.__ocm_table.exist_monitor(node, controller_monitor_node):
                self.__ocm_table.add_monitor(node, controller_monitor_node)
            self.__response(node, controller_monitor_node, ReturnCode.CTPC_SUCCESS)
            MessageOutput.NODE_REGISTERED(node, register_type, controller_monitor_node)
            return
    def __PROCESS_UNREGISTER(self, peer, ctp_command, ctp_command_name, argv):
        node = argv[CTPCommandParser.COMMANDPARA_NODE].strip()
        if not self.__ocm_table.exist_owner(node):
            # we don't know where the node is, just ignore it.
            MessageOutput.NODE_UNFOUND(node)
            return

        owner = self.__ocm_table.get_owner(node)
        if owner != self:
            self.__send(owner, ctp_command)
            if peer == self:
                MessageOutput.CTPCOMMAND_SEND(node, ctp_command_name)
            else:
                MessageOutput.CTPCOMMAND_PASS(node, ctp_command_name)
            return

        if CTPCommandParser.COMMANDPARA_CONTROLLER in argv.keys():
            unregister_type = CTPCommandParser.COMMANDPARA_CONTROLLER
            controller_monitor_node = argv[CTPCommandParser.COMMANDPARA_CONTROLLER].strip()
        elif CTPCommandParser.COMMANDPARA_MONITOR in argv.keys():
            unregister_type = CTPCommandParser.COMMANDPARA_MONITOR
            controller_monitor_node = argv[CTPCommandParser.COMMANDPARA_MONITOR].strip()

        if unregister_type == CTPCommandParser.COMMANDPARA_CONTROLLER:
            if not self.__ocm_table.exist_controller(node):
                self.__response(node, controller_monitor_node, ReturnCode.CTPC_NOT_ASSIGNED)
                MessageOutput.NODE_NOT_ASSIGNED(node, unregister_type, controller_monitor_node)
                return
            if self.__ocm_table.get_controller(node) != controller_monitor_node:
                self.__response(node, controller_monitor_node, ReturnCode.CTPC_ALREADY_ASSIGNED)
                MessageOutput.CONTROLLER_ALREADY_ASSIGNED(node, controller_monitor_node)
                return
            self.__ocm_table.remove_controller(node)
            self.__response(node, controller_monitor_node, ReturnCode.CTPC_SUCCESS)
            MessageOutput.NODE_UNREGISTERED(node, unregister_type, controller_monitor_node)
        elif unregister_type == CTPCommandParser.COMMANDPARA_MONITOR:
            if not self.__ocm_table.exist_monitor(node, controller_monitor_node):
                self.__response(node, controller_monitor_node, ReturnCode.CTPC_NOT_ASSIGNED)
                MessageOutput.NODE_NOT_ASSIGNED(node, unregister_type, controller_monitor_node)
                return
            self.__ocm_table.remove_monitor(node, controller_monitor_node)
            self.__response(node, controller_monitor_node, ReturnCode.CTPC_SUCCESS)
            MessageOutput.NODE_UNREGISTERED(node, unregister_type, controller_monitor_node)
            return
    def __PROCESS_CONTROL(self, peer, ctp_command, ctp_command_name, argv):
        node = argv[CTPCommandParser.COMMANDPARA_NODE].strip()
        if not self.__ocm_table.exist_owner(node):
            # we don't know where the node is, just ignore it.
            MessageOutput.NODE_UNFOUND(node)
            return

        owner = self.__ocm_table.get_owner(node)
        if owner != self:
            self.__send(owner, ctp_command)
            if peer == self:
                MessageOutput.CTPCOMMAND_SEND(node, ctp_command_name)
            else:
                MessageOutput.CTPCOMMAND_PASS(node, ctp_command_name)
            return

        controller_node = argv[CTPCommandParser.COMMANDPARA_CONTROLLER].strip()
        if not self.__ocm_table.exist_controller(node):
            self.__response(node, controller_node, ReturnCode.CTPC_NOT_ASSIGNED)
            MessageOutput.NODE_NOT_ASSIGNED(node, CTPCommandParser.COMMANDPARA_CONTROLLER,
                                            controller_node)
            return
        if self.__ocm_table.get_controller(node) != controller_node:
            self.__response(node, controller_node, ReturnCode.CTPC_REJECTED)
            MessageOutput.CONTROLLER_ALREADY_ASSIGNED(node, controller_node)
            return

        self.__inform_ctpc_received(ctp_command_name, argv)
        MessageOutput.CTPCOMMAND_RECV(node, ctp_command_name)
        return
    def __PROCESS_POST(self, peer, ctp_command, ctp_command_name, argv):
        node = argv[CTPCommandParser.COMMANDPARA_NODE].strip()
        if not self.__ocm_table.exist_owner(node):
            MessageOutput.NODE_UNFOUND(node)
            return

        owner = self.__ocm_table.get_owner(node)
        if owner != self:
            self.__send(owner, ctp_command)
            if peer == self:
                MessageOutput.CTPCOMMAND_SEND(node, ctp_command_name)
            else:
                MessageOutput.CTPCOMMAND_PASS(node, ctp_command_name)
            return

        MessageOutput.CTPCOMMAND_RECV(node, ctp_command_name)
        content = argv[CTPCommandParser.COMMANDPARA_CONTENT]
        self.CTPC_EVENT(node, content)
        return
    def __PROCESS_RESPONSE(self, peer, ctp_command, ctp_command_name, argv):
        node = argv[CTPCommandParser.COMMANDPARA_NODE].strip()
        controller_node = argv[CTPCommandParser.COMMANDPARA_CONTROLLER].strip()
        if not self.__ocm_table.exist_owner(controller_node):
            MessageOutput.CONTROLLER_UNFOUND(controller_node)
            return
        controller = self.__ocm_table.get_owner(controller_node)
        if controller != self:
            self.__send(controller, ctp_command)
            if peer == self:
                MessageOutput.CTPCOMMAND_SEND(node, ctp_command_name)
            else:
                MessageOutput.CTPCOMMAND_PASS(node, ctp_command_name)
            return
        MessageOutput.CTPCOMMAND_RECV(node, ctp_command_name)
        returncode = ReturnCode.str_gen(argv[CTPCommandParser.COMMANDPARA_CONTENT])
        self.__inform_ctpc_response(node, returncode)
        return
    def __PROCESS_EVENT(self, peer, ctp_command, ctp_command_name, argv):
        node = argv[CTPCommandParser.COMMANDPARA_NODE].strip()
        monitor_node = argv[CTPCommandParser.COMMANDPARA_MONITOR].strip()
        if not self.__ocm_table.exist_owner(monitor_node):
            MessageOutput.MONITOR_UNFOUND(monitor_node)
            return
        monitor = self.__ocm_table.get_owner(monitor_node)
        if monitor != self:
            self.__send(monitor, ctp_command)
            if peer == self:
                MessageOutput.CTPCOMMAND_SEND(node, ctp_command_name)
            else:
                MessageOutput.CTPCOMMAND_PASS(node, ctp_command_name)
            return

        self.__inform_ctpc_received(ctp_command_name, argv)
        MessageOutput.CTPCOMMAND_RECV(node, ctp_command_name)
        return