コード例 #1
0
def send(sock, numbers):
    #global mutex
    global base
    global send_timer

    # Add all the packets to the buffer
    packets = []
    seq_num = 0
    while True:
        if seq_num >= len(numbers):
            break
        data = numbers[seq_num]
        packets.append(packet.make(seq_num, data))
        seq_num += 1

    num_packets = len(packets)
    print('I got', num_packets, ' to send')
    window_size = set_window_size(num_packets)
    next_to_send = 0
    base = 0

    # Start the receiver thread
    thread.start_new_thread(receive, (sock, ))

    while base < num_packets:
        mutex.acquire()
        # Send all the packets in the window
        while next_to_send < base + window_size:
            print('Sending packet', next_to_send)
            sock.sendto(packets[next_to_send], RECEIVER_ADDR)
            next_to_send += 1

        # Start the timer
        if not send_timer.running():
            print('Starting timer')
            send_timer.start()

        # Wait until a timer goes off or we get an ACK
        while send_timer.running() and not send_timer.timeout():
            mutex.release()
            print('Sleeping')
            time.sleep(SLEEP_INTERVAL)
            mutex.acquire()

        if send_timer.timeout():
            # Looks like we timed out
            print('Timeout')
            send_timer.stop()
            next_to_send = base
        else:
            print('Shifting window')
            window_size = set_window_size(num_packets)
        mutex.release()

    # Send empty packet as sentinel
    sock.sendto(packet.make_empty(), RECEIVER_ADDR)
コード例 #2
0
def send(sock, filename, drop_prob):
    global mutex
    global base
    global send_timer

    try:
        file = open(filename, 'rb')
    except IOError:
        print('Unable to open', filename)
        return

    packets = []
    seq_num = 0
    while True:
        data = file.read(PACKET_SIZE)
        if not data:
            break
        packets.append(packet.make(seq_num, data))
        seq_num += 1

    num_packets = len(packets)
    print('Total number of packets: ', num_packets)
    window_size = set_window_size(num_packets)
    next_to_send = 0
    base = 0

    _thread.start_new_thread(receive, (sock, ))

    while base < num_packets:
        mutex.acquire()
        while next_to_send < base + window_size:
            print('Sending packet', next_to_send)
            udt.send(packets[next_to_send], sock, RECEIVER_ADDR, drop_prob)
            next_to_send += 1

        if not send_timer.running():
            print('Starting timer')
            send_timer.start()

        while send_timer.running() and not send_timer.timeout():
            mutex.release()
            print('Sleeping')
            time.sleep(SLEEP_INTERVAL)
            mutex.acquire()

        if send_timer.timeout():
            print('Timeout')
            send_timer.stop()
            next_to_send = base
        else:
            print('Shifting window')
            window_size = set_window_size(num_packets)
        mutex.release()

    udt.send(packet.make_empty(), sock, RECEIVER_ADDR, drop_prob)
    file.close()
コード例 #3
0
ファイル: Sender.py プロジェクト: tlwilliams2/rdtp
def send_gbn(sock, packets):
    global mutex
    global base
    global timer

    # get packets from file and set all necessary values
    num_packets = len(packets)
    print("Packets to be sent: %s" % num_packets)
    window_size = get_window_size(num_packets)
    window_start = 0
    next_to_send = 0
    base = 0

    # Start the receiver thread
    print("Starting gbn receive thread")
    _thread.start_new_thread(receive_gbn, (sock,))

    while base < num_packets:
        # set mutex lock to this thread to execute send tasks
        # send all packets in given window
        mutex.acquire()
        while next_to_send < window_start + window_size:
            print("Sending sequence #: %s" % next_to_send)
            udt.send(packets[next_to_send], sock, RECEIVER_ADDR)
            next_to_send += 1

        # Start the timer
        if not timer.running():
            print("Starting window timer")
            timer.start()

        # Wait until a timer times out and pass mutex lock to receive thread
        while timer.running() and not timer.timeout():
            mutex.release()
            print("Waiting for ACKs")
            time.sleep(SLEEP_INTERVAL)
            mutex.acquire()
        # if we time out or we have not received ACKs for all window packets
        # then we reset window. Else we move the window
        if timer.timeout() or ((window_start + window_size) > base):
            print("Window timed out")
            timer.stop()
            next_to_send = window_start
        else:
            next_to_send = base
            window_start = base
            print("Moving window")
            window_size = get_window_size(num_packets)
        mutex.release()

    # Send sentinel packets
    udt.send(packet.make_empty(), sock, RECEIVER_ADDR)
    print("All packets have been sent")
