Exemplo n.º 1
0
        def create_proxy(index, become_exit_node, crypto_enabled):
            from Tribler.Core.Session import Session

            self.setUpPreSession()
            config = self.config.copy()
            config.set_libtorrent(True)
            config.set_dispersy(True)
            config.set_state_dir(self.getStateDir(index))

            session = Session(config, ignore_singleton=True, autoload_discovery=False)

            upgrader = session.prestart()

            session.start()
            self.sessions.append(session)

            while not session.lm.initComplete:
                time.sleep(1)

            dispersy = session.get_dispersy_instance()

            def load_community(session):
                keypair = dispersy.crypto.generate_key(u"curve25519")
                dispersy_member = dispersy.get_member(private_key=dispersy.crypto.key_to_bin(keypair))
                settings = TunnelSettings(tribler_session=session)
                if not crypto_enabled:
                    settings.crypto = NoCrypto()
                settings.become_exitnode = become_exit_node

                return dispersy.define_auto_load(HiddenTunnelCommunity,
                                                 dispersy_member,
                                                 (session, settings),
                                                 load=True)[0]

            return blockingCallFromThread(reactor, load_community, session)
Exemplo n.º 2
0
    def test_create_channel(self):
        """
        Test the pass through function of Session.create_channel to the ChannelManager.
        """

        class LmMock(object):
            class ChannelManager(object):
                invoked_name = None
                invoked_desc = None
                invoked_mode = None

                def create_channel(self, name, description, mode=u"closed"):
                    self.invoked_name = name
                    self.invoked_desc = description
                    self.invoked_mode = mode

            channel_manager = ChannelManager()

        config = TriblerConfig()
        config.set_state_dir(self.getStateDir())
        session = Session(config)
        session.lm = LmMock()
        session.lm.api_manager = None

        session.create_channel("name", "description", "open")
        self.assertEqual(session.lm.channel_manager.invoked_name, "name")
        self.assertEqual(session.lm.channel_manager.invoked_desc, "description")
        self.assertEqual(session.lm.channel_manager.invoked_mode, "open")
Exemplo n.º 3
0
 def __init__(self):
     if GUIDBProducer.__single:
         raise RuntimeError, "GuiDBProducer is singleton"
     
     #Lets get the reference to the shared database_thread
     from Tribler.Core.Session import Session
     if Session.has_instance():
         self.database_thread = Session.get_instance().lm.database_thread
     else:
         raise RuntimeError('Session not initialized')
     
     self.guitaskqueue = GUITaskQueue.getInstance()
     
     #Lets get a reference to utility
     from Tribler.Main.vwxGUI.GuiUtility import GUIUtility
     if GUIUtility.hasInstance():
         self.utility = GUIUtility.getInstance().utility
     else:
         Utility = namedtuple('Utility', ['abcquitting',])
         self.utility = Utility(False)
     
     self.uIds = set()
     self.uIdsLock = Lock()
     
     self.nrCallbacks = {}
Exemplo n.º 4
0
class AbstractDB(AbstractServer):

    def setUp(self):
        super(AbstractDB, self).setUp()

        # dummy session
        self.config = SessionStartupConfig()
        self.config.set_state_dir(self.getStateDir())
        self.config.set_torrent_checking(False)
        self.config.set_multicast_local_peer_discovery(False)
        self.config.set_megacache(False)
        self.config.set_dispersy(False)
        self.config.set_mainline_dht(False)
        self.config.set_torrent_collecting(False)
        self.config.set_libtorrent(False)
        self.config.set_dht_torrent_collecting(False)
        self.config.set_videoplayer(False)
        self.config.set_torrent_store(False)
        self.session = Session(self.config, ignore_singleton=True)

        dbpath = init_bak_tribler_sdb('bak_new_tribler.sdb', destination_path=self.getStateDir(), overwrite=True)
        self.sqlitedb = SQLiteCacheDB(self.session, busytimeout=BUSYTIMEOUT)
        self.sqlitedb.initialize(dbpath)
        self.session.sqlite_db = self.sqlitedb

    @blocking_call_on_reactor_thread
    def tearDown(self):
        self.sqlitedb.close()
        self.sqlitedb = None
        self.session.del_instance()
        self.session = None

        super(AbstractDB, self).tearDown(self)
Exemplo n.º 5
0
    def test_close_dbhandler(self):
        handler = MockObject()
        self.called = False

        def verify_close_called():
            self.called = True
        handler.close = verify_close_called
        Session.close_dbhandler(handler)
        self.assertTrue(self.called)
Exemplo n.º 6
0
    def tearDown(self):
        if SQLiteCacheDB.hasInstance():
            SQLiteCacheDB.getInstance().close_all()
            SQLiteCacheDB.delInstance()

        if Session.has_instance():  # Upgrading will create a session instance
            Session.del_instance()

        sqlitecachedb.INITIAL_UPGRADE_PAUSE, sqlitecachedb.SUCCESIVE_UPGRADE_PAUSE, sqlitecachedb.UPGRADE_BATCH_SIZE, sqlitecachedb.TEST_OVERRIDE = self.original_values
        self.tearDownCleanup()
Exemplo n.º 7
0
    def test_close_dbhandler(self):
        handler = MockObject()
        self.called = False

        def verify_close_called():
            self.called = True

        handler.close = verify_close_called
        Session.close_dbhandler(handler)
        self.assertTrue(self.called)
Exemplo n.º 8
0
    def checkReactor(self, phase, *_):
        delayed_calls = reactor.getDelayedCalls()
        if delayed_calls:
            self._logger.error("The reactor was dirty during %s:", phase)
            for dc in delayed_calls:
                self._logger.error(">     %s" % dc)
                dc.cancel()

        has_network_selectables = False
        for item in reactor.getReaders() + reactor.getWriters():
            if isinstance(item, HTTPChannel) or isinstance(item, Client):
                has_network_selectables = True
                break

        if has_network_selectables:
            # TODO(Martijn): we wait a while before we continue the check since network selectables
            # might take some time to cleanup. I'm not sure what's causing this.
            yield deferLater(reactor, 0.2, lambda: None)

        # This is the same check as in the _cleanReactor method of Twisted's Trial
        selectable_strings = []
        for sel in reactor.removeAll():
            if interfaces.IProcessTransport.providedBy(sel):
                self._logger.error("Sending kill signal to %s", repr(sel))
                sel.signalProcess('KILL')
            selectable_strings.append(repr(sel))

        self.assertFalse(delayed_calls,
                         "The reactor was dirty during %s" % phase)
        if Session.has_instance():
            try:
                yield Session.get_instance().shutdown()
            except:
                pass
            Session.del_instance()

            raise RuntimeError("Found a leftover session instance during %s" %
                               phase)

        self.assertFalse(
            selectable_strings,
            "The reactor has leftover readers/writers during %s: %r" %
            (phase, selectable_strings))

        # Check whether we have closed all the sockets
        open_readers = reactor.getReaders()
        for reader in open_readers:
            self.assertNotIsInstance(
                reader, BasePort,
                "Listening ports left on the reactor during %s: %s" %
                (phase, reader))

        # Check whether the threadpool is clean
        self.assertEqual(len(reactor.getThreadPool().working), 0,
                         "Still items left in the threadpool")
