def joinTheData(param): if param == '': return "the node is not valid" try: param = int(param) except: return "the join node data value, is not valid" server = dhtServerManager.GetServerByDataValue(param) if server == None: dhtServer4 = DHTServer() dhtServer4.DHTNodeDataValue = DHTNodeDataValue(param) dhtServerManager.AssignHostAndPort(dhtServer4) socketAddress4 = dhtServer4.GetDHTSocketAddress() serverLovestValue = dhtServerManager.GetTheServerWithTheLowestDataValue( ) msg = DHTMessage(DHTMessageType.Join) msg.Argument = dhtServer4.Host msg.Tag = str(dhtServer4.Port) msg.Nodevalue = str(param) return dhtServerManager.SendRequestTo(serverLovestValue, msg) else: return 'chord table already has this node'
def AddShortcutToNode(nodeValue, shortcutNodeValue): if (not nodeValue) or nodeValue == '': return "the node to add the shortcut to, is not valid" if (not shortcutNodeValue) or shortcutNodeValue == '': return "the shortcut node to add the shortcut to, is not valid" try: nodeValue = int(nodeValue) except: return "the node data value for the node to add the shortcut to, is not valid" try: shortcutNodeValue = int(shortcutNodeValue) except: return "the shortcut node data value, is not valid" server = dhtServerManager.GetServerByDataValue(nodeValue) if server == None: return "the node to add the shortcut to, was not found" shortcutserver = dhtServerManager.GetServerByDataValue(shortcutNodeValue) if shortcutserver == None: return "the node is supposed to be added as a shortcut, was not found" msg = DHTMessage(DHTMessageType.Shortcut) msg.Argument = shortcutserver.Host msg.Tag = str(shortcutserver.Port) return dhtServerManager.SendRequestTo(server, msg)
def OnRequest(self, dataBytes): dataStr = dataBytes.decode("utf-8") msg = DHTMessage.deserialize(dataStr) if self.OnRequestHandler != None: self.OnRequestHandler(msg) return str(self.handleMessage(msg))
def List(): server = dhtServerManager.GetTheServerWithTheLowestDataValue() if server == None: click.echo("the Ring is not initialized!!") return result = dhtServerManager.SendRequestTo(server, DHTMessage(DHTMessageType.List)) click.echo(result)
def GetNextSuccessorDataValueAsString(self, socketAddresses_tuple): nextSuccessorDataAsString = "" nextSuccessorDataAsString += self.getValueFromTheCurrentRequest(socketAddresses_tuple, self.NextSuccessorDHTServerSocketAddress) if nextSuccessorDataAsString == "": nextSuccessorDataAsString = DHTSocket.OpenASocketAndSendTheRequest(self.NextSuccessorDHTServerSocketAddress.Host, self.NextSuccessorDHTServerSocketAddress.Port, DHTMessage(DHTMessageType.DataAsString)) return nextSuccessorDataAsString
def GetShortcutsDataValueAsString(self, socketAddresses_tuple): shortcuts = '' if self.FingerDHTServerSocketAddresses: for fingerSocketAddress in self.FingerDHTServerSocketAddresses: res = '' res += self.getValueFromTheCurrentRequest(socketAddresses_tuple, fingerSocketAddress) if res == None or res == '': shortcuts += DHTSocket.OpenASocketAndSendTheRequest(fingerSocketAddress.Host, fingerSocketAddress.Port, DHTMessage(DHTMessageType.DataAsString)) + ',' else: shortcuts += res + ',' if shortcuts == '': shortcuts += ',' return shortcuts
def ping(host, port): result = dhtServerManager.PingTheHostAndPort( host, int(port), DHTMessage(DHTMessageType.Ping)) click.echo(result)