Пример #1
0
    def __init__(self, version_label, my_node, conf_path, routing_m_mod,
                 lookup_m_mod, experimental_m_mod, private_dht_name,
                 bootstrap_mode):
        self.bootstrapper = bootstrap.OverlayBootstrapper(conf_path)
        my_addr = my_node.addr
        self._my_id = my_node.id  # id indicated by user
        if not self._my_id:
            self._my_id = self._my_id = identifier.RandomId()  # random id
        self._my_node = Node(my_addr, self._my_id, version=version_label)
        self.msg_f = message.MsgFactory(version_label, self._my_id,
                                        private_dht_name)
        self._querier = Querier()
        self._routing_m = routing_m_mod.RoutingManager(self._my_node,
                                                       self.msg_f,
                                                       self.bootstrapper)

        self._responder = responder.Responder(self._my_id, self._routing_m,
                                              self.msg_f, bootstrap_mode)
        self._tracker = self._responder._tracker

        self._lookup_m = lookup_m_mod.LookupManager(self._my_id, self.msg_f,
                                                    self.bootstrapper)
        self._experimental_m = experimental_m_mod.ExperimentalManager(
            self._my_node.id, self.msg_f)

        current_ts = time.time()
        self._next_maintenance_ts = current_ts
        self._next_timeout_ts = current_ts
        self._next_main_loop_call_ts = current_ts
        self._cached_lookups = []
Пример #2
0
    def __init__(self, dht_addr, state_path, routing_m_mod, lookup_m_mod):
        self.state_filename = os.path.join(state_path, STATE_FILENAME)
        self.load_state()
        if not self._my_id:
            self._my_id = identifier.RandomId()
        self._my_node = Node(dht_addr, self._my_id)
        self._tracker = tracker.Tracker()
        self._token_m = token_manager.TokenManager()

        self._reactor = ThreadedReactor()
        self._reactor.listen_udp(self._my_node.addr[1],
                                 self._on_datagram_received)
        #self._rpc_m = RPCManager(self._reactor)
        self._querier = Querier(self._my_id)
        bootstrap_nodes = self.loaded_nodes or BOOTSTRAP_NODES
        del self.loaded_nodes
        self._routing_m = routing_m_mod.RoutingManager(self._my_node, 
                                                       bootstrap_nodes)
#        self._responder = Responder(self._my_id, self._routing_m,
#                                    self._tracker, self._token_m)

        self._lookup_m = lookup_m_mod.LookupManager(self._my_id)
        current_time = time.time()
        self._next_maintenance_ts = current_time
        self._next_save_state_ts = current_time + SAVE_STATE_DELAY
        
        self._running = False
Пример #3
0
    def __init__(self, dht_addr):
        self.my_addr = dht_addr
        self.my_id = identifier.RandomId()
        self.my_node = Node(self.my_addr, self.my_id)
        self.tracker = tracker.Tracker()
        self.token_m = token_manager.TokenManager()

        self.reactor = ThreadedReactor()
        self.rpc_m = RPCManager(self.reactor, self.my_addr[1])
        self.querier = Querier(self.rpc_m, self.my_id)
        self.routing_m = RoutingManager(self.my_node, self.querier,
                                        bootstrap_nodes)
        self.responder = Responder(self.my_id, self.routing_m,
                                   self.tracker, self.token_m)

        self.responder.set_on_query_received_callback(
            self.routing_m.on_query_received)
        self.querier.set_on_response_received_callback(
            self.routing_m.on_response_received)
        self.querier.set_on_error_received_callback(
            self.routing_m.on_error_received)
        self.querier.set_on_timeout_callback(self.routing_m.on_timeout)
        self.querier.set_on_nodes_found_callback(self.routing_m.on_nodes_found)

        self.routing_m.do_bootstrap()

        self.rpc_m.add_msg_callback(QUERY,
                                    self.responder.on_query_received)

        self.lookup_m = LookupManager(self.my_id, self.querier,
                                      self.routing_m)
Пример #4
0
    def __init__(self, dht_addr):
        my_addr = dht_addr
        my_id = identifier.RandomId()
        my_node = Node(my_addr, my_id)
        tracker_ = tracker.Tracker()
        token_m = token_manager.TokenManager()

        self.reactor = ThreadedReactor()
        rpc_m = RPCManager(self.reactor, my_addr[1])
        querier_ = Querier(rpc_m, my_id)
        routing_m = RoutingManager(my_node, querier_, bootstrap_nodes)
        responder_ = Responder(my_id, routing_m, tracker_, token_m)

        responder_.set_on_query_received_callback(routing_m.on_query_received)
        querier_.set_on_response_received_callback(
            routing_m.on_response_received)
        querier_.set_on_error_received_callback(routing_m.on_error_received)
        querier_.set_on_timeout_callback(routing_m.on_timeout)
        querier_.set_on_nodes_found_callback(routing_m.on_nodes_found)

        routing_m.do_bootstrap()

        rpc_m.add_msg_callback(QUERY, responder_.on_query_received)

        self.lookup_m = LookupManager(my_id, querier_, routing_m)
        self._routing_m = routing_m
