Exemplo n.º 1
0
    def __init__(self,
                 udpPort=4000,
                 dataStore=None,
                 routingTable=None,
                 networkProtocol=None,
                 reactor=None):
        """
        @param dataStore: The data store to use. This must be class inheriting
                          from the C{DataStore} interface (or providing the
                          same API). How the data store manages its data
                          internally is up to the implementation of that data
                          store.
        @type dataStore: entangled.kademlia.datastore.DataStore
        @param routingTable: The routing table to use. Since there exists some
                             ambiguity as to how the routing table should be
                             implemented in Kademlia, a different routing table
                             may be used, as long as the appropriate API is
                             exposed.
        @type routingTable: entangled.kademlia.routingtable.RoutingTable
        @param networkProtocol: The network protocol to use. This can be
                                overridden from the default to (for example)
                                change the format of the physical RPC messages
                                being transmitted.
        @type networkProtocol: entangled.kademlia.protocol.KademliaProtocol
        """
        if reactor is None:
            reactor = twisted.internet.reactor
        self.reactor = reactor
        self.id = self._generateID()
        self.port = udpPort
        # This will contain a deferred created when joining the network, to enable publishing/retrieving information from
        # the DHT as soon as the node is part of the network (add callbacks to this deferred if scheduling such operations
        # before the node has finished joining the network)
        self._joinDeferred = None
        # Create k-buckets (for storing contacts)
        #self._buckets = []
        #for i in range(160):
        #    self._buckets.append(kbucket.KBucket())
        if routingTable == None:
            self._routingTable = routingtable.OptimizedTreeRoutingTable(
                self.id)

        # Initialize this node's network access mechanisms
        if networkProtocol == None:
            self._protocol = protocol.KademliaProtocol(self)
        else:
            self._protocol = networkProtocol
        # Initialize the data storage mechanism used by this node
        if dataStore == None:
            self._dataStore = datastore.DictDataStore()
        else:
            self._dataStore = dataStore
            # Try to restore the node's state...
            if 'nodeState' in self._dataStore:
                state = self._dataStore['nodeState']
                self.id = state['id']
                for contactTriple in state['closestNodes']:
                    contact = Contact(contactTriple[0], contactTriple[1],
                                      contactTriple[2], self._protocol)
                    self._routingTable.addContact(contact)
Exemplo n.º 2
0
    def __init__(self, my_ip, my_port, my_guid):
        self._peers = {}
        self._callbacks = defaultdict(list)
        self._port = my_port
        self._ip = my_ip
        self._guid = my_guid
        self._uri = 'tcp://%s:%s' % (self._ip, self._port)

        # Routing table
        self._routingTable = routingtable.OptimizedTreeRoutingTable(self.guid)
        self._dataStore = datastore.MongoDataStore()
        self._knownNodes = []

        self._log = logging.getLogger(self.__class__.__name__)
Exemplo n.º 3
0
    def __init__(self, transport, market_id, settings, db_connection):

        self.log = logging.getLogger('[%s] %s' %
                                     (market_id, self.__class__.__name__))
        self.settings = settings
        self.knownNodes = []
        self.searches = []
        self.search_keys = {}
        self.activePeers = []
        self.republishThreads = []
        self.transport = transport
        self.market_id = market_id

        # Routing table
        self.routingTable = routingtable.OptimizedTreeRoutingTable(
            self.settings['guid'], market_id)
        self.dataStore = datastore.SqliteDataStore(db_connection)
Exemplo n.º 4
0
    def __init__(self, market_id, settings):

        self._log = logging.getLogger('[%s] %s' %
                                      (market_id, self.__class__.__name__))
        self._settings = settings

        self._knownNodes = []

        self._searches = []

        self._searchKeys = {}
        self._activePeers = []

        self._republishThreads = []

        # Routing table
        self._routingTable = routingtable.OptimizedTreeRoutingTable(
            self._settings['guid'])
        self._dataStore = datastore.MongoDataStore()
Exemplo n.º 5
0
    def __init__(self, market_id, settings):

        self._log = logging.getLogger('[%s] %s' %
                                      (market_id, self.__class__.__name__))
        self._settings = settings

        self._knownNodes = []
        self._shortlist = {}
        self._activePeers = []
        self._alreadyContacted = {}
        self._activeProbes = {}
        self._findValueResult = {}
        self._pendingIterationCalls = []
        self._slowNodeCount = [0]
        self._contactedNow = 0
        self._dhtCallbacks = []
        self._republishThreads = []

        # Routing table
        self._routingTable = routingtable.OptimizedTreeRoutingTable(
            self._settings['guid'])
        self._dataStore = datastore.MongoDataStore()
Exemplo n.º 6
0
    def __init__(self, node_id=None, udpPort=4000, dataStore=None,
                 routingTableClass=None, networkProtocol=None,
                 externalIP=None):
        """
        @param dataStore: The data store to use. This must be class inheriting
                          from the C{DataStore} interface (or providing the
                          same API). How the data store manages its data
                          internally is up to the implementation of that data
                          store.
        @type dataStore: entangled.kademlia.datastore.DataStore
        @param routingTable: The routing table class to use. Since there exists
                             some ambiguity as to how the routing table should be
                             implemented in Kademlia, a different routing table
                             may be used, as long as the appropriate API is
                             exposed. This should be a class, not an object,
                             in order to allow the Node to pass an
                             auto-generated node ID to the routingtable object
                             upon instantiation (if necessary).
        @type routingTable: entangled.kademlia.routingtable.RoutingTable
        @param networkProtocol: The network protocol to use. This can be
                                overridden from the default to (for example)
                                change the format of the physical RPC messages
                                being transmitted.
        @type networkProtocol: entangled.kademlia.protocol.KademliaProtocol
        """
        self.node_id = node_id or self._generateID()
        self.port = udpPort
        self._listeningPort = None  # object implementing Twisted
        # IListeningPort This will contain a deferred created when
        # joining the network, to enable publishing/retrieving
        # information from the DHT as soon as the node is part of the
        # network (add callbacks to this deferred if scheduling such
        # operations before the node has finished joining the network)
        self._joinDeferred = None
        self.next_refresh_call = None
        self.change_token_lc = task.LoopingCall(self.change_token)
        # Create k-buckets (for storing contacts)
        if routingTableClass is None:
            self._routingTable = routingtable.OptimizedTreeRoutingTable(self.node_id)
        else:
            self._routingTable = routingTableClass(self.node_id)

        # Initialize this node's network access mechanisms
        if networkProtocol is None:
            self._protocol = protocol.KademliaProtocol(self)
        else:
            self._protocol = networkProtocol
        # Initialize the data storage mechanism used by this node
        self.token_secret = self._generateID()
        self.old_token_secret = None
        if dataStore is None:
            self._dataStore = datastore.DictDataStore()
        else:
            self._dataStore = dataStore
            # Try to restore the node's state...
            if 'nodeState' in self._dataStore:
                state = self._dataStore['nodeState']
                self.node_id = state['id']
                for contactTriple in state['closestNodes']:
                    contact = Contact(
                        contactTriple[0], contactTriple[1], contactTriple[2], self._protocol)
                    self._routingTable.addContact(contact)
        self.externalIP = externalIP
        self.hash_watcher = HashWatcher()