Exemplo n.º 9
0
    def start(self):
        # Determine socks5 ports
        socks5_port = self.options['socks5']
        if "HELPER_INDEX" in os.environ and "HELPER_BASE" in os.environ:
            base_port = int(os.environ["HELPER_BASE"])
            socks5_port = base_port + int(os.environ["HELPER_INDEX"]) * 5

        if socks5_port is not None:
            socks_listen_ports = range(socks5_port, socks5_port + 5)
        else:
            socks_listen_ports = [
                random.randint(1000, 65535) for _ in range(5)
            ]

        config = TriblerConfig()
        config.set_state_dir(
            os.path.join(config.get_state_dir(), "tunnel-%d") %
            socks_listen_ports[0])
        config.set_tunnel_community_socks5_listen_ports(socks_listen_ports)
        config.set_torrent_checking_enabled(False)
        config.set_megacache_enabled(False)
        config.set_dispersy_enabled(False)
        config.set_ipv8_enabled(True)
        config.set_mainline_dht_enabled(True)
        config.set_torrent_collecting_enabled(False)
        config.set_libtorrent_enabled(False)
        config.set_video_server_enabled(False)
        config.set_dispersy_port(self.ipv8_port)
        config.set_ipv8_address(self.ipv8_address)
        config.set_torrent_search_enabled(False)
        config.set_channel_search_enabled(False)
        config.set_trustchain_enabled(True)
        config.set_credit_mining_enabled(False)
        config.set_market_community_enabled(False)
        config.set_mainline_dht_enabled(False)
        config.set_tunnel_community_exitnode_enabled(bool(
            self.options["exit"]))
        config.set_trustchain_testnet(bool(self.options["testnet"]))

        if self.options["restapi"] is not None:
            config.set_http_api_enabled(True)
            config.set_http_api_port(self.options["restapi"])

        if "ipv8_bootstrap_override" in self.options:
            config.set_ipv8_bootstrap_override(
                self.options["ipv8_bootstrap_override"])

        self.session = Session(config)
        logger.info("Using IPv8 port %d" %
                    self.session.config.get_dispersy_port())

        self.session.notifier.add_observer(self.circuit_removed, NTFY_TUNNEL,
                                           [NTFY_REMOVE])

        return self.session.start().addCallback(self.tribler_started)
Exemplo n.º 10
0
    def setupPeer(self):
        from Tribler.Core.Session import Session

        self.config2 = self.config.copy()
        self.config2.set_state_dir(self.getStateDir(2))

        self.session2 = Session(self.config2, ignore_singleton=True)

        yield self.session2.start()
        self.load_communities(self.session2,
                              self.session2.get_dispersy_instance())
Exemplo n.º 11
0
    def setup_downloader(self):
        self.config2 = self.config.copy()
        self.config2.set_megacache(True)
        self.config2.set_torrent_collecting(True)
        self.config2.set_torrent_store_dir(None)
        self.config2.set_metadata_store_dir(None)
        self.config2.set_enable_metadata(True)
        self.config2.set_state_dir(self.getStateDir(2))

        self.session2 = Session(self.config2, ignore_singleton=True)
        return self.session2.start()
