Ejemplo n.º 1
0
    def collect(self, stream, request_body):
        ''' Invoked when we must save the result of a session '''
        sha1 = self._stream_to_sha1(stream)
        if sha1 not in self.peers:
            raise RuntimeError('Not authorized to collect')
        else:

            # Note: no more than one collect per session
            result = self.peers[sha1]
            del self.peers[sha1]

            #
            # Backward compatibility: the variable name changed from
            # can_share to can_publish after Neubot 0.4.5
            #
            if 'privacy_can_share' in request_body:
                request_body['privacy_can_publish'] = request_body[
                  'privacy_can_share']
                del request_body['privacy_can_share']

            #
            # Note that the following is not a bug: it's just that
            # the server saves results using the point of view of the
            # client, i.e. upload_speed _is_ client's upload speed.
            #
            request_body['timestamp'] = result['timestamp']
            request_body['upload_speed'] = result['upload_speed']

            if privacy.collect_allowed(request_body):
                BACKEND.bittorrent_store(request_body)
            else:
                logging.warning('* bad privacy settings: %s', str(stream))

            #
            # After we've saved the result into the dictionary we
            # can add extra information we would like to return to
            # the client.
            #
            request_body['target_bytes'] = result['target_bytes']
            return request_body