示例#1
0
    def main(self):
        """\
        Start the Mainline client and block forever listening for connectons
        """

        uiname = "bittorrent-console"
        defaults = get_defaults(uiname)
        config, args = configfile.parse_configuration_and_args(
            defaults, uiname)
        config = Preferences().initWithDict(config)
        data_dir = config['data_dir']
        self.core_doneflag = DeferredEvent()
        self.rawserver_doneflag = DeferredEvent()

        rawserver = RawServer(config)  #event and I/O scheduler
        self.multitorrent = MultiTorrent(
            config, rawserver,
            data_dir)  #class used to add, control and remove torrents

        self.tick()  #add periodic function call

        rawserver.add_task(0, self.core_doneflag.addCallback,
                           lambda r: rawserver.external_add_task(0, shutdown))
        rawserver.listen_forever(
            self.rawserver_doneflag)  # runs until the component terminates

        self.send(producerFinished(self), "signal")
        print "TorrentClient has shutdown"
示例#2
0
def create_multitorrent(config, rawserver, i):
    config = Preferences().initWithDict(dict(config))
    config['data_dir'] = config['data_dir'] + ("_%s" % i)
    if os.path.exists(config['data_dir']):
        shutil.rmtree(config['data_dir'])
    for d in ('', 'resume', 'metainfo', 'torrents'):
        ddir = os.path.join(config['data_dir'], d)
        no_really_makedirs(ddir)
    multitorrent = MultiTorrent(config,
                                rawserver,
                                config['data_dir'],
                                init_torrents=False)

    # Butlers
    multitorrent.add_policy(DownloadTorrentButler(multitorrent))
    multitorrent.add_policy(SeedTorrentButler(multitorrent))

    # register shutdown action
    def shutdown():
        df = multitorrent.shutdown()
        stop_rawserver = lambda r: rawserver.stop()
        df.addCallbacks(stop_rawserver, stop_rawserver)

    rawserver.add_task(0, core_doneflag.addCallback,
                       lambda r: rawserver.external_add_task(0, shutdown))
    return multitorrent
示例#3
0
    def init_core(mainloop):

        core_doneflag = DeferredEvent()

        class UILogger(logging.Handler):
            def emit(self, record):
                msg = "[%s] %s" % (record.name, self.format(record))
                gui_wrap(mainloop.do_log, record.levelno, msg)

        logging.getLogger('').addHandler(UILogger())

        try:
            multitorrent = MultiTorrent(config, rawserver, config['data_dir'],
                                        listen_fail_ok=True,
                                        init_torrents=False)

            # Butlers            
            multitorrent.add_policy(DownloadTorrentButler(multitorrent))
            multitorrent.add_policy(SeedTorrentButler(multitorrent))
            auto_update_butler = AutoUpdateButler(multitorrent, rawserver,
                                                  test_new_version=config['new_version'],
                                                  test_current_version=config['current_version'])
            multitorrent.add_auto_update_policy(auto_update_butler)

            # attach to the UI
            tpm = ThreadProxy(multitorrent,
                              gui_wrap,
                              wrap_task(rawserver.external_add_task))
            mainloop.attach_multitorrent(tpm, core_doneflag)
            ipc.start(mainloop.external_command)
            #rawserver.associate_thread()

            # register shutdown action
            def shutdown():
                df = multitorrent.shutdown()
                stop_rawserver = lambda r : rawserver.stop()
                df.addCallbacks(stop_rawserver, stop_rawserver)
            rawserver.add_task(0, core_doneflag.addCallback,
                               lambda r: rawserver.external_add_task(0, shutdown))

            rawserver.listen_forever()

        except:
            # oops, we failed.
            # one message for the log w/ exception info
            global_logger.exception("BitTorrent core initialization failed!")
            # one message for the user w/o info
            global_logger.critical("BitTorrent core initialization failed!")

            core_doneflag.set()
            rawserver.stop()
            try:
                gui_wrap(mainloop.ExitMainLoop)
            except:
                pass
            try:
                gui_wrap(mainloop.doneflag.set)
            except:
                pass
            raise
 def listen_forever(self):
     pool = NSAutoreleasePool.alloc().init()
     # XXX
     #self.profile = Profile("BT.prof");self.profile.start()
     self.rawserver = RawServer(self.config)
     self.mt = MultiTorrent(self.config, self.doneflag, self.rawserver,
                            self.multi_errorfunc, self.config['data_dir'])
     self.rawserver.ident = thread.get_ident()
     self.mt.set_option("max_upload_rate",
                        self.config['max_upload_rate'] * 1024)
     self.rawserver.listen_forever(self.doneflag)
