def testNetworkHeaders(source, dest1, dest2, isACK, sequenceNumber, packetType): header = pm.createNetworkHeader(source, dest1, dest2, isACK, sequenceNumber, packetType) source2, dest12, dest22, isACK2, sequenceNumber2, packetType2 = pm.readNetworkHeader(header) # print(source2, dest12, dest22, isACK2, sequenceNumber2, packetType2) if source == source2 and dest1 == dest12 and dest2 == dest22 and isACK == isACK2 \ and sequenceNumber == sequenceNumber2 and packetType == packetType2: return True return False
def send_token(self, new_token_owner, gateway): packet = pm.createNetworkPacket( self.history_to_string(self.historical_data), FancyDataSender.BaseDataSender.address, gateway, new_token_owner, False, 0, CTE.NETWORK_PAQUET_TYPE_PASSTOKEN) ack = tx.sendPacket(self, packet) return ack
def polling(self, file2send): ID = FancyDataSender.BaseDataSender.address thereIsReply = False timeout = 5 reply_yes_node = [] nodes_with_packet = [] reply_no_node = [] for x in range(1, 8): dest1 = x dest2 = x source = ID if dest1 != ID: #create hello packet data = PacketManager.createNetworkPacket( CTE.NETWORK_PAQUET_CONTROL_HELLO_PAYLOAD, source, dest1, dest2, 0, 0, CTE.NETWORK_PAQUET_TYPE_CONTROL) #send hello packet ack = tx.sendPacket(self, data) time_first = time.time() while True: response = self.receiver.receiveMessage() time_actual = time.time() #LO DEL TIMEOUT NO S'HAURIA DE GESTIONAR DINS DE RECEIVE MESSAGE? if time_actual >= time_first + timeout: break if response is not None: # payload, source, dest, _, _, _, packetType = pm.readNetworkPacket( response) if (packetType == CTE.NETWORK_PAQUET_TYPE_CONTROL and dest == ID): if (payload == CTE. NETWORK_PAQUET_CONTROL_REPLY_YES_PAYLOAD ): # Received reply yes thereIsReply = True # SEND FILE to dest1 of the hello packet/source of the reply packet reply_yes_node.append(dest1) #send whole file ack = self.send_file(ID, dest1, file2send) self.file_already_received = True #ACK WILL BE TRUE IF THE FILE HAS BEEN CORRECTLY SENT (INSIDE SEND_FILE, RETRANSMISSIONS SHOULD BE HANDLED if ack: nodes_with_packet.append(dest1) elif (payload == CTE.NETWORK_PAQUET_CONTROL_REPLY_NO_PAYLOAD): reply_no_node.append(dest1) break if not thereIsReply: everyoneHasTheToken = False everyoneHasTheToken = check_token( self.history_to_string(self.historical_data)) return everyoneHasTheToken, reply_yes_node, reply_no_node, nodes_with_packet
def send_hello(self, transmitter, dest): hello_msg = "Hello" # According to standard: Control: 00, Data: 01, Token:10 data = pm.createNetworkPacket(hello_msg, FancyDataSender.BaseDataSender.address, dest, dest, False, 0, CTE.NETWORK_PAQUET_TYPE_CONTROL) # Send data and wait for Reply Yes # Place here the function that sends the data and checks whether it's Reply YES or NO or timeout has occurred reply = tx.sendPacket(self, data) # returns True/False or Yes/No according if data has been successfully send or not return reply
def send_data(self, transmitter, receiver, data): data = pm.createNetworkPacket(data, transmitter, receiver, receiver, False, 0, CTE.NETWORK_PAQUET_TYPE_DATA) ack = tx.sendPacket(self, data) return ack
def testUnicastHeaders(sequenceNumber, isACK): header = pm.createUnicastHeader(sequenceNumber, isACK) sequenceNumber2, isACK2 = pm.readUnicastHeader(header) if sequenceNumber == sequenceNumber2 and isACK == isACK2: return True return False
def testNetworkCreateHeader(source1, dest1, dest2, isACK, sequenceNumber, packetType, valueExcpected): header = pm.createNetworkHeader(source1, dest1, dest2, isACK, sequenceNumber, packetType) print("Value expected: ", valueExcpected[0], valueExcpected[1], " Actual value: ", header[0], header[1])
def testUnicastPacket(payload, sequenceNumber, isACK): packet = pm.createUnicastPacket(payload, sequenceNumber, isACK) payload2, sequenceNumber2, isACK2 = pm.readUnicastPacket(packet) if payload == payload2 and sequenceNumber == sequenceNumber2 and isACK == isACK2: return True return False
def testNetworkPacket(payload, source, dest1, dest2, isACK, sequenceNumber, packetType): return True N = 10 result = [False for i in range(0, N)] i = 0 while i < N: source = random.randint(0, 15) dest1 = random.randint(0, 15) dest2 = random.randint(0, 15) isACK = pm.numToBool(random.randint(0, 1)) sequenceNumbers = random.randint(0, 1) packetType = random.randint(0, 3) # print('Test Network ', i) data = ' (', source, ' ,', dest1, ' ,', dest2, ' ,', isACK, ' ,', sequenceNumbers, ' ,', packetType, ')' # print(data) result[i] = testNetworkHeaders(source, dest1, dest2, isACK, sequenceNumbers, packetType) i = i + 1 containsFalse = result.__contains__(False) if containsFalse: print("Something Bad") else: print("Network headers OK")