コード例 #1
0
ファイル: request_hndl.py プロジェクト: NouranBakry/RDT
    def GBN(self):

        window = Window(self.window_size)
        # ack = None
        # new_ack = False
        current_seq = 0
        with open('serverfiles\\' + self.file_requested, 'rb') as f:

            size = len(f.read())
            f.seek(0)
            packet_num = math.ceil(size / 256)
            sent = 0
            line = f.read(256)

            while sent != packet_num:
                ready = select([self.sock], [self.sock], [])
                if ready[1]:
                    if window.within_win():
                        if line:
                            pkt = Packet.create_udp_packet(
                                self.port, self.dest_addr[1],
                                window.next_seq(), line)
                            window.send_next_pkt(line)
                            if random.random() > self.loss_prob:
                                print("SERVER sending packet with seq",
                                      window.next_seq() - 1)
                                self.sock.sendto(pkt, self.dest_addr)
                                # print("Server sending packet", window.next_seq()-1)
                                print(window.current_s())
                            line = f.read(256)
                    else:
                        window.update()

                    for s in window.current_s():
                        if s.sent and not s.acked and (time.time() -
                                                       s.timeout) >= TIMEOUT:
                            pkt = Packet.create_udp_packet(
                                self.port, self.dest_addr[1], s.seq_no, s.data)
                            window.update_timer(s.seq_no)
                            if random.random() > self.loss_prob:
                                print("TIMEOUT ", s.seq_no)
                                print("SERVER resending packet with seq",
                                      s.seq_no)
                                self.sock.sendto(pkt, self.dest_addr)
                if ready[0]:
                    ack = self.sock.recv(8)
                    ack_no = int.from_bytes(ack, 'big')
                    print("Receiving ack", ack_no)
                    if current_seq == ack_no:
                        window.acknowledge(ack_no)
                        sent += 1
                        current_seq += 1
                        # print("acknowledged", ack_no)
                        print(window.current_s())
            self.sock.sendto(b'EOF', self.dest_addr)
コード例 #2
0
ファイル: request_hndl.py プロジェクト: NouranBakry/RDT
    def selective_repeat(self):

        window = Window(self.window_size)
        ack = None
        new_ack = False

        with open('serverfiles\\' + self.file_requested, 'rb') as f:

            # Calculates the number of required packets to send the file in
            # Advance.
            size = len(f.read())
            f.seek(0)
            packet_num = math.ceil(size / 256)

            # Number of sent packets
            sent = 0

            line = f.read(256)

            while sent != packet_num:

                # returns 3 lists one for ready sockets to read from, one for
                # ready sockets to send on and one for exceptions.
                ready = select([self.sock], [self.sock], [])

                # Ready to send on sockets
                if ready[1]:

                    # If the next_sequence value is still in the window frame
                    # if this is false, then it means we sent all of the packets
                    # currently in the window and we're waiting for ACKs
                    if window.within_win():
                        if line:
                            pkt = Packet.create_udp_packet(
                                self.port, self.dest_addr[1],
                                window.next_seq(), line)
                            print("SERVER sending packet with seq",
                                  window.next_seq())
                            window.send_next_pkt(line)
                            if random.random() > self.loss_prob:
                                self.sock.sendto(pkt, self.dest_addr)
                            line = f.read(256)
                    # Update the window manually if we sent all of the window's
                    # Packets.
                    else:
                        window.update()

                    # If the packets are send but not acked after TIMEOUT time
                    # Send them again with probability of loss.
                    # RESETS TIMER.

                    for s in window.current_s():
                        if s.sent and not s.acked and (time.time() -
                                                       s.timeout) >= TIMEOUT:
                            pkt = Packet.create_udp_packet(
                                self.port, self.dest_addr[1], s.seq_no, s.data)
                            print("SERVER resending packet with seq", s.seq_no)
                            window.update_timer(s.seq_no)
                            if random.random() > self.loss_prob:
                                self.sock.sendto(pkt, self.dest_addr)

                if ready[0]:
                    ack = self.sock.recv(8)
                    sent += 1
                    ack_no = int.from_bytes(ack, 'big')
                    window.acknowledge(ack_no)

            self.sock.sendto(b'EOF', self.dest_addr)