def check_packet(self, packet): seqno = packet.seqno rec_cksum = packet.cksum calc_cksum = checksum.gen_cksum( checksum.string_to_byte_arr(packet.data)) if rec_cksum != calc_cksum and seqno != 0: print( "\tBy comparing the checksum received and that calculated: packet corrupted. Discard." ) else: # Received expected inorder seqno if seqno == self.expected_seqno: print("Sending ACK for packet ", seqno) ack_packet = ack.Ack(0, seqno) self.socket.sendto(pick.dumps(ack_packet), self.dest) self.last_ackno = seqno self.expected_seqno = self.expected_seqno + 1 for i in range(len(packet.data)): self.file.write(packet.data[i]) print("\tFlushing: packet ", packet.seqno) self.file.flush() # Not expected seno else: print("Sending DUPLICATE ACK for packet ", self.last_ackno) ack_packet = ack.Ack(0, self.last_ackno) self.socket.sendto(pick.dumps(ack_packet), self.dest)
def check_packet(self, packet): index = packet.seqno - self.base_seqno rec_cksum = packet.cksum calc_cksum = checksum.gen_cksum( checksum.string_to_byte_arr(packet.data)) if rec_cksum != calc_cksum and packet.seqno != 0: print( "\tBy comparing the checksum received and that calculated: packet corrupted. Discard." ) else: if index < 0: #Already written packet ack_packet = ack.Ack(0, packet.seqno) self.socket.sendto(pick.dumps(ack_packet), self.dest) if index >= 0 and index < self.list_size: #packet in range if not self.buffer[index]: #packet received for the first time self.buffer[index] = packet ack_packet = ack.Ack(0, packet.seqno) self.socket.sendto(pick.dumps(ack_packet), self.dest) else: #packet was received before ack_packet = ack.Ack(0, packet.seqno) self.socket.sendto(pick.dumps(ack_packet), self.dest) self.check_buffer()
def recv_one_packet(self): byte, addr = self.socket.recvfrom(self.BUF_SIZE) packet = pick.loads(byte) print("Received packet ", packet.seqno) rec_cksum = packet.cksum calc_cksum = checksum.gen_cksum(checksum.string_to_byte_arr(packet.data)) if rec_cksum != calc_cksum and packet.seqno != 0: print("\tBy comparing the checksum received and that calculated: packet corrupted. Discard.") return 1 else: if packet.seqno == 0: print("Close packet received, file received successfully") return 0 else: for i in range(len(packet.data)): self.file.write(packet.data[i]) self.file.flush() ack_packet = ack.Ack(0, packet.seqno) self.socket.sendto(pick.dumps(ack_packet), self.dest) return 1