def _handle_arrival_msg_as_receiver(self, msg): self.recvlock.acquire() type_pkt_arv = int.from_bytes(msg[0:2], 'big') seq_pkt_arv = int.from_bytes(msg[2:4], 'big') checksum_pkt_arv = int.from_bytes(msg[4:6], 'big') audit_checksum = util.calculate_checksum(msg[6:]) if type_pkt_arv == config.MSG_TYPE_DATA and seq_pkt_arv == self.seq_pkt_to_receive and checksum_pkt_arv == audit_checksum: self.msg_handler(msg) self.seq_pkt_received += 1 self.seq_pkt_to_receive += 1 ackpkt = util.make_pkt(config.MSG_TYPE_ACK, self.seq_pkt_to_receive, 0, b'') self.network_layer.send(ackpkt) print('[receiver] good msg, -> application') elif type_pkt_arv == config.MSG_TYPE_DATA and seq_pkt_arv < self.seq_pkt_to_receive: ackpkt = util.make_pkt(config.MSG_TYPE_ACK, self.seq_pkt_to_receive, 0, b'') self.network_layer.send(ackpkt) print('[receiver] dated msg, ignore') else: ackpkt = util.make_pkt(config.MSG_TYPE_ACK, self.seq_pkt_to_receive, 0, b'') self.network_layer.send(ackpkt) print('[receiver] corrupted msg, ignore') self.recvlock.release()
def handle_arrival_msg(self): msg = self.network_layer.recv() # TODO: impl protocol to handle arrived packet from network layer. # call self.msg_handler() to deliver to application layer. data_type = int.from_bytes(msg[0:2], byteorder='big') seq_num = int.from_bytes(msg[2:4], byteorder='big') checksum = int.from_bytes(msg[4:6], byteorder='big') print("base", self.base) if data_type == config.MSG_TYPE_ACK: if checksum == util.my_check_sum(data_type, seq_num, None): if self.base == seq_num + 1: return self.base = seq_num + 1 if self.base == self.nextseqnum: self.timer.stop() else: self.timer.stop() self.timer.start() else: if checksum == util.my_check_sum(data_type, seq_num, msg[6:]): if seq_num == self.expseqnum: self.msg_handler(msg[6:]) new_check_sum = util.my_check_sum(config.MSG_TYPE_ACK, self.expseqnum, None) pkt = util.make_pkt(config.MSG_TYPE_ACK, self.expseqnum, None, new_check_sum) self.network_layer.send(pkt) self.expseqnum += 1 else: new_check_sum = util.my_check_sum(config.MSG_TYPE_ACK, self.expseqnum-1, None) pkt = util.make_pkt(config.MSG_TYPE_ACK, self.expseqnum-1, None, new_check_sum) self.network_layer.send(pkt)
def _handle_arrival_msg_as_receiver(self, msg): self.recvlock.acquire() type_pkt_arv = int.from_bytes(msg[0:2], 'big') seq_pkt_arv = int.from_bytes(msg[2:4], 'big') checksum_pkt_arv = int.from_bytes(msg[4:6], 'big') audit_checksum = util.calculate_checksum(msg[6:]) if type_pkt_arv == config.MSG_TYPE_DATA and seq_pkt_arv == self.seq_pkt_to_receive and checksum_pkt_arv == audit_checksum: print('[receiver] is perfect pkt ', seq_pkt_arv) self.msg_handler(msg[6:]) self.seq_pkt_received += 1 self.seq_pkt_to_receive += 1 ackpkt = util.make_pkt(config.MSG_TYPE_ACK, self.seq_pkt_to_receive, 0, b'') print('[receiver] reply ackpkt', ackpkt) self.network_layer.send(ackpkt) elif type_pkt_arv == config.MSG_TYPE_DATA and seq_pkt_arv < self.seq_pkt_to_receive: ackpkt = util.make_pkt(config.MSG_TYPE_ACK, self.seq_pkt_to_receive, 0, b'') self.network_layer.send(ackpkt) else: ackpkt = util.make_pkt(config.MSG_TYPE_ACK, self.seq_pkt_to_receive, 0, b'') self.network_layer.send(ackpkt) self.recvlock.release()
def handle_arrival_msg(self): msg = self.network_layer.recv() # TODO: impl protocol to handle arrived packet from network layer. # call self.msg_handler() to deliver to application layer. data_type = int.from_bytes(msg[0:2], byteorder='big') seq_num = int.from_bytes(msg[2:4], byteorder='big') checksum = int.from_bytes(msg[4:6], byteorder='big') # data_type = struct.unpack('!h', msg[0:2])[0] # seq_num = struct.unpack('!h', msg[2:4])[0] # checksum = struct.unpack('!h', msg[4:6])[0] print("seq_num ", data_type) if data_type == config.MSG_TYPE_ACK: if checksum == util.my_check_sum(data_type, seq_num, None): if not self.can_sent and seq_num == 1 - self.nextseqnum: self.can_sent = True self.can_end = True self.timer.stop() else: if checksum == util.my_check_sum(data_type, seq_num, msg[6:]): if seq_num == self.expseqnum: self.msg_handler(msg[6:]) new_check_sum = util.my_check_sum(config.MSG_TYPE_ACK, self.expseqnum, None) pkt = util.make_pkt(config.MSG_TYPE_ACK, self.expseqnum, None, new_check_sum) self.recv_buffer = pkt self.network_layer.send(pkt) self.expseqnum = 1 - self.expseqnum else: self.network_layer.send(self.recv_buffer)
def handle_arrival_msg(self): msg = self.network_layer.recv() print("Received packet: ", msg) if util.is_corrupt_pkt(msg): print("Received corrupt packet: ", msg) return received_msg = False if util.is_ack_pkt(msg): while not received_msg: with self.buffer_lock: if len(self.msg_buffer) < self.msg_buffer.maxlen: self.msg_buffer.append(msg) received_msg = True else: seq_number = util.pkt_seq_number(msg) print("Received seq number: ", seq_number) with self.recv_seq_number_lock: print("Expected seq number: ", self.recv_seq_number) if seq_number == self.recv_seq_number: received_msg = True self.recv_seq_number ^= 1 if received_msg: self.msg_handler(util.pkt_data(msg)) ack_pkt = util.make_pkt(config.MSG_TYPE_ACK, seq_number) self.network_layer.send(ack_pkt)
def send(self, msg): # TODO: impl protocol to send packet from application layer. # call self.network_layer.send() to send to network layer. msg_size = len(msg) bytes_sent = 0 start = 0 end = 0 try: while bytes_sent < msg_size: if msg_size - bytes_sent < config.MAX_MESSAGE_SIZE: end = msg_size else: end += config.MAX_MESSAGE_SIZE with self.send_seq_number_lock: seq_number = self.send_seq_number pkt = util.make_pkt(config.MSG_TYPE_DATA, seq_number, msg[start:end]) ack_pkt = None while ack_pkt is None: print("Sending pkt: ", pkt) self.network_layer.send(pkt) ack_pkt = self.start_timer_and_wait_for_ack(seq_number) bytes_sent += (end - start) start = end with self.send_seq_number_lock: self.send_seq_number ^= 1 except: raise # return False return True
def send(self, msg): # TODO: impl protocol to send packet from application layer. # call self.network_layer.send() to send to network layer. msg_size = len(msg) bytes_sent = 0 start = 0 end = 0 send_pkts = {} try: while True: with self.next_seq_number_lock: with self.base_seq_number_lock: base_seq_number = self.base_seq_number if self.next_seq_number < base_seq_number + self._N: if self.next_seq_number not in send_pkts: if bytes_sent < msg_size: if msg_size - bytes_sent < config.MAX_MESSAGE_SIZE: end = msg_size else: end += config.MAX_MESSAGE_SIZE pkt = util.make_pkt(config.MSG_TYPE_DATA, self.next_seq_number, msg[start:end]) print("Saving pkt with seq_number: ", self.next_seq_number) send_pkts[self.next_seq_number] = pkt bytes_sent += (end - start) start = end if end == msg_size: # we have reached end of message, track last seq number here self.last_seq_number_to_be_sent = self.next_seq_number else: if self.last_seq_number_to_be_sent is None: # return once we have received the ack for the last seq number return True if self.next_seq_number in send_pkts: self.network_layer.send(send_pkts[self.next_seq_number]) with self.base_seq_number_lock: base_seq_number = self.base_seq_number if self.next_seq_number == base_seq_number: # start timer with self.timer_lock: if self.timer: # if timer already exists, cancel and restart self.timer.cancel() self.timer = threading.Timer(config.TIMEOUT_MSEC/1000.0, self.reset_next_seq_num) self.timer.start() print("Started timer for pkt with seq_number: ", self.next_seq_number) self.next_seq_number += 1 except: raise # return False return True
def send(self, msg): assert self.role == config.ROLE_TYPE_SENDER if self.seq_pkt_index > self.seq_pkt_window_right: return False self.timer.stop() self.lock.acquire() sndpkt = util.make_pkt(config.MSG_TYPE_DATA, self.seq_pkt_index, util.calculate_checksum(msg), msg) self.network_layer.send(sndpkt) print('[sender] pkt ', self.seq_pkt_index, ' sent') self.buffer.append(copy.deepcopy(sndpkt)) self.seq_pkt_index += 1 self.lock.release() self.timer.start() return True
def send(self, msg): assert self.role == config.ROLE_TYPE_SENDER if self.seq_pkt_delivered + 1 == self.seq_pkt_to_send: self.lock.acquire() print('[sender] acquire lock') checksum_pkt_to_send = util.calculate_checksum(msg) sndpkt = util.make_pkt(config.MSG_TYPE_DATA, self.seq_pkt_to_send, checksum_pkt_to_send, msg) self.msg_to_send = copy.deepcopy(msg) self.pkt_to_send = util.make_pkt(config.MSG_TYPE_DATA, self.seq_pkt_to_send, checksum_pkt_to_send, self.msg_to_send) print('[sender] sndpkt ', sndpkt) self.network_layer.send(sndpkt) self.timer.start() self.seq_pkt_to_send += 1 print('[sender] seq_pkt_delivered ', self.seq_pkt_delivered, ' seq_pkt_to_send ', self.seq_pkt_to_send) self.lock.release() print('[sender] release lock') return True else: return False
def send(self, msg): # TODO: impl protocol to send packet from application layer. # call self.network_layer.send() to send to network layer. if self.can_sent: checksum = util.my_check_sum(config.MSG_TYPE_DATA, self.nextseqnum, msg) pkt = util.make_pkt(config.MSG_TYPE_DATA, self.nextseqnum, msg, checksum) self.buffer = pkt self.network_layer.send(pkt) self.nextseqnum = 1 - self.nextseqnum self.timer.start() self.can_sent = False self.can_end = False return True else: return False
def send(self, msg): # TODO: impl protocol to send packet from application layer. # call self.network_layer.send() to send to network layer. if self.nextseqnum < self.base + config.WINDOW_SIZE: checksum = util.my_check_sum(config.MSG_TYPE_DATA, self.nextseqnum, msg) pkt = util.make_pkt(config.MSG_TYPE_DATA, self.nextseqnum, msg, checksum) self.ls.append(pkt) self.network_layer.send(pkt) if self.base == self.nextseqnum: self.timer.start() self.nextseqnum += 1 return True else: return False
def handle_arrival_msg(self): # TODO: impl protocol to handle arrived packet from network layer. # call self.msg_handler() to deliver to application layer. msg = self.network_layer.recv() if util.is_corrupt_pkt(msg): return seq_number = util.pkt_seq_number(msg) if util.is_ack_pkt(msg): with self.base_seq_number_lock: if seq_number >= self.base_seq_number and seq_number < self.base_seq_number + self._N: # slide the window by 1 self.base_seq_number = seq_number + 1 if seq_number == self.last_seq_number_to_be_sent: # if this is the last seq number of the stream, set it to None self.last_seq_number_to_be_sent = None # restart timer with self.timer_lock: if self.timer: self.timer.cancel() self.timer = threading.Timer( config.TIMEOUT_MSEC / 1000.0, self.reset_next_seq_num) self.timer.start() else: received_msg = False with self.last_ack_number_lock: if seq_number == self.last_ack_number + 1: # if we receive a data pkt with next seq number, accept. otherwise, discard and send ack with last ack number received_msg = True self.last_ack_number = seq_number else: seq_number = self.last_ack_number if received_msg: self.msg_handler(util.pkt_data(msg)) # send ack pkt for accepted/discarded pkts with last ack number ack_pkt = util.make_pkt(config.MSG_TYPE_ACK, seq_number) self.network_layer.send(ack_pkt)