def handle_data(self): self.logger.debug("Handling incoming data") message = None if self.data: log_message = "Incoming discovery data from (" + str(self.address[0]) + ", " + str( self.address[1]) + "):\n<<<START>>>\n" + self.data.decode() + "\n<<<END>>>" self.logger.info(log_message) message = Message.to_object(self.data.decode()) return message
def respond_to_description_request(self): """ Handles the description request and sends a response. :return: """ self.logger.debug("Responding to description request") data = self.connection.recv(BUFFER_SIZE).decode('UTF-8') # print(self.node.name + " received a description request:\n" + data) log_message = "Description request received \n'" + data + "'" self.logger.info(log_message) with open(self.filename, 'a') as f: log_message = str(datetime.now()) + ": " + "Description request received" f.write(log_message) log_message = "\t\t" + data f.write(log_message) f.flush() if len(data) <= 0: return try: message = Message.to_object(data) except ValueError as e: self.logger.error(e.args[0]) # print(e.args) return # TODO: fix message type checking (message.to_object always assigns "2"!) self.logger.debug("Skipping Description request message type check; Responding to request.") # Create description message from node info response = Message(MessageType.description_response, self.node.name, self.node.address, Timestamp.create_timestamp(), self.node.service_list) # Convert message to json json_message = json.dumps(response.to_json()) # Encode message to utf-8 for sending through socket data = json_message.encode() self.connection.send(data) """
def request_description(self): """ Request description of a node's services. Expects name of the node. """ if len(self.received_command) != 2: self.logger.error("Wrong argument count! Expected \"describe node_name\".") return node_name = self.received_command[1] address = self.peer_list.get_node_address(node_name) # TODO: send description request to address in peer_list self.logger.info("Sending description request to " + str(address)) # Create message and send it. message = Message(MessageType.description_request, node_name, address, Timestamp.create_timestamp()) data = message.to_json() data_str = json.dumps(data) self.output_socket.connect(address[0], address[1]) self.measurer.start_timer() self.output_socket.send(bytes(data_str, 'UTF-8')) response = self.output_socket.read() self.measurer.stop_timer() duration = self.measurer.get_last_duration() self.measurer.description_duration(self.node.name, duration) self.logger.info("Description request duration: {0}".format(duration)) response_str = response.decode('UTF-8') self.logger.debug(self.node.name + " received description response:\n" + response_str) if self.remote_socket: self.remote_socket.sendall(response) else: pass #print(response_str) update_message = Message.to_object(response_str) self.peer_list.update_node(node_name, update_message) #message = Message.to_object(response_str) return None