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 gen_packet_from_file(self) : # returns None if file was not registered to the PacketGen at the construction time # check by "if <return> is not None : <file was registered at construction time> " if self.file_is_reg == 0 : return None data_bytes = self.f_handler.get_next_chunk() if not data_bytes : return None packet = class_packet.Packet(checksum.gen_cksum(checksum.string_to_byte_arr(data_bytes)),len(data_bytes)+12,self.next_seqno,data_bytes) self.next_seqno = self.next_seqno + 1 return packet
def gen_packet_from_file( self ): # returns None if file was not registered to the PacketGen at the construction time # check by "if <return> is not None : <file was registered at construction time> " if self.file_is_reg == 0: return None data_bytes = self.f_handler.get_next_chunk() if not data_bytes: return None packet = class_packet.Packet( checksum.gen_cksum(checksum.string_to_byte_arr(data_bytes)), len(data_bytes) + 12, self.next_seqno, data_bytes) self.next_seqno = self.next_seqno + 1 return packet
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
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
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 gen_packet(self, data_string): packet = class_packet.Packet( checksum.gen_cksum(checksum.string_to_byte_arr(data_string)), (len(data_string) * 8 + 12), self.next_seqno, data_string) self.next_seqno = self.next_seqno + 1 return packet
def gen_packet(self,data_string) : packet = class_packet.Packet(checksum.gen_cksum(checksum.string_to_byte_arr(data_string)),(len(data_string)*8+12),self.next_seqno,data_string) self.next_seqno = self.next_seqno + 1 return packet