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 update_status(self): """Update torrent information based on the results of making a status request.""" df = self.multitorrent.get_torrents() yield df torrents = df.getResult() infohashes = set() au_torrents = {} for torrent in torrents: torrent = self._thread_proxy(torrent) infohashes.add(torrent.metainfo.infohash) if torrent.metainfo.infohash not in self.torrents: if self.config.get( 'show_hidden_torrents') or not torrent.hidden: # create new torrent widget to = self.new_displayed_torrent(torrent) if torrent.is_auto_update: au_torrents[torrent.metainfo.infohash] = torrent for infohash, torrent in copy(self.torrents).iteritems(): # remove nonexistent torrents if infohash not in infohashes: self._do_remove_torrent(infohash) total_completion = 0 total_bytes = 0 for infohash, torrent in copy(self.torrents).iteritems(): # update existing torrents df = self.multitorrent.torrent_status(infohash, torrent.wants_peers(), torrent.wants_files()) yield df try: core_torrent, statistics = df.getResult() except UnknownInfohash: # looks like it's gone now if infohash in self.torrents: self._do_remove_torrent(infohash) else: # the infohash might have been removed from torrents # while we were yielding above, so we need to check if infohash in self.torrents: core_torrent = self._thread_proxy(core_torrent) torrent.update(core_torrent, statistics) self.update_torrent(torrent) if statistics['fractionDone'] is not None: amount_done = statistics[ 'fractionDone'] * torrent.metainfo.total_bytes total_completion += amount_done total_bytes += torrent.metainfo.total_bytes all_completed = False if total_bytes == 0: average_completion = 0 else: average_completion = total_completion / total_bytes if total_completion == total_bytes: all_completed = True df = self.multitorrent.auto_update_status() yield df available_version, installable_version, delay = df.getResult() if available_version is not None: if installable_version is None: self.notify_of_new_version(available_version) else: if self.installer_to_launch_at_exit is None: atexit.register(self.launch_installer_at_exit) if installable_version not in au_torrents: df = self.multitorrent.get_torrent(installable_version) yield df torrent = df.getResult() torrent = ThreadProxy(torrent, self.gui_wrap) else: torrent = au_torrents[installable_version] self.installer_to_launch_at_exit = torrent.working_path if bttime() > self.next_autoupdate_nag: self.prompt_for_quit_for_new_version(available_version) self.next_autoupdate_nag = bttime() + delay def get_global_stats(mt): stats = {} u, d = mt.get_total_rates() stats['total_uprate'] = Rate(u) stats['total_downrate'] = Rate(d) u, d = mt.get_total_totals() stats['total_uptotal'] = Size(u) stats['total_downtotal'] = Size(d) torrents = mt.get_visible_torrents() running = mt.get_visible_running() stats['num_torrents'] = len(torrents) stats['num_running_torrents'] = len(running) stats['num_connections'] = 0 for t in torrents: stats['num_connections'] += t.get_num_connections() try: stats['avg_connections'] = (stats['num_connections'] / stats['num_running_torrents']) except ZeroDivisionError: stats['avg_connections'] = 0 stats['avg_connections'] = "%.02f" % stats['avg_connections'] return stats df = self.multitorrent.call_with_obj(get_global_stats) yield df global_stats = df.getResult() yield average_completion, all_completed, global_stats
def _thread_proxy(self, obj): return ThreadProxy(obj, self.gui_wrap, wrap_task(self.rawserver.external_add_task))