class PeerActivityReportEmitter(PeerCallbackInterface): def __init__(self, own_addr, emit_interval, sis_iop_endpoint_url="http://localhost:8080/sis/IoPEndpoint"): self._emit_interval = emit_interval # in seconds self._ws = IoP_WSClientImpl(sis_iop_endpoint_url) self._last_timestamp = time.time() self._own_address = own_addr self._logger = logging.getLogger("Status.%s" % self.__class__.__name__) def state_callback(self, ds): self._logger.debug("state_callback called") ts = time.time() #self._logger.info("ts is %s" % str(ts)) # check if we have to file an activity report right now # if not, just return if not ts - self._last_timestamp >= self._emit_interval: return try: d = ds.get_download() self._logger.info("torrent: %s" % ds.get_download().get_def().get_name_as_unicode()) torrent_id = get_id(d.get_def()) torrent_url = "unknown" torrent_progress = int(ds.get_progress()*100) torrent_size = d.get_def().get_length() downloads = [ ( torrent_id, torrent_url, torrent_size, torrent_progress ) ] self._ws.report_activity(self._own_address[0], self._own_address[1], downloads) self._last_timestamp = ts except: self._logger.error("an error occured: could not retrieve torrent stats from resp. download object") def video_event_callback(self, d, event, params): # not implemented pass
def _init_selector(self): selector = None mode = self._cache_config.get_torrent_selection_mechanism() if mode == 'local_folder': selector = LocalFolderSelector.LocalFolderSelector(self._cache_config.get_torrent_directory()) self._logger.info("Run in local folder mode") elif mode == 'SIS': #selector = self._cache_config.get_ws_client() selector = IoP_WSClientImpl(self._cache_config.get_sis_iop_url()) selector.set_torrent_dir(self._cache_config.get_torrent_directory()) #FIXME: we now create these instances twice!!! once in cache config and then here! Should rather use a wrapper here! self._logger.info("Run in WS torrent selection mode") else: raise ConfigurationException("Unsupported torrent selection mechanism!"+str()) assert selector is not None self._logger.info("Use torrent selection method "+str(selector)) interval = self._cache_config.get_torrent_selection_interval() max_torrents = self._cache_config.get_max_torrents() assert max_torrents >= self.max_downloads stay_in_torrents = self._cache_config.get_stay_in_torrents() self.torrent_selection = TorrentSelection.start_torrent_selection( self, selector, selection_interval=interval, max_torrents=max_torrents, stay_in_torrents=stay_in_torrents)
def _update_from_sis(self): def set_attribute(attribute, func_name): if attribute is not None: func_name(attribute) ws_client = IoP_WSClientImpl(self.get_sis_iop_url()) config_dict = {} try: config_dict = ws_client.retrieve_configuration_parameters( net_utils.get_own_ip_addr()) self._logger.warning("Config dict returned was: %s" % config_dict) if len(config_dict.keys()) == 0: self._logger.warning( "Received an empty dict. Cache configuration was not updated." ) return max_torrents = int( min( float(config_dict.get('u')) / float(config_dict.get('ulow')), float(config_dict.get('d')) / float(config_dict.get('dlow')))) sis_modes = { 'Collaboration': constants.SELECTION_MODE_IOP, 'Plain': constants.SELECTION_MODE_LOCAL, 'Combination': constants.SELECTION_MODE_LOCAL } set_attribute(config_dict.get('t'), self.set_torrent_selection_interval) set_attribute(config_dict.get('u'), self.set_upload_limit) set_attribute(config_dict.get('d'), self.set_download_limit) set_attribute(config_dict.get('slots'), self.set_max_upload_slots_per_download) set_attribute(config_dict.get('remotes'), self.set_establish_remote_connections) set_attribute(config_dict.get('x'), self.set_stay_in_torrents) set_attribute(config_dict.get('localIPs'), self.set_ip_prefixes) set_attribute(sis_modes[config_dict.get('mode')], self.set_torrent_selection_mechanism) set_attribute(max_torrents, self.set_max_torrents) set_attribute(max_torrents, self.set_max_downloads) except: self._logger.error( "Failed to retrieve cache configuration from SIS at %s" % self.get_sis_iop_url()) self._logger.error("Cache configuration was not updated.", exc_info=True) self._logger.error("Received config dict was: %s" % config_dict) raise ConfigurationException( "Failed to retrieve cache configuration from SIS at %s" % self.get_sis_iop_url()) return
def _update_from_sis(self): def set_attribute(attribute, func_name): if attribute is not None: func_name(attribute) ws_client = IoP_WSClientImpl(self.get_sis_iop_url()) config_dict = {} try: config_dict = ws_client.retrieve_configuration_parameters(net_utils.get_own_ip_addr()) self._logger.warning("Config dict returned was: %s" % config_dict) if len(config_dict.keys()) == 0: self._logger.warning("Received an empty dict. Cache configuration was not updated.") return max_torrents = int(min( float(config_dict.get('u')) / float(config_dict.get('ulow')), float(config_dict.get('d')) / float(config_dict.get('dlow')))) sis_modes = { 'Collaboration' : constants.SELECTION_MODE_IOP, 'Plain' : constants.SELECTION_MODE_LOCAL, 'Combination' : constants.SELECTION_MODE_LOCAL } set_attribute(config_dict.get('t'), self.set_torrent_selection_interval) set_attribute(config_dict.get('u'), self.set_upload_limit) set_attribute(config_dict.get('d'), self.set_download_limit) set_attribute(config_dict.get('slots'), self.set_max_upload_slots_per_download) set_attribute(config_dict.get('remotes'), self.set_establish_remote_connections) set_attribute(config_dict.get('x'), self.set_stay_in_torrents) set_attribute(config_dict.get('localIPs'), self.set_ip_prefixes) set_attribute(sis_modes[config_dict.get('mode')], self.set_torrent_selection_mechanism) set_attribute(max_torrents, self.set_max_torrents) set_attribute(max_torrents, self.set_max_downloads) except: self._logger.error("Failed to retrieve cache configuration from SIS at %s" % self.get_sis_iop_url()) self._logger.error("Cache configuration was not updated.", exc_info=True) self._logger.error("Received config dict was: %s" % config_dict) raise ConfigurationException("Failed to retrieve cache configuration from SIS at %s" % self.get_sis_iop_url()) return
class TestIoP_WSClient(unittest.TestCase): def setUp(self): self.wsclient = IoP_WSClientImpl(sis_iop_endpoint_url="http://localhost:8080/sis/IoPEndpoint") self._logger = logging.getLogger(self.__class__.__name__) def tearDown(self): self.wsclient = None pass def testRetrieveTorrentStats(self): # First send an activity report to the server! #torrent_id, torrent_url, filesize(bytes), progress(value in [0..1]) #data = ('60d5d82328b4547511fdeac9bf4d0112daa0ce00', "./torrents/ubuntu.org", 28184, 0) #self.wsclient.report_activity("127.0.0.1", 12345, [data]) tstats_list = self.wsclient.retrieve_torrent_stats(10) self.assertTrue(len(tstats_list) >= 0, tstats_list)# nothing comes back right now self._logger.warn("Got torrents stats "+str(tstats_list)) #for (id, url, rate) in tstats_list: # assert type(rate) is int # assert urlparse.urlparse(url) # assert type(id) is str def testRetrieveLocalPeersForActiveTorrents(self): tdef = TorrentDef.load("torrents/ubuntu.torrent") infohash_hex = common_utils.get_id(tdef) torrent_dict = self.wsclient.retrieve_local_peers_for_active_torrents(list_of_torrent_ids=[infohash_hex], maximum_number_of_peers=10, include_seeds=False) assert len(torrent_dict) >= 0, torrent_dict #exp_id = '60d5d82328b4547511fdeac9bf4d0112daa0ce00' #self.assertTrue(torrent_dict.has_key(exp_id), torrent_dict) #self.assertEquals(torrent_dict[exp_id], [('127.0.0.1', 80)]) def testRetrieveConfigurationParameters(self): config = self.wsclient.retrieve_configuration_parameters('127.0.0.1') keys = ['localIPRanges', 'd', 'ulow', 'remotes', 'u', 'mode', 'x', 'slots', 'dlow', 't'] for key in config.keys(): self.assertTrue(key in keys) def testReportActivityWithEmptyList(self): self.wsclient.report_activity('127.0.0.1', 80, []) def testReportActivityWithFilledList(self): self.wsclient.report_activity('127.0.0.1', 80, [("", "", 489202, 0.9)])
def setUp(self): self.wsclient = IoP_WSClientImpl(sis_iop_endpoint_url="http://localhost:8080/sis/IoPEndpoint") self._logger = logging.getLogger(self.__class__.__name__)
def __init__(self, own_addr, emit_interval, sis_iop_endpoint_url="http://localhost:8080/sis/IoPEndpoint"): self._emit_interval = emit_interval # in seconds self._ws = IoP_WSClientImpl(sis_iop_endpoint_url) self._last_timestamp = time.time() self._own_address = own_addr self._logger = logging.getLogger("Status.%s" % self.__class__.__name__)