def test_get_best_route_for_destination_good(self): with patch.object(protocol.Protocol, 'send_header'): raw_message = 'LR,0133,16,|0131|2|5|0133|99fc8d|' header_obj = header.create_header_obj_from_raw_message(raw_message) self.protocol.process_ack_header(header_obj) self.assertEqual('|0131|2|4|0133|99fc8d|', header_obj.get_header_str())
def test_create_route_request_header_obj_good(self): header_obj = header.create_header_obj_from_raw_message( 'LR,0136,10,|0137|3|8|4|0138|') self.assertEqual(header_obj.source, '0137') self.assertEqual(header_obj.flag, 3) self.assertEqual(header_obj.ttl, 8) self.assertEqual(header_obj.end_node, '0138') self.assertEqual(header_obj.hops, 4)
def test_create_registration_header_from_message_str_good(self): registration_header_obj = header.create_header_obj_from_raw_message( 'LR,0131,10,|0131|6|4|true|test|') self.assertEqual('0131', registration_header_obj.source) self.assertEqual(6, registration_header_obj.flag) self.assertEqual(4, registration_header_obj.ttl) self.assertEqual(True, registration_header_obj.subscribe) self.assertEqual('test', registration_header_obj.peer_id)
def test_create_route_error_header_from_message_str(self): route_error_header_obj = header.create_header_obj_from_raw_message( 'LR,0131,10,|0131|5|4|0132|') self.assertEqual(route_error_header_obj.source, '0131') self.assertEqual(route_error_header_obj.ttl, 4) self.assertEqual(route_error_header_obj.broken_node, '0132') self.assertEqual(route_error_header_obj.flag, header.RouteErrorHeader.HEADER_TYPE)
def test_create_route_reply_header_obj_good(self): header_obj = header.create_header_obj_from_raw_message( 'LR,0136,10,|0137|4|8|3|0139|0140|') self.assertEqual(header_obj.source, '0137') self.assertEqual(header_obj.received_from, '0136') self.assertEqual(header_obj.end_node, '0139') self.assertEqual(header_obj.next_node, '0140') self.assertEqual(header_obj.flag, 4) self.assertEqual(header_obj.ttl, 8) self.assertEqual(header_obj.hops, 3)
def test_create_message_header_obj_good(self): header_obj = header.create_header_obj_from_raw_message( 'LR,0136,10,|0135|1|3|0138|0137|000001|hello|') self.assertEqual('0135', header_obj.source) self.assertEqual('0138', header_obj.destination) self.assertEqual(1, header_obj.flag) self.assertEqual(3, header_obj.ttl) self.assertEqual('0137', header_obj.next_node) self.assertEqual('hello', header_obj.payload) self.assertEqual('0136', header_obj.received_from) self.assertEqual(1, header_obj.message_id)
def test_create_message_header_obj_edge_pipe_in_message(self): header_obj = header.create_header_obj_from_raw_message( 'LR,0136,10,|0135|1|3|0138|0137|000010|hello, good | ' 'morning|') self.assertEqual(header_obj.source, '0135') self.assertEqual(header_obj.destination, '0138') self.assertEqual(header_obj.flag, 1) self.assertEqual(header_obj.ttl, 3) self.assertEqual(header_obj.next_node, '0137') self.assertEqual(header_obj.payload, 'hello, good | morning') self.assertEqual(header_obj.received_from, '0136') self.assertEqual(header_obj.message_id, 10)
def process_incoming_message(self): """ get messages from LoRa module, create header object and call appropriate method to process the received message """ while self.PROCESS_INCOMING_MESSAGES: if not serial_connection.response_q.empty() and not self.PAUSE_PROCESSING_INCOMING_MESSAGES: raw_message = serial_connection.response_q.get() logging.debug(f'process: {raw_message}') try: header_obj = header.create_header_obj_from_raw_message(raw_message) if header_obj.ttl > 1: self.routing_table.add_neighbor_to_routing_table(header_obj) if header_obj.flag == header.RouteRequestHeader.HEADER_TYPE: self.process_route_request(header_obj) elif header_obj.flag == header.MessageHeader.HEADER_TYPE: self.process_message_header(header_obj) elif header_obj.flag == header.RouteReplyHeader.HEADER_TYPE: self.process_route_reply_header(header_obj) elif header_obj.flag == header.RouteErrorHeader.HEADER_TYPE: self.process_route_error_header(header_obj) elif header_obj.flag == header.MessageAcknowledgeHeader.HEADER_TYPE: self.process_ack_header(header_obj) elif header_obj.flag == header.RegistrationHeader.HEADER_TYPE: self.process_registration_header(header_obj) elif header_obj.flag == header.ConnectRequestHeader.HEADER_TYPE: self.process_connect_request_header(header_obj) elif header_obj.flag == header.DisconnectRequestHeader.HEADER_TYPE: self.process_disconnect_request_header(header_obj) except ValueError as e: logging.warning(str(e)) traceback.print_exc() try: logging.debug('try to add received signal to unsupported devices list...') addr = header.get_received_from_value(raw_message) self.routing_table.add_neighbor_with_unsupported_protocol(addr) except ValueError as e: logging.warning(str(e))
def test_create_message_ack_header_good(self): header_obj = header.create_header_obj_from_raw_message( 'LR,0137,16,|0137|2|5|0138|8774d3|') self.assertEqual(header_obj.flag, 2)