class TestHAP_WSClient(unittest.TestCase):
    def setUp(self):
        self._logger = logging.getLogger(self.__class__.__name__)
    
    def tearDown(self):
        pass
    
    def testReportActivity(self):
        self._client = HAP_WSClient("http://localhost:8080/sis/HAPEndpoint",
                                    "127.0.0.1", 5577)
        n1 = HAPNeighbourStatisticsDTO(1024, "192.168.0.75", 1024)
        n2 = HAPNeighbourStatisticsDTO(2048, "192.168.0.83", 2048)
        
        nstats = [n1, n2]
        
        self._client.report_activity(nstats)
        self._client = None
        
    def testOwnIPCanBeNone(self):
        self._client = HAP_WSClient("http://localhost:8080/sis/HAPEndpoint",
                                    None, 5588)
        n1 = HAPNeighbourStatisticsDTO(1024, "192.168.0.75", 1024)
        n2 = HAPNeighbourStatisticsDTO(2048, "192.168.0.83", 2048)
        
        nstats = [n1, n2]
        
        self._client.report_activity(nstats)
        self._client = None
Example #2
0
class TestHAP_WSClient(unittest.TestCase):
    def setUp(self):
        self._logger = logging.getLogger(self.__class__.__name__)

    def tearDown(self):
        pass

    def testReportActivity(self):
        self._client = HAP_WSClient("http://localhost:8080/sis/HAPEndpoint",
                                    "127.0.0.1", 5577)
        n1 = HAPNeighbourStatisticsDTO(1024, "192.168.0.75", 1024)
        n2 = HAPNeighbourStatisticsDTO(2048, "192.168.0.83", 2048)

        nstats = [n1, n2]

        self._client.report_activity(nstats)
        self._client = None

    def testOwnIPCanBeNone(self):
        self._client = HAP_WSClient("http://localhost:8080/sis/HAPEndpoint",
                                    None, 5588)
        n1 = HAPNeighbourStatisticsDTO(1024, "192.168.0.75", 1024)
        n2 = HAPNeighbourStatisticsDTO(2048, "192.168.0.83", 2048)

        nstats = [n1, n2]

        self._client.report_activity(nstats)
        self._client = None
Example #3
0
    def testOwnIPCanBeNone(self):
        self._client = HAP_WSClient("http://localhost:8080/sis/HAPEndpoint",
                                    None, 5588)
        n1 = HAPNeighbourStatisticsDTO(1024, "192.168.0.75", 1024)
        n2 = HAPNeighbourStatisticsDTO(2048, "192.168.0.83", 2048)

        nstats = [n1, n2]

        self._client.report_activity(nstats)
        self._client = None
 def __init__(self, client_config):
     # neighbours is a multidimensional dictionary, where the first dimension's key
     # is the download for which upload/download statistics are gathered, and the second
     # dimension is the peer identifier. it stores and updates total upload and download
     # for traffic for active and non-active neighbours
     # example: neighbours[{torrend_id}][{peer_id}]['upload']
     # peer_id is comprised of ip address and port!
     self._neighbours = {}
     self._neighbour_ips = []
     self._client_config = client_config
     self._hap_client = HAP_WSClient(client_config.get_sis_hap_url(), None,
                                     client_config.get_port())
     self._last_report_sent = time.time() - client_config.get_hap_interval()
     self._logger = logging.getLogger("Status.HAP")
     self._last_period_peer_total = {}
 def testOwnIPCanBeNone(self):
     self._client = HAP_WSClient("http://localhost:8080/sis/HAPEndpoint",
                                 None, 5588)
     n1 = HAPNeighbourStatisticsDTO(1024, "192.168.0.75", 1024)
     n2 = HAPNeighbourStatisticsDTO(2048, "192.168.0.83", 2048)
     
     nstats = [n1, n2]
     
     self._client.report_activity(nstats)
     self._client = None
 def __init__(self, client_config):
     # neighbours is a multidimensional dictionary, where the first dimension's key
     # is the download for which upload/download statistics are gathered, and the second
     # dimension is the peer identifier. it stores and updates total upload and download
     # for traffic for active and non-active neighbours
     # example: neighbours[{torrend_id}][{peer_id}]['upload']
     # peer_id is comprised of ip address and port!
     self._neighbours = {}
     self._neighbour_ips = []
     self._client_config = client_config
     self._hap_client = HAP_WSClient(client_config.get_sis_hap_url(), None,
                                     client_config.get_port())
     self._last_report_sent = time.time()-client_config.get_hap_interval()
     self._logger = logging.getLogger("Status.HAP")
     self._last_period_peer_total = {}
