Exemplo n.º 1
0
 def handle_message(self, message, address):
     self.receivedDataPackets += 1
     decoded_data = decode_packet(message)
     if self.method == "t":
         self.triplePacket.receive(message)
     elif self.method == "x":
         self.xorPacket.receive(message)
Exemplo n.º 2
0
 def receive(self, packet):
     decoded_data = decode_packet(packet)
     packetNumber = decoded_data[0]
     text = convert_bytes_to_string(decoded_data[1])
     if packetNumber % 3 == 1:
         # packetA
         self.previouslyReceivedA = decoded_data
         if self.received_package_is_legit(packetNumber, text):
             self.receivedPackets += 1
     elif packetNumber % 3 == 2:
         # packetB
         self.previouslyReceivedB = decoded_data
         if self.received_package_is_legit(packetNumber, text):
             self.receivedPackets += 1
     else:
         # packetC
         if self.previouslyReceivedA[
                 0] + 2 == packetNumber and self.previouslyReceivedB[
                     0] + 1 != packetNumber:
             # We can get packetB with A and C
             packetB = sxor(
                 convert_bytes_to_string(self.previouslyReceivedA[1]), text)
             if self.received_package_is_legit(packetNumber - 1, packetB):
                 self.receivedPackets += 1
         elif self.previouslyReceivedB[
                 0] + 1 == packetNumber and self.previouslyReceivedA[
                     0] + 2 != packetNumber:
             # We can get packetA with B and C
             packetA = sxor(
                 convert_bytes_to_string(self.previouslyReceivedB[1]), text)
             if self.received_package_is_legit(packetNumber - 2, packetA):
                 self.receivedPackets += 1
Exemplo n.º 3
0
 def receive(self, packet):
   decoded_data = decode_packet(packet)
   packetNumber = decoded_data[0]
   text = decoded_data[1].decode('utf-8')
   if self.previouslyReceived != packetNumber and self.filehandler.text_matches_file_part((packetNumber - 1) * 100, text):
     self.previouslyReceived = packetNumber
     self.receivedPackets += 1
def receive_message(name, ID):
    "Send message procedure (default)"
    global thread_count

    print("Waiting for client (Thread %s)" % ID)
    data, addr = sock.recvfrom(1024)
    ack_sock = socket(AF_INET, SOCK_DGRAM)
    ack_sock.connect((addr[0], addr[1]))

    port_used = port_list[0]
    port_list.remove(port_used)
    ack_sock.send(str(port_used).encode())
    server_sock = socket(AF_INET, SOCK_DGRAM)
    server_sock.bind((UDP_IP, port_used))

    filename = data.decode()

    with open('received_file/' + filename, 'wb') as f:
        print('receiving data packet... (Thread %s)' % ID)
        while True:
            byte_packet, addr = server_sock.recvfrom(MAX_PACKET_LENGTH)
            data_packet = packet.decode_packet(byte_packet)
            type = data_packet.get_type()
            data = data_packet.get_data()
            checksum = get_checksum(data)
            ori_checksum = data_packet.get_checksum()

            if (checksum == ori_checksum):
                if (type == FIN):
                    print("File received successfully (Thread %s)" % ID)
                    ack = packet.packet(FIN_ACK, data_packet.get_id(),
                                        data_packet.get_seq_number(),
                                        ori_checksum)
                    ack_sock.send(ack.get_packet_byte())
                    f.write(data)
                    break
                else:
                    ack = packet.packet(ACK, data_packet.get_id(),
                                        data_packet.get_seq_number(),
                                        ori_checksum)
                    ack_sock.send(ack.get_packet_byte())
                    f.write(data)

    port_list.append(port_used)
    id_list.append(ID)
    thread_count -= 1
Exemplo n.º 5
0
	def add_samples(self, samples):
		bitstream = self.pwm_decoder.add_samples(samples)
		packet_data = self.bitstream_decoder.add_bits(bitstream)
		packets = [decode_packet(p) for p in packet_data]
		
		return packets
Exemplo n.º 6
0
    def add_samples(self, samples):
        bitstream = self.pwm_decoder.add_samples(samples)
        packet_data = self.bitstream_decoder.add_bits(bitstream)
        packets = [decode_packet(p) for p in packet_data]

        return packets