def start_receiving(self): fileReceivers = [] buffer_message = [] self.sock = socket.socket(socket.AF_INET, # this specifies address family - IPv4 in this case socket.SOCK_DGRAM) # UDP print('myport: ', settings.my_port) self.sock.bind(('', settings.my_port)) while True: data, addr = self.sock.recvfrom(1480) data = cryptograph.decode(cryptograph, data) packet_identifier = data.get('identifier') packet_flag = data.get('flag') if (packet_flag == 'FIL' or packet_flag == 'FIE' or packet_flag == 'MSF'): file_receiver_index = self.get_list_index(fileReceivers, packet_identifier) if (file_receiver_index == -1): fileReceivers.append(file_receiver.FileReceiver()) file_receiver_index = len(fileReceivers) - 1 fileReceivers[file_receiver_index].receive_file_packet(self.sock, addr, data) if (packet_flag == 'FIE'): fileReceivers.remove(fileReceivers[file_receiver_index]) elif (packet_flag == 'KIA'): sender.build_and_send(self.sock, packet_identifier, 'KIA', 1, 0, '', addr) elif (packet_flag == 'FGM'): identifier = data.get('identifier') fragmentNumber = data.get('fragmented') buffer_message.append(data.get('data')) self.confirmPacket(addr, identifier, fragmentNumber) elif (packet_flag == 'FGE'): identifier = data.get('identifier') fragmentNumber = data.get('fragmented') buffer_message.append(data.get('data')) for fragment in buffer_message: print(fragment, end="") buffer_message.clear() self.confirmPacket(addr, identifier, fragmentNumber) else: identifier = data.get('identifier') fragmentNumber = data.get('fragmented') self.confirmPacket(addr, identifier, fragmentNumber) print('Received data: ', data.get('data'))
def sendFragmentedMsg(self, soc): lastByte = 0 unsentMessageSize = len(self.MESSAGE) identifier = cryptograph.generateIdentifier() fragmentNumber = 0 while (unsentMessageSize > 0): newLastByte = lastByte + settings.maxFragSize paycheck = cryptograph.calculatePayCheck(self.MESSAGE[lastByte:newLastByte]) # completeMessage = sender.build_and_send(soc, identifier, 'FGM', fragmentNumber, paycheck, self.MESSAGE[lastByte:newLastByte]) new_flag = 'FGM' if (unsentMessageSize - newLastByte - lastByte <= 0): new_flag = 'FGE' if (fragmentNumber % 2 == 0): completeMessage = sender.build_and_send(soc, identifier, new_flag, fragmentNumber, paycheck, self.MESSAGE[lastByte:newLastByte]) self.waitForConfirmation(soc) unsentMessageSize -= newLastByte - lastByte lastByte = newLastByte fragmentNumber += 1
def sendNOTFragmentedMsg(self, soc): identifier = cryptograph.generateIdentifier() paycheck = cryptograph.calculatePayCheck(self.MESSAGE) completeMessage = sender.build_and_send(soc, identifier, 'MSG', 0, paycheck, self.MESSAGE) self.waitForConfirmation(soc)
def waitForConfirmation(self, soc, identifier) -> bool: self.soc.settimeout(settings.timeOutKeepAlive) kia = 0 while True: try: data, addr = soc.recvfrom(1024) data = cryptograph.decode(cryptograph, data) packet_flag = data.get('flag') if (packet_flag == 'KIA'): kia = 0 continue packetIdentifier = data.get('identifier') if not (identifier == packetIdentifier): print('ERROR: NOT MATCHING IDENTIFIERS (identifier: ', identifier, ') !!! !!! !!!') flag = data.get('flag') if (flag == 'OKE'): return False elif (flag == 'MSF'): self.missing_fragments = data.get('data').split('-') return True else: print('ERROR: CONFIRMATION PACKET CONTAINS UNKNOWN FLAG: ', flag, " !!! !!! !!!") return True except timeout: print('Timeout') sender.build_and_send(soc, identifier, 'KIA', 0, 0, '') if (kia < 3): kia += 1 print('KeepAlive send') continue else: print('Connection was lost') self.kill_thread = True return False continue except ConnectionResetError: print('Connection was lost') self.kill_thread = True return False
def send_initial_fragment(self, soc, identifier, file): payCheck = cryptograph.calculatePayCheck(file.name) completeMessage = sender.build_and_send(soc, identifier, 'FIL', 0, payCheck, file.name) while (self.waitForConfirmation(soc, identifier)): continue if (self.kill_thread == True): return False return True
def send_fragments(self, soc, identifier, file): lastByte = 0 max_payload = settings.maxFragSize - settings.MY_HEADER message = ' ' messages_list = [] fragmentNumber = 1 sending = True initial_fragment_send = False while sending: for i in range(0, 50): if not (initial_fragment_send): if not (self.send_initial_fragment(self.soc, identifier, file)): return initial_fragment_send = True continue message = b'' + file.read(max_payload) lastByte += message.__sizeof__() - 25 if (message == b''): flag = 'FIE' else: flag = 'FIL' payCheck = cryptograph.calculatePayCheck(message) if (fragmentNumber == 10 and settings.sent_faulty): payCheck = 0 completeMessage = sender.build_and_send( soc, identifier, flag, fragmentNumber, payCheck, message) messages_list.append(completeMessage) if (flag == 'FIE'): sending = False print('File sent') break else: fragmentNumber += 1 while (self.waitForConfirmation(soc, identifier)): print('Re-sending fragment') for miss_fragment in self.missing_fragments: completeMessage = messages_list.__getitem__( int(miss_fragment)) sender.send_message(soc, completeMessage) if (self.kill_thread): return
def requestFragments(self, addr, soc, identifier): str_missing_fragments = self.get_string_of_missing_fragments() sender.build_and_send(soc, identifier, 'MSF', 0, 0, str_missing_fragments, addr)
def confirmPacket(self, addr, soc, identifier, fragmentNumber): sender.build_and_send(soc, identifier, 'OKE', fragmentNumber, 0, '', addr)
def requestFragment(self, addr, identifier, fragmentNumber): sender.build_and_send(self.sock, identifier, 'MSF', fragmentNumber, 0, '', addr)