class ClientHAPHandler(PeerCallbackInterface):
    def __init__(self, client_config):
        # neighbours is a multidimensional dictionary, where the first dimension's key
        # is the download for which upload/download statistics are gathered, and the second
        # dimension is the peer identifier. it stores and updates total upload and download
        # for traffic for active and non-active neighbours
        # example: neighbours[{torrend_id}][{peer_id}]['upload']
        # peer_id is comprised of ip address and port!
        self._neighbours = {}
        self._neighbour_ips = []
        self._client_config = client_config
        self._hap_client = HAP_WSClient(client_config.get_sis_hap_url(), None,
                                        client_config.get_port())
        self._last_report_sent = time.time()-client_config.get_hap_interval()
        self._logger = logging.getLogger("Status.HAP")
        self._last_period_peer_total = {}
    
    def state_callback(self, ds):

        torrent_id = common_utils.get_id(ds.get_download().get_def())
        
        if not ds.get_peerlist() is None:
            for peer in ds.get_peerlist():
                self._update_volume(torrent_id, peer['ip'], peer['dtotal'], peer['utotal'])
                
        # only send the report if a waiting period of hap_interval seconds passed by
        if not abs(self._last_report_sent - time.time()) >= self._client_config.get_hap_interval():
            return
        
        neighbour_stats = []
        new_last_period_peer_total = {}
        for peer in self._neighbour_ips:
            (download_volume, upload_volume) = self.aggregate_total_peer_traffic(peer)
            
            if download_volume == 0 and upload_volume == 0:
                continue
            
            if self._last_period_peer_total.has_key(peer):
                # we have old statistics for this peer
                old_dl, old_ul = self._last_period_peer_total[peer]
                
                if download_volume - old_dl == 0 and upload_volume - old_ul == 0:
                    continue
                
                neighbour_stats.append(HAPNeighbourStatisticsDTO(download_volume-old_dl, peer, upload_volume-old_ul))
                new_last_period_peer_total[peer] = (download_volume, upload_volume)
            else:
                # the peer must have joined during the current period
                neighbour_stats.append(HAPNeighbourStatisticsDTO(download_volume, peer, upload_volume))
                new_last_period_peer_total[peer] = (download_volume, upload_volume)
                
        # dont send a report if there are no neighbour stats
        if len(neighbour_stats) == 0:
            self._start_next_cycle(new_last_period_peer_total)
            return
        
        self._logger.info("Sending statistics to HAP...")
        self._hap_client.report_activity(neighbour_stats)
        self._start_next_cycle(new_last_period_peer_total)
        
    def _start_next_cycle(self, new_last_period_peer_total):
        self._last_report_sent = time.time()
        self._last_period_peer_total = new_last_period_peer_total
        # reset neighbours as well
        self._neighbours = {}
        self._neighbour_ips = []
            
    def _update_volume(self, torrent_id, peer_id, download, upload):
        '''Updates upload and download statistics for a given peer ID.
           Total upload and download are stored in KBytes.
        
           Arguments:
                torrent_id    --    Torrent ID in hexadecimal format
                peer_id       --    Peer ID that adheres to the format '<IP>'
                download      --    Current total download for given torrent and peer in bytes
                upload        --    Current total upload for given torrent and peer in bytes
        '''
        if not peer_id in self._neighbour_ips:
            self._neighbour_ips.append(peer_id)
        if not self._neighbours.has_key(torrent_id):
            self._neighbours[torrent_id] = {}
        if not self._neighbours[torrent_id].has_key(peer_id):
            self._neighbours[torrent_id][peer_id] = { 'upload'   :   0,
                                                      'download' :   0 }
        self._neighbours[torrent_id][peer_id]['upload'] = int(upload / 1024.0)
        self._neighbours[torrent_id][peer_id]['download'] = int(download / 1024.0)
        
    def aggregate_total_peer_traffic(self, peer_id):
        '''Calculates the total amount of upload and download traffic in KBytes for the peer
           identified by peer_id.
           
           Arguments:
               peer_id        -- The peer's ID (in this case, this should be the peer's
                                 IP address)
                                 
           Returns:
               A 2-tuple of the form (total_download, total_upload) where both values
               are given in KBytes.
        '''
        total_upload, total_download = 0.0, 0.0
        for torrent_id in self._neighbours.keys():
            if self._neighbours[torrent_id].has_key(peer_id):
                total_upload += self._neighbours[torrent_id][peer_id]['upload']
                total_download += self._neighbours[torrent_id][peer_id]['download']
        return (total_download, total_upload)
    
    def video_event_callback(self, d, event, params):
        pass
