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 do_maintenance(self): queries_to_send = [] maintenance_lookup_target = None if self._maintenance_mode == BOOTSTRAP_MODE: try: node_ = self.bootstrap_nodes.next() queries_to_send = [self._get_maintenance_query(node_)] except (StopIteration): maintenance_lookup_target = self.my_node.id self._maintenance_mode = FILL_BUCKETS elif self._maintenance_mode == FILL_BUCKETS: if self._num_pending_filling_lookups: self._num_pending_filling_lookups -= 1 maintenance_lookup_target = identifier.RandomId() else: self._maintenance_mode = NORMAL_MODE elif self._maintenance_mode == NORMAL_MODE: for _ in range(len(self._maintenance_tasks)): # We try maintenance tasks till one of them actually does work # or we have tried them all (whatever happens first) We loop # in range because I'm going to modify self._maintenance_tasks task = self._maintenance_tasks.pop(0) self._maintenance_tasks.append(task) node_ = task() if node_: queries_to_send = [self._get_maintenance_query(node_)] # This task did do some work. We are done here! break return (_MAINTENANCE_DELAY[self._maintenance_mode], queries_to_send, maintenance_lookup_target)
def main(options, args): if not os.path.isdir(options.path): if os.path.exists(options.path): print >> sys.stderr, 'FATAL:', options.path, 'must be a directory' return print >> sys.stderr, options.path, 'does not exist. Creating directory...' os.mkdir(options.path) logs_path = options.path if options.lookup_delay and not options.daemon: print >> sys.stderr, 'Switching to DAEMON mode (no user interface)' if options.lookup_delay or options.daemon: # redirect output stdout_file = os.path.join(options.path, 'pymdht.stdout') stderr_file = os.path.join(options.path, 'pymdht.stderr') print >> sys.stderr, 'Redirecting output to %s and %s' % (stdout_file, stderr_file) sys.stdout = open(stdout_file, 'w') sys.stderr = open(stderr_file, 'w') my_addr = (options.ip, int(options.port)) my_id = None if options.node_id: base_id = identifier.Id(options.node_id) my_id = base_id.generate_close_id(options.log_distance) my_node = node.Node(my_addr, my_id, version=pymdht.VERSION_LABEL) if options.debug: logs_level = logging.DEBUG # This generates HUGE (and useful) logs else: # logs_level = logging.INFO # This generates some (useful) logs logs_level = logging.WARNING # This generates warning and error logs print 'Using the following plug-ins:' print '*', options.routing_m_file print '*', options.lookup_m_file print '*', options.experimental_m_file print 'Path:', options.path print 'Private DHT name:', options.private_dht_name print 'debug mode:', options.debug print 'bootstrap mode:', options.bootstrap_mode print 'Swift tracker port:', options.swift_port routing_m_name = '.'.join(os.path.split(options.routing_m_file))[:-3] routing_m_mod = __import__(routing_m_name, fromlist=['']) lookup_m_name = '.'.join(os.path.split(options.lookup_m_file))[:-3] lookup_m_mod = __import__(lookup_m_name, fromlist=['']) experimental_m_name = '.'.join(os.path.split( options.experimental_m_file))[:-3] experimental_m_mod = __import__(experimental_m_name, fromlist=['']) dht = pymdht.Pymdht(my_node, logs_path, routing_m_mod, lookup_m_mod, experimental_m_mod, options.private_dht_name, logs_level, options.bootstrap_mode, options.swift_port) if options.lookup_delay: loop_forever = not options.num_lookups remaining_lookups = options.num_lookups while loop_forever or remaining_lookups: time.sleep(options.lookup_delay) if options.lookup_target: target = identifier.Id(options.lookup_target) else: target = identifier.RandomId() print 'lookup', target dht.get_peers(None, target, None, options.announce_port) remaining_lookups = remaining_lookups - 1 time.sleep(options.stop_delay) dht.stop() elif options.ttl: stop_timestamp = time.time() + int(options.ttl) while time.time() < stop_timestamp: time.sleep(1) dht.stop() elif options.daemon: # Just loop for ever while True: time.sleep(10) elif options.gui: import wx import ui.gui app = wx.PySimpleApp() frame = ui.gui.Interactive_GUI(None, "Interactive Look@MDHT", None, (1440, 900), dht, logs_path) frame.Show(True) app.MainLoop() elif options.telnet_port: import ui.telnet telnet_ui = ui.telnet.Telnet(dht, options.telnet_port) telnet_ui.start() elif options.cli: import ui.cli ui.cli.command_user_interface(dht)
if peers: msg = 'got %d peers\n' % len(peers) else: msg = 'END OF LOOKUP\n' button.text += msg class LookupButton(Button): def __init__(self): Button.__init__(self, text='DHT lookup\n') def on_press(self): dht.get_peers( self, identifier.Id('e936e73881ee1920b8edbd263d001fffed424c5f'), _on_peers_handler) class MyApp(App): def build(self): return LookupButton() if __name__ in ('__android__', '__main__'): my_addr = ('127.0.0.1', 7000) my_node = node.Node(my_addr, identifier.RandomId(), version=pymdht.VERSION_LABEL) dht = pymdht.Pymdht(my_node, '.', routing_m_mod, lookup_m_mod, exp_mod, None, 0, False) MyApp().run()