示例#5
0
    def __init__(self, config, display, configfile_key):
        """Starts torrents for all .torrent files in a directory tree.

         All errors are logged using Python logging to 'configfile_key' logger.

         @param config: Preferences object storing config.
         @param display: output function for stats.
      """

        # 4.4.x version of LaunchMany output exceptions to a displayer.
        # This version only outputs stats to the displayer.  We do not use
        # the logger to output stats so that a caller-provided object
        # can provide stats formatting as opposed to using the
        # logger Formatter, which is specific to exceptions, warnings, and
        # info messages.
        self.logger = logging.getLogger(configfile_key)
        try:
            self.multitorrent = None
            self.rawserver = None
            self.config = config
            self.configfile_key = configfile_key
            self.display = display

            self.torrent_dir = config['torrent_dir']

            # Ex: torrent_cache = infohash -> (path,metainfo)
            self.torrent_cache = {}

            # maps path -> [(modification time, size), infohash]
            self.file_cache = {}

            # used as set containing paths of files that do not have separate
            # entries in torrent_cache either because torrent_cache already
            # contains the torrent or because the torrent file is corrupt.
            self.blocked_files = {}

            #self.torrent_list = []
            #self.downloads = {}

            self.hashcheck_queue = []
            #self.hashcheck_store = {}
            self.hashcheck_current = None

            self.core_doneflag = DeferredEvent()
            self.rawserver = RawServer(self.config)
            try:

                # set up shut-down procedure before we begin doing things that
                # can throw exceptions.
                def shutdown():
                    self.logger.critical(_("shutting down"))
                    if self.multitorrent:
                        if len(self.multitorrent.get_torrents()) > 0:
                            for t in self.multitorrent.get_torrents():
                                self.logger.info(
                                    _('dropped "%s"') %
                                    self.torrent_cache[t.infohash][0])

                        def after_mt(r):
                            self.logger.critical(
                                "multitorrent shutdown completed. Calling rawserver.stop"
                            )
                            self.rawserver.stop()

                        self.logger.critical("calling multitorrent shutdown")
                        df = self.multitorrent.shutdown()
                        #set_flag = lambda *a : self.rawserver.stop()
                        df.addCallbacks(after_mt, after_mt)
                    else:
                        self.rawserver.stop()

                    ### PROFILER POSTPROCESSING.
                    #self.logger.critical( "Disabling profiles" )
                    #prof.disable()
                    #self.logger.critical( "Running profiler post-processing" )
                    #stats = Stats(prof.getstats())
                    #stats.sort("inlinetime")
                    #self.logger.info( "Calling stats.pprint")
                    #stats.pprint()
                    #self.logger.info( "After stats.pprint")
                    ### PROFILER POSTPROCESSING

                # It is safe to addCallback here, because there is only one thread,
                # but even if the code were multi-threaded, core_doneflag has not
                # been passed to anyone.  There is no chance of a race condition
                # between the DeferredEvent's callback and addCallback.
                self.core_doneflag.addCallback(
                    lambda r: self.rawserver.external_add_task(0, shutdown))

                self.rawserver.install_sigint_handler(self.core_doneflag)

                data_dir = config['data_dir']
                self.multitorrent = MultiTorrent(
                    config,
                    self.rawserver,
                    data_dir,
                    resume_from_torrent_config=False)

                # first time sleep_time is 0 so that it will startup fast.  On
                # subsequent scans, the scan is slowed down to not affect performance.
                self.rawserver.add_task(0, self.scan, sleep_time=0)
                self.rawserver.add_task(0.5,
                                        self.periodic_check_hashcheck_queue)
                self.rawserver.add_task(self.config['display_interval'],
                                        self.periodic_stats)

                try:
                    import signal

                    def handler(signum, frame):
                        self.rawserver.external_add_task(0, self.read_config)

                    if hasattr(signal, 'SIGHUP'):
                        signal.signal(signal.SIGHUP, handler)
                except Exception, e:
                    self.logger.error(
                        _("Could not set signal handler: ") + str_exc(e))
                    self.rawserver.add_task(0, self.core_doneflag.set)

            except UserFailure, e:
                self.logger.error(str_exc(e))
                self.rawserver.add_task(0, self.core_doneflag.set)
            except:
                #data = StringIO()
                #print_exc(file = data)
                #self.logger.error(data.getvalue())
                self.logger.exception(
                    "Exception raised while initializing LaunchMany")
                self.rawserver.add_task(0, self.core_doneflag.set)

            # always make sure events get processed even if only for
            # shutting down.
            self.rawserver.listen_forever()
            self.logger.info("After rawserver.listen_forever")