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_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 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 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_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_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 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 __response(self, node, peer, return_code): ctpcmd_response = CTPCommandParser.generate_response(node, peer, ReturnCode.gen_str(return_code)) self.receive(peer, ctpcmd_response)
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 __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 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)