def set_hostname(self): ''' This method is used to get the hostname of the cache, which is used by the cache_local mode of the NeighborSelection. ''' own_ip = self.session.get_external_ip() hostname = socket.gethostbyaddr(own_ip) NeighborSelection.getInstance().set_cache_hostname(hostname)
def testFillUp(self): ''' Check that the locality cut-off for a sorted list works fine! ''' ns = NeighborSelection.NeighborSelection() ranked = range(1, 20) biased = ns._fillUp(ranked, -1, locality_pref=1) assert ranked == biased[0:len(ranked )], "locality_pref = 1, biased=" + biased # Now test cut-off biased = ns._fillUp(ranked, 6, locality_pref=1) assert len( biased) == 6, "locality_pref = 1, biased and cut to 6=" + biased assert ranked[0:6] == biased, "expect %s result %s" % (ranked[0:6], biased) ranked = range(1, 16) biased = ns._fillUp(ranked, -1, locality_pref=0) assert set(ranked) == set( biased), "locality_pref = 0, biased=%s" % biased ranked = range(0, 20) biased = ns._fillUp(ranked, -1, locality_pref=0.25) assert len(ranked) == len(biased) assert set(ranked[0:5]) == set( biased[0:5] ), "locality_pref = 0.25, biased=" + biased #first quarter -> only ranked ranks
def __init__(self, trackerlist, interval, sched, howmany, minpeers, connect, externalsched, amount_left, up, down, port, ip, myid, infohash, timeout, errorfunc, excfunc, maxpeers, doneflag, upratefunc, downratefunc, unpauseflag = fakeflag(True), config=None): self.excfunc = excfunc newtrackerlist = [] for tier in trackerlist: if len(tier) > 1: shuffle(tier) newtrackerlist += [tier] self.trackerlist = newtrackerlist self.lastsuccessful = '' self.rejectedmessage = 'rejected by tracker - ' self.port = port if DEBUG: print >>sys.stderr,"Rerequest tracker: infohash is",`infohash`,"port is",self.port,"myid",`myid`,"quoted id",quote(myid) self.url = ('?info_hash=%s&peer_id=%s&port=%s' % (quote(infohash), quote(myid), str(port))) self.ip = ip self.interval = interval self.last = None self.trackerid = None self.announce_interval = 1 * 60 self.sched = sched self.howmany = howmany self.minpeers = minpeers self.connect = connect self.externalsched = externalsched self.amount_left = amount_left self.up = up self.down = down self.timeout = timeout self.errorfunc = errorfunc self.maxpeers = maxpeers self.doneflag = doneflag self.upratefunc = upratefunc self.downratefunc = downratefunc self.unpauseflag = unpauseflag self.last_failed = True self.never_succeeded = True self.errorcodes = {} self.lock = SuccessLock() self.special = None self.started = False self.stopped = False self.schedid = 'arno481' self.infohash = infohash self.dht = mainlineDHT.dht self.config = config # SmoothIT_ self.neighborselection = NeighborSelection.getInstance() self.logger = logging.getLogger("Tribler.Rerequester")
def _init_before_session(self): self.ranking = RankingPolicy.selectRankingSource(self._cache_config.get_ranking_source(), conf=self._cache_config) self._logger.warning("Ranking source is %s" % self.ranking) exclude_below = 1# allow only non-remote connections! self.filtering = NeighborSelection.createMechanism(self._cache_config.get_ns_mode(), self.ranking, exclude_below=exclude_below, locality_pref=self._cache_config.get_locality_preference()) self._logger.warning("Neighbor selection is %s" % self.filtering) self.max_downloads = self._cache_config.get_max_downloads()
def testNone(self): mech = NeighborSelection.NoFiltering() assert mech.acceptAsNeighbor(('209.34.91.45', 12345)) iplist = [('209.34.91.45', 123), ('209.34.91.44', 123), ('209.34.91.47', 123), ('81.19.23.42', 123)] sorted = mech.selectAsNeighbors(iplist) assert iplist == sorted sorted = mech.selectAsNeighbors(iplist, 3) assert iplist[0:3] == sorted, "original %s, \nselected %s" % (str( iplist[0:3]), str(sorted))
def testMechanismCreation(self): self.assertEquals(NeighborSelection.ns_instance(), None) mech = NeighborSelection.getInstance() assert isinstance(NeighborSelection.ns_instance(), NeighborSelection.NoFiltering) mech2 = NeighborSelection.createMechanism("none") assert isinstance(mech2, NeighborSelection.NoFiltering) assert mech<>mech2 assert NeighborSelection.ns_instance() is mech2 local = NeighborSelection.createMechanism("enable", rankingSource = OddEvenPolicy()) self.assertTrue(isinstance(local, NeighborSelection.SISFiltering)) self.assertEquals(NeighborSelection.ns_instance(), local)
def testMechanismCreation(self): self.assertEquals(NeighborSelection.ns_instance(), None) mech = NeighborSelection.getInstance() assert isinstance(NeighborSelection.ns_instance(), NeighborSelection.NoFiltering) mech2 = NeighborSelection.createMechanism("none") assert isinstance(mech2, NeighborSelection.NoFiltering) assert mech <> mech2 assert NeighborSelection.ns_instance() is mech2 local = NeighborSelection.createMechanism( "enable", rankingSource=OddEvenPolicy()) self.assertTrue(isinstance(local, NeighborSelection.SISFiltering)) self.assertEquals(NeighborSelection.ns_instance(), local)
def __init__(self, trackerlist, interval, sched, howmany, minpeers, connect, externalsched, amount_left, up, down, port, ip, myid, infohash, timeout, errorfunc, excfunc, maxpeers, doneflag, upratefunc, downratefunc, unpauseflag=fakeflag(True), config=None): self.excfunc = excfunc newtrackerlist = [] for tier in trackerlist: if len(tier) > 1: shuffle(tier) newtrackerlist += [tier] self.trackerlist = newtrackerlist self.lastsuccessful = '' self.rejectedmessage = 'rejected by tracker - ' self.port = port if DEBUG: print >> sys.stderr, "Rerequest tracker: infohash is", ` infohash `, "port is", self.port, "myid", ` myid `, "quoted id", quote( myid) self.url = ('?info_hash=%s&peer_id=%s&port=%s' % (quote(infohash), quote(myid), str(port))) self.ip = ip self.interval = interval self.last = None self.trackerid = None self.announce_interval = 1 * 60 self.sched = sched self.howmany = howmany self.minpeers = minpeers self.connect = connect self.externalsched = externalsched self.amount_left = amount_left self.up = up self.down = down self.timeout = timeout self.errorfunc = errorfunc self.maxpeers = maxpeers self.doneflag = doneflag self.upratefunc = upratefunc self.downratefunc = downratefunc self.unpauseflag = unpauseflag self.last_failed = True self.never_succeeded = True self.errorcodes = {} self.lock = SuccessLock() self.special = None self.started = False self.stopped = False self.schedid = 'arno481' self.infohash = infohash self.dht = mainlineDHT.dht self.config = config # SmoothIT_ self.neighborselection = NeighborSelection.getInstance() self.logger = logging.getLogger("Tribler.Rerequester")
def _setup_client(self): def setup_directories(): if not os.access(self._config.get_directory(), os.F_OK): os.mkdir(self._config.get_directory()) if self._config.get_state_directory(): if not os.access(self._config.get_state_directory(), os.F_OK): os.mkdir(self._config.get_state_directory()) else: state_dir = tempfile.mkdtemp() self._config.set_state_directory(state_dir) def setup_scfg(): scfg = SessionStartupConfig() scfg.set_state_dir(self._config.get_state_directory()) scfg.set_listen_port(self._config.get_port()) scfg.set_overlay(False) scfg.set_megacache(False) scfg.set_upnp_mode(UPNPMODE_DISABLED) return scfg def setup_callback_handlers(session, scfg): self._handlers = PeerCallbackHandlers( self._get_name(), self._config.get_report_interval()) self._handlers.register_handler(PeerReporter(self._get_name())) self._handlers.register_handler( ClientStatistics(self._config.get_directory(), self._config.get_id())) if self._config.is_hap_enabled(): self._logger.info("HAP support enabled") self._handlers.register_handler(ClientHAPHandler(self._config)) if self._config.get_sis_url() != None: ip_addr = net_utils.get_own_ip_addr() self._handlers.register_handler( PeerActivityReportEmitter( (ip_addr, self._config.get_port()), self._config.get_activity_report_interval(), sis_iop_endpoint_url=self._config.get_sis_iop_url())) if self._config.get_exit_on() == constants.EXIT_ON_ALL_FINISHED: self._handlers.register_handler( ClientUptimeHandler(session, method=constants.EXIT_ON_ALL_FINISHED, callback=self._on_exit)) elif self._config.get_exit_on() == constants.EXIT_ON_PLAYBACK_DONE: self._handlers.register_handler( ClientUptimeHandler(session, method=constants.EXIT_ON_PLAYBACK_DONE, callback=self._on_exit)) elif self._config.get_exit_on() == constants.EXIT_ON_SEEDING_TIME: self._handlers.register_handler( ClientUptimeHandler( session, method=constants.EXIT_ON_SEEDING_TIME, max_seeding_time=self._config.get_seeding_time(), callback=self._on_exit)) if self._config.get_report_to() is not None: if self._config.get_report_to() == 'local_report': self._local_reporter = PeerLocalReporter( self._get_name(), self._config.get_id(), session, self._config.get_directory()) self._handlers.register_handler(self._local_reporter) else: self._handlers.register_handler( PeerHTTPReporter( self._get_name(), self._config.get_id(), self._config.get_report_to(), scfg, self._config.get_compress_xml_reports(), self._config.get_serialization_method(), report_interval=self._config.get_report_interval()) ) setup_directories() self._logger.info("Client directory is at %s" % self._config.get_directory()) self._logger.info("Client state directory is at %s" % self._config.get_state_directory()) scfg = setup_scfg() self._session = Session(scfg) self._session.set_supporter_seed( self._config.is_supporter_seed()) #TODO: parameterize!!! self._logger.error("Supporter IPs are: " + self._config.get_supporter_ip()) self._session.set_supporter_ips(self._config.get_supporter_ip()) setup_callback_handlers(self._session, scfg) source = self._config.get_ranking_source() endpoint = self._config.get_sis_client_endpoint() ranking = RankingPolicy.selectRankingSource(source, conf=self._config) NeighborSelection.createMechanism( self._config.get_ns_mode(), ranking, locality_pref=self._config.get_locality_preference()) BiasedUnchoking.BiasedUnchoking(self._config.get_peer_selection_mode(), ranking)
def _setup_client(self): def setup_directories(): if not os.access(self._config.get_directory(), os.F_OK): os.mkdir(self._config.get_directory()) if self._config.get_state_directory(): if not os.access(self._config.get_state_directory(), os.F_OK): os.mkdir(self._config.get_state_directory()) else: state_dir = tempfile.mkdtemp() self._config.set_state_directory(state_dir) def setup_scfg(): scfg = SessionStartupConfig() scfg.set_state_dir(self._config.get_state_directory()) scfg.set_listen_port(self._config.get_port()) scfg.set_overlay(False) scfg.set_megacache(False) scfg.set_upnp_mode(UPNPMODE_DISABLED) return scfg def setup_callback_handlers(session, scfg): self._handlers = PeerCallbackHandlers(self._get_name(), self._config.get_report_interval()) self._handlers.register_handler(PeerReporter(self._get_name())) self._handlers.register_handler(ClientStatistics(self._config.get_directory(), self._config.get_id())) if self._config.is_hap_enabled(): self._logger.info("HAP support enabled") self._handlers.register_handler(ClientHAPHandler(self._config)) if self._config.get_sis_url() != None: ip_addr = net_utils.get_own_ip_addr() self._handlers.register_handler(PeerActivityReportEmitter( (ip_addr, self._config.get_port()), self._config.get_activity_report_interval(), sis_iop_endpoint_url=self._config.get_sis_iop_url())) if self._config.get_exit_on() == constants.EXIT_ON_ALL_FINISHED: self._handlers.register_handler(ClientUptimeHandler(session, method=constants.EXIT_ON_ALL_FINISHED, callback=self._on_exit)) elif self._config.get_exit_on() == constants.EXIT_ON_PLAYBACK_DONE: self._handlers.register_handler(ClientUptimeHandler(session, method=constants.EXIT_ON_PLAYBACK_DONE, callback=self._on_exit)) elif self._config.get_exit_on() == constants.EXIT_ON_SEEDING_TIME: self._handlers.register_handler(ClientUptimeHandler(session, method=constants.EXIT_ON_SEEDING_TIME, max_seeding_time=self._config.get_seeding_time(), callback=self._on_exit)) if self._config.get_report_to() is not None: if self._config.get_report_to() == 'local_report': self._local_reporter = PeerLocalReporter(self._get_name(), self._config.get_id(), session, self._config.get_directory()) self._handlers.register_handler(self._local_reporter) else: self._handlers.register_handler(PeerHTTPReporter(self._get_name(), self._config.get_id(), self._config.get_report_to(), scfg, self._config.get_compress_xml_reports(), self._config.get_serialization_method(), report_interval=self._config.get_report_interval())) setup_directories() self._logger.info("Client directory is at %s" % self._config.get_directory()) self._logger.info("Client state directory is at %s" % self._config.get_state_directory()) scfg = setup_scfg() self._session = Session(scfg) self._session.set_supporter_seed(self._config.is_supporter_seed())#TODO: parameterize!!! self._logger.error("Supporter IPs are: "+self._config.get_supporter_ip()) self._session.set_supporter_ips(self._config.get_supporter_ip()) setup_callback_handlers(self._session, scfg) source = self._config.get_ranking_source() endpoint = self._config.get_sis_client_endpoint() ranking = RankingPolicy.selectRankingSource(source, conf=self._config) NeighborSelection.createMechanism(self._config.get_ns_mode(), ranking, locality_pref=self._config.get_locality_preference()) BiasedUnchoking.BiasedUnchoking(self._config.get_peer_selection_mode(), ranking)
def testOddEvenLocally(self): ranking = OddEvenPolicy() mech = NeighborSelection.SISFiltering(1.0, ranking) self.tryOddEvenMech(mech)