コード例 #4
0
def send(sock, filename):
    global mutex
    global base
    global send_timer

    # Open the file
    try:
        file = open(filename, 'rb')
    except IOError:
        print('Unable to open', filename)
        return

    # Add all the packets to the buffer
    packets = []
    seq_num = 0
    while True:
        data = file.read(PACKET_SIZE)
        # print(data)
        if not data:
            break

        packets.append(packet.make(seq_num, data, is_checksum=True))
        seq_num += 1

    num_packets = len(packets)
    print('packets total number:', num_packets)
    window_size = set_window_size(num_packets)
    next_to_send = 0
    base = 0

    # receive thread init
    _thread.start_new_thread(receive, (sock, ))

    # print("#####packets num: " + str(num_packets))
    while base < num_packets:

        print(base)
        mutex.acquire()
        # Send all the packets in the window
        while next_to_send < base + window_size:
            print('Sending packet', next_to_send)
            if random.randint(0, DROP_PROB) > 0:
                sock.sendto(packets[next_to_send], RECEIVER_ADDR)
            next_to_send += 1

        # Start the timer
        if not send_timer.running():
            print('Starting timer')
            send_timer.start()

        # Wait until a timer goes off or we get an ACK
        while send_timer.running() and not send_timer.timeout():
            mutex.release()
            print('Sleeping')
            time.sleep(SLEEP_INTERVAL)
            mutex.acquire()

        if send_timer.timeout():
            # Looks like we timed out
            print('Timeout')
            send_timer.stop()
            next_to_send = base
        else:
            print('Shifting window')
            # base += window_size
            window_size = set_window_size(num_packets)
        mutex.release()

    # Send empty packet as sentinel
    if random.randint(0, DROP_PROB) > 0:
        sock.sendto(packet.make_empty(), RECEIVER_ADDR)
    file.close()
コード例 #5
0
ファイル: sender.py プロジェクト: Clarit7/go-back-N
def send(sock):
    global mutex
    global base
    global send_timer
    global rtt_q
    global rtt_timer
    global process_start_time

    packets = []
    packets_start_time = []

    for i in range(1000):
        data = "packet" + str(i)
        packets.append(packet.make(i, data.encode()))
        packets_start_time.append(None)

    num_packets = len(packets)

    # 파라미터 정보 출력
    print('Number of packets :', num_packets)
    print('Unit time :', UNIT_TIME, 'seconds')
    print('RTT time :', RTT, 'seconds')
    print('Timeout :', TIMEOUT, 'seconds')
    print('Window size :', WINDOW_SIZE)

    window_size = set_window_size(num_packets)
    next_to_send = 0
    base = 0

    _thread.start_new_thread(receive, (sock, num_packets))
    _thread.start_new_thread(rtt_queue, ())

    process_start_time = time.time()

    while base < num_packets:
        mutex.acquire()

        while next_to_send < base + window_size:
            rtt_q.append([packets[next_to_send], sock, RECEIVER_ADDR])
            current_time = time.time()
            print('Sending packet', next_to_send, 'Time',
                  round(time.time() - process_start_time, 3), 'seconds')
            print('Window state:', min(base, num_packets - 1), 'to',
                  min(WINDOW_SIZE, num_packets - base) + base - 1)
            rtt_timer.append(current_time)
            time.sleep(UNIT_TIME)
            next_to_send += 1

        if not send_timer.running():
            send_timer.start()

        while send_timer.running() and not send_timer.timeout():
            mutex.release()
            mutex.acquire()

        # 송신한 packet의 timeout을 검사함
        if send_timer.timeout():
            print('Timeout')
            send_timer.stop()
            next_to_send = base
        else:
            window_size = set_window_size(num_packets)
        mutex.release()

    udt.send(packet.make_empty(), sock, RECEIVER_ADDR)
