def putFileLogic(fatherWindow, entry1, entry2): global rowIndex global filesList global filename fatherWindow.grab_set() rowIndex += 1 print("Name ", filename) ip = SecureUDP.sock.getsockname()[0] port = int(entry2.get()) newFilePack = obPackage(20) newFilePack.fileName = filename byteNewFilePack = newFilePack.serialize(20) SecureUDP.sendto(byteNewFilePack, ip, int(port)) bytePackage, addr = SecureUDP.recivefrom() FileAdrrPack = obPackage() FileAdrrPack.unserialize(bytePackage, 2) fileID = (FileAdrrPack.fileIDByte1, FileAdrrPack.fileIDRest) filesList.append((filename, fileID)) lbl1 = Label(master=fatherWindow, text="File Successfully transferred your key: ", font=("Helvetica", 14)) lbl1.place(relx=0.10, rely=0.50) lbl1 = Label(master=fatherWindow, text=fileID, font=("Helvetica", 14)) lbl1.place(relx=0.55, rely=0.50) print(filename, str(rowIndex)) fatherWindow.grab_release()
def inputThreadBlue(secureUDPBlue,inputQueue): while True: obPack = obPackage() # Unserealize the payload to a obPack, in order to access the data inside payload , addr = secureUDPBlue.recivefrom() if int.from_bytes(payload[:1], byteorder='big') == 14: obPack2 = obPackage(16,0,0,addr[0],addr[1]) byteobPack = obPack2.serialize(16) inputQueue.put(byteobPack)
def sendLocate(window, ip, port, fileIDByte1, fileIDRest): window.grab_set() locatePack = obPackage(8) #Tipe 8 locatePack.fileIDByte1 = int(fileIDByte1.get()) locatePack.fileIDRest = int(fileIDRest.get()) clear = Label(master=window, text=" ", font=("Helvetica", 14)) clear.place(relx=0.10, rely=0.55) label1 = Label(master=window, text="Looking for the file", font=("Helvetica", 14)) label1.place(relx=0.10, rely=0.55) greenIP = ip.get() greenPort = int(port.get()) byteLocatePack = locatePack.serialize(8) SecureUDP.sendto(byteLocatePack, greenIP, greenPort) bytePackage, addr = SecureUDP.recivefrom() responsePack = obPackage(20) clear = Label(master=window, text=" ", font=("Helvetica", 14)) clear.place(relx=0.10, rely=0.55) responsePack.unserialize(bytePackage, 20) if len(responsePack.fileName) == 0: label1 = Label(master=window, text="FILE CAN NOT BE LOCATE!!!!", font=("Helvetica", 14)) label1.place(relx=0.10, rely=0.55) else: label1 = Label(master=window, text="FILE LOCATED!!!!", font=("Helvetica", 14)) print(responsePack.fileName) lista = responsePack.fileName.split(sep=';') for num in range(len(lista)): temp = lista[num] lista[num] = int(temp) grafo = Graph() grafo.crear_grafo() grafo.mostrar_nodos_con_chunks(lista) label1.place(relx=0.10, rely=0.50) window.grab_release()
def sendGet(window, ip, port, fileIDByte1, fileIDRest, filename): window.grab_set() clear = Label(master=window, text=" ", font=("Helvetica", 14)) clear.place(relx=0.10, rely=0.55) label1 = Label(master=window, text="Looking for the file", font=("Helvetica", 14)) label1.place(relx=0.10, rely=0.55) greenIP = ip.get() greenPort = int(port.get()) getPack = obPackage(21) getPack.fileIDByte1 = int(fileIDByte1.get()) getPack.fileIDRest = int(fileIDRest.get()) getPack.fileName = filename.get() byteGetPack = getPack.serialize(21) SecureUDP.sendto(byteGetPack, greenIP, greenPort) bytePackage, addr = SecureUDP.recivefrom() responsePack = obPackage(21) clear = Label(master=window, text=" ", font=("Helvetica", 14)) clear.place(relx=0.10, rely=0.55) responsePack.unserialize(bytePackage, 21) if responsePack.fileIDByte1 == 0: label1 = Label(master=window, text="UNABLE TO RETRIEVE FILE!!!!", font=("Helvetica", 14)) label1.place(relx=0.10, rely=0.55) else: label1 = Label(master=window, text="FILE RETRIEVED!!!!", font=("Helvetica", 14)) label1.place(relx=0.10, rely=0.55) lbl1 = Label(master=window, text=responsePack.fileName) lbl1.place(relx=0.10, rely=0.55) window.grab_release()
def run(self): server = (self.ip, self.port) inputQueue = queue.Queue() outputQueue = queue.Queue() ##Creates the routingtable routingTable = RoutingTable(self.routingTableDir) #Starts the UDP server sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(server) print("Listening on " + self.ip + ":" + str(self.port)) ##Creates the Threads t = threading.Thread(target=inputThread, args=(inputQueue,sock,self.nodeID )) #t.start() t2 = threading.Thread(target=outputThread, args=(outputQueue,sock,routingTable )) t2.start() t3 = threading.Thread(target=logicalThread, args=(inputQueue,outputQueue,sock,self.blueGraphDir,self.nodeID )) t3.start() #Testing while True: test = int(input()) if test == 1: neighborList = [] testPack = obPackage(1,2,'e',0,"0.0.0.1",2,neighborList) ByteTestPack = testPack.serialize() inputQueue.put(ByteTestPack)
def inputThread(inputQueue,sock,nodeID): while True: #Receive a package payload, client_address = sock.recvfrom(5000) #this determines what type of packet it is (Orange&Orange = 0 or Orange&Blue = 1 ) if int.from_bytes(payload[:1],byteorder='little') == 0: #Orange & Orange ##BYTE 9 has the orangetarget targetNode = int.from_bytes(payload[9:10],byteorder='little') #If this is a package for me then send it to the inputQueue if nodeID == targetNode: inputQueue.put(payload) #If not then just put it to the outputQueue else: outputQueue.put(payload) ##Orange & Blue else: obPack = obPackage() obPack.unserialize(payload) obPack.blueAddressIP = client_address[0] obPack.blueAddressPort = client_address[1] byteobPack = obPack.serialize() inputQueue.put(byteobPack)
def outputThread(outputQueue, sock, routingTable, debug): file = open("output.out", "w+") file.truncate(0) while True: # Takes a package from the queue. If the queue is empty it waits until a package arrives bytePacket = outputQueue.get() # this determines what type of packet it is (Orange&Orange = 0 or Orange&Blue = 1 ) if int.from_bytes(bytePacket[:1], byteorder='little') == 0: # Orange & Orange # BYTE 9 has the orangetarget if debug == True: pack = ooPackage() pack.unserialize(bytePacket) file.write("I send a Orange & Orange packetCategory: %d sn: %d orangeSource: %d orangeTarget: %d communicationType: %s requestedGraphPosition: %d blueAddressIP: %s blueAddressPort: %d priority: %d \n" % ( pack.packetCategory, pack.sn, pack.orangeSource, pack.orangeTarget, pack.communicationType, pack.requestedGraphPosition, pack.blueAddressIP, pack.blueAddressPort, pack.priority)) bytePacket = pack.serialize() targetNode = int.from_bytes( bytePacket[9:10], byteorder='little') # Routing_table returns the address address = routingTable.retrieveAddress(targetNode) # Sends the pack to the other Orange node try: sock.sendto(bytePacket, address) if debug == True: file.write("\t Sended to %s:%d\n" % (address[0], address[1])) except OSError: if debug == True: file.write("Network is unreachable\n") else: obPack = obPackage() # Unserealize the payload to a obPack, in order to access the data inside obPack.unserialize(bytePacket) # Takes the ip and port ip = obPack.blueAddressIP port = obPack.blueAddressPort client = (ip, port) if debug == True: file.write("Sending a Orange & Blue packetCategory: %d sn: %d communicationType: %s obtainedGraphPosition: %d blueAddressIP: %s blueAddressPort: %d neighborList: %s \n" % ( obPack.packetCategory, obPack.sn, obPack.communicationType, obPack.obtainedGraphPosition, obPack.blueAddressIP, obPack.blueAddressPort, obPack.neighborList)) # Serialize the package byteobPack = obPack.serialize() # try: # secureUDP.sendto(byteobPack, client) # except: # file.write("ERROR: clientAddress is wrong %s:%d" % # (ip, port)) if debug == True: file.flush()
def userInputThread(self): while True: userInput = input() if userInput == "1": print("Files ", self.fileDataBase) # filename = input("Enter path of the file: ") # self.fileDataBase[self.fileIDRest] = (filename,0) # print("Your ID for the file ",filename," is: fileIDByte1: ",self.fileIDByte1," fileIDRest: ",self.fileIDRest) # self.chunkSeparator(filename,self.fileIDByte1,self.fileIDRest) # self.fileIDRest += 1 elif userInput == "2": #Check if file Exist fileIDByte1 = input("Enter fileIDByte1: ") fileIDRest = input("Enter fileIDRest: ") filename = input("Enter filename: ") #Creates a generic Exists package print("(Get) from ") getPack = obPackage(6) getPack.fileIDByte1 = int(fileIDByte1) getPack.fileIDRest = int(fileIDRest) TimeStamp = time.time() ListBlueNodes = {} self.getMap[(getPack.fileIDByte1, getPack.fileIDRest)] = (TimeStamp, self.BlueIP, self.BluePort, ListBlueNodes, filename) serializedObject = getPack.serialize(6) self.SecureUDP.sendto(serializedObject, self.BlueIP, self.BluePort) elif userInput == "4": #Check if file Complete UNTESTED UNTESTED print("Going to check if a file is complete") fileIDByte1 = input("Enter fileIDByte1: ") fileIDRest = input("Enter fileIDRest: ") filename = input("Enter filename: ") #Creates a generic Complete package completePack = obPackage(userInput) completePack.fileIDByte1 = int(fileIDByte1) completePack.fileIDRest = int(fileIDRest) serializedObject = completePack.serialize(userInput) self.SecureUDP.sendto(serializedObject, self.BlueIP, self.BluePort)
def sendComplete(window, ip, port, fileIDByte1, fileIDRest): window.grab_set() completePack = obPackage(4) #Tipe 4 completePack.fileIDByte1 = int(fileIDByte1.get()) completePack.fileIDRest = int(fileIDRest.get()) clear = Label(master=window, text=" ", font=("Helvetica", 14)) clear.place(relx=0.10, rely=0.55) label1 = Label(master=window, text="Looking for the file", font=("Helvetica", 14)) label1.place(relx=0.10, rely=0.55) greenIP = ip.get() greenPort = int(port.get()) byteCompletePack = completePack.serialize(4) SecureUDP.sendto(byteCompletePack, greenIP, greenPort) bytePackage, addr = SecureUDP.recivefrom() responsePack = obPackage(5) clear = Label(master=window, text=" ", font=("Helvetica", 14)) clear.place(relx=0.10, rely=0.55) responsePack.unserialize(bytePackage, 5) if responsePack.chunkID == 0: label1 = Label(master=window, text="FILE NOT COMPLETE!!!!", font=("Helvetica", 14)) label1.place(relx=0.10, rely=0.55) else: label1 = Label(master=window, text="FILE COMPLETE!!!!", font=("Helvetica", 14)) label1.place(relx=0.10, rely=0.55) window.grab_release()
def SendExist(window, ip, port, fileIDByte1, fileIDRest): window.grab_set() existsPack = obPackage(2) #Tipo 2 existsPack.fileIDByte1 = int(fileIDByte1.get()) existsPack.fileIDRest = int(fileIDRest.get()) clear = Label(master=window, text=" ", font=("Helvetica", 14)) clear.place(relx=0.10, rely=0.55) label1 = Label(master=window, text="Looking for the file", font=("Helvetica", 14)) label1.place(relx=0.10, rely=0.55) greenIP = ip.get() greenPort = int(port.get()) byteExistPack = existsPack.serialize(2) SecureUDP.sendto(byteExistPack, greenIP, greenPort) bytePackage, addr = SecureUDP.recivefrom() responsePack = obPackage(3) clear = Label(master=window, text=" ", font=("Helvetica", 14)) clear.place(relx=0.10, rely=0.55) responsePack.unserialize(bytePackage, 3) if responsePack.fileIDByte1 == 0: label1 = Label(master=window, text="FILE NOT found!!!!", font=("Helvetica", 14)) label1.place(relx=0.10, rely=0.55) else: label1 = Label(master=window, text="FILE FOUND!!!!", font=("Helvetica", 14)) label1.place(relx=0.10, rely=0.55) window.grab_release()
def inputThread(inputQueue, outputQueue, sock, nodeID, debug): file = open("input.out", "w+") file.truncate(0) while True: # Receive a package payload, client_address = sock.recvfrom(5000) # this determines what type of packet it is (Orange&Orange = 0 or Orange&Blue = 1 ) if int.from_bytes(payload[:1], byteorder='little') == 0: # Orange & Orange # BYTE 9 has the orangetarget if debug == True: pack = ooPackage() pack.unserialize(payload) file.write("I received a Orange & Orange packetCategory: %d sn: %d orangeSource: %d orangeTarget: %d communicationType: %s requestedGraphPosition: %d blueAddressIP: %s blueAddressPort: %d priority: %d \n" % ( pack.packetCategory, pack.sn, pack.orangeSource, pack.orangeTarget, pack.communicationType, pack.requestedGraphPosition, pack.blueAddressIP, pack.blueAddressPort, pack.priority)) payload = pack.serialize() targetNode = int.from_bytes(payload[9:10], byteorder='little') # If this is a package for me then send it to the inputQueue if nodeID == targetNode: inputQueue.put(payload) if debug == True: file.write("\tthe package is for me\n") # If not then just put it to the outputQueue else: outputQueue.put(payload) if debug == True: file.write("\tthe package is not for me\n") # Orange & Blue else: obPack = obPackage() # Unserealize the payload to a obPack, in order to access the data inside obPack.unserialize(payload) # Puts the ip and port given by the sock.recvfrom inside the package obPack.blueAddressIP = client_address[0] obPack.blueAddressPort = client_address[1] if debug == True: file.write("I received a Orange & Blue packetCategory: %d sn: %d communicationType: %s obtainedGraphPosition: %d blueAddressIP: %s blueAddressPort: %d neighborList: %s \n" % ( obPack.packetCategory, obPack.sn, obPack.communicationType, obPack.obtainedGraphPosition, obPack.blueAddressIP, obPack.blueAddressPort, obPack.neighborList)) # Serialize the package byteobPack = obPack.serialize() # Puts the package into the inputQueue so the logicalThread can proccess it inputQueue.put(byteobPack) if debug == True: file.flush()
def SendDelete(window, ip, port, fileIDByte1, fileIDRest): window.withdraw() deletePack = obPackage(10) #Tipo 10 deletePack.fileIDByte1 = int(fileIDByte1.get()) deletePack.fileIDRest = int(fileIDRest.get()) greenIP = ip.get() greenPort = int(port.get()) byteDeletePack = deletePack.serialize(10) SecureUDP.sendto(byteDeletePack, greenIP, greenPort) label1 = Label(master=window, text="FILE DELETED!!!!", font=("Helvetica", 14)) label1.place(relx=0.10, rely=0.55) window.deiconify()
def __init__(self,orangeIP,orangePort): self.SecureUDP = SecureUdp(100,1,True) #ventana de 100 con timeout de 1s print("BlueNode Listening on ip: %s port %d " % ( self.SecureUDP.sock.getsockname()[0], self.SecureUDP.sock.getsockname()[1])) # Creates the Threads t = threading.Thread(target=self.inputThread, args=()) t.start() t2 = threading.Thread(target=self.logicalThread, args=()) t2.start() t2 = threading.Thread(target=self.userInputThread, args=()) t2.start() obPackagex = obPackage(14) serializedObject = obPackagex.serialize(14) self.SecureUDP.sendto(serializedObject,orangeIP,orangePort)
def joinSpanningTree(self): # Espera poder ser incluido while (True): neighborAlive = 0 for neighbour in self.neighborTuple: #neighborTuple es de la forma (node, IP, PORT) print("Vecino ",self.neighborTuple[neighbour]) if self.neighborTuple[neighbour][0] != -1: #If the neighbor has a address # Creo el paquetico de tipo 11 JoinTreePack = obPackage(11) JoinTreePack.nodeID = self.myID bytesJoinTreePack = JoinTreePack.serialize(11) print("Sending Join ", self.neighborTuple[neighbour]) self.SecureUDP.sendto(bytesJoinTreePack, self.neighborTuple[neighbour][0],self.neighborTuple[neighbour][1]) neighborAlive += 1 # Saca las respuestas responseList = [] #(nodeID,IP,PORT) for i in range(neighborAlive): response = self.spanningTreeMsgQueue.get(block=True,timeout=None) # se bloquea hasta tener respuesta (Type,nodeID,IP,PORT) print("Receiving ",response) if response[0] == 12: # si si hay un IDo, no guarda IDoNot responseList.append((response[1],response[2],response[3])) #(nodeID,IP,PORT) responseListSize = len(responseList) if responseListSize != 0: # Encontro al menos un vecino if responseListSize == 1: # Si solo hay un vecino, se guarda automático dad = responseList[0] #print("response ",dad) return (dad[0],dad[1],dad[2]) #(nodeID,IP,PORT) elif responseListSize > 1: # si hay 0 vecinos maxNodeID = (99999999,"0.0.0.0",0) for candidate in responseList: # Battle to ask for a sugar Daddy if candidate[0] < maxNodeID[0]: maxNodeID = candidate return maxNodeID else: time.sleep(5)
def chunkSeparator(self, filename, fileIDByte1, fileIDRest): #chunkID = 0 #Creates a generic putChunk package chunkPacketToSend = obPackage(0) chunkPacketToSend.fileIDByte1 = fileIDByte1 chunkPacketToSend.fileIDRest = fileIDRest fd = os.open(filename, os.O_RDWR) totalChunks = math.ceil(os.fstat(fd).st_size / 1024) self.fileDataBase[(fileIDByte1, fileIDRest)] = (filename, totalChunks) #Envia los chunks for chunkID in range(totalChunks): fileSlice = os.read(fd, 1024) chunkPacketToSend.chunkPayload = fileSlice chunkPacketToSend.chunkID = chunkID # chunkPacketToSend.print_data() serializedObject = chunkPacketToSend.serialize(0) self.SecureUDP.sendto(serializedObject, self.BlueIP, self.BluePort) os.close(fd) print("File: ", filename, "sent ", self.fileDataBase[(fileIDByte1, fileIDRest)])
def logicalThread(inputQueue,outputQueue,sock,blueGraphDir,nodeID): #Creates the orange graph and the blueNodeTable table = blueNodeTable(blueGraphDir) requestNode = -1 blueNodeIP = "0.0.0.0" blueNodePort = 8888 MAXORANGENODES = 6 acks = [] acksDone = False #True when all the acks have been received, False otherwise acksWrite = [] acksWriteDone = False #True when all the acksWrite have been received, False otherwise priority = -1 sn= nodeID while True: ##Takes a package from the inputQueue. If the queue is empty it waits until a package arrives bytePacket = inputQueue.get() #this determines what type of packet it is (Orange&Orange = 0 or Orange&Blue = 1 ) if int.from_bytes(bytePacket[:1],byteorder='little') == 0: #Orage & Orange pack = ooPackage() pack.unserialize(bytePacket) if pack.communicationType == 'r': ##This a request package print("This is a request pack from: %s requesting the number: %d with the priority: %d " % (pack.orangeSource,pack.requestedGraphPosition,pack.priority)) if pack.requestedGraphPosition == requestNode: ##If I request the same number if pack.priority < priority: ##If my priority is bigger then I win print("I won the request of the blueNode: %d (myID: %d myPriority: %d) (otherNodeID: %d otherNodeIDpriority: %d)" % (requestNode,nodeID,priority,pack.orangeSource,pack.priority)) #Creates a decline package declinePack = ooPackage(0,sn,nodeID,pack.orangeSource,'d',requestNode,blueNodeIP,blueNodePort,priority) #Serialize the package bytePacket = declinedPack.serialize() #Puts the package to the outputQueue outputQueue.put(bytePacket) elif pack.priority > priority: ##If my priority is smaller then the other node wins print("I lost the request of the blueNode: %d (myID: %d myPriority: %d) (otherNodeID: %d otherNodeIDpriority: %d)" % (requestNode,nodeID,priority,pack.orangeSource,pack.priority)) #Creates a accept acceptPack = ooPackage(0,sn,nodeID,pack.orangeSource,'a',pack.requestedGraphPosition,pack.blueAddressIP,pack.blueAddressPort,pack.priority) #Serialize the package bytePacket = acceptPack.serialize() #Puts the package to the outputQueue outputQueue.put(bytePacket) else: #When both priorities are equal print("We draw the request of the blueNode: %d (myID: %d myPriority: %d) (otherNodeID: %d otherNodeIDpriority: %d)" % (requestNode,nodeID,priority,pack.orangeSource,pack.priority)) #Checks the nodeID and the bigger wins if nodeID > pack.orangeSource: #I win print("I won the request of the blueNode:%d SecondRound (myID:%d) (otherNodeID:%d)" % (requestNode,nodeID,pack.orangeSource)) #Creates a decline package declinePack = ooPackage(0,sn,nodeID,pack.orangeSource,'d',requestNode,blueNodeIP,blueNodePort,priority) #Serialize the package bytePacket = declinedPack.serialize() #Puts the package to the outputQueue outputQueue.put(bytePacket) else: ## The other node wins print("I lost the request of the blueNode:%d SecondRound (myID:%d) (otherNodeID:%d)" % (requestNode,nodeID,pack.orangeSource)) #Creates a accept acceptPack = ooPackage(0,sn,nodeID,pack.orangeSource,'a',pack.requestedGraphPosition,pack.blueAddressIP,pack.blueAddressPort,pack.priority) #Serialize the package bytePacket = acceptPack.serialize() #Puts the package to the outputQueue outputQueue.put(bytePacket) else: #I did not request that node print("I dont have a problem with the request of the blueNode: %d from the orangeNode: %d" % (pack.requestedGraphPosition,pack.orangeSource)) #Creates a accept acceptPack = ooPackage(0,sn,nodeID,pack.orangeSource,'a',pack.requestedGraphPosition,pack.blueAddressIP,pack.blueAddressPort,pack.priority) #Serialize the package bytePacket = acceptPack.serialize() #Puts the package to the outputQueue outputQueue.put(bytePacket) #Marks the node as requested table.markNodeAsRequested(pack.requestedGraphPosition) elif pack.communicationType == 'd': #This is a declined package print("Received ack type declined from orangeNode: %d about the request of the blueNode: %d and my request was: %d" % (pack.orangeSource,pack.requestedGraphPosition,requestNode)) #Append the ack to the acks list acks.append('d') #Checks if the acks list is done. The list is done when the size is MAXORANGENODES - 1 if len(acks) == MAXORANGENODES - 1: acksDone = True print("Received all the acks for the requestNode: %d" % (requestNode)) elif pack.communicationType == 'a': #This is a accept package print("Received ack type accept from orangeNode: %d about the request of the blueNode: %d and my request was: %d" % (pack.orangeSource,pack.requestedGraphPosition,requestNode)) #Append the ack to the acks list acks.append('a') #Checks if the acks list is done. The list is done when the size is MAXORANGENODES - 1 if len(acks) == MAXORANGENODES - 1: acksDone = True print("Received all the acks for the requestNode: %d" % (requestNode)) elif pack.communicationType == 'w': #This is a write package print("Received write package from orangeNode: %d about the request of the blueNode: %d" % (pack.orangeSource,pack.requestedGraphPosition)) #Writes the node IP and Port into the blueTable address = (pack.blueAddressIP,pack.blueAddressPort) table.write(pack.requestedGraphPosition,address) #Creates the saved package savedPack = ooPackage(0,sn,nodeID,pack.orangeSource,'s',pack.requestedGraphPosition,blueNodeIP,blueNodePort,pack.priority) byteSavedPack = savedPack.serialize() outputQueue.put(byteSavedPack) else: ##This is a saved package print("Received saved ack from orangeNode:%d about the request of the blueNode: %d my request is: %d" % (pack.orangeSource,pack.requestedGraphPosition,requestNode)) #Apeend the ack to the acksWrite list acksWrite.append('s') #Checks if the list is done. The list is done when the size is MAXORANGENODES-1 if len(acksWrite) == MAXORANGENODES-1: ##Stop the timer print("All the nodes wrote the blueNode %d with the ip %s port %d" % (requestNode,blueNodeIP,blueNodePort)) else: #Orange & Blue Tiene que mandar uno a la vez. Hay que ver como implementar eso pack = obPackage() pack.unserialize(bytePacket) if pack.communicationType == 'e': #Enroll package print("I just receive a enroll package from the blueNode IP: %s Port: %d" % (pack.blueAddressIP,pack.blueAddressPort)) #Creates the request packages blueNodeIP = pack.blueAddressIP blueNodePort = pack.blueAddressPort requestNode = table.obtainAvailableNode() priority = random.randrange(4294967294) for node in range(0,MAXORANGENODES): if not node == nodeID: requestPack = ooPackage(0,sn,nodeID,node,'r',requestNode,blueNodeIP,blueNodePort,priority) requestPack.print_data() byteRequestPack = requestPack.serialize() outputQueue.put(byteRequestPack) #Once the acks list is done. Send the write package if acksDone == True: #Creates the writePackages for node in range(0,MAXORANGENODES): if not node == nodeID: writePack = ooPackage(0,sn,nodeID,node,'w',requestNode,blueNodeIP,blueNodePort,priority) writePack.print_data() byteWritePack = writePack.serialize() outputQueue.put(byteWritePack) #Once the acksWrite list is done. Send the commit package if acksDone == True: print("Creating the commitPackage for the requestNode: %d to the blueNode IP: %s Port: %d" % (requestNode,blueNodeIP,blueNodePort)) #Creates the commitPackage neighborList = table.obtainNodesNeighborsAdressList(requestNode) commitPack = obPackage(1,sn,nodeID,node,'c',requestNode,blueNodeIP,blueNodePort,neighborList) commitPack.print_data() byteCommitPack = writePack.serialize() outputQueue.put(byteCommitPack)
def logicThread(self): while True: # If the queue is not empty if not self.inputQueue.empty(): # print("IM in") inputPack = self.inputQueue.get() bytePackage = inputPack[0] addr = inputPack[1] # print(inputPack) Type = int.from_bytes(bytePackage[:1], byteorder='big') genericPack = obPackage() if Type == 3: print("(EXIST Resp) from ", addr) genericPack.unserialize(bytePackage, 3) print("The File (", str(genericPack.fileIDByte1), str(genericPack.fileIDRest), ") Exist") if (genericPack.fileIDByte1, genericPack.fileIDRest) in self.existsMap: print("Enviando1") responseAddr = self.existsMap[(genericPack.fileIDByte1, genericPack.fileIDRest)] test = obPackage(3) test.fileIDByte1 = genericPack.fileIDByte1 test.fileIDRest = genericPack.fileIDRest test.print_data() test = genericPack.serialize(3) self.SecureUDP.sendto(test, responseAddr[1], responseAddr[2]) del self.existsMap[(genericPack.fileIDByte1, genericPack.fileIDRest)] else: print("Error!!! That file no longer exist") elif Type == 20: #Going to recieve a new file genericPack.unserialize(bytePackage, 20) print("New File: ", genericPack.fileName) #genericPack.print_data() #Sends the id of the file to the user responseNewFilePack = obPackage(2) responseNewFilePack.fileIDByte1 = self.fileIDByte1 responseNewFilePack.fileIDRest = self.fileIDRest byteResponseNewFilePack = responseNewFilePack.serialize(2) self.SecureUDP.sendto(byteResponseNewFilePack, addr[0], addr[1]) #Separates the file into chunks self.chunkSeparator(genericPack.fileName, self.fileIDByte1, self.fileIDRest) self.fileIDRest += 1 elif Type == 21: print("(Get) from ") getPack = obPackage(21) getPack.unserialize(bytePackage, 21) getPack.print_data() TimeStamp = time.time() ListBlueNodes = {} print(getPack.fileName) self.getMap[(getPack.fileIDByte1, getPack.fileIDRest)] = (TimeStamp, addr[0], addr[1], ListBlueNodes, getPack.fileName) getPack.packetCategory = 6 serializedObject = getPack.serialize(6) self.SecureUDP.sendto(serializedObject, self.BlueIP, self.BluePort) elif Type == 2: print("(Exist) from ", addr) existsPack = obPackage(2) existsPack.unserialize(bytePackage, 2) TimeStamp = time.time() self.existsMap[(existsPack.fileIDByte1, existsPack.fileIDRest)] = (TimeStamp, addr[0], addr[1]) serializedObject = existsPack.serialize(2) self.SecureUDP.sendto(serializedObject, self.BlueIP, self.BluePort) elif Type == 8: print("(Locate) from ", addr) locatePack = obPackage(8) locatePack.unserialize(bytePackage, 8) TimeStamp = time.time() ListBlueNodes = [] self.locateMap[(locatePack.fileIDByte1, locatePack.fileIDRest)] = (TimeStamp, addr[0], addr[1], ListBlueNodes) serializedObject = locatePack.serialize(8) self.SecureUDP.sendto(serializedObject, self.BlueIP, self.BluePort) elif Type == 9: print("(Locate Res) from ", addr) locateResPack = obPackage(9) locateResPack.unserialize(bytePackage, 9) fileIDByte1 = locateResPack.fileIDByte1 fileIDRest = locateResPack.fileIDRest #If theres a request for that FileID if (fileIDByte1, fileIDRest) in self.locateMap: #If the TimeOut is not over if (time.time() - self.locateMap[ (fileIDByte1, fileIDRest)][0]) <= 10: #Add the blueNode to the listBlueNodes self.locateMap[(fileIDByte1, fileIDRest)][3].append( str(locateResPack.nodeID)) elif Type == 10: print("(Delete) from ", addr) deletePack = obPackage(10) deletePack.unserialize(bytePackage, 10) if (deletePack.fileIDByte1, deletePack.fileIDRest) in self.fileDataBase: del self.fileDataBase[(deletePack.fileIDByte1, deletePack.fileIDRest)] serializedObject = deletePack.serialize(10) self.SecureUDP.sendto(serializedObject, self.BlueIP, self.BluePort) else: print("I dont have that fileID") elif Type == 4: #UNTESTED UNTESTED print("(COMPLETE) from ", addr) completePack = obPackage(Type) completePack.unserialize(bytePackage, Type) TimeStamp = time.time() ListChunkIDs = [False] * self.fileDataBase[ completePack.fileIDByte1, completePack.fileIDRest][ 1] #obtain chunkNumber from filedatabase self.completeMap[(completePack.fileIDByte1, completePack.fileIDRest)] = ( TimeStamp, addr[0], addr[1], ListChunkIDs) serializedObject = completePack.serialize(Type) self.SecureUDP.sendto(serializedObject, self.BlueIP, self.BluePort) elif Type == 5: #UNTESTED UNTESTED print("(COMPLETE RES) from ", addr) completeResPack = obPackage(Type) completeResPack.unserialize(bytePackage, Type) fileIDByte1 = completeResPack.fileIDByte1 fileIDRest = completeResPack.fileIDRest chunkID = completeResPack.chunkID chunk = completeResPack.chunkPayload print(completeResPack.chunkPayload) #If theres a request for that FileID if (fileIDByte1, fileIDRest) in self.completeMap: #If the TimeOut is not over if (time.time() - self.completeMap[ (fileIDByte1, fileIDRest)][0]) <= 10: #Add the blueNode to the DictChunks self.completeMap[(fileIDByte1, fileIDRest)][3][chunkID] = True elif Type == 6: print("(Get) from ", addr) getPack = obPackage(6) getPack.unserialize(bytePackage, 6) TimeStamp = time.time() chunksList = [] filename = self.fileDataBase[(getPack.fileIDByte1, getPack.fileIDRest)][0] print(filename) self.getMap[(getPack.fileIDByte1, getPack.fileIDRest)] = (TimeStamp, addr[0], addr[1], chunksList, filename) serializedObject = getPack.serialize(6) self.SecureUDP.sendto(serializedObject, self.BlueIP, self.BluePort) elif Type == 7: print("(Get Res) from ", addr) getResPack = obPackage(7) getResPack.unserialize(bytePackage, 7) fileIDByte1 = getResPack.fileIDByte1 fileIDRest = getResPack.fileIDRest chunkID = getResPack.chunkID chunk = getResPack.chunkPayload # print(getResPack.chunkPayload) #If theres a request for that FileID if (fileIDByte1, fileIDRest) in self.getMap: #If the TimeOut is not over if (time.time() - self.getMap[ (fileIDByte1, fileIDRest)][0]) <= 10: #Add the blueNode to the DictChunks self.getMap[(fileIDByte1, fileIDRest)][3][chunkID] = chunk else: #Checks TimeOuts for the exist request. TimeOut is 5s for request in list(self.existsMap): if (time.time() - self.existsMap[request][0]) >= 5: print("self.existsMap[request]") genericPack = obPackage(3) serializedObject = genericPack.serialize(3) self.SecureUDP.sendto(serializedObject, self.existsMap[request][1], self.existsMap[request][2]) del self.existsMap[request] #Checks TimeOuts for the Locate request. TimeOut is 10s for request in list(self.locateMap): if (time.time() - self.locateMap[request][0]) > 10: print("Sending locateMap for ", request) locateListPack = obPackage(20) locateListPack.fileName = ';'.join( self.locateMap[request] [3]) #1;2;...;4 NodeID;NodeID;...;NodeID byteLocateListPack = locateListPack.serialize(20) self.SecureUDP.sendto(byteLocateListPack, self.locateMap[request][1], self.locateMap[request][2]) del self.locateMap[request] #Checks TimeOuts for the Complete request. TimeOut is 10s for request in list(self.completeMap): if (time.time() - self.completeMap[request][0]) > 10: print("Sending completeMap for ", request) completeResponsePack = obPackage(5) completeResponsePack.fileIDByte1, completeResponsePack.fileIDRest = request allpacksavailable = all( chunkID == True for chunkID in self.completeMap[request][3]) completeResponsePack.chunkID = allpacksavailable bytecompleteResponsePack = completeResponsePack.serialize( 5) self.SecureUDP.sendto(bytecompleteResponsePack, self.completeMap[request][1], self.completeMap[request][2]) del self.completeMap[request] #Checks TimeOuts for the Get request. TimeOut is 10s for request in list(self.getMap): if (time.time() - self.getMap[request][0]) > 10: print("Writing the getMap for ", request) listChunks = self.getMap[request][3] if len(listChunks) > 0: listChunks = sorted(listChunks.items()) filename = self.getMap[request][4] filename = "ArchivosReensamblado/" + filename fd = os.open(filename, os.O_RDWR | os.O_CREAT) for chunk in listChunks: print("[", str(chunk[0]), "] ", end='') os.write(fd, chunk[1]) os.close(fd) print(" ") # getResPack = obPackage(7) # byteGetResPack = getResPack.serialize(7) # self.SecureUDP.sendto(byteGetResPack,self.getMap[request][1],self.getMap[request][2]) resp = obPackage(21) resp.fileIDByte1 = request[0] resp.fileIDRest = request[1] resp.fileName = filename byteResp = resp.serialize(21) self.SecureUDP.sendto(byteResp, self.getMap[request][1], self.getMap[request][2]) del self.getMap[request]
def logicalThread(inputQueue, outputQueue, sock, table, nodeID, maxOrangeNodes, debug,secureUDPBlue): file2 = open("logicThread.out", "w+") file2.truncate(0) processingBlueNode = False requestNode = -1 requestNodeWon = False blueNodeIP = "0.0.0.0" blueNodePort = 8888 MAXORANGENODES = maxOrangeNodes acks = {} # Map de ACK {key:node id, value:"a"} acksWrite = {} acksDone = False # True when all the acks have been received, False otherwise acksWriteDone = False # True when all the acksWrite have been received, False otherwise priority = -1 sn = 0 stop_eventMainThread = Event() # If this is true then I received all the acks stop_eventTimerThread = Event() # If this is true then the times up flagTimesUp = False # True when the timer thread finished before receiving all the acks # Event object used to send signals from one thread to another stop_eventWrite = Event() # True when the timer thread finished before receiving all the acks flagWriteTimesUp = False testingConfli = 0 blueNodeEnrolls = queue.Queue() # Pasarla como parámetro blueNodes = [] # Tuple with (requestNode,blueNodeIP,blueNodePort). Saves all the connections with the blueNodes cheackingDebug = 0 workDone = False while True: # If the queue is not empty if not inputQueue.empty(): # Takes a package from the inputQueue. bytePacket = inputQueue.get() # this determines what type of packet it is (Orange&Orange = 0 or Orange&Blue = 1 ) if int.from_bytes(bytePacket[:1], byteorder='little') == 0: # Orage & Orange pack = ooPackage() pack.unserialize(bytePacket) if pack.communicationType == 'r': # This a request package requestPackProcessing(outputQueue,table,nodeID,debug,file2,requestNode,priority,sn,blueNodeIP,blueNodePort,pack,blueNodeEnrolls,stop_eventMainThread,processingBlueNode) elif pack.communicationType == 'd': # This is a declined package if debug == True: print("(Declined) from orangeNode: %d about the request of: %d and my request was: %d" % ( pack.orangeSource, pack.requestedGraphPosition, requestNode)) # If this is a declined for my request if requestNode == pack.requestedGraphPosition: # We send a signal that the other thread should stop. stop_eventMainThread.set() requestNodeWon = False requestNode = -1 processingBlueNode = False else: if debug == True: print("This is a old ack") elif pack.communicationType == 'a': # This is a accept package print("(Accept) from orangeNode: %d about the request of: %d and my request was: %d" % ( pack.orangeSource, pack.requestedGraphPosition, requestNode)) # If this is a accept for my request if requestNode == pack.requestedGraphPosition: # Adds the ack to the map acks[pack.orangeSource] = 'a' flagNoAck = False for keyNode in acks: # print(acks[keyNode]) if acks[keyNode] == 'x': # print("found one") flagNoAck = True break if flagNoAck == False: # I got all the acks acksDone = True else: if debug == True: print("This is a old ack") elif pack.communicationType == 'w': # This is a write package print("(Write) the blueNode: %d" % (pack.requestedGraphPosition)) # Writes the node IP and Port into the blueTable address = (pack.blueAddressIP, pack.blueAddressPort) table.write(pack.requestedGraphPosition, address) file2.write("Wrote the request %d\n" % (pack.requestedGraphPosition)) # Creates the saved package savedPack = ooPackage(0, sn, nodeID, pack.orangeSource, 's', pack.requestedGraphPosition, blueNodeIP, blueNodePort, pack.priority) byteSavedPack = savedPack.serialize() outputQueue.put(byteSavedPack) else: # This is a saved package print("(Saved) from orangeNode:%d about the request of: %d my request is: %d" % ( pack.orangeSource, pack.requestedGraphPosition, requestNode)) if pack.requestedGraphPosition == requestNode: # Adds the ack to the map acksWrite[pack.orangeSource] = 's' flagNoAckSaved = False for keyNode in acksWrite: # print(acksWrite[keyNode]) if acksWrite[keyNode] == 'x': # print("found one") flagNoAckSaved = True break if flagNoAckSaved == False: # I got all the acks # print("Got all writeAcks") # print("Local variables requestWon: %s mainThread %s timerThread %s" %(requestNodeWon,stop_eventMainThread.is_set(),stop_eventTimerThread.is_set())) acksWriteDone = True else: if debug == True: print("This is a old ack") else: # Orange & Blue Tiene que mandar uno a la vez. Hay que ver como implementar eso pack = obPackage() pack.unserialize(bytePacket,16) blueNodeEnrolls.put(pack) if not requestNode == -1: # Once the acks list is done. Send the write package (if u won the request) if acksDone == True: if debug == True: print("\tReceived all the acks for the requestNode: %d" % ( requestNode)) file2.write("I won my request %d\n" % (requestNode)) stop_eventMainThread.set() # stop_event.clear() requestNodeWon = True if requestNodeWon == True: # Writes the address address = (blueNodeIP, blueNodePort) table.write(requestNode, address) flagTimesUp = True # Creates the writePackages for node in range(0, MAXORANGENODES): if not node == nodeID: writePack = ooPackage( 0, sn, nodeID, node, 'w', requestNode, blueNodeIP, blueNodePort, priority) byteWritePack = writePack.serialize() outputQueue.put(byteWritePack) # Resets the variables acks.clear() acksDone = False flagTimesUp == True cheackingDebug += 1 stop_eventMainThread.clear() stop_eventTimerThread.clear() # print("Local variables requestWon: %s mainThread %s timerThread %s" %(requestNodeWon,stop_eventMainThread.is_set(),stop_eventTimerThread.is_set())) # Checks if the acks Timer is done else: if requestNodeWon == False and stop_eventMainThread.is_set() == False and stop_eventTimerThread.is_set() == True: print("\tTimes Up Acks from the request %d" % (requestNode)) stop_eventMainThread.clear() stop_eventTimerThread.clear() flagTimesUp == False # Needs to resend the package sn += 1 # print("Local Variables request: %d requestWon %s blueIP: %s bluePort: %d maxOrange: %d acks :%d acksWrite :%d acksDone: %s acksWriteDone: %s Prio: %d sn :%d" %(requestNode,requestNodeWon,blueNodeIP,blueNodePort, MAXORANGENODES,len(acks), len(acksWrite),acksDone,acksWriteDone,priority,sn)) for node in range(0, MAXORANGENODES): if acks[node] == 'x': requestPack = ooPackage( 0, sn, nodeID, node, 'r', requestNode, blueNodeIP, blueNodePort, priority) if debug == True: print( "\t\tResending the request pack to the orangeNode %d" % (node)) byteRequestPack = requestPack.serialize() outputQueue.put(byteRequestPack) # Creates the Timer Thread timeout = 5 * sn # Waits 5 seconds t = threading.Thread(target=timer, args=( timeout, stop_eventMainThread, stop_eventTimerThread, )) t.start() # Once the acksWrite list is done. Send the commit package if requestNodeWon == True and acksWriteDone == True: if debug == True: print("\tReceived all the acksWrite for the requestNode: %d" % ( requestNode)) # if debug == True: print("Creating the commitPackage for the requestNode: %d to the blueNode IP: %s Port: %d" % (requestNode,blueNodeIP,blueNodePort)) # Creates the commitPackage # neighborList = table.obtainNodesNeighborsAdressList(requestNode) # for neighbor in neighborList: # if neighbor[2] == 0: #No tengo la IP y Port del nodo # commitPack = obPackage(15,requestNode,neighbor[0]) # test = commitPack.serialize(15) # secureUDPBlue.sendto(test,blueNodeIP,blueNodePort) # else: # commitPack = obPackage(16,requestNode,neighbor[0],neighbor[1],neighbor[2]) # test = commitPack.serialize(16) # secureUDPBlue.sendto(test,blueNodeIP,blueNodePort) if debug == True: print("Creating a (commit) package") blueNodes.append((requestNode,blueNodeIP,blueNodePort)) print("(Done) with the blueNode %d" % (requestNode)) # Creates the commitPackage neighborList = table.obtainNodesNeighborsAdressList(requestNode) for neighbor in neighborList: # print(neighbor[0],neighbor[1],neighbor[2]) if neighbor[2] == 0: #No tengo la IP y Port del nodo commitPack = obPackage(15,requestNode,neighbor[0]) test = commitPack.serialize(15) secureUDPBlue.sendto(test,blueNodeIP,blueNodePort) else: commitPack = obPackage(16,requestNode,neighbor[0],neighbor[1],neighbor[2]) test = commitPack.serialize(16) secureUDPBlue.sendto(test,blueNodeIP,blueNodePort) #Resets the variables processingBlueNode = False requestNode = -1 requestNodeWon = False blueNodeIP = "0.0.0.0" blueNodePort = 8888 MAXORANGENODES = maxOrangeNodes acksWrite.clear() acksWriteDone = False # True when all the acksWrite have been received, False otherwise priority = -1 sn = nodeID flagTimesUp = False # Checks if the acksWrite Timer is done elif requestNodeWon == True: if stop_eventMainThread.is_set() == False and stop_eventTimerThread.is_set() == True: print("\tTimes Up WriteAcks from the requesr %d" % (requestNode)) stop_eventMainThread.clear() stop_eventTimerThread.clear() # Needs to resend the package sn += 1 # print("Local Variables request: %d requestWon %s blueIP: %s bluePort: %d maxOrange: %d acks :%d acksWrite :%d acksDone: %s acksWriteDone: %s Prio: %d sn :%d" %(requestNode,requestNodeWon,blueNodeIP,blueNodePort, MAXORANGENODES,len(acks), len(acksWrite),acksDone,acksWriteDone,priority,sn)) for node in range(0, MAXORANGENODES): if acksWrite[node] == 'x': requestPack = ooPackage( 0, sn, nodeID, node, 'w', requestNode, blueNodeIP, blueNodePort, priority) if debug == True: print( "\t\tResending the write pack to the orangeNode %d" % (node)) byteRequestPack = requestPack.serialize() outputQueue.put(byteRequestPack) # Creates the Timer Thread timeout = 5 * sn # Waits 5 seconds t = threading.Thread(target=timer, args=( timeout, stop_eventMainThread, stop_eventTimerThread, )) t.start() if processingBlueNode == False: # If the queue is not empty takes out a new request if not blueNodeEnrolls.empty(): # Takes a new request pack = obPackage() pack = blueNodeEnrolls.get() # Creates the request packages blueNodeIP = pack.blueAddressIP blueNodePort = pack.blueAddressPort requestNode = table.obtainAvailableNode() #print("Testing %d" % (requestNode)) sn = 0 # if debug == True: print("(Enroll) from the blueNode IP: %s Port: %d requesting %d" % (pack.blueAddressIP,pack.blueAddressPort,requestNode)) if not requestNode == -1: # Checks if there is more requestNodes if debug == True: print("NodeId: %d"%(pack.nodeID)) print("(Enroll) from the blueNode IP: %s Port: %d requesting %d" % ( pack.blueAddressIP, pack.blueAddressPort, requestNode)) testingConfli += 1 priority = random.randrange(4294967294) # Marks the node as requested table.markNodeAsRequested(requestNode) flagTimesUp == False processingBlueNode = True stop_eventMainThread.clear() stop_eventTimerThread.clear() # print("Local Variables request: %d requestWon %s blueIP: %s bluePort: %d maxOrange: %d acks :%d acksWrite :%d acksDone: %s acksWriteDone: %s Prio: %d sn :%d" %(requestNode,requestNodeWon,blueNodeIP,blueNodePort, MAXORANGENODES,len(acks), len(acksWrite),acksDone,acksWriteDone,priority,sn)) for node in range(0, MAXORANGENODES): if node == nodeID: acks[node] = 'a' acksWrite[node] = 's' elif not node == nodeID: # Fills with an x (not ack recived)------------------------------------------------- acks[node] = "x" acksWrite[node] = 'x' requestPack = ooPackage( 0, sn, nodeID, node, 'r', requestNode, blueNodeIP, blueNodePort, priority) # if debug == True: requestPack.print_data() #print("NodeID: %d, node: %d" % (nodeID, node)) byteRequestPack = requestPack.serialize() outputQueue.put(byteRequestPack) # Creates the Timer Thread timeout = 5 #Waits 5s t = threading.Thread(target=timer, args=( timeout, stop_eventMainThread, stop_eventTimerThread, )) t.start() else: print("No more requestNumers available") #Sends the graphComplete package to the blueNodes, when theres no more blueNodesID's to assign if table.tableIsDone() == True and workDone == False: for element in range(len(blueNodes)): addr = blueNodes[element] completeGraph = obPackage(17) bytePacket = completeGraph.serialize(17) print("sending completeGraph to ",addr[1],str(addr[2])) secureUDPBlue.sendto(bytePacket,addr[1],addr[2]) workDone = True file2.flush()
def logicalThread(self): while True: package = self.packageQueue.get(block=True,timeout=None) #(payload,addr) # print(package) bytePackage = package[0] Type = int.from_bytes(bytePackage[:1], byteorder='big') genericPack = obPackage() if Type == 0: print("(PutChunk) from ",package[1]) chunkPack = obPackage() chunkPack.unserialize(bytePackage,0) actions = ["save","save&Clone","clone","drop"] percentages = [0.80,0,0.20,0] # 80% save 0% save&Clone %20 clone 0% drop result = 0 if self.chunksStored < self.maxChunks: result = self.putChunkRandomChoiceGenerator(percentages) else: percentages = [0,0,1,0] # 0% save 0% save&Clone %100 clone 0% drop result = self.putChunkRandomChoiceGenerator(percentages) print("Accion ",actions[result]) if actions[result] == "save": #Checks if the key exists if (chunkPack.fileIDByte1,chunkPack.fileIDRest) in self.blueSavedChunks: #If the key exists then just append the new chunkID and chunkPayload self.blueSavedChunks[(chunkPack.fileIDByte1,chunkPack.fileIDRest)].append((chunkPack.chunkID,chunkPack.chunkPayload)) else: #If not then creates a list witht the chunkID and chunkPayload and assign it to the key tempList = [(chunkPack.chunkID,chunkPack.chunkPayload)] self.blueSavedChunks[(chunkPack.fileIDByte1,chunkPack.fileIDRest)] = tempList self.chunksStored += 1 elif actions[result] == "clone": #1 Neighbor randomNeighbor = random.choice(list(self.neighborTuple)) self.SecureUDP.sendto(bytePackage,self.neighborTuple[randomNeighbor][0],self.neighborTuple[randomNeighbor][1]) # listNode = self.getSpanningTreeNodes((package[1][0],package[1][1])) # print("Path ",listNode) #If theres a path to take # if len(listNode) != 0: # serializedPutChunkPack = chunkPack.serialize(0) # for node in listNode: # self.SecureUDP.sendto(serializedPutChunkPack,node[0],node[1]) # totalNeighbors = len(self.neighborTuple) # if totalNeighbors > 0: # copyNeighborTuple = self.neighborTuple.copy() # #Picks random how many neighbors are gonna get the chunk # randomNeighbors = totalNeighbors #random.randrange(1,totalNeighbors+1) # #Picks random one neighbor # for x in range(randomNeighbors): # randomChoiceNeighbor = random.choice(list(copyNeighborTuple)) # # #Creates a putChunk package # serializedPutChunkPack = chunkPack.serialize(0) # # print("Clone to",copyNeighborTuple[randomChoiceNeighbor][0],str(copyNeighborTuple[randomChoiceNeighbor][1])) # self.SecureUDP.sendto(serializedPutChunkPack,copyNeighborTuple[randomChoiceNeighbor][0],copyNeighborTuple[randomChoiceNeighbor][1]) # del copyNeighborTuple[randomChoiceNeighbor] elif actions[result] == "save&Clone": #Checks if the key exists if (chunkPack.fileIDByte1,chunkPack.fileIDRest) in self.blueSavedChunks: #If the key exists then just append the new chunkID and chunkPayload self.blueSavedChunks[(chunkPack.fileIDByte1,chunkPack.fileIDRest)].append((chunkPack.chunkID,chunkPack.chunkPayload)) else: #If not then creates a list witht the chunkID and chunkPayload and assign it to the key tempList = [] tempList.append((chunkPack.chunkID,chunkPack.chunkPayload)) self.blueSavedChunks[(chunkPack.fileIDByte1,chunkPack.fileIDRest)] = tempList self.chunksStored += 1 listNode = self.getSpanningTreeNodes((package[1][0],package[1][1])) # print("Path ",listNode) #If theres a path to take if len(listNode) != 0: serializedPutChunkPack = chunkPack.serialize(0) for node in listNode: self.SecureUDP.sendto(serializedPutChunkPack,node[0],node[1]) # totalNeighbors = len(self.neighborTuple) # if totalNeighbors > 0: # copyNeighborTuple = self.neighborTuple.copy() # #Picks random how many neighbors are gonna get the chunk # randomNeighbors = random.randrange(1,totalNeighbors+1) # #Picks random one neighbor # for x in range(randomNeighbors): # randomChoiceNeighbor = random.choice(list(copyNeighborTuple)) # #Creates a putChunk package # serializedPutChunkPack = chunkPack.serialize(0) # # print("Save Clone to",copyNeighborTuple[randomChoiceNeighbor][0],str(copyNeighborTuple[randomChoiceNeighbor][1])) # self.SecureUDP.sendto(serializedPutChunkPack,copyNeighborTuple[randomChoiceNeighbor][0],copyNeighborTuple[randomChoiceNeighbor][1]) # del copyNeighborTuple[randomChoiceNeighbor] # #If theres less than 40 chunks saved, then save the chunk # if self.chunksStored < self.maxChunks: # #Checks if the key exists # if (chunkPack.fileIDByte1,chunkPack.fileIDRest) in self.blueSavedChunks: # #If the key exists then just append the new chunkID and chunkPayload # self.blueSavedChunks[(chunkPack.fileIDByte1,chunkPack.fileIDRest)].append((chunkPack.chunkID,chunkPack.chunkPayload)) # else: # #If not then creates a list witht the chunkID and chunkPayload and assign it to the key # tempList = [] # tempList.append((chunkPack.chunkID,chunkPack.chunkPayload)) # self.blueSavedChunks[(chunkPack.fileIDByte1,chunkPack.fileIDRest)] = tempList # self.chunksStored += 1 # #Otherwise it sends the chunk to the neighbors # else: # #Creates a putChunk package # serializedPutChunkPack = chunkPack.serialize(0) # listNode = self.getSpanningTreeNodes((package[1][0],package[1][1])) # print("Path ",listNode) # #If theres a path to take # if len(listNode) != 0: # for node in listNode: # self.SecureUDP.sendto(serializedPutChunkPack,node[0],node[1]) elif Type == 2: print("(Exist) from ",package[1]) genericPack.unserialize(bytePackage,2) #Checks if I have a chunk of that file if (genericPack.fileIDByte1,genericPack.fileIDRest) in self.blueSavedChunks: genericPack.packetCategory = 3 responseExist = genericPack.serialize(3) self.SecureUDP.sendto(responseExist,package[1][0],package[1][1]) else: # print("I dont have a chunk. I need to ask my spanningTree") listNode = self.getSpanningTreeNodes((package[1][0],package[1][1])) # print("Path ",listNode) #If theres a path to take if len(listNode) != 0: self.existsMap[(genericPack.fileIDByte1,genericPack.fileIDRest)] = (package[1][0],package[1][1]) for node in listNode: #Creates a Exists package existsPack = obPackage(2) existsPack.fileIDByte1 = genericPack.fileIDByte1 existsPack.fileIDRest = genericPack.fileIDRest byteExistsPack = existsPack.serialize(2) self.SecureUDP.sendto(byteExistsPack,node[0],node[1]) elif Type == 3: print("(Exist R) from ",package[1]) responseExist = obPackage(3) responseExist.unserialize(bytePackage,3) if (responseExist.fileIDByte1,responseExist.fileIDRest) in self.existsMap: #If theres a exist request for that file id addr = self.existsMap[(responseExist.fileIDByte1,responseExist.fileIDRest)] del self.existsMap[(responseExist.fileIDByte1,responseExist.fileIDRest)] byteResponseExist = responseExist.serialize(3) self.SecureUDP.sendto(byteResponseExist,addr[0],addr[1]) else: print("I dont have a exist request for that file") elif Type == 17: print("(GraphComplete) from ",package[1]) print("vecinos ",self.neighborTuple) #If im the blueNode 0 then im the root of the spanningTree if self.myID == self.root: print("Hey im the root") self.imInTheSpanningTree = True #If not then join the spanningTree else: time.sleep(5) #ask for daddy # if self.root in self.neighborTuple: # addr = self.neighborTuple[1] # self.sTreeDadNode = (self.root,addr[0],addr[1]) # self.imInTheSpanningTree = True # else: self.sTreeDadNode = self.joinSpanningTree() #(node, IP, PORT) print("Im part of the spanning Tree ",self.myID) self.imInTheSpanningTree = True #Sends the daddyPack daddyPackage = obPackage(13) daddyPackage.nodeID = self.myID bytesDaddyPackage = daddyPackage.serialize(13) self.SecureUDP.sendto(bytesDaddyPackage, self.sTreeDadNode[1], self.sTreeDadNode[2])
def requestPackProcessing(outputQueue,table,nodeID,debug,file2,requestNode,priority,sn,blueNodeIP,blueNodePort,pack,blueNodeEnrolls,stop_eventMainThread,processingBlueNode): # Marks the node as requested table.markNodeAsRequested(pack.requestedGraphPosition) if debug == True: print("(Request) from: %s requesting the number: %d with the priority: %d " % ( pack.orangeSource, pack.requestedGraphPosition, pack.priority)) if pack.requestedGraphPosition == requestNode: # If I request the same number if pack.priority < priority: # If my priority is bigger then I win if debug == True: print("\tI won the request of the blueNode: %d (myID: %d myPriority: %d) (otherNodeID: %d otherNodeIDpriority: %d)" % ( requestNode, nodeID, priority, pack.orangeSource, pack.priority)) # Creates a decline package declinedPack = ooPackage( 0, sn, nodeID, pack.orangeSource, 'd', requestNode, blueNodeIP, blueNodePort, priority) # Serialize the package bytePacket = declinedPack.serialize() # Puts the package to the outputQueue outputQueue.put(bytePacket) elif pack.priority > priority: # If my priority is smaller then the other node wins if debug == True: print("\tI lost the request of the blueNode: %d (myID: %d myPriority: %d) (otherNodeID: %d otherNodeIDpriority: %d)" % ( requestNode, nodeID, priority, pack.orangeSource, pack.priority)) # Creates a accept acceptPack = ooPackage( 0, sn, nodeID, pack.orangeSource, 'a', pack.requestedGraphPosition, pack.blueAddressIP, pack.blueAddressPort, pack.priority) # Serialize the package bytePacket = acceptPack.serialize() # Puts the package to the outputQueue outputQueue.put(bytePacket) # If i was requesting the same number put my requestNode to -1 and clear the timer signals requestNode = -1 # Creates a new joinGraph package obPack2 = obPackage(16,0,0,blueNodeIP,blueNodePort) byteobPack = obPack2.serialize(16) blueNodeEnrolls.put(byteobPack) # We send a signal that the other thread should stop. stop_eventMainThread.set() processingBlueNode = False else: # When both priorities are equal if debug == True: print("\tWe draw the request of the blueNode: %d (myID: %d myPriority: %d) (otherNodeID: %d otherNodeIDpriority: %d)" % ( requestNode, nodeID, priority, pack.orangeSource, pack.priority)) # Checks the nodeID and the bigger wins if nodeID > pack.orangeSource: # I win if debug == True: print("\t\tI won the request of the blueNode:%d SecondRound (myID:%d) (otherNodeID:%d)" % ( requestNode, nodeID, pack.orangeSource)) # Creates a decline package declinedPack = ooPackage( 0, sn, nodeID, pack.orangeSource, 'd', requestNode, blueNodeIP, blueNodePort, priority) # Serialize the package bytePacket = declinedPack.serialize() # Puts the package to the outputQueue outputQueue.put(bytePacket) else: # The other node wins if debug == True: print("\t\tI lost the request of the blueNode:%d SecondRound (myID:%d) (otherNodeID:%d)" % ( requestNode, nodeID, pack.orangeSource)) # Creates a accept acceptPack = ooPackage( 0, sn, nodeID, pack.orangeSource, 'a', pack.requestedGraphPosition, pack.blueAddressIP, pack.blueAddressPort, pack.priority) # Serialize the package bytePacket = acceptPack.serialize() # Puts the package to the outputQueue outputQueue.put(bytePacket) # Creates a new joinGraph package obPack2 = obPackage(16,0,0,blueNodeIP,blueNodePort) byteobPack = obPack2.serialize(16) blueNodeEnrolls.put(byteobPack) # If i was requesting the same number put my requestNode to -1 and clear the timer signals requestNode = -1 # We send a signal that the other thread should stop. stop_eventMainThread.set() processingBlueNode = False else: # I did not request that node if debug == True: print("\tI dont have a problem with the request of the blueNode: %d from the orangeNode: %d" % ( pack.requestedGraphPosition, pack.orangeSource)) # Creates an accept package acceptPack = ooPackage(0, sn, nodeID, pack.orangeSource, 'a', pack.requestedGraphPosition, pack.blueAddressIP, pack.blueAddressPort, pack.priority) # Serialize the package bytePacket = acceptPack.serialize() # Puts the package to the outputQueue outputQueue.put(bytePacket)
def inputThread(self): while True: payload , addr = self.SecureUDP.recivefrom() # print(payload) Type = int.from_bytes(payload[:1], byteorder='big') if Type == 12 or Type == 18: spanningTreePack = obPackage(Type) spanningTreePack.unserialize(payload,Type) self.spanningTreeMsgQueue.put((spanningTreePack.packetCategory,spanningTreePack.nodeID,addr[0],addr[1])) elif Type == 11: #JoinTree print("(JoinTree)") joinTreeResponsePack = obPackage() if self.imInTheSpanningTree == True: joinTreeResponsePack.packetCategory = 12 #IDo pack joinTreeResponsePack.nodeID = self.myID byteJoinTreeResponsePack = joinTreeResponsePack.serialize(12) self.SecureUDP.sendto(byteJoinTreeResponsePack,addr[0],addr[1]) else: joinTreeResponsePack.packetCategory = 18 #IDoNot pack joinTreeResponsePack.nodeID = self.myID byteJoinTreeResponsePack = joinTreeResponsePack.serialize(18) self.SecureUDP.sendto(byteJoinTreeResponsePack,addr[0],addr[1]) elif Type == 13: print("(Daddy)") DaddyPack = obPackage(13) DaddyPack.unserialize(payload,13) self.sTreeSonsNodes.append((DaddyPack.nodeID,addr[0],addr[1])) elif Type == 1: print("(Hello)") helloPack = obPackage(1) helloPack.unserialize(payload,1) self.neighborTuple[helloPack.nodeID] = (addr[0],addr[1]) elif Type == 15: print("(NeighborNoAddrs)") obPackagex = obPackage() obPackagex.unserialize(payload,15) self.neighborTuple[obPackagex.neighborID] = (-1,-1) self.myID = obPackagex.nodeID elif Type == 16: print("(Neighbor) from ") obPackagex = obPackage() obPackagex.unserialize(payload,16) self.neighborTuple[obPackagex.neighborID] = (obPackagex.blueAddressIP,obPackagex.blueAddressPort) self.myID = obPackagex.nodeID #Creates the hello pack helloPack = obPackage(1) helloPack.nodeID = self.myID serializedHelloPack = helloPack.serialize(1) self.SecureUDP.sendto(serializedHelloPack,obPackagex.blueAddressIP,obPackagex.blueAddressPort) elif Type == 8: print("(Locate) from ",addr[0],str(addr[1])) genericPack = obPackage() genericPack.unserialize(payload,8) self.locateMap[(genericPack.fileIDByte1,genericPack.fileIDRest)] = (addr[0],addr[1]) #Checks if I have a chunk of that file if (genericPack.fileIDByte1,genericPack.fileIDRest) in self.blueSavedChunks: print("I have a chunk") responseLocate = obPackage(9) responseLocate.fileIDByte1 = genericPack.fileIDByte1 responseLocate.fileIDRest = genericPack.fileIDRest responseLocate.nodeID = self.myID byteResponseLocate = responseLocate.serialize(9) self.SecureUDP.sendto(byteResponseLocate,addr[0],addr[1]) # print("I need to ask my spanningTree") listNode = self.getSpanningTreeNodes((addr[0],addr[1])) #If theres a path to take if (len(listNode) != 0): for node in listNode: #Creates a Exists package print("Creando Locate ",node) existsPack = obPackage(8) existsPack.fileIDByte1 = genericPack.fileIDByte1 existsPack.fileIDRest = genericPack.fileIDRest byteExistsPack = existsPack.serialize(8) self.SecureUDP.sendto(byteExistsPack,node[0],node[1]) elif Type == 9: print("(LocateRes) from ",addr[0],str(addr[1])) responseLocate = obPackage(9) responseLocate.unserialize(payload,9) # responseLocate.print_data() if (responseLocate.fileIDByte1,responseLocate.fileIDRest) in self.locateMap: #If theres a exist request for that file id addrs = self.locateMap[(responseLocate.fileIDByte1,responseLocate.fileIDRest)] responseLocate = responseLocate.serialize(9) self.SecureUDP.sendto(responseLocate,addrs[0],addrs[1]) else: print("I dont have a exist request for that file") elif Type == 6: print("(Get) from ",addr[0],str(addr[1])) genericPack = obPackage() genericPack.unserialize(payload,6) self.getMap[(genericPack.fileIDByte1,genericPack.fileIDRest)] = (addr[0],addr[1]) #Checks if I have a chunk of that file if (genericPack.fileIDByte1,genericPack.fileIDRest) in self.blueSavedChunks: for chunk in self.blueSavedChunks[(genericPack.fileIDByte1,genericPack.fileIDRest)]: print("I have a chunk") responseGet = obPackage(7) responseGet.fileIDByte1 = genericPack.fileIDByte1 responseGet.fileIDRest = genericPack.fileIDRest responseGet.chunkID = chunk[0] responseGet.chunkPayload = chunk[1] byteResponseGet= responseGet.serialize(7) self.SecureUDP.sendto(byteResponseGet,addr[0],addr[1]) #Asks the neighbors listNode = self.getSpanningTreeNodes((addr[0],addr[1])) #If theres a path to take if (len(listNode) != 0): for node in listNode: #Creates a Exists package print("Creando Get ",node) getPack = obPackage(6) getPack.fileIDByte1 = genericPack.fileIDByte1 getPack.fileIDRest = genericPack.fileIDRest byteGetPack = getPack.serialize(6) self.SecureUDP.sendto(byteGetPack,node[0],node[1]) elif Type == 7: print("(GetRes) from ",addr[0],str(addr[1])) responseGet = obPackage(7) responseGet.unserialize(payload,7) if (responseGet.fileIDByte1,responseGet.fileIDRest) in self.getMap: #If theres a exist request for that file id addrs = self.getMap[(responseGet.fileIDByte1,responseGet.fileIDRest)] byteResponseGet = responseGet.serialize(7) self.SecureUDP.sendto(byteResponseGet,addrs[0],addrs[1]) else: print("I dont have a exist request for that file") elif Type == 4: #UNTESTED UNTESTED UNTESTED print("(Complete) from", addr) genericPack = obPackage(Type) genericPack.unserialize(payload,Type) self.completeMap[(genericPack.fileIDByte1,genericPack.fileIDRest)] = (addr[0],addr[1]) #Checks if I have a chunk of that file for fileIDTuple, chunkList in self.blueSavedChunks.items(): if fileIDTuple == (genericPack.fileIDByte1,genericPack.fileIDRest): for chunkTuple in chunkList: responceComplete = obPackage(5) responceComplete.packetCategory = 5 responceComplete.fileIDByte1 = genericPack.fileIDByte1 responceComplete.fileIDRest = genericPack.fileIDRest responceComplete.chunkID = chunkTuple[0] byteresponseComplete = responceComplete.serialize(5) self.SecureUDP.sendto(byteresponseComplete,addr[0],addr[1]) #Sending complete request to the rest of the spanning three listNode = self.getSpanningTreeNodes((addr[0],addr[1])) # print("Path ",listNode) #If theres a path to take if len(listNode) != 0: for node in listNode: bytecompletePack = genericPack.serialize(Type) self.SecureUDP.sendto(bytecompletePack, node[0], node[1]) elif Type == 5: #UNTESTED UNTESTED UNTESTED print("(Complete R) from ",addr) responseComplete = obPackage(Type) responseComplete.unserialize(payload,Type) if (responseComplete.fileIDByte1,responseComplete.fileIDRest) in self.completeMap: #If theres a complete request for that file id addr = self.completeMap[(responseComplete.fileIDByte1,responseComplete.fileIDRest)] #del self.completeMap[(responseComplete.fileIDByte1,responseComplete.fileIDRest)] byteresponseComplete = responseComplete.serialize(Type) self.SecureUDP.sendto(byteresponseComplete,addr[0],addr[1]) else: print("I dont have a complete request for that file") elif Type == 10: print("(Delete) from ",addr) deletePack = obPackage(Type) deletePack.unserialize(payload,Type) #If I have that FileID delete all the chunks if (deletePack.fileIDByte1,deletePack.fileIDRest) in self.blueSavedChunks: totalChunks = len(self.blueSavedChunks[(deletePack.fileIDByte1,deletePack.fileIDRest)]) self.chunksStored -= totalChunks del self.blueSavedChunks[(deletePack.fileIDByte1,deletePack.fileIDRest)] #Sending delete request to the rest of the spanning three listNode = self.getSpanningTreeNodes((addr[0],addr[1])) # print("Path ",listNode) #If theres a path to take if len(listNode) != 0: for node in listNode: byteDeletePack = deletePack.serialize(Type) self.SecureUDP.sendto(byteDeletePack, node[0], node[1]) else: self.packageQueue.put((payload,addr))