def packet(data, seq, ack_field): ## chunk, seq, '00' flag = "7E" seq_string = str(seq) print("Frame num: ", seq_string) print("Data: ", data) crc = CRC.calc_checksum(data) print( "Checksum (frame) ", seq_string, ": ", crc ) # when the server does a checksum on this chunk, should yield same result - send ack data = str(data) header = flag + "-" + seq_string + "-" + ack_field trailer = crc + "-" + flag packet = header + "-" + data + "-" + trailer return str(packet)
if ("101010101010" in data) == False: # not corrupted isValid = True # valid for the moment - not corrupted print"Expected sequence number is ", correct_seq, ", got sequence number ", int(ack_seq) # -- start error-checking the data -- # if correct_seq != int(ack_seq): # error has occurred - send a nack to request retransmission isValid = False print"Incorrect sequence number - the original packet was dropped" dropped = True nack_packet = packet.packet(bytes(payload), correct_seq, "01") connectionSocket.send(bytes(nack_packet)) # NACK else: # we have the correct sequence num - continue error-checking print"Correct sequence number" # print"CRC received: ", crc_recvd chk = CRC.calc_checksum(bytes(payload)) if chk != crc_recvd: isValid = False # request a resend print"CRC fields do not match" else: isValid = True print"CRC correct" else: # packet corrupt print"Corrupt packet!" isValid = False if isValid == True: # no errors - ACK frame ack_packet = packet.packet(bytes(payload), correct_seq, '10') connectionSocket.send(bytes(ack_packet)) print"Sent the ack frame (",ack_packet ," ) for seq ", correct_seq, " back to the client"