Beispiel #1
0
 def __init__(self, host, port, peer_sock, local_sock, start_port, port_range):
     self.node = NetNode(host, port)
     self.router = Router(self.node)
     self.data = Data()
     self.peer_server = Simple_Server(peer_sock, self)
     self.local_listener = Simple_Listener(local_sock, self)
     self.start_port = start_port
     self.port_range = port_range
Beispiel #2
0
 def __init__(self, host, port, peer_sock, local_sock, start_port, port_range):
     self.node = NetNode(host, port)
     self.router = Router(self.node)
     self.data = Data()
     self.peer_server = Simple_Server(peer_sock, self)
     self.local_listener = Simple_Listener(local_sock, self)
     self.start_port = start_port
     self.port_range = port_range
Beispiel #3
0
class PeerHead:
    def __init__(self, host, port, peer_sock, local_sock, start_port, port_range):
        self.node = NetNode(host, port)
        self.router = Router(self.node)
        self.data = Data()
        self.peer_server = Simple_Server(peer_sock, self)
        self.local_listener = Simple_Listener(local_sock, self)
        self.start_port = start_port
        self.port_range = port_range

    def start_peer_server(self):
        self.peer_server.serve()

    def start_local_listener(self):
        self.local_listener.serve()

    def findPeer(self, host):
        # the function below finds the host we contacted
        r_node = self.router.find_peer(host, self.start_port, self.port_range)
        # The remote host and remote port will be set as the DHT entry point for this node
        self.router.setEntry(r_node)
        return str(r_node)

    def joinNetwork(self):
        # LOOKUP OF THIS NODE:#####################################################
        # If the Router gave us a peer, then we can lookup in the network
        # First call lookup( ) to get the location where we belong in the network (succ(my_id))
        # Lookup of hashed_ID will give us the succ
        succ = self.router.lookup(self.node.hash, self.router.getEntry())
        # print ("Did lookup! Host: " + succ.host + " at  Port: " + str(succ.port) + " Hash: " + succ.hash)

        if type(succ) is not str and self.node.hash == succ.hash:
            return "Already a member of network"

        # INSERT NODE#############################################################
        # Insert sets pred, succ after coordination in the network
        pred = self.router.insert(self.node.hash, succ)
        # print ("Did insert! Host: " + pred.host + " at  Port: " + str(pred.port) + " Hash: " + pred.hash)

        # We can set pred and succ now, probably could add error checks (for null) and a loop to retry
        self.router.setPred(pred)
        # they should also be set at the same time so they are consistent in the network
        self.router.setSucc(succ)
        # the successor should default to succ as well
        self.router.setEntry(succ)
        return "Joined at: {}".format(str(succ))

    def getFullNetStatus(self):
        return (
            "######### Node "
            + str(self.router.port)
            + " Routing Information########|\n"
            + "SELF:"
            + self.node.hash
            + "|"
            + self.node.host
            + "|"
            + str(self.node.port)
            + "|\n"
            "PRED:"
            + self.router.getPred().hash
            + "|"
            + self.router.getPred().host
            + "|"
            + str(self.router.getPred().port)
            + "|\n"
            "SUCC:"
            + self.router.getSucc().hash
            + "|"
            + self.router.getSucc().host
            + "|"
            + str(self.router.getSucc().port)
            + "|\n"
        )

    def getCSVNetStatus(self):
        return (
            str(self.router.port)
            + ","
            + self.node.hash
            + ","
            + self.node.host
            + ","
            + str(self.node.port)
            + ","  # this node info
            + self.router.getPred().hash
            + ","
            + self.router.getPred().host
            + ","
            + str(self.router.getPred().port)
            + ","  # pred node
            + self.router.getSucc().hash
            + ","
            + self.router.getSucc().host
            + ","
            + str(self.router.getSucc().port)
        )  # succ node
Beispiel #4
0
class PeerHead:
    def __init__(self, host, port, peer_sock, local_sock, start_port,
                 port_range):
        self.node = NetNode(host, port)
        self.router = Router(self.node)
        self.data = Data()
        self.peer_server = Simple_Server(peer_sock, self)
        self.local_listener = Simple_Listener(local_sock, self)
        self.start_port = start_port
        self.port_range = port_range

    def start_peer_server(self):
        self.peer_server.serve()

    def start_local_listener(self):
        self.local_listener.serve()

    def findPeer(self, host):
        #the function below finds the host we contacted
        r_node = self.router.find_peer(host, self.start_port, self.port_range)
        #The remote host and remote port will be set as the DHT entry point for this node
        self.router.setEntry(r_node)
        return str(r_node)

    def joinNetwork(self):
        #LOOKUP OF THIS NODE:#####################################################
        #If the Router gave us a peer, then we can lookup in the network
        #First call lookup( ) to get the location where we belong in the network (succ(my_id))
        #Lookup of hashed_ID will give us the succ
        succ = self.router.lookup(self.node.hash, self.router.getEntry())
        #print ("Did lookup! Host: " + succ.host + " at  Port: " + str(succ.port) + " Hash: " + succ.hash)

        if type(succ) is not str and self.node.hash == succ.hash:
            return "Already a member of network"

        #INSERT NODE#############################################################
        #Insert sets pred, succ after coordination in the network
        pred = self.router.insert(self.node.hash, succ)
        #print ("Did insert! Host: " + pred.host + " at  Port: " + str(pred.port) + " Hash: " + pred.hash)

        # We can set pred and succ now, probably could add error checks (for null) and a loop to retry
        self.router.setPred(pred)
        # they should also be set at the same time so they are consistent in the network
        self.router.setSucc(succ)
        # the successor should default to succ as well
        self.router.setEntry(succ)
        return "Joined at: {}".format(str(succ))

    def getFullNetStatus(self):
        return ("######### Node " + str(self.router.port) +
                " Routing Information########|\n" + "SELF:" + self.node.hash +
                "|" + self.node.host + "|" + str(self.node.port) + "|\n"
                "PRED:" + self.router.getPred().hash + "|" +
                self.router.getPred().host + "|" +
                str(self.router.getPred().port) + "|\n"
                "SUCC:" + self.router.getSucc().hash + "|" +
                self.router.getSucc().host + "|" +
                str(self.router.getSucc().port) + "|\n")

    def getCSVNetStatus(self):
        return (str(self.router.port) + "," + self.node.hash + "," +
                self.node.host + "," + str(self.node.port) +  #this node info
                "," + self.router.getPred().hash + "," +
                self.router.getPred().host + "," +
                str(self.router.getPred().port) +  #pred node
                "," + self.router.getSucc().hash + "," +
                self.router.getSucc().host + "," +
                str(self.router.getSucc().port))  #succ node