Esempio n. 1
0
def main_receive_loop(file_name, probability, seed_num):
    seq_number = 0
    buffer = []
    corrupt_or_not = get_corrupt_simulator(probability, seed_num)

    LOGGER.info("Starting Stop and Wait Client..")

    pkt = Packet(seq_num=seq_number, data=file_name)
    send_packet_and_set_timer(pkt.bytes(), S_CLIENT)
    # S_CLIENT.sendto(pkt.bytes(), (SERVER_IP, SERVER_PORT))
    LOGGER.info("File Request Sent.")

    recieved_pkt = None
    while True:
        # FIXME: 512 should be el buffer size exactly
        packet, address = S_CLIENT.recvfrom(512)
        timer.cancel()
        # Simulates network layer delay
        # time.sleep(0.5)
        LOGGER.info("RECEIVED PACKET: {}".format(packet))
        print("RECEIVED PACKET: {}".format(packet))

        try:
            recieved_pkt = Packet(packet_bytes=corrupt_or_not(packet))
        except ValueError:
            # Packet is corrupted resend last packet
            LOGGER.info("Packet {} is corrupted".format(packet))
            S_CLIENT.sendto(ack_pkt.bytes(), (SERVER_IP, SERVER_PORT))
            continue

        if recieved_pkt.seq_num is not seq_number:
            # Last packet is not received
            print("OUT OF SEQ PACKET RECIEVED")
            S_CLIENT.sendto(ack_pkt.bytes(), (SERVER_IP, SERVER_PORT))
            continue
        # Acknowledge received packet
        buffer.append(recieved_pkt.data)
        seq_number = (seq_number + 1) % 2
        ack_pkt = Packet(seq_num=seq_number, data='')

        S_CLIENT.sendto(ack_pkt.bytes(), (SERVER_IP, SERVER_PORT))
        LOGGER.info("PACKET SEQ {} SENT...".format(seq_number))

        if recieved_pkt.is_last_pkt:
            with open("recieved/{}".format(file_name.split('/')[-1]),
                      'w') as f:
                f.write(''.join(buffer))
            LOGGER.info("FILE RECEIVED AND SAVED...")
            break