本文整理汇总了Python中Checksum类的典型用法代码示例。如果您正苦于以下问题:Python Checksum类的具体用法?Python Checksum怎么用?Python Checksum使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Checksum类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_response
def handle_response(self,response):
if (response==None):
buffer=deque()
while (len(self.queue)!=0):
packet =self.queue.popleft()
self.send(packet)
buffer.append(packet)
while (len(buffer) !=0):
packet=buffer.popleft()
self.queue.append(packet)
response = self.receive(0.50)
self.handle_response(response)
elif Checksum.validate_checksum(response)==False:
return
elif Checksum.validate_checksum(response):
type1, seq ,data,checksum =self.split_packet(response)
if int(seq)-1 < self.unacknowledge:
if int(seq) < self.unacknowledge:
return
packet=self.queue.popleft()
self.send(packet)
self.queue.appendleft(packet)
response=self.receive(0.50)
self.handle_response(response)
else:
self.difference = int(seq)- self.unacknowledge
d=self.difference
while (d >0):
self.unacknowledge +=1
self.queue.popleft()
d -=1
示例2: handle_sacks
def handle_sacks(self, packet_dict, pckt_id, response_pckt, msg_type, sack_lst):
if (response_pckt != None and Checksum.validate_checksum(response_pckt)):
response = self.split_sack(response_pckt)
msg_type = response[0] # msg_type
sack_seqno = response[1] # sack seqno
sack_lst = self.update_sack_lst(response[2], sack_lst) # updates the sack list.
if (msg_type != 'sack'):
return
if (sack_seqno in packet_dict.keys()):
packet_dict[sack_seqno][2] += 1
if (sack_seqno in packet_dict.keys() and packet_dict[sack_seqno][2] >= 4):
self.retransmitOnlyPacket(packet_dict, sack_seqno)
# Handle correct ack by deleting the appropriate packets from the window.
elif (((int(sack_seqno) - int(pckt_id[0])) >= 1) and msg_type != 'end'):
diff = int(sack_seqno) - int(pckt_id[0]) # how many elements we should delete from dict/ lst.
packet_dict, pckt_id = self.delete_elems(diff, pckt_id, packet_dict)
elif (int(sack_seqno) - int(pckt_id[0]) == 0):
self.retransmitSackPackets(packet_dict, response_pckt, sack_lst)
elif(response_pckt == None or (not Checksum.validate_checksum(response_pckt))):
self.retransmitPackets(packet_dict)
示例3: handle_acks
def handle_acks(self, packet_dict, pckt_id, response_packet, msg_type):
if (response_packet != None and Checksum.validate_checksum(response_packet)):
response = self.split_ack(response_packet)
msg_type = response[0]
ack_seqno = response[1]
# checks to see if we have a corrupted message type if were are not in sack mode.
if (msg_type != 'ack'):
return
# update the number of acks in the packet dictionary for each packet.
if (ack_seqno in packet_dict.keys()):
packet_dict[ack_seqno][2] += 1
# Handle duplicate acknowledgements. IF we have 3 acks for the same packet,
# we should retransmit only that specific packet.
if (ack_seqno in packet_dict.keys() and packet_dict[ack_seqno][2] >= 4):
self.retransmitOnlyPacket(packet_dict, ack_seqno)
# Handle correct ack by deleting the appropriate packets from the window.
elif (((int(ack_seqno) - int(pckt_id[0])) >= 1) and msg_type != 'end'):
diff = int(ack_seqno) - int(pckt_id[0]) # how many elements we should delete from dict/ lst.
packet_dict, pckt_id = self.delete_elems(diff, pckt_id, packet_dict)
# if ackqno == lowest packet number, then we have dropped that packet.
elif (int(ack_seqno) - int(pckt_id[0]) == 0):
self.retransmitPackets(packet_dict)
# Handle packet loss and corruption. If the ack is never received, or
# the checksum for the ack is invalid, we should retransmit the entire window.
elif(response_packet == None or (not Checksum.validate_checksum(response_packet))):
self.retransmitPackets(packet_dict)
示例4: start
def start(self):
initialSeqNo = 0
print self.packets
startData = self.infile.read(self.dataSize)
firstPacket = self.make_packet("start", initialSeqNo, startData)
self.packets[0] = firstPacket
self.send(firstPacket)
startTime = time.clock()
while (True):
response = self.receive(.5 - (time.clock()-startTime))
if response is None:
self.send(firstPacket)
startTime = time.clock()
elif Checksum.validate_checksum(response):
break
self.base = 1
for i in range(1, self.N):
data = self.infile.read(self.dataSize)
if len(data) < self.dataSize:
packet = self.make_packet("end", i, data)
self.packets[i] = packet
self.lastSeqNo = i
self.send(packet)
break
else:
packet = self.make_packet("data", i, data)
self.packets[i] = packet
self.send(packet)
startTime = time.clock()
while True:
receivedPacket = self.receive(.5 - (time.clock() - startTime))
if receivedPacket is None:
self.handle_timeout()
elif not Checksum.validate_checksum(receivedPacket):
pass
else:
msgType, seqNoStr, data, checkSum = self.split_packet(receivedPacket)
seqNo = int(seqNoStr)
if seqNo == self.lastSeqNo+1:
break
if not seqNo in self.acks:
self.acks[seqNo] = 1
self.handle_new_ack(seqNo)
else:
self.acks[seqNo] += 1
self.handle_dup_ack(seqNo)
示例5: _tear_down_connection
def _tear_down_connection(self, retry_count):
#print "TEAR DOWN THIS WALL"
if retry_count > 0:
#Fields of the packet
msg_type = 'end'
msg = ""
#Create and send the tear down packet
tear_down_packet = self.make_packet(msg_type, self.send_base, msg)
self.send(tear_down_packet)
#Wait 500ms to receive the packet
response = self.receive(timeout=self.timeout)
if response:
if Checksum.validate_checksum(response):
#Good Packet!
msg_type, seqno, data, checksum = self.split_packet(response)
seqno = int(seqno)
if seqno == self.send_base and msg_type == "ack":
return True
else:
#Wrong SEQ NO EXPECTED
return self._tear_down_connection(retry_count - 1)
else:
#Not good packet!
return self._tear_down_connection(retry_count - 1)
else:
#Timeout
return self._tear_down_connection(retry_count - 1)
else:
#Could not tear down packet. Should we just stop sending messages?
return False
示例6: _initialize_connection
def _initialize_connection(self, retry_count):
#print "WHY HELLO THERE"
#Three Way Handshake
if retry_count > 0:
#Fields of the packet
msg_type = 'start'
msg = ""
#Create and send the initlization packet
start_packet = self.make_packet(msg_type, self.isn, msg)
self.send(start_packet)
#Wait 500ms to receive the packet
response = self.receive(timeout=self.timeout)
if response:
if Checksum.validate_checksum(response):
#Good Packet!
msg_type, seqno, data, checksum = self.split_packet(response)
ack = int(seqno)
if msg_type == "ack" and ack == self.isn:
self.send_base = self.isn + 1
return True
else:
return self._initialize_connection(retry_count - 1)
else:
#Not good packet!
return self._initialize_connection(retry_count - 1)
else:
#Timeout
return self._initialize_connection(retry_count - 1)
else:
#Could not connect
return False
示例7: _listen_for_acks
def _listen_for_acks(self):
self.time_til_timeout = self.timeout
while True:
#Always listen for acks
start_time = datetime.now()
response = self.receive(timeout=self.time_til_timeout)
end_time = datetime.now()
delta = end_time - start_time
time_elapsed = delta.seconds + delta.microseconds/1E6
self.time_til_timeout = max(self.time_til_timeout - time_elapsed, 0)
#print self.time_til_timeout
if response:
if Checksum.validate_checksum(response):
#Good Packet!
msg_type, seqno, data, checksum = self.split_packet(response)
if msg_type != "ack":
continue
ack = int(seqno)
if ack < self.send_base:
continue
if self.handle_new_ack(ack):
return
else:
#Not good packet! Listen for acks again
pass
else:
#TIMEOUT!
self.handle_timeout()
示例8: check_packet
def check_packet(self, r_pck):
if self.sackMode:
pieces = r_pck.split('|')
seqnums = pieces[1]
sackpieces = seqnums.split(';')
seqno = int(sackpieces[0])
sack_str = sackpieces[1]
sacks = []
if sack_str != '':
sack_str = sackpieces[1].split(',')
for i in sack_str:
sacks.append(int(i))
return seqno, Checksum.validate_checksum(r_pck), sacks
else:
seqno = self.get_seq(r_pck)
return seqno, Checksum.validate_checksum(r_pck), []
示例9: initiate_conversation
def initiate_conversation(self):
for i in range(1, self.windowSize + 1):
msg = self.infile.read(500)
self.buffer[i] = msg
# End of file has been read.
if msg == "":
# Empty msg, need to send empty start and end packets.
if i == 1:
self.buffer[i + 1] = ""
self.last_seqno = i + 1
else:
self.last_seqno = i
self.segmented_all_file = True
self.infile.close()
break
# Will break out if we get a valid ack
received_valid_ack = False
while not received_valid_ack:
# Will keep sending all the packets in the first window, except the end packet.
for i in range(1, len(self.buffer.keys()) + 1):
if i == 1:
msg_type = "start"
elif i == len(self.buffer.keys()):
break
else:
msg_type = "data"
packet = self.make_packet(msg_type, i, self.buffer[i])
self.send(packet)
self.packets_in_transit += 1
response = self.receive(0.5)
received_valid_ack = response != None and Checksum.validate_checksum(response)
self.handle_response(response)
示例10: checkresponse
def checkresponse(self, resp, minack= 0, maxack= 1):
""" Helper. Checks if the ack packet is not
corrupted and that the seqno in the ack is
the next seqno. Also checks if resp is null or Ack is non-int."""
# response is nil, not a valid response
if not resp:
return False
# get the pieces of the response packet
spacket = self.split_packet(resp)
msg_type, ackno = spacket[0], spacket[1]
# msgs from receiver should only be of type 'ack'
if msg_type != 'ack':
return False
#if ackno is not an int, exit
try:
ackno = int(ackno)
except ValueError:
return False
# keep track of dupacks to resend lost packet(s)
if ackno == minack:
self.dupacks += 1
else:
self.dupacks = 0
# make sure checksum is valid, and the ackno is in the sequence of acknos we are expecting
return Checksum.validate_checksum(resp) and ackno > minack and ackno <= maxack
示例11: handle_response
def handle_response(self,response_packet,sendNext):
if Checksum.validate_checksum(response_packet):
print "recv: %s" % response_packet
else:
print "recv: %s <--- CHECKSUM FAILED" % response_packet
sendNext = False
示例12: hand_shake
def hand_shake(self):
# Create a syn packet with the random initial sequence number
syn_packet = self.make_packet(MSG_TYPE_SYN, self.sequence_number, PACKET_BODY_EMPTY)
# Prepare to receive
ack_packet = None
ack_msg_type = None
ack_sequence_number = None
# We want to populate ack_packet, make sure the response message type is ack,
# and check that the sequence number is the self.sequence_number + 1
while not ack_packet and ack_msg_type != MSG_TYPE_ACK and ack_sequence_number != self.sequence_number + 1:
# Send the packet
self.send(syn_packet)
# After sending the synmessage, the sender waits for an ackpacket to finish a handshake
ack_packet = self.receive(RETRANSMISSION_TIME)
if ack_packet:
# Split apart the packet
components = self.packet_components(ack_packet)
ack_msg_type = components[PACKET_COMPONENT_MSG_TYPE]
ack_sequence_number = int(components[PACKET_COMPONENT_SEQUENCE_NUMBER])
# Validate the checksum
valid_checksum = Checksum.validate_checksum(ack_packet)
if not valid_checksum:
ack_packet = None
示例13: start
def start(self):
while not self.should_stop_transmission:
if not self.end_reached:
self.send__fill_the_window(5 - len(self.sending_window), len(self.sending_window))
response = self.receive(0.5)
if response and Checksum.validate_checksum(response):
response_type, ack_num_str, data, checksum = self.split_packet(response)
if response_type == "sack":
#print "SACK got!"
#print ack_num_str
ack_num = int(ack_num_str.split(';')[0])
self.received = ack_num_str.split(';')[1].split(',')
else:
ack_num = int(ack_num_str)
self.check_for_stop(ack_num)
#checks for out of order acks
#if ack_num is smaller than current window, then better ignore it, but we didn't
if ack_num - self.window_start_number > 0:
self.handle_new_ack(ack_num)
else:
self.handle_dup_ack(ack_num)
else:
self.handle_timeout(self.received)
self.infile.close()
示例14: update_packet
def update_packet(self, msg_type=None, seqno=None, data=None, full_packet=None, update_checksum=True):
"""
This function handles safely changing the contents of a packet. By
default, we re-compute the checksum every time the packet is updated.
However, you can disable this if you intend to create a corrupted
packet.
Note that the checksum is calculated over the NON-0-indexed sequence number.
"""
if not self.bogon:
if msg_type == None:
msg_type = self.msg_type
if seqno == None:
seqno = self.seqno
if data == None:
data = self.data
if msg_type == "ack": # doesn't have a data field, so handle separately
body = "%s|%d|" % (msg_type, seqno)
checksum_body = "%s|%d|" % (msg_type, seqno + self.start_seqno_base)
else:
body = "%s|%d|%s|" % (msg_type, seqno, data)
checksum_body = "%s|%d|%s|" % (msg_type, seqno + self.start_seqno_base, data)
if update_checksum:
checksum = Checksum.generate_checksum(checksum_body)
else:
checksum = self.checksum
self.msg_type = msg_type
self.seqno = seqno
self.data = data
self.checksum = checksum
if full_packet:
self.full_packet = full_packet
else:
self.full_packet = "%s%s" % (body, checksum)
示例15: handle_dup_ack
def handle_dup_ack(self, ack):
# invalid checksum
if not Checksum.validate_checksum(ack):
pass
msg_type, seqno, data, checksum = self.split_packet(ack)
if int(seqno) > self.window[0]:
self.send(self.s_window[seqno])