class ClientHAPHandler(PeerCallbackInterface):
    def __init__(self, client_config):
        # neighbours is a multidimensional dictionary, where the first dimension's key
        # is the download for which upload/download statistics are gathered, and the second
        # dimension is the peer identifier. it stores and updates total upload and download
        # for traffic for active and non-active neighbours
        # example: neighbours[{torrend_id}][{peer_id}]['upload']
        # peer_id is comprised of ip address and port!
        self._neighbours = {}
        self._neighbour_ips = []
        self._client_config = client_config
        self._hap_client = HAP_WSClient(client_config.get_sis_hap_url(), None,
                                        client_config.get_port())
        self._last_report_sent = time.time() - client_config.get_hap_interval()
        self._logger = logging.getLogger("Status.HAP")
        self._last_period_peer_total = {}

    def state_callback(self, ds):

        torrent_id = common_utils.get_id(ds.get_download().get_def())

        if not ds.get_peerlist() is None:
            for peer in ds.get_peerlist():
                self._update_volume(torrent_id, peer['ip'], peer['dtotal'],
                                    peer['utotal'])

        # only send the report if a waiting period of hap_interval seconds passed by
        if not abs(self._last_report_sent -
                   time.time()) >= self._client_config.get_hap_interval():
            return

        neighbour_stats = []
        new_last_period_peer_total = {}
        for peer in self._neighbour_ips:
            (download_volume,
             upload_volume) = self.aggregate_total_peer_traffic(peer)

            if download_volume == 0 and upload_volume == 0:
                continue

            if self._last_period_peer_total.has_key(peer):
                # we have old statistics for this peer
                old_dl, old_ul = self._last_period_peer_total[peer]

                if download_volume - old_dl == 0 and upload_volume - old_ul == 0:
                    continue

                neighbour_stats.append(
                    HAPNeighbourStatisticsDTO(download_volume - old_dl, peer,
                                              upload_volume - old_ul))
                new_last_period_peer_total[peer] = (download_volume,
                                                    upload_volume)
            else:
                # the peer must have joined during the current period
                neighbour_stats.append(
                    HAPNeighbourStatisticsDTO(download_volume, peer,
                                              upload_volume))
                new_last_period_peer_total[peer] = (download_volume,
                                                    upload_volume)

        # dont send a report if there are no neighbour stats
        if len(neighbour_stats) == 0:
            self._start_next_cycle(new_last_period_peer_total)
            return

        self._logger.info("Sending statistics to HAP...")
        self._hap_client.report_activity(neighbour_stats)
        self._start_next_cycle(new_last_period_peer_total)

    def _start_next_cycle(self, new_last_period_peer_total):
        self._last_report_sent = time.time()
        self._last_period_peer_total = new_last_period_peer_total
        # reset neighbours as well
        self._neighbours = {}
        self._neighbour_ips = []

    def _update_volume(self, torrent_id, peer_id, download, upload):
        '''Updates upload and download statistics for a given peer ID.
           Total upload and download are stored in KBytes.
        
           Arguments:
                torrent_id    --    Torrent ID in hexadecimal format
                peer_id       --    Peer ID that adheres to the format '<IP>'
                download      --    Current total download for given torrent and peer in bytes
                upload        --    Current total upload for given torrent and peer in bytes
        '''
        if not peer_id in self._neighbour_ips:
            self._neighbour_ips.append(peer_id)
        if not self._neighbours.has_key(torrent_id):
            self._neighbours[torrent_id] = {}
        if not self._neighbours[torrent_id].has_key(peer_id):
            self._neighbours[torrent_id][peer_id] = {
                'upload': 0,
                'download': 0
            }
        self._neighbours[torrent_id][peer_id]['upload'] = int(upload / 1024.0)
        self._neighbours[torrent_id][peer_id]['download'] = int(download /
                                                                1024.0)

    def aggregate_total_peer_traffic(self, peer_id):
        '''Calculates the total amount of upload and download traffic in KBytes for the peer
           identified by peer_id.
           
           Arguments:
               peer_id        -- The peer's ID (in this case, this should be the peer's
                                 IP address)
                                 
           Returns:
               A 2-tuple of the form (total_download, total_upload) where both values
               are given in KBytes.
        '''
        total_upload, total_download = 0.0, 0.0
        for torrent_id in self._neighbours.keys():
            if self._neighbours[torrent_id].has_key(peer_id):
                total_upload += self._neighbours[torrent_id][peer_id]['upload']
                total_download += self._neighbours[torrent_id][peer_id][
                    'download']
        return (total_download, total_upload)

    def video_event_callback(self, d, event, params):
        pass