本文整理汇总了Python中Checksum.validate_checksum方法的典型用法代码示例。如果您正苦于以下问题:Python Checksum.validate_checksum方法的具体用法?Python Checksum.validate_checksum怎么用?Python Checksum.validate_checksum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Checksum
的用法示例。
在下文中一共展示了Checksum.validate_checksum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_sacks
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
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)
示例2: handle_response
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
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
示例3: handle_acks
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
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
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
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: start
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
def start(self):
# NOTE: Packet payload size should be larger than 1000 bytes (unless it is the last packet in the stream)
# but less than 1472 bytes.
msg_type = None
while not self.done_sending:
# Repeatedly send packets until our window is full or until our chunking is complete (i.e. msg_type == false)
while not self.window.window_is_full() and self.is_chunking_done is False:
# Send the next packet chunk and return a boolean that represents whether we are done chunking
self.is_chunking_done = self.send_next_packet_chunk()
if self.is_chunking_done:
msg_type = 'end'
# Receive an ACK from the Receiver
packet_response = self.receive(0.5)
# If we haven't received a response in 500ms, then handle timeout
if (packet_response == None):
self.handle_timeout()
else:
# Via the spec, we ignore all ACK packets with an invalid checksum
if Checksum.validate_checksum(packet_response):
msg_type, seqno, data, checksum = self.split_packet(packet_response)
if self.sackMode:
seqno = int(seqno.split(";")[0])
# For some reason, 'seqno' is returned as a string... so we parse it into an integer
else:
seqno = int(seqno)
# Do ACKs have data? Probably not?
if (self.debug):
print("Received packet: %s | %d | %s | %s" % (msg_type, seqno, data, checksum))
# Put the current sequence number in our <sequence number -> ACKs> map, given some conditions
# If we haven't seen the current ACK before
if not self.window.is_seqno_contained_in_ack_map(seqno):
self.handle_new_ack(seqno)
# Current sequence number is NOT already contained within our map...
else:
# Increment the number of ACKs (for seqno) by 1
self.window.add_acks_count_to_map(seqno, self.window.get_ack_number_via_seqno(seqno) + 1)
# Algorithm: If ACK count is 3, then we use fast retransmit and resend the seqno with count == 3
if (self.window.get_ack_number_via_seqno(seqno) == 3):
self.handle_dup_ack(seqno)
else:
# Ignore ACKs with invalid checksum
# Do we need to handle_timeout() if we receive an ACK with an invalid checksum?
self.handle_timeout()
pass
# Declare that we are done sending if our window is empty AND we are done chunking
if (self.window.get_number_of_packets_in_window() == 0 and self.is_chunking_done is True):
self.done_sending = True
示例6: checkresponse
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
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
示例7: initiate_conversation
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
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)
示例8: _tear_down_connection
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
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
示例9: _initialize_connection
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
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
示例10: handle_dup_ack
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
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])
示例11: hand_shake
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
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
示例12: handle_response
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
def handle_response(self,response):
if response == None:
self.handle_timeout()
# elif Checksum.validate_checksum(response) == False:
# # print "recv: %s <--- CHECKSUM FAILED" % response
elif Checksum.validate_checksum(response):
msgtype, ack_str, data, checksum = self.split_packet(response)
if sackMode:
split = ack_str.split(';')
ack = int(split[0])
else:
ack = int(ack_str)
if ack < self.acktracker:
# print ("rcv: buggy ack")
response = self.receive(0.50)
if self.printstatements:
print response
self.handle_response(response)
elif ack == self.acktracker:
self.handle_dup_ack(ack_str)
elif ack > self.acktracker:
self.handle_new_ack(ack_str)
示例13: handle_response
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
def handle_response(self,response_packet):
if Checksum.validate_checksum(response_packet):
msg_type, seqno, data, checksum = self.split_packet(response_packet)
print(type(seqno))
print "recv: %s" % response_packet
else:
print "recv: %s <--- CHECKSUM FAILED" % response_packet
示例14: recvMyACKs
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
def recvMyACKs(self):
ACKlist = []
currentACK = self.receive(0.5)
while currentACK != None: # If we get something, then check CHECKSUM
temp = currentACK.split("|")
if not (len(temp) == 3 and temp[1].isdigit() and temp[2].isdigit() and temp[0] == "ack"): # ignore
if self.DEBUG:
print "Disregarding corrupted packet: %s" % currentACK
currentACK = self.receive(0.5)
continue
if Checksum.validate_checksum(currentACK):
msg_type, seqno, checksum = currentACK.split('|')
ACKlist.append(seqno) #put the next expected packet number in the list
elif self.DEBUG:
print "Received corrupted packet. Disregarding."
#wait for next packet
currentACK = self.receive(0.5)
ACKlist = map(int, ACKlist)
if self.DEBUG:
print "Received %d ACK's: %s. Highest is %d: the receiver has everything to %d" % (len(ACKlist), ACKlist, max(ACKlist) if ACKlist else -1, max(ACKlist)-1 if ACKlist else -1)
# No ACKs to be received. Act accordingly.
if ACKlist:
for key in self.packetList.keys(): # Cleaning up packets stored
if key < max(ACKlist) and len(self.packetList) > 1:
del self.packetList[key]
return ACKlist
示例15: handle_response
# 需要导入模块: import Checksum [as 别名]
# 或者: from Checksum import validate_checksum [as 别名]
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