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
Exemplo n.º 2
0
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)])