Beispiel #1
0
    def handle_packets(self) -> None:
        packet = self.recieved_packets.pop(0)

        if isinstance(packet, DataPacket):
            #Scrable packet
            message = packet.to_binary()
            data_packet = DataPacket()
            data_packet.to_packet(
                CommunicationSettings.scramble_message(message))

            is_eot = False
            if data_packet.get_valid():
                #If this is the end of transmition and all packets in the window are valid then recreate the image
                if data_packet.is_eot() and self.all_packets_valid:
                    is_eot = True

                #If the content isn't the same then the error wasn't detected
                if message != data_packet.to_binary():
                    self.stats.undetected_errors += 1
            else:
                self.stats.detected_errors += 1
                self.all_packets_valid = False  #Not all packets in this window were valid

            if is_eot == False:
                self.current_window.append(data_packet.get_data(
                ))  #Add data to the current window if not end of transmition

            #Send a response when the amount of recieved packets is equal to the window size or eot
            if len(self.current_window
                   ) == CommunicationSettings.window_size or is_eot:
                response = ResponsePacket()
                #If all packets in the window were valid add the to the good data
                if self.all_packets_valid:
                    if CommunicationSettings.logging:
                        print(f"{self.name}: Valid window")

                    response.mark_as_not_retransmit()
                    self.good_packets.extend(self.current_window)
                else:
                    if CommunicationSettings.logging:
                        print(f"{self.name}: Invalid window")

                    response.mark_as_retransmit()

                #Clear the window after sending the response
                self.current_window.clear()
                #Reset window retransmition flag
                self.all_packets_valid = True

                if is_eot:
                    self.simulate = False
                    self.recreate_image()

                self.sender.recieve_packet(response)

        else:
            #Is a response packet. That means that we should transmit our last response again
            response = ResponsePacket(data=self.last_response)
            self.sender.recieve_packet(response)
Beispiel #2
0
    def handle_packets(self) -> None:
        packet = self.recieved_packets.pop(0)

        response = ResponsePacket()  # Create the response

        if isinstance(packet, DataPacket):
            #Scrable packet
            message = packet.to_binary()
            data_packet = DataPacket()
            data_packet.to_packet(
                CommunicationSettings.scramble_message(message))

            if data_packet.get_valid():
                if CommunicationSettings.logging:
                    print(f"{self.name}: Valid packet")

                if data_packet.is_eot():
                    self.simulate = False
                    self.recreate_image()
                else:
                    self.good_packets.append(
                        data_packet.get_data())  #Add good data
                response.mark_as_not_retransmit(
                )  #Say that there is no need for retransmition

                #If the content isn't the same then the error wasn't detected
                if message != data_packet.to_binary():
                    self.stats.undetected_errors += 1
            else:
                self.stats.detected_errors += 1
                response.mark_as_retransmit()
                if CommunicationSettings.logging:
                    print(f"{self.name}: Invalid packet")

            #Save the last response
            self.last_response = response.get_data()
            self.sender.recieve_packet(response)
        else:
            #Is a response packet. That means that we should transmit our last response again
            response = ResponsePacket(data=self.last_response)
            self.sender.recieve_packet(response)