def add_message(self, new_message: UDPMessage) -> NoReturn: """Read incoming message and do the needed action associated to the message. This function contains three cases, the first one is when the added message requires the creation of a new topic. The function will create the topic if it is possible and then will check the dead letter queue to check if there are messages associated to this new topic. The outdated messages in the dlq will be deleted at this step. The second case is when a data message is received. If the associated topic exists, the message will be added to this topic. If the topic is completed with the incoming message, the image will be rebuild and added to img_queue. The third case is when the message cannot be processed now. If it is the case it will be put in the dlq to be processed later. :param new_message: The message to process. """ topic = int.from_bytes(new_message.topic, 'little') msg_nb = int.from_bytes(new_message.message_nb, 'little') if msg_nb == 0 and (topic not in self.open_topic.keys()): self.open_topic[topic] = VideoTopic.from_message(new_message) self.process_dlq(topic) elif topic in self.open_topic.keys( ) and self.open_topic[topic].nb_packet >= msg_nb: self.open_topic[topic].add_message(new_message) self.check_topic(topic) else: self.put_dlq(new_message)
def test_from_message_correctly_create_a_new_video_topic(): # Given nb_packet = 3 total_bytes = 90 height = 10 length = 3 pixel_size = 3 expected_payload = nb_packet.to_bytes(ImageManager.NB_PACKET_SIZE, 'little') + total_bytes.to_bytes( ImageManager.TOTAL_BYTES_SIZE, 'little') + height.to_bytes(ImageManager.HEIGHT_SIZE, 'little') + length.to_bytes( ImageManager.LENGTH_SIZE, 'little') + pixel_size.to_bytes(ImageManager.SIZE_PIXEL_SIZE, 'little') expected_topic = 10 # When header = ImageManager.get_header_msg(expected_topic, nb_packet, total_bytes, height, length, pixel_size) header_message = UDPMessage.from_bytes(header) result = VideoTopic.from_message(header_message) # Then assert result.nb_packet == nb_packet assert result.total_bytes == total_bytes assert result.height == height assert result.length == length assert result.pixel_size == pixel_size assert result.time_creation == int.from_bytes(header_message.time_creation, 'little')