Exemplo n.º 12
0
    def start_tribler(self, options):
        """
        Main method to startup Tribler.
        """
        def on_tribler_shutdown(_):
            msg("Tribler shut down")
            reactor.stop()
            self.process_checker.remove_lock_file()

        def signal_handler(sig, _):
            msg("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                self.session.shutdown().addCallback(on_tribler_shutdown)

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        config = TriblerConfig()

        # Check if we are already running a Tribler instance
        self.process_checker = ProcessChecker()
        if self.process_checker.already_running:
            self.shutdown_process("Another Tribler instance is already using statedir %s" % config.get_state_dir())
            return

        msg("Starting Tribler")

        if options["statedir"]:
            config.set_state_dir(options["statedir"])

        if options["restapi"] > 0:
            config.set_http_api_enabled(True)
            config.set_http_api_port(options["restapi"])

        if options["ipv8"] > 0:
            config.set_ipv8_port(options["ipv8"])
        elif options["ipv8"] == 0:
            config.set_ipv8_enabled(False)

        if options["libtorrent"] != -1 and options["libtorrent"] > 0:
            config.set_libtorrent_port(options["libtorrent"])

        if options["ipv8_bootstrap_override"] is not None:
            config.set_ipv8_bootstrap_override(options["ipv8_bootstrap_override"])

        if "testnet" in options and options["testnet"]:
            config.set_testnet(True)

        self.session = Session(config)
        self.session.start().addErrback(lambda failure: self.shutdown_process(failure.getErrorMessage()))
        msg("Tribler started")
Exemplo n.º 13
0
    def start_tribler(self, options):
        """
        Main method to startup Tribler.
        """
        def on_tribler_shutdown(_):
            msg("Tribler shut down")
            reactor.stop()
            self.process_checker.remove_lock_file()

        def signal_handler(sig, _):
            msg("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                self.session.shutdown().addCallback(on_tribler_shutdown)

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        config = TriblerConfig()
        config.set_torrent_checking_enabled(False)
        config.set_megacache_enabled(True)
        config.set_dispersy_enabled(False)
        config.set_mainline_dht_enabled(True)
        config.set_torrent_collecting_enabled(False)
        config.set_libtorrent_enabled(False)
        config.set_http_api_enabled(True)
        config.set_video_server_enabled(False)
        config.set_torrent_search_enabled(False)
        config.set_channel_search_enabled(False)

        # Check if we are already running a Tribler instance
        self.process_checker = ProcessChecker()
        if self.process_checker.already_running:
            self.shutdown_process("Another Tribler instance is already using statedir %s" % config.get_state_dir())
            return

        msg("Starting Tribler")

        if options["statedir"]:
            config.set_state_dir(options["statedir"])

        if options["restapi"] > 0:
            config.set_http_api_enabled(True)
            config.set_http_api_port(options["restapi"])

        if options["ipv8"] != -1 and options["ipv8"] > 0:
            config.set_dispersy_port(options["ipv8"])

        self.session = Session(config)
        self.session.start().addErrback(lambda failure: self.shutdown_process(failure.getErrorMessage()))
        msg("Tribler started")
Exemplo n.º 14
0
    def start_tribler():
        config = SessionStartupConfig().load()
        config.set_http_api_port(API_PORT)
        config.set_http_api_enabled(True)

        # Check if we are already running a Tribler instance
        process_checker = ProcessChecker()
        if process_checker.already_running:
            return

        session = Session(config)

        signal.signal(signal.SIGTERM, lambda signum, stack: shutdown(session, signum, stack))
        session.start()
Exemplo n.º 15
0
class TestSqliteCacheDB(AbstractServer):
    def setUp(self):
        super(TestSqliteCacheDB, self).setUp()
        self.config = SessionStartupConfig()
        self.config.set_state_dir(self.getStateDir())
        self.config.set_torrent_checking(False)
        self.config.set_multicast_local_peer_discovery(False)
        self.config.set_megacache(False)
        self.config.set_dispersy(False)
        self.config.set_mainline_dht(False)
        self.config.set_torrent_collecting(False)
        self.config.set_libtorrent(False)
        self.config.set_dht_torrent_collecting(False)
        self.config.set_videoplayer(False)
        self.session = Session(self.config, ignore_singleton=True)
        self.sqlitedb = None

    def tearDown(self):
        super(TestSqliteCacheDB, self).tearDown()
        if self.sqlitedb:
            self.sqlitedb.close()
        self.sqlitedb = None
        self.session.del_instance()
        self.session = None

    def test_upgrade_from_obsolete_version(self):
        """We no longer support DB versions older than 17 (Tribler 6.0)"""
        dbpath = init_bak_tribler_sdb(u"bak_old_tribler.sdb",
                                      destination_path=self.getStateDir(),
                                      overwrite=True)

        self.sqlitedb = SQLiteCacheDB(self.session)
        self.sqlitedb.initialize(dbpath)

        class MockTorrentStore(object):
            def flush():
                pass

            def close():
                pass

        db_migrator = DBUpgrader(self.session,
                                 self.sqlitedb,
                                 torrent_store=MockTorrentStore())
        self.assertRaises(VersionNoLongerSupportedError,
                          db_migrator.start_migrate)

    def test_upgrade_from_17(self):
        pass
Exemplo n.º 16
0
    def start(self):
        # Determine ipv8 port
        ipv8_port = -1
        if self.options["ipv8_port"] != -1:
            ipv8_port = self.options["ipv8_port"]
        elif "HELPER_INDEX" in os.environ and "HELPER_BASE" in os.environ:
            base_port = int(os.environ["HELPER_BASE"])
            ipv8_port = base_port + int(os.environ["HELPER_INDEX"]) * 5

        config = TriblerConfig()
        config.set_state_dir(
            os.path.join(config.get_state_dir(), "tunnel-%d") % ipv8_port)
        config.set_tunnel_community_socks5_listen_ports([])
        config.set_tunnel_community_random_slots(self.options["random_slots"])
        config.set_tunnel_community_competing_slots(
            self.options["competing_slots"])
        config.set_torrent_checking_enabled(False)
        config.set_ipv8_enabled(True)
        config.set_libtorrent_enabled(False)
        config.set_video_server_enabled(False)
        config.set_ipv8_port(ipv8_port)
        config.set_ipv8_address(self.options["ipv8_address"])
        config.set_trustchain_enabled(True)
        config.set_credit_mining_enabled(False)
        config.set_market_community_enabled(False)
        config.set_dht_enabled(True)
        config.set_tunnel_community_exitnode_enabled(bool(
            self.options["exit"]))
        config.set_popularity_community_enabled(False)
        config.set_testnet(bool(self.options["testnet"]))
        config.set_chant_enabled(False)

        if not self.options['no-rest-api']:
            config.set_http_api_enabled(True)
            api_port = self.options["restapi"]
            if "HELPER_INDEX" in os.environ and "HELPER_BASE" in os.environ:
                api_port = int(os.environ["HELPER_BASE"]) + 10000 + int(
                    os.environ["HELPER_INDEX"])
            config.set_http_api_port(api_port)

        if self.options["ipv8_bootstrap_override"] is not None:
            config.set_ipv8_bootstrap_override(
                self.options["ipv8_bootstrap_override"])

        self.session = Session(config)
        self.session.notifier.add_observer(self.circuit_removed, NTFY_TUNNEL,
                                           [NTFY_REMOVE])

        return self.session.start().addCallback(self.tribler_started)
Exemplo n.º 17
0
    def setUp(self):
        yield super(AbstractDB, self).setUp()

        self.setUpPreSession()
        self.session = Session(self.config, ignore_singleton=True)

        tar = tarfile.open(
            os.path.join(TESTS_DATA_DIR, 'bak_new_tribler.sdb.tar.gz'), 'r|gz')
        tar.extractall(self.session_base_dir)

        db_path = os.path.join(self.session_base_dir, 'bak_new_tribler.sdb')

        self.sqlitedb = SQLiteCacheDB(db_path, busytimeout=BUSYTIMEOUT)
        self.sqlitedb.initialize()
        self.session.sqlite_db = self.sqlitedb
Exemplo n.º 18
0
 def setUp(self):
     super(TestSqliteCacheDB, self).setUp()
     self.config = SessionStartupConfig()
     self.config.set_state_dir(self.getStateDir())
     self.config.set_torrent_checking(False)
     self.config.set_multicast_local_peer_discovery(False)
     self.config.set_megacache(False)
     self.config.set_dispersy(False)
     self.config.set_mainline_dht(False)
     self.config.set_torrent_collecting(False)
     self.config.set_libtorrent(False)
     self.config.set_dht_torrent_collecting(False)
     self.config.set_videoplayer(False)
     self.session = Session(self.config, ignore_singleton=True)
     self.sqlitedb = None
Exemplo n.º 19
0
def can_edit_channel(channel_id, channel_vote):
    """
    This method returns whether the channel can be edited or not.
    """
    from Tribler.Core.Session import Session
    if Session.get_instance().get_dispersy():
        dispersy = Session.get_instance().get_dispersy_instance()
        try:
            cmty = dispersy.get_community(channel_id)
            channel_type, is_mod = cmty.get_channel_mode()
            if is_mod or channel_vote == VOTE_SUBSCRIBE and channel_type == ChannelCommunity.CHANNEL_OPEN:
                return True
        except CommunityNotFoundException:
            return False
    return False
Exemplo n.º 20
0
    def setup_seeder(self, tdef, seed_dir, port=None):
        self.seed_config = TriblerConfig()
        self.seed_config.set_torrent_checking_enabled(False)
        self.seed_config.set_ipv8_enabled(False)
        self.seed_config.set_http_api_enabled(False)
        self.seed_config.set_libtorrent_enabled(True)
        self.seed_config.set_video_server_enabled(False)
        self.seed_config.set_tunnel_community_enabled(False)
        self.seed_config.set_market_community_enabled(False)
        self.seed_config.set_dht_enabled(False)
        self.seed_config.set_state_dir(self.getStateDir(2))
        self.seed_config.set_version_checker_enabled(False)
        self.seed_config.set_bitcoinlib_enabled(False)
        self.seed_config.set_chant_enabled(False)
        self.seed_config.set_credit_mining_enabled(False)
        self.seed_config.set_resource_monitor_enabled(False)

        if port:
            self.seed_config.set_libtorrent_port(port)

        def start_seed_download(_):
            self.dscfg_seed = DownloadStartupConfig()
            self.dscfg_seed.set_dest_dir(seed_dir)
            d = self.seeder_session.start_download_from_tdef(tdef, self.dscfg_seed)
            d.set_state_callback(self.seeder_state_callback)

        self._logger.debug("starting to wait for download to reach seeding state")

        self.seeder_session = Session(self.seed_config)
        self.seeder_session.upgrader_enabled = False

        self.seeder_session.start().addCallback(start_seed_download)

        return self.seeding_deferred
Exemplo n.º 21
0
    def report(self):
        """
        Create the report in XML and send it
        """

        # Create the report
        doc = xml.dom.minidom.Document()
        root = doc.createElement("nextsharedata")
        doc.appendChild(root)
        
        # Create the header
        header = doc.createElement("header")
        root.appendChild(header)
        header.appendChild(self.new_element(doc, "deviceid", self.device_id))
        header.appendChild(self.new_element(doc, "timestamp", long(round(time.time()))))

        # ProxyService 90s Test_
        try:
            from Tribler.Core.Session import Session
            session = Session.get_instance()
            if session.lm.overlay_apps.proxy_peer_manager.connectable:
                    connectable = 1
            else:
                    connectable = 0
            
            start_time = long(round(session.start_time))

            my_permid = show_permid_short(session.get_permid())
        except Exception,e:
            connectable = 0
            start_time = 0
            my_permid = 0
Exemplo n.º 22
0
    def setUp(self, annotate=True):
        super(TestTorrentChecker, self).setUp(annotate=annotate)

        config = SessionStartupConfig()
        config.set_state_dir(self.getStateDir())
        config.set_megacache(True)

        self.session = Session(config, ignore_singleton=True)
        self.session.start_database()
        self.session.lm.torrent_db = TorrentDBHandler(self.session)
        self.session.lm.torrent_checker = TorrentChecker(self.session)
        self.session.lm.tracker_manager = TrackerManager(self.session)

        self.torrent_checker = self.session.lm.torrent_checker
        self.torrent_checker._torrent_db = self.session.open_dbhandler(NTFY_TORRENTS)
        self.torrent_checker._torrent_db.category = Category()
Exemplo n.º 23
0
    def setupSeeder(self, hops=0, session=None):
        from Tribler.Core.Session import Session
        from Tribler.Core.TorrentDef import TorrentDef
        from Tribler.Core.DownloadConfig import DownloadStartupConfig

        self.setUpPreSession()
        self.config.set_libtorrent(True)

        self.config2 = self.config.copy()
        self.config2.set_state_dir(self.getStateDir(2))
        if session is None:
            self.session2 = Session(self.config2, ignore_singleton=True, autoload_discovery=False)
            upgrader = self.session2.prestart()
            while not upgrader.is_done:
                time.sleep(0.1)
            self.session2.start()
            session = self.session2

        tdef = TorrentDef()
        tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi"))
        tdef.set_tracker("http://fake.net/announce")
        tdef.set_private()  # disable dht
        tdef.finalize()
        torrentfn = os.path.join(session.get_state_dir(), "gen.torrent")
        tdef.save(torrentfn)

        dscfg = DownloadStartupConfig()
        dscfg.set_dest_dir(TESTS_DATA_DIR)  # basedir of the file we are seeding
        dscfg.set_hops(hops)
        d = session.start_download(tdef, dscfg)
        d.set_state_callback(self.seeder_state_callback)

        return torrentfn
Exemplo n.º 24
0
        def wait_for_instance():
            print >> sys.stderr, "tgs: found instance, staring to wait for lm to be initcomplete"
            self.session = Session.get_instance()
            self.lm = self.session.lm

            self.CallConditional(30, lambda: self.lm.initComplete,
                                 wait_for_init)
Exemplo n.º 25
0
    def setup_torrentdownloader(self):
        self.download_event = Event()
        self.session1_port = self.session.get_dispersy_port()

        self.infohash_strs = ["41aea20908363a80d44234e8fef07fab506cd3b4",
                              "45a647b1120ed9fe7f793e17585efb4b0efdf1a5"]

        for i, infohash in enumerate(self.infohash_strs):
            self.infohashes[i] = infohash.decode('hex')
            self.file_names[i] = file_name = u"%s.torrent" % infohash

            # Put the torrents into the uploader's store
            with open(os.path.join(TESTS_DATA_DIR, file_name), 'r') as torrent_file:
                self.session.lm.torrent_store.put(infohash, torrent_file.read())

        from Tribler.Core.Session import Session

        self.setUpPreSession()

        self.config2 = self.config.copy()
        self.config2.set_megacache(True)
        self.config2.set_torrent_collecting(True)

        self.session2_state_dir = self.session.get_state_dir() + u"2"
        self.config2.set_state_dir(self.session2_state_dir)

        self.session2 = Session(self.config2, ignore_singleton=True)
        upgrader = self.session2.prestart()
        while not upgrader.is_done:
            sleep(0.1)
        assert not upgrader.failed, upgrader.current_status
        self.session2.start()
        sleep(1)
Exemplo n.º 26
0
def relevance_score_remote_torrent(torrent_name):
    """
    Calculate the relevance score of a remote torrent, based on the name and the matchinfo object
    of the last torrent from the database.
    The algorithm used is the same one as in search_in_local_torrents_db in SqliteCacheDBHandler.py.
    """
    from Tribler.Core.Session import Session
    torrent_db = Session.get_instance().open_dbhandler(NTFY_TORRENTS)
    if torrent_db.latest_matchinfo_torrent is None:
        return 0.0
    matchinfo, keywords = torrent_db.latest_matchinfo_torrent

    num_phrases, num_cols, num_rows = unpack_from('III', matchinfo)
    unpack_str = 'I' * (3 * num_cols * num_phrases)
    matchinfo = unpack_from('I' * 9 + unpack_str, matchinfo)[9:]

    score = 0.0
    for phrase_ind in xrange(num_phrases):
        rows_with_term = matchinfo[3 * (phrase_ind * num_cols) + 2]
        term_freq = torrent_name.lower().count(keywords[phrase_ind])

        inv_doc_freq = math.log(
            (num_rows - rows_with_term + 0.5) / (rows_with_term + 0.5), 2)
        right_side = ((term_freq * (1.2 + 1)) / (term_freq + 1.2))

        score += inv_doc_freq * right_side

    return score
Exemplo n.º 27
0
    def initialize(self):
        sscfg = SessionStartupConfig()
        if self._opt.statedir:
            sscfg.set_state_dir(unicode(os.path.realpath(self._opt.statedir)))
        if self._opt.port:
            sscfg.set_dispersy_port(self._opt.port)
        if self._opt.nickname:
            sscfg.set_nickname(self._opt.nickname)

        # pass rss config
        if not self._opt.rss_config:
            self._logger.error(u"rss_config unspecified")
        self.rss_list = parge_rss_config_file(self._opt.rss_config)

        sscfg.set_megacache(True)
        sscfg.set_torrent_collecting(True)
        sscfg.set_torrent_checking(True)
        sscfg.set_enable_torrent_search(True)
        sscfg.set_enable_channel_search(True)

        self._logger.info(u"Starting session...")
        self.session = Session(sscfg)
        self.session.prestart()

        # add dispersy start callbacks
        self.session.add_observer(self.dispersy_started, NTFY_DISPERSY, [NTFY_STARTED])
        self.session.start()
Exemplo n.º 28
0
    def setup_tunnel_seeder(self, hops):
        """
        Setup the seeder.
        """
        from Tribler.Core.Session import Session

        self.seed_config = self.config.copy()
        self.seed_config.set_state_dir(self.getStateDir(2))
        self.seed_config.set_megacache_enabled(True)
        self.seed_config.set_tunnel_community_socks5_listen_ports(self.get_socks5_ports())
        if self.session2 is None:
            self.session2 = Session(self.seed_config)
            self.session2.start()

        tdef = TorrentDef()
        tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi"))
        tdef.set_tracker("http://localhost/announce")
        tdef.finalize()
        torrentfn = os.path.join(self.session2.config.get_state_dir(), "gen.torrent")
        tdef.save(torrentfn)
        self.seed_tdef = tdef

        if hops > 0:  # Safe seeding enabled
            self.tunnel_community_seeder = self.load_tunnel_community_in_session(self.session2)
            self.tunnel_community_seeder.build_tunnels(hops)
        else:
            self.sanitize_network(self.session2)

        dscfg = DownloadStartupConfig()
        dscfg.set_dest_dir(TESTS_DATA_DIR)  # basedir of the file we are seeding
        dscfg.set_hops(hops)
        d = self.session2.start_download_from_tdef(tdef, dscfg)
        d.set_state_callback(self.seeder_state_callback)
Exemplo n.º 29
0
    def __init__(self, parent):
        HomePanel.__init__(self, parent, 'Newest Torrents' , LIST_BLUE)
        self.Layout()

        self.torrentdb = TorrentDBHandler.getInstance()
        session = Session.get_instance()
        session.add_observer(self.OnNotify, NTFY_TORRENTS, [NTFY_INSERT])
Exemplo n.º 30
0
    def _showInspectionTool(self):
        import wx.lib.inspection
        itool = wx.lib.inspection.InspectionTool()
        itool.Show()
        try:
            frame = itool._frame

            import Tribler
            frame.locals['Tribler'] = Tribler

            from Tribler.Core.Overlay.SecureOverlay import SecureOverlay
            overlay = SecureOverlay.getInstance()
            frame.locals['overlay'] = overlay

            session = Session.get_instance()
            frame.locals['session'] = session

            from Tribler.Core.BuddyCast.buddycast import BuddyCastFactory
            channelcast = BuddyCastFactory.getInstance().channelcast_core
            frame.locals['channelcast'] = channelcast

            frame.locals['dispersy'] = Dispersy.get_instance()

        except Exception:
            import traceback
            traceback.print_exc()
Exemplo n.º 31
0
 def __init__(self, interval = 15):
     if TorrentChecking.__single:
         raise RuntimeError, "Torrent Checking is singleton"
     TorrentChecking.__single = self
     
     Thread.__init__(self)
     
     self.setName('TorrentChecking'+self.getName())
     if DEBUG:
         print >> sys.stderr, 'TorrentChecking: Started torrentchecking from', threading.currentThread().getName()
     self.setDaemon(True)
     
     self.retryThreshold = 10
     self.interval = interval
     self.shouldquit = False
     
     self.queue = deque()
     self.queueset = set()
     self.queueLock = Lock()
     
     self.announceQueue = Queue()
     
     self.mldhtchecker = mainlineDHTChecker.getInstance()
     self.torrentdb = TorrentDBHandler.getInstance()
     
     self.sleepEvent = threading.Event()
     self.torrent_collection_dir = Session.get_instance().get_torrent_collecting_dir()
             
     self.start()
Exemplo n.º 32
0
    def __init__(self, parent):
        HomePanel.__init__(self, parent, 'Newest Torrents', LIST_BLUE)
        self.Layout()

        self.torrentdb = TorrentDBHandler.getInstance()
        session = Session.get_instance()
        session.add_observer(self.OnNotify, NTFY_TORRENTS, [NTFY_INSERT])
Exemplo n.º 33
0
 def start_tribler(self):
     """
     Starts the headless Tribler session with the correct configuration set
     """
     config = SessionStartupConfig()
     config.set_state_dir(os.path.join(config.get_state_dir(), "tunnel-%d") % self.settings.socks_listen_ports[0])
     config.set_torrent_checking(False)
     config.set_multicast_local_peer_discovery(False)
     config.set_megacache(False)
     config.set_dispersy(True)
     config.set_mainline_dht(True)
     config.set_torrent_collecting(False)
     config.set_libtorrent(True)
     config.set_dht_torrent_collecting(False)
     config.set_enable_torrent_search(False)
     config.set_videoplayer(False)
     config.set_dispersy_port(self.dispersy_port)
     config.set_enable_torrent_search(False)
     config.set_enable_channel_search(False)
     config.set_enable_multichain(True)
     
     self.session = Session(config)
     upgrader = self.session.prestart()
     while not upgrader.is_done:
         time.sleep(0.1)
     self.session.start()
     
     logger.info("Using port %d" % self.session.get_dispersy_port())
Exemplo n.º 34
0
    def setupSeeder(self):
        from Tribler.Core.Session import Session
        from Tribler.Core.TorrentDef import TorrentDef
        from Tribler.Core.DownloadConfig import DownloadStartupConfig

        self.setUpPreSession()
        self.config.set_libtorrent(True)

        self.config2 = self.config.copy()

        self.session2 = Session(self.config2, ignore_singleton=True)
        upgrader = self.session2.prestart()
        while not upgrader.is_done:
            time.sleep(0.1)
        assert not upgrader.failed, upgrader.current_status
        self.session2.start()

        tdef = TorrentDef()
        tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi"))
        tdef.set_tracker("http://fake.net/announce")
        tdef.finalize()
        torrentfn = os.path.join(self.session.get_state_dir(), "gen.torrent")
        tdef.save(torrentfn)

        dscfg = DownloadStartupConfig()
        dscfg.set_dest_dir(TESTS_DATA_DIR)  # basedir of the file we are seeding
        d = self.session2.start_download(tdef, dscfg)
        d.set_state_callback(self.seeder_state_callback)

        return torrentfn
Exemplo n.º 35
0
    def setUp(self, annotate=True):
        super(TestRestApiUtil, self).setUp(annotate=annotate)

        config = TriblerConfig()
        config.set_state_dir(self.getStateDir())
        config.get_dispersy_enabled = lambda: False
        self.session = Session(config)
    def test_create_channel_duplicate_name_error(self):
        config = TriblerConfig()
        config.set_state_dir(self.getStateDir())
        self.session = Session(config)

        class LmMock(object):
            channel_manager = ChannelManager(self.session)

        self.session.lm = LmMock()

        class MockCommunity(object):
            cid = ""

            def get_channel_name(self):
                return "Channel name"

        channel_obj = ChannelObject(self.session,
                                    MockCommunity(),
                                    is_created=True)
        self.session.lm.channel_manager._channel_list = [channel_obj]

        with self.assertRaises(DuplicateChannelNameError) as cm:
            self.session.lm.channel_manager.create_channel(
                "Channel name", "description", "open")
        self.assertEqual(cm.exception.message,
                         u"Channel name already exists: Channel name")
Exemplo n.º 37
0
 def readTorrent(self, torrent):
     try:
         torrent_path = torrent['torrent_path']
         
         if not path.isfile(torrent_path):
             #torrent not found, try filename + current torrent collection directory
             torrent_collection_dir = Session.get_instance().get_torrent_collecting_dir()
             _, torrent_filename = path.split(torrent_path)
             torrent_path = path.join(torrent_collection_dir, torrent_filename)
         
         if not path.isfile(torrent_path):
             #torrent still not found, determine filename + current torrent collection directory
             torrent_path = path.join(torrent_collection_dir, get_collected_torrent_filename(torrent['infohash']))
             
         if path.isfile(torrent_path):
             f = open(torrent_path,'rb')
             _data = f.read()
             f.close()
         
             data = bdecode(_data)
         
             assert 'info' in data
             del data['info']
             
             torrent['info'] = data
         return torrent
     except Exception:
         #print_exc()
         return torrent
Exemplo n.º 38
0
    def _showInspectionTool(self):
        import wx.lib.inspection
        itool = wx.lib.inspection.InspectionTool()
        itool.Show()
        try:
            frame = itool._frame

            import Tribler
            frame.locals['Tribler'] = Tribler

            from Tribler.Core.Overlay.SecureOverlay import SecureOverlay
            overlay = SecureOverlay.getInstance()
            frame.locals['overlay'] = overlay

            session = Session.get_instance()
            frame.locals['session'] = session

            from Tribler.Core.BuddyCast.buddycast import BuddyCastFactory
            channelcast = BuddyCastFactory.getInstance().channelcast_core
            frame.locals['channelcast'] = channelcast

            frame.locals['dispersy'] = Dispersy.get_instance()

        except Exception:
            import traceback
            traceback.print_exc()
Exemplo n.º 39
0
 def test_rel_score_remote_torrent(self):
     mocked_db = MockObject()
     mocked_db.latest_matchinfo_torrent = struct.pack(
         "I" * 12, *([1] * 12)), "torrent"
     Session.get_instance().open_dbhandler = lambda _: mocked_db
     self.assertNotEqual(relevance_score_remote_torrent("my-torrent.iso"),
                         0.0)
Exemplo n.º 40
0
    def readTorrent(self, torrent):
        try:
            torrent_path = torrent['torrent_path']

            if not path.isfile(torrent_path):
                #torrent not found, try filename + current torrent collection directory
                torrent_collection_dir = Session.get_instance(
                ).get_torrent_collecting_dir()
                _, torrent_filename = path.split(torrent_path)
                torrent_path = path.join(torrent_collection_dir,
                                         torrent_filename)

            if not path.isfile(torrent_path):
                #torrent still not found, determine filename + current torrent collection directory
                torrent_path = path.join(
                    torrent_collection_dir,
                    get_collected_torrent_filename(torrent['infohash']))

            if path.isfile(torrent_path):
                f = open(torrent_path, 'rb')
                _data = f.read()
                f.close()

                data = bdecode(_data)

                assert 'info' in data
                del data['info']

                torrent['info'] = data
            return torrent
        except Exception:
            #print_exc()
            return torrent
Exemplo n.º 41
0
    def create_session(self, index):
        """
        Create a single session and load the tunnel community in the session of that proxy.
        """
        from Tribler.Core.Session import Session

        config = self.config.copy()
        config.set_state_dir(self.getStateDir(index))

        session = Session(config, ignore_singleton=True, autoload_discovery=False)
        yield session.start()
        self.sessions.append(session)

        market_member = self.generate_member(session)
        mc_community = self.load_triblerchain_community_in_session(session)
        self.load_market_community_in_session(session, market_member, mc_community)
        returnValue(session)
Exemplo n.º 42
0
    def tearDown(self):
        self.annotate(self._testMethodName, start=False)

        """ unittest test tear down code """
        if self.session is not None:
            self._shutdown_session(self.session)
            Session.del_instance()

        time.sleep(10)
        gc.collect()

        ts = enumerate_threads()
        self._logger.debug("test_as_server: Number of threads still running %d", len(ts))
        for t in ts:
            self._logger.debug("Thread still running %s, daemon: %s, instance: %s", t.getName(), t.isDaemon(), t)

        super(TestAsServer, self).tearDown(annotate=False)
Exemplo n.º 43
0
    def __init__(self, parent):
        HomePanel.__init__(self, parent, 'Peer Discovery' , LIST_BLUE)

        session = Session.get_instance()
        session.add_observer(self.OnNotify, NTFY_PROXYDISCOVERY, [NTFY_INSERT])
        
        self.proxies=[]
        self.OnNotify(None, None, None, session.lm.overlay_apps.proxy_peer_manager.available_proxies.keys())
Exemplo n.º 44
0
    def start_tribler():
        config = TriblerConfig()
        patch_wallet_methods()
        patch_iom_methods()

        config.set_http_api_port(API_PORT)
        config.set_http_api_enabled(True)

        # Check if we are already running a Tribler instance
        process_checker = ProcessChecker(config.get_state_dir())
        if process_checker.already_running:
            return

        session = Session(config)

        signal.signal(signal.SIGTERM, lambda signum, stack: shutdown(session, signum, stack))
        session.start()
Exemplo n.º 45
0
class TestSqliteCacheDB(AbstractServer):

    def setUp(self):
        super(TestSqliteCacheDB, self).setUp()
        self.config = SessionStartupConfig()
        self.config.set_state_dir(self.getStateDir())
        self.config.set_torrent_checking(False)
        self.config.set_multicast_local_peer_discovery(False)
        self.config.set_megacache(False)
        self.config.set_dispersy(False)
        self.config.set_mainline_dht(False)
        self.config.set_torrent_collecting(False)
        self.config.set_libtorrent(False)
        self.config.set_dht_torrent_collecting(False)
        self.config.set_videoplayer(False)
        self.session = Session(self.config, ignore_singleton=True)
        self.sqlitedb = None

    def tearDown(self):
        super(TestSqliteCacheDB, self).tearDown()
        if self.sqlitedb:
            self.sqlitedb.close()
        self.sqlitedb = None
        self.session.del_instance()
        self.session = None

    def test_upgrade_from_obsolete_version(self):
        """We no longer support DB versions older than 17 (Tribler 6.0)"""
        dbpath = init_bak_tribler_sdb(u"bak_old_tribler.sdb", destination_path=self.getStateDir(), overwrite=True)

        self.sqlitedb = SQLiteCacheDB(self.session)
        self.sqlitedb.initialize(dbpath)

        class MockTorrentStore(object):

            def flush():
                pass

            def close():
                pass

        db_migrator = DBUpgrader(self.session, self.sqlitedb, torrent_store=MockTorrentStore())
        self.assertRaises(VersionNoLongerSupportedError, db_migrator.start_migrate)

    def test_upgrade_from_17(self):
        pass
Exemplo n.º 46
0
    def create_proxy(self, index, exitnode=False):
        """
        Create a single proxy and load the tunnel community in the session of that proxy.
        """
        from Tribler.Core.Session import Session

        self.setUpPreSession()
        config = self.config.copy()
        config.set_libtorrent_enabled(True)
        config.set_state_dir(self.getStateDir(index))
        config.set_tunnel_community_socks5_listen_ports(self.get_socks5_ports())

        session = Session(config)
        yield session.start()
        self.sessions.append(session)

        returnValue(self.load_tunnel_community_in_session(session, exitnode=exitnode))
Exemplo n.º 47
0
    def setUp(self):
        """ override TestAsServer """
        TestAsServer.setUp(self)

        self.session2 = Session(self.config2, ignore_singleton=True)
        self.session2.start()

        self.seeding_event = threading.Event()
Exemplo n.º 48
0
    def setup_peer(self):
        """
        Setup a second peer that contains some search results.
        """
        self.setUpPreSession()

        self.config2 = self.config.copy()
        self.config2.set_state_dir(self.getStateDir(2))

        self.session2 = Session(self.config2)

        yield self.session2.start()
        self.dispersy2 = self.session2.get_dispersy_instance()

        @inlineCallbacks
        def unload_communities():
            for community in self.dispersy.get_communities():
                if isinstance(community, SearchCommunity) or isinstance(community, AllChannelCommunity):
                    yield community.unload_community()

            for community in self.dispersy2.get_communities():
                if isinstance(community, SearchCommunity) or isinstance(community, AllChannelCommunity):
                    yield community.unload_community()

        def load_communities():
            self.search_community = \
            self.dispersy.define_auto_load(SearchCommunityTests, self.session.dispersy_member, load=True,
                                           kargs={'tribler_session': self.session})[0]
            self.dispersy2.define_auto_load(SearchCommunityTests, self.session2.dispersy_member, load=True,
                                            kargs={'tribler_session': self.session2})

            self.allchannel_community = \
            self.dispersy.define_auto_load(AllChannelCommunityTests, self.session.dispersy_member, load=True,
                                           kargs={'tribler_session': self.session})[0]
            self.dispersy2.define_auto_load(AllChannelCommunityTests, self.session2.dispersy_member, load=True,
                                            kargs={'tribler_session': self.session2})

        yield unload_communities()
        load_communities()

        self.search_community.add_discovered_candidate(Candidate(self.dispersy2.lan_address, tunnel=False))
        self.allchannel_community.add_discovered_candidate(Candidate(self.dispersy2.lan_address, tunnel=False))

        # Add some content to second session
        torrent_db_handler = self.session2.open_dbhandler(NTFY_TORRENTS)
        torrent_db_handler.addExternalTorrentNoDef(str(unichr(97)) * 20, 'test test', [('Test.txt', 1337)], [], 1337)
        torrent_db_handler.updateTorrent(str(unichr(97)) * 20, is_collected=1)

        channel_db_handler = self.session2.open_dbhandler(NTFY_CHANNELCAST)
        channel_db_handler.on_channel_from_dispersy('f' * 20, 42, "test", "channel for unit tests")
        torrent_list = [
            [1, 1, 1, ('a' * 40).decode('hex'), 1460000000, "ubuntu-torrent.iso", [['file1.txt', 42]], []]
        ]
        channel_db_handler.on_torrents_from_dispersy(torrent_list)

        # We also need to add the channel to the database of the session initiating the search
        channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST)
        channel_db_handler.on_channel_from_dispersy('f' * 20, 42, "test", "channel for unit tests")
Exemplo n.º 49
0
    def start_tribler(self, options):
        """
        Main method to startup Tribler.
        """
        def on_tribler_shutdown(_):
            msg("Tribler shut down")
            reactor.stop()
            self.process_checker.remove_lock_file()

        def signal_handler(sig, _):
            msg("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                self.session.shutdown().addCallback(on_tribler_shutdown)

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        config = TriblerConfig()
        config.set_torrent_checking_enabled(False)
        config.set_megacache_enabled(True)
        config.set_dispersy_enabled(False)
        config.set_mainline_dht_enabled(True)
        config.set_torrent_collecting_enabled(False)
        config.set_libtorrent_enabled(False)
        config.set_http_api_enabled(True)
        config.set_video_server_enabled(False)
        config.set_torrent_search_enabled(False)
        config.set_channel_search_enabled(False)
        config.set_credit_mining_enabled(False)
        config.set_dummy_wallets_enabled(True)
        config.set_popularity_community_enabled(False)

        # Check if we are already running a Tribler instance
        self.process_checker = ProcessChecker()
        if self.process_checker.already_running:
            self.shutdown_process("Another Tribler instance is already using statedir %s" % config.get_state_dir())
            return

        msg("Starting Tribler")

        if options["statedir"]:
            config.set_state_dir(options["statedir"])

        if options["restapi"] > 0:
            config.set_http_api_enabled(True)
            config.set_http_api_port(options["restapi"])

        if options["ipv8"] != -1 and options["ipv8"] > 0:
            config.set_dispersy_port(options["ipv8"])

        if "testnet" in options and options["testnet"]:
            config.set_testnet(True)

        self.session = Session(config)
        self.session.start().addErrback(lambda failure: self.shutdown_process(failure.getErrorMessage()))
        msg("Tribler started")
Exemplo n.º 50
0
    def setUp(self, annotate=True):
        super(TestTorrentChecker, self).setUp(annotate=annotate)

        config = TriblerConfig()
        config.set_state_dir(self.getStateDir())
        config.set_megacache_enabled(True)

        self.session = Session(config)
        self.session.start_database()
        self.session.lm.torrent_db = TorrentDBHandler(self.session)
        self.session.lm.torrent_checker = TorrentChecker(self.session)
        self.session.lm.tracker_manager = TrackerManager(self.session)
        self.session.lm.popularity_community = MockObject()

        self.torrent_checker = self.session.lm.torrent_checker
        self.torrent_checker._torrent_db = self.session.open_dbhandler(NTFY_TORRENTS)
        self.torrent_checker._torrent_db.category = Category()
        self.torrent_checker.listen_on_udp = lambda: None
Exemplo n.º 51
0
 def setUp(self):
     yield super(AbstractUpgrader, self).setUp()
     self.config = TriblerConfig(ConfigObj(configspec=CONFIG_SPEC_PATH))
     self.config.set_state_dir(self.getStateDir())
     self.config.set_torrent_collecting_dir(
         os.path.join(self.session_base_dir, 'torrent_collecting_dir'))
     self.session = Session(self.config)
     self.sqlitedb = None
     self.torrent_store = None
Exemplo n.º 52
0
    def setUp(self, annotate=True):
        super(TestDHTSession, self).setUp(annotate=annotate)

        config = SessionStartupConfig()
        config.set_state_dir(self.getStateDir())

        self.session = Session(config, ignore_singleton=True)

        self.dht_session = FakeDHTSession(self.session, 'a' * 20, 10)
Exemplo n.º 53
0
 def __init__(self):
     from Tribler.Core.Session import Session # Circular import fix
     self.session = Session.get_instance()
     self.peerdb = RePEXLogDB.getInstance()
     self.downloads = {} # infohash -> Download 
     self.swarmcaches = {} # Download -> [SwarmCache]
     self.repexers = {} # Download -> [repexer]
     # register as global observer
     RePEXer.attach_observer(self)
Exemplo n.º 54
0
    def setUp(self, annotate=True):
        super(TestDHTSession, self).setUp(annotate=annotate)

        config = TriblerConfig()
        config.set_state_dir(self.getStateDir())

        self.session = Session(config)

        self.dht_session = FakeDHTSession(self.session, 'a' * 20, 10)
Exemplo n.º 55
0
    def start_tribler():
        config = TriblerConfig()

        priority_order = config.get_cpu_priority_order()
        set_process_priority(pid=os.getpid(), priority_order=priority_order)

        config.set_http_api_port(int(api_port))
        config.set_http_api_enabled(True)

        # Check if we are already running a Tribler instance
        process_checker = ProcessChecker(config.get_state_dir())
        if process_checker.already_running:
            return
        else:
            process_checker.create_lock_file()

        session = Session(config)

        signal.signal(signal.SIGTERM, lambda signum, stack: shutdown(session, signum, stack))
        session.start()
Exemplo n.º 56
0
    def saveDefaultDownloadConfig(self, scfg):
        state_dir = self.utility.session.get_state_dir()

        # Save DownloadStartupConfig
        dlcfgfilename = get_default_dscfg_filename(state_dir)
        self.defaultDLConfig.save(dlcfgfilename)

        # Save SessionStartupConfig
        cfgfilename = Session.get_default_config_filename(state_dir)

        scfg.save(cfgfilename)
Exemplo n.º 57
0
    def _stop(self):
        if self.clean_messages_lc:
            self.clean_messages_lc.stop()
            self.clean_messages_lc = None

        if self.build_history_lc:
            self.build_history_lc.stop()
            self.build_history_lc = None

        if self.session:
            session_shutdown_start = time.time()
            waittime = 60
            self.session.shutdown()
            while not self.session.has_shutdown():
                diff = time.time() - session_shutdown_start
                assert diff < waittime, "Took too long for Session to shutdown"
                logger.info("ONEXIT Waiting for Session to shutdown, will wait for %d more seconds" % (
                    waittime - diff))
                time.sleep(1)
            logger.info("Session is shut down")
            Session.del_instance()
Exemplo n.º 58
0
 def __init__(self):
     # always use getInstance() to create this object
     # ARNOCOMMENT: why isn't the lock used on this read?!
     if self.__single != None:
         raise RuntimeError, "RePEXScheduler is singleton"
     from Tribler.Core.Session import Session # Circular import fix
     self.session = Session.get_instance()
     self.lock = RLock()
     self.active = False
     self.current_repex = None # infohash
     self.downloads = {} # infohash -> Download; in order to stop Downloads that are done repexing
     self.last_attempts = {} # infohash -> ts; in order to prevent starvation when a certain download