def post_node_config(self, node_id): """ 处理HTTP URL,参见start_bottle方法的注释。 本方法将处理Server发来的请求(HTTP POST),要求更新Node的当前配置。 方法将试图解析POST正文中的新配置,并试图给Node自身应用这个配置。 如果中途出现错误,将返回HTTP 500错误,并在响应正文中附上错误的信息和产生的异常。 如果没有出现错误,将返回HTTP 200正常响应,并在响应正文中附上更新后的配置,以供检查。 :param basestring node_id: URL中的<node_id>部分。 """ # 检查URL中的node_id是否和自身ID匹配 if node_id != self.config.node_id: return generate_500("Cannot find node with node_id='%s'" % node_id) # 尝试解析POST正文中的NodeConfig try: new_config = parse_node_config_from_string(request.body.read()) except ValueError as e: return generate_500("Error on parsing new config.", e) # 尝试应用新的配置 try: self.apply_config(new_config) except Exception as e: return generate_500("CAUTION: Error on applying new config.", e) # 返回更新后的配置 return self.config.get_json_string()
def post_unreg_node(self): """ 处理HTTP URL,参见start_bottle方法的注释。 本方法处理由Node发送的解除注册请求(HTTP POST)。 请求的正文包含Node的配置(NodeConfig)。 收到请求后,本Server在已知的Node列表中删除这个Node。 """ # 从请求的POST正文中解析Node的配置(NodeConfig) body = request.body.read() try: node_config = parse_node_config_from_string(body) except ValueError as e: return generate_500("Error on parsing node config.", e) # 从已知Node列表中删除这个Node node_id = node_config.node_id self.remove_known_node(node_id) return
def post_reg_node(self): """ 处理HTTP URL,参见start_bottle方法的注释。 本方法处理由Node发送的注册请求(HTTP POST)。 请求的正文包含Node的配置(NodeConfig)。 收到请求后,本Server在已知的Node列表中添加这个Node。 """ # 从请求的POST正文中解析Node的配置(NodeConfig) body = request.body.read() try: node_config = parse_node_config_from_string(body) except ValueError as e: return generate_500("Error on parsing node config.", e) # 添加Node node_addr = request.environ.get("REMOTE_ADDR") node_port = node_config.node_port node_id = node_config.node_id node_desc = node_config.node_desc self.add_known_node(node_addr, node_port, node_id, node_desc, node_config) return