コード例 #6
0
def send(sock, filename):
    global mutex
    global base
    global send_timer

    # Open the file
    try:
        file = open(filename, 'rb')
    except IOError:
        print('Unable to open', filename)
        return

    # Add all the packets to the buffer
    packets = []
    seq_num = 0
    while True:
        PACKET_SIZE = randint(512, 1024)
        data = file.read(PACKET_SIZE)
        if not data:
            break
        packets.append(packet.make(seq_num, errp, data))
        seq_num += 1

    num_packets = len(packets)
    print('I gots', num_packets)
    window_size = set_window_size(num_packets)
    next_to_send = 0
    base = 0

    # Start the receiver thread
    _thread.start_new_thread(receive, (sock, ))

    while base < num_packets:
        mutex.acquire()
        # Send all the packets in the window
        while next_to_send < base + window_size:
            print('Sending packet', next_to_send)
            udt.send(packets[next_to_send], sock, RECEIVER_ADDR)
            next_to_send += 1

        # Start the timer
        if not send_timer.running():
            print('Starting timer')
            send_timer.start()

        # Wait until a timer goes off or we get an ACK
        while send_timer.running() and not send_timer.timeout():
            mutex.release()
            print('Sleeping')
            time.sleep(SLEEP_INTERVAL)
            mutex.acquire()

        if send_timer.timeout():
            # Looks like we timed out
            print('Timeout')
            send_timer.stop()
            next_to_send = base
        else:
            print('Shifting window')
            window_size = set_window_size(num_packets)
        mutex.release()

    # Send empty packet as sentinel
    udt.send(packet.make_empty(), sock, RECEIVER_ADDR)
    file.close()
コード例 #7
0
ファイル: sender.py プロジェクト: haseeb-saeed/go-back-N
def send(sock, filename):
    global mutex
    global base
    global send_timer

    # Open the file
    try:
        file = open(filename, 'rb')
    except IOError:
        print('Unable to open', filename)
        return
    
    # Add all the packets to the buffer
    packets = []
    seq_num = 0
    while True:
        data = file.read(PACKET_SIZE)
        if not data:
            break
        packets.append(packet.make(seq_num, data))
        seq_num += 1

    num_packets = len(packets)
    print('I gots', num_packets)
    window_size = set_window_size(num_packets)
    next_to_send = 0
    base = 0

    # Start the receiver thread
    _thread.start_new_thread(receive, (sock,))

    while base < num_packets:
        mutex.acquire()
        # Send all the packets in the window
        while next_to_send < base + window_size:
            print('Sending packet', next_to_send)
            udt.send(packets[next_to_send], sock, RECEIVER_ADDR)
            next_to_send += 1

        # Start the timer
        if not send_timer.running():
            print('Starting timer')
            send_timer.start()

        # Wait until a timer goes off or we get an ACK
        while send_timer.running() and not send_timer.timeout():
            mutex.release()
            print('Sleeping')
            time.sleep(SLEEP_INTERVAL)
            mutex.acquire()

        if send_timer.timeout():
            # Looks like we timed out
            print('Timeout')
            send_timer.stop();
            next_to_send = base
        else:
            print('Shifting window')
            window_size = set_window_size(num_packets)
        mutex.release()

    # Send empty packet as sentinel
    udt.send(packet.make_empty(), sock, RECEIVER_ADDR)
    file.close()
