def run(poller, conf): ''' This function is invoked when Neubot is already running and you want to leverage some functionalities of this module. ''' # Make sure the conf makes sense before we go config.finalize_conf(conf) if conf["bittorrent.listen"]: if conf["bittorrent.negotiate"]: # # We assume that the caller has already started # the HTTP server and that it contains our negotiator # so here we just bring up the test server. # server = ServerPeer(poller) server.configure(conf) server.listen((conf["bittorrent.address"], conf["bittorrent.port"])) else: server = PeerNeubot(poller) server.configure(conf) server.listen((conf["bittorrent.address"], conf["bittorrent.port"])) else: # # Make sure there is someone ready to receive the # "testdone" event. If there is noone it is a bug # none times out of ten. # if not NOTIFIER.is_subscribed("testdone"): log.oops("The 'testdone' event is not subscribed") if conf["bittorrent.negotiate"]: client = BitTorrentClient(poller) client.configure(conf) # # The rendezvous client uses this hidden variable # to pass us the URI to connect() to (the rendezvous # returns an URI, not address and port). # uri = None if "bittorrent._uri" in conf: uri = conf["bittorrent._uri"] client.connect_uri(uri) else: client = PeerNeubot(poller) client.configure(conf) client.connect((conf["bittorrent.address"], conf["bittorrent.port"]))
def main(args): config.register_descriptions() common.main("bittorrent", "Neubot BitTorrent module", args) conf = CONFIG.copy() config.finalize_conf(conf) if conf["bittorrent.listen"]: # # If we need to negotiate and we're runing # standalone we also need to bring up the # global HTTP server. # if conf["bittorrent.negotiate"]: HTTP_SERVER.configure(conf) HTTP_SERVER.listen((conf["bittorrent.address"], conf["bittorrent.negotiate.port"])) conf["negotiate.listen"] = True negotiate.run(POLLER, conf) # # Drop privileges after listen() so we can # bind() to privileged ports # if conf["bittorrent.daemonize"]: system.change_dir() system.go_background() LOG.redirect() system.drop_privileges(LOG.error) else: # # When we're connecting to a remote host to perform a test # we want Neubot to quit at the end of the test. When this # happens the test code publishes the "testdone" event, so # here we prepare to intercept the event and break our main # loop. # NOTIFIER.subscribe("testdone", lambda event, ctx: POLLER.break_loop()) run(POLLER, conf) POLLER.loop()
def test_finalize_server(self): ''' Test finalize conf in the server case ''' conf = { 'bittorrent.my_id': '', 'bittorrent.infohash': '', 'bittorrent.bytes.down': 0, 'bittorrent.bytes.up': 0, 'bittorrent.listen': True, 'bittorrent.address': '', } config.finalize_conf(conf) self.assertTrue(len(conf['bittorrent.my_id']), 20) self.assertTrue(len(conf['bittorrent.infohash']), 20) self.assertEqual(conf['bittorrent.bytes.down'], estimate.DOWNLOAD) self.assertEqual(conf['bittorrent.bytes.up'], estimate.UPLOAD) self.assertEqual(conf['bittorrent.address'], ':: 0.0.0.0')
def test_finalize_client(self): ''' Test finalize conf in the client case ''' conf = { 'bittorrent.my_id': '', 'bittorrent.infohash': '', 'bittorrent.bytes.down': 0, 'bittorrent.bytes.up': 0, 'bittorrent.listen': False, 'bittorrent.address': '', } config.finalize_conf(conf) self.assertTrue(len(conf['bittorrent.my_id']), 20) self.assertTrue(len(conf['bittorrent.infohash']), 20) self.assertEqual(conf['bittorrent.bytes.down'], estimate.DOWNLOAD) self.assertEqual(conf['bittorrent.bytes.up'], estimate.UPLOAD) self.assertEqual(conf['bittorrent.address'], 'neubot.blupixel.net')
def main(args): ''' Main function ''' try: options, arguments = getopt.getopt(args[1:], 'lO:v') except getopt.error: sys.exit('usage: neubot bittorrent_peer [-lv] [-O setting]') if arguments: sys.exit('usage: neubot bittorrent_peer [-lv] [-O setting]') settings = [ 'address "127.0.0.1 ::1"', 'port 6881', 'version 1' ] listener = False for name, value in options: if name == '-l': listener = True elif name == '-O': settings.append(value) elif name == '-v': CONFIG['verbose'] = 1 settings = utils_rc.parse_safe(iterable=settings) config_copy = CONFIG.copy() config.finalize_conf(config_copy) peer = PeerNeubot(POLLER) peer.configure(config_copy) # BLEAH peer.version = int(settings['version']) if not listener: peer.connect((settings['address'], int(settings['port']))) else: peer.listen((settings['address'], int(settings['port']))) POLLER.loop()
def main(args): config.register_descriptions() common.main("bittorrent", "Neubot BitTorrent module", args) conf = CONFIG.copy() config.finalize_conf(conf) if conf["bittorrent.listen"]: # # If we need to negotiate and we're runing # standalone we also need to bring up the # global HTTP server. # if conf["bittorrent.negotiate"]: HTTP_SERVER.configure(conf) HTTP_SERVER.listen((conf["bittorrent.address"], conf["bittorrent.negotiate.port"])) conf["negotiate.listen"] = True negotiate.run(POLLER, conf) # # Drop privileges after listen() so we can # bind() to privileged ports # if conf["bittorrent.daemonize"]: system.change_dir() system.go_background() LOG.redirect() system.drop_privileges(LOG.error) else: # # If possible use the runner, which will execute the # test in the context of the neubot daemon. Then exit # to bypass the run() invokation that is below here. # If the runner fails, fallback to the usual code path, # which executes the test in the context of the local # process. # Set 'runned.enabled' to 0 to bypass the runner and # run the test locally. # if (utils.intify(conf['runner.enabled']) and runner_clnt.runner_client(conf["agent.api.address"], conf["agent.api.port"], LOG.noisy, "bittorrent")): sys.exit(0) LOG.info('Will run the test in the local context...') # # When we're connecting to a remote host to perform a test # we want Neubot to quit at the end of the test. When this # happens the test code publishes the "testdone" event, so # here we prepare to intercept the event and break our main # loop. # NOTIFIER.subscribe("testdone", lambda event, ctx: POLLER.break_loop()) run(POLLER, conf) POLLER.loop()
def main(args): ''' This function is invoked when the user wants to run precisely this module. ''' try: options, arguments = getopt.getopt(args[1:], '6A:fp:v') except getopt.error: sys.exit('usage: neubot bittorrent [-6fv] [-A address] [-p port]') if arguments: sys.exit('usage: neubot bittorrent [-6fv] [-A address] [-p port]') prefer_ipv6 = 0 address = 'master.neubot.org' force = 0 port = 6881 noisy = 0 for name, value in options: if name == '-6': prefer_ipv6 = 1 elif name == '-A': address = value elif name == '-f': force = 1 elif name == '-p': port = int(value) elif name == '-v': noisy = 1 if os.path.isfile(DATABASE.path): DATABASE.connect() CONFIG.merge_database(DATABASE.connection()) else: logging.warning('bittorrent: database file is missing: %s', DATABASE.path) BACKEND.use_backend('null') if noisy: log.set_verbose() config.register_descriptions() # Needed? conf = CONFIG.copy() config.finalize_conf(conf) conf['bittorrent.address'] = address conf['bittorrent.port'] = port conf['prefer_ipv6'] = prefer_ipv6 if not force: if runner_clnt.runner_client(conf["agent.api.address"], conf["agent.api.port"], CONFIG['verbose'], "bittorrent"): sys.exit(0) logging.warning( 'bittorrent: failed to contact Neubot; is Neubot running?') sys.exit(1) logging.info('bittorrent: run the test in the local process context...') # # When we're connecting to a remote host to perform a test # we want Neubot to quit at the end of the test. When this # happens the test code publishes the "testdone" event, so # here we prepare to intercept the event and break our main # loop. # NOTIFIER.subscribe("testdone", lambda event, ctx: POLLER.break_loop()) run(POLLER, conf) POLLER.loop()