def send(self): packet = (self.gen).gen_packet_from_file() while packet: print("Sending: packet ", packet.seqno) if not PLS.lose_packet(self.p_loss): (self.socket).sendto(pick.dumps(packet), self.dest) self.timer = threading.Timer(self.time_out, timer_handler, args=(self, packet,)) self.timer.start() # wait for ACK or abort after a long period of time while 1: ack = None addr = None try: print("\tWaiting For ACK: packet ", packet.seqno) ack, addr = (self.socket).recvfrom(self.BUF_SIZE) except socket.timeout: print("Error: 10 retransmissions of packet have occured yet no ACKs were received. Aborting.") self.timer.cancel() return None if addr == self.dest and pick.loads(ack).ackno == packet.seqno: # correct ACK received print("\tACK received for packet ", packet.seqno) self.timer.cancel() packet = self.gen.gen_packet_from_file() # update the packet break else: print("\tWrong ACK or Wrong sender") print("File has been transferred successfully, sending close packet") end = (self.gen).gen_close_packet() (self.socket).sendto(pick.dumps(end), self.dest)
def timer_handler(self): self.lock.acquire() #Start timer #Resend all window self.threshold = self.cur_list_size/2 for i in range(0, len(self.window.packet_list)): self.window.resend_list.append(self.window.packet_list[i]) self.window.packet_list = [] self.window.ack_list = [] self.cur_list_size = 1 print("Time out resending packet: ", self.base_seqno) if len(self.window.resend_list) > 0: self.window.packet_list.append(self.window.resend_list[0]) self.timer = threading.Timer(self.time_out,self.timer_handler,args=()) self.timer.start() self.window.resend_list.pop(0) self.window.ack_list.append(0) self.base_seqno = self.window.packet_list[0].seqno if not PLS.lose_packet(self.p_loss) : self.socket.sendto(pick.dumps(self.window.packet_list[0]), self.dest) self.lock.release()
def timer_handler(self): self.lock.acquire() # Start timer # Resend all window self.threshold = self.cur_list_size / 2 for i in range(0, len(self.window.packet_list)): self.window.resend_list.append(self.window.packet_list[i]) self.window.packet_list = [] self.window.ack_list = [] self.cur_list_size = 1 print("Time out resending packet: ", self.base_seqno) if len(self.window.resend_list) > 0: self.window.packet_list.append(self.window.resend_list[0]) self.timer = threading.Timer(self.time_out, self.timer_handler, args=()) self.timer.start() self.window.resend_list.pop(0) self.window.ack_list.append(0) self.base_seqno = self.window.packet_list[0].seqno if not PLS.lose_packet(self.p_loss): self.socket.sendto(pick.dumps(self.window.packet_list[0]), self.dest) self.lock.release()
def send(self): packet = (self.gen).gen_packet_from_file() while packet: print("Sending: packet ", packet.seqno) if not PLS.lose_packet(self.p_loss): (self.socket).sendto(pick.dumps(packet), self.dest) self.timer = threading.Timer(self.time_out, timer_handler, args=(self, packet)) self.timer.start() # wait for ACK or abort after a long period of time while 1: ack = None addr = None try: print("\tWaiting For ACK: packet ", packet.seqno) ack, addr = (self.socket).recvfrom(self.BUF_SIZE) except socket.timeout: print("Error: 10 retransmissions of packet have occured yet no ACKs were received. Aborting.") self.timer.cancel() return None if addr == self.dest and pick.loads(ack).ackno == packet.seqno: # correct ACK received print("\tACK received for packet ", packet.seqno) self.timer.cancel() packet = self.gen.gen_packet_from_file() # update the packet break else: print("\tWrong ACK or Wrong sender") print("File has been transferred successfully, sending close packet") end = (self.gen).gen_close_packet() (self.socket).sendto(pick.dumps(end), self.dest)
def send_packet(self, packet, index): logging.debug("Sending: packet %d", packet.seqno) if not PLS.lose_packet(self.p_loss) : self.socket.sendto(pick.dumps(packet), self.dest) self.packt.timer_list[index] = threading.Timer(self.time_out,self.timer_handler,args=(packet,)) self.packt.timer_list[index].setName("Timer on packet: "+str(packet.seqno)) self.packt.timer_list[index].start()
def timer_handler(self, packet): # retransmit packet to the same client print("\tTimeout: retransmitting packet ", packet.seqno) if not PLS.lose_packet(self.p_loss): (self.socket).sendto(pick.dumps(packet), self.dest) self.timer.cancel() self.timer = threading.Timer(self.time_out, timer_handler, args=(self, packet,)) self.timer.start()
def timer_handler(self, packet): # retransmit packet to the same client print("\tTimeout: retransmitting packet ", packet.seqno) if not PLS.lose_packet(self.p_loss): (self.socket).sendto(pick.dumps(packet), self.dest) self.timer.cancel() self.timer = threading.Timer(self.time_out, timer_handler, args=(self, packet)) self.timer.start()
def send_packet(self, packet, index): logging.debug("Sending: packet %d", packet.seqno) if not PLS.lose_packet(self.p_loss): self.socket.sendto(pick.dumps(packet), self.dest) self.packt.timer_list[index] = threading.Timer(self.time_out, self.timer_handler, args=(packet, )) self.packt.timer_list[index].setName("Timer on packet: " + str(packet.seqno)) self.packt.timer_list[index].start()
def check_list(self, ackno): self.lock.acquire() # seqno is in window if ackno >= self.base_seqno and ackno <= self.base_seqno + self.list_size: # Ack all packets <= ackno (Cumulative Ack) i = self.base_seqno while i <= ackno: self.window.ack_list[i - self.base_seqno] = 1 i = i + 1 # Slide window # if self.packt.ack_list[0] == 1: if self.cur_list_size < self.list_size: self.fix_list() while self.window.ack_list[0] == 1: self.window.packet_list.pop(0) self.window.ack_list.pop(0) if len(self.window.resend_list) > 0: packet = self.window.resend_list.pop(0) else: packet = self.gen.gen_packet_from_file() if len(self.window.packet_list) > 0: self.base_seqno = self.window.packet_list[0].seqno # Reset timer for new base self.timer.cancel() self.timer = threading.Timer(self.time_out, self.timer_handler, args=()) self.timer.start() else: # print("WE ARE HERE") break if packet: self.window.packet_list.append(packet) self.window.ack_list.append(0) if not PLS.lose_packet(self.p_loss): self.socket.sendto(pick.dumps(packet), self.dest) self.lock.release()
def check_list(self, ackno): self.lock.acquire() #seqno is in window if ackno >=self.base_seqno and ackno <=self.base_seqno+self.list_size : #Ack all packets <= ackno (Cumulative Ack) i = self.base_seqno while i <= ackno: self.window.ack_list[i - self.base_seqno] = 1 i = i + 1 #Slide window # if self.packt.ack_list[0] == 1: if self.cur_list_size < self.list_size: self.fix_list() while self.window.ack_list[0] == 1: self.window.packet_list.pop(0) self.window.ack_list.pop(0) packet = None if len(self.window.resend_list) > 0 : packet = self.window.resend_list.pop(0) else: packet = self.gen.gen_packet_from_file() if len(self.window.packet_list) > 0: self.base_seqno = self.window.packet_list[0].seqno #Reset timer for new base self.timer.cancel() self.timer = threading.Timer(self.time_out,self.timer_handler,args=()) self.timer.start() else: # print("WE ARE HERE") break if packet: self.window.packet_list.append(packet) self.window.ack_list.append(0) if not PLS.lose_packet(self.p_loss) : self.socket.sendto(pick.dumps(packet), self.dest) self.lock.release()
def send_packet(self, packet): index = packet.seqno - self.base_seqno if index >= 0: print("Sending: packet ", packet.seqno) if not PLS.lose_packet(self.p_loss) : self.socket.sendto(pick.dumps(packet), self.dest)
def send_packet(self, packet): index = packet.seqno - self.base_seqno if index >= 0: print("Sending: packet ", packet.seqno) if not PLS.lose_packet(self.p_loss): self.socket.sendto(pick.dumps(packet), self.dest)