コード例 #8
0
def send(sock, filename):
    global mutex
    global base
    global window
    global start_of_program

    # Open file
    try:
        file = open(filename, 'rb')
    except IOError:
        print('Unable to open', filename)
        return

    # Add all the packets to the buffer
    packets = []
    seq_num = 0
    while True:
        data = file.read(PACKET_SIZE)
        if not data:
            break
        packets.append(packet.make(seq_num, data))
        seq_num += 1

    num_packets = len(packets)
    print('There are :', num_packets)
    next_to_send = 0
    base = 0
    arr_of_pack = []

    # Start the receiver thread
    _thread.start_new_thread(receive, (sock, ))

    while base < num_packets:
        mutex.acquire()
        # start sending packets
        if window.contain() < 4 and next_to_send < num_packets:
            print('Sending packet', next_to_send)
            arr_of_pack.append(packet.Packeto(packets[next_to_send]))
            window.add_packet(packet.Packeto(packets[next_to_send]))
            udt.send(packets[next_to_send], sock, RECEIVER_ADDR)
            next_to_send = next_to_send + 1

        elif window.packet1().stat() == 1 and next_to_send < num_packets:
            print('Sending packet', next_to_send)
            arr_of_pack.append(packet.Packeto(packets[next_to_send]))
            window.add_packet(packet.Packeto(packets[next_to_send]))
            udt.send(packets[next_to_send], sock, RECEIVER_ADDR)
            next_to_send = next_to_send + 1

        elif next_to_send == num_packets and window.packet1().stat(
        ) == 1 and window.packet2().stat() == 1 and window.packet3().stat(
        ) == 1 and window.packet4().stat() == 1:
            break

        while window.timer_status() and window.contain() > 3:
            print('Waiting')
            mutex.release()
            time.sleep(SLEEP_INTERVAL)
            mutex.acquire()

        if window.contain() < 4:
            wid = 0

        elif window.packet1().stat() == 0 and window.packet1().did_runout():
            # timed out
            print('Time out 1')
            ack, _ = packet.extract(window.packet1().returnpacket())
            udt.send(window.packet1().returnpacket(), sock, RECEIVER_ADDR)
            window.packet1().starttimer()
            mutex.release()
            time.sleep(SLEEP_INTERVAL)
            mutex.acquire()
        elif window.contain() > 1 and (window.packet2().stat() == 0
                                       and window.packet2().did_runout()):
            # timed out
            print('Time out 2')
            ack, _ = packet.extract(window.packet2().returnpacket())
            udt.send(window.packet2().returnpacket(), sock, RECEIVER_ADDR)
            window.packet2().starttimer()
            mutex.release()
            time.sleep(SLEEP_INTERVAL)
            mutex.acquire()
        elif window.contain() > 2 and (window.packet3().stat() == 0
                                       and window.packet3().did_runout()):
            # timed out
            print('Time out 3')
            ack, _ = packet.extract(window.packet3().returnpacket())
            udt.send(window.packet1().returnpacket(), sock, RECEIVER_ADDR)
            window.packet1().starttimer()
            mutex.release()
            time.sleep(SLEEP_INTERVAL)
            mutex.acquire()
        elif window.contain() > 3 and (window.packet4().stat() == 0
                                       and window.packet4().did_runout()):
            # timed out
            print('Time out 4')
            ack, _ = packet.extract(window.packet4().returnpacket())
            udt.send(window.packet4().returnpacket(), sock, RECEIVER_ADDR)
            window.packet4().starttimer()
            mutex.release()
            time.sleep(SLEEP_INTERVAL)
            mutex.acquire()
        #sees if it is time to leave while
        elif window.contain() > 3 and window.packet1().stat(
        ) == 1 and window.packet2().stat() == 1 and window.packet3().stat(
        ) == 1 and window.packet4().stat() == 1:
            ack, _ = packet.extract(window.packet4().returnpacket())
            if ack + 1 == num_packets:
                break
        else:
            mutex.release()
            time.sleep(SLEEP_INTERVAL)
            mutex.acquire()
        mutex.release()

    # Send empty packet as end
    udt.send(packet.make_empty(), sock, RECEIVER_ADDR)
    time.sleep(SLEEP_INTERVAL)
    udt.send(packet.make_empty(), sock, RECEIVER_ADDR)
    time.sleep(SLEEP_INTERVAL)
    udt.send(packet.make_empty(), sock, RECEIVER_ADDR)
    file.close()