Пример #5
0
 def _get_maintenance_lookup(self, lookup_target=None, nodes=[]):
     if not lookup_target:
         lookup_target = identifier.RandomId()
     if not nodes:
         log_distance = lookup_target.distance(self.my_node.id).log
         nodes = self.get_closest_rnodes(log_distance, 0, True)
     return lookup_target, nodes
 def _send_maintenance_ping(self, node_):
     m_bucket, r_bucket = self.table.buckets[0]
     if m_bucket.there_is_room() and r_bucket.there_is_room():
         target = identifier.RandomId()
         msg = message.OutgoingFindNodeQuery(self.my_node.id, target)
         m_bucket, r_bucket = self.table.buckets[0]
         print 'FIND_NODE(%d:%d:%d)' % (0, len(m_bucket), len(r_bucket)),
     else:
         print 'PING',
         msg = self.ping_msg
     if node_.id:
         m_bucket, r_bucket = self.table.buckets[0]
         print 'to %r - %d:%d:%d --' % (node_.addr,
                                        node_.log_distance(self.my_node),
                                        len(m_bucket), len(r_bucket))
     else:
         print 'to UNKNOWN id'
     self.querier.send_query(msg, node_)
    def __init__(self, version_label,
                 my_node, state_filename,
                 routing_m_mod, lookup_m_mod,
                 experimental_m_mod,
                 private_dht_name,
                 bootstrap_mode):

        if size_estimation:
            self._size_estimation_file = open('size_estimation.dat', 'w')


        self.state_filename = state_filename
        saved_id, saved_bootstrap_nodes = state.load(self.state_filename)
        my_addr = my_node.addr
        self._my_id = my_node.id # id indicated by user
        if not self._my_id:
            self._my_id = saved_id # id loaded from file
        if not self._my_id:
            self._my_id = self._my_id = identifier.RandomId() # random id
        self._my_node = Node(my_addr, self._my_id, version=version_label)
        self.msg_f = message.MsgFactory(version_label, self._my_id,
                                        private_dht_name)
        self._querier = Querier()
        self._routing_m = routing_m_mod.RoutingManager(
            self._my_node, saved_bootstrap_nodes, self.msg_f)

        self._responder = responder.Responder(self._my_id, self._routing_m,
                                              self.msg_f, bootstrap_mode)
        self._tracker = self._responder._tracker

        self._lookup_m = lookup_m_mod.LookupManager(self._my_id, self.msg_f)
        self._experimental_m = experimental_m_mod.ExperimentalManager(
            self._my_node.id, self.msg_f)

        current_ts = time.time()
        self._next_save_state_ts = current_ts + SAVE_STATE_DELAY
        self._next_maintenance_ts = current_ts
        self._next_timeout_ts = current_ts
        self._next_main_loop_call_ts = current_ts
        self._pending_lookups = []
        self._cached_lookups = {}
Пример #8
0

def peers_found(peers):
    print 'Peers found:', time.time()
    return
    for peer in peers:
        print peer
    print '-' * 20


def lookup_done():
    print 'Lookup DONE'


info_hashes = (
    identifier.RandomId(),
    identifier.Id('28f2e5ea2bf87eae4bcd5e3fc9021844c01a4df9'),
    identifier.RandomId(),
    identifier.Id('dd5c25b4b8230e108fbf9d07f87a86c6b05c9b6d'),
    identifier.RandomId(),
    identifier.Id('bcbdb9c2e7b49c65c9057431b492cb7957c8a330'),
    identifier.RandomId(),
    identifier.Id('d93df7a507f3c9d2ebfbe49762a217ab318825bd'),
    identifier.RandomId(),
    identifier.Id('6807e5d151e2ac7ae92eabb76ddaf4237e4abb60'),
    identifier.RandomId(),
    identifier.Id('83c7b3b7d36da4df289670592be68f9dc7c7096e'),
    identifier.RandomId(),
    identifier.Id('9b16aecf952597f9bb051fecb7a0d8475d060fa0'),
    identifier.RandomId(),
    identifier.Id('24f2446365d3ef782ec16ad63aea1206df4b8d21'),
Пример #9
0
def peers_found(peers):
    print 'Peers found:'
    for peer in peers:
        print peer
    print '-' * 20


def lookup_done():
    print 'Lookup DONE'


if len(sys.argv) == 4 and sys.argv[0] == 'server_dht.py':
    log.critical('argv %r' % sys.argv)
    RUN_DHT = True
    my_addr = (sys.argv[1], int(sys.argv[2]))  #('192.16.125.242', 7000)
    logs_path = sys.argv[3]
    dht = kadtracker.KadTracker(my_addr, logs_path)
else:
    RUN_DHT = False
    print 'usage: python server_dht.py dht_ip dht_port path'

try:
    print 'Type Control-C to exit.'
    while (RUN_DHT):
        time.sleep(10 * 60)
        info_hash = identifier.RandomId()
        dht.get_peers(info_hash, peers_found)
except (KeyboardInterrupt):
    dht.stop()