# Path: neubot/negotiate/server.py
# class NegotiateServerModule(object):
# 
#     ''' Each test should implement this interface '''
# 
#     # The minimal collect echoes the request body
#     def collect(self, stream, request_body):
#         ''' Invoked at the end of the test, to collect data '''
#         return request_body
# 
#     # Only speedtest reimplements this method
#     def collect_legacy(self, stream, request_body, request):
#         ''' Legacy interface to collect that also receives the
#             request object: speedtest needs to inspect the Authorization
#             header when the connecting client is pretty old '''
#         return self.collect(stream, request_body)
# 
#     # The minimal unchoke returns the stream unique identifier only
#     def unchoke(self, stream, request_body):
#         ''' Invoked when a stream is authorized to take the test '''
#         return { 'authorization': str(hash(stream)) }
# 
# Path: neubot/backend.py
# BACKEND = BackendProxy()


import logging
import hashlib
from neubot.negotiate.server import NegotiateServerModule
from neubot.backend import BACKEND
        ''' Stream to SHA512 identifier '''
        sha512 = hashlib.new('sha512')
        sha512.update(str(hash(stream)))
        return sha512.digest()

    def unchoke(self, stream, request_body):
        ''' Invoked when we must unchoke a session '''
        sha512 = self._stream_to_sha512(stream)
        if sha512 not in self.peers:
            # Create record for this stream
            self.peers[sha512] = {}
            logging.debug('negotiate_server_raw: add sha512: %s',
              sha512.encode('hex'))
            stream.atclose(self._update_peers)
            return {'authorization': sha512.encode('hex'), 'port': 12345}
        else:
            raise RuntimeError('negotiate_server_raw: multiple unchoke')

    def collect(self, stream, request_body):
        ''' Invoked when we must save the result of a session '''
        sha512 = self._stream_to_sha512(stream)
        if sha512 not in self.peers:
            raise RuntimeError('negotiate_server_raw: not authorized')
        else:
            result = self.peers[sha512]
            # Note: no more than one collect per session
            del self.peers[sha512]
            logging.debug('negotiate_server_raw: del sha512 OK: %s',
              sha512.encode('hex'))
            complete_result = {'client': request_body, 'server': result}

# Path: neubot/negotiate/server.py
# class NegotiateServerModule(object):
# 
#     ''' Each test should implement this interface '''
# 
#     # The minimal collect echoes the request body
#     def collect(self, stream, request_body):
#         ''' Invoked at the end of the test, to collect data '''
#         return request_body
# 
#     # Only speedtest reimplements this method
#     def collect_legacy(self, stream, request_body, request):
#         ''' Legacy interface to collect that also receives the
#             request object: speedtest needs to inspect the Authorization
#             header when the connecting client is pretty old '''
#         return self.collect(stream, request_body)
# 
#     # The minimal unchoke returns the stream unique identifier only
#     def unchoke(self, stream, request_body):
#         ''' Invoked when a stream is authorized to take the test '''
#         return { 'authorization': str(hash(stream)) }
# 
# Path: neubot/backend.py
# BACKEND = BackendProxy()


# Path: neubot/negotiate/server_raw.py
import logging
import hashlib
from neubot.negotiate.server import NegotiateServerModule
from neubot.backend import BACKEND

        ''' Stream to SHA512 identifier '''
        sha512 = hashlib.new('sha512')
        sha512.update(str(hash(stream)))
        return sha512.digest()

    def unchoke(self, stream, request_body):
        ''' Invoked when we must unchoke a session '''
        sha512 = self._stream_to_sha512(stream)
        if sha512 not in self.peers:
            # Create record for this stream
            self.peers[sha512] = {}
            logging.debug('negotiate_server_raw: add sha512: %s',
              sha512.encode('hex'))
            stream.atclose(self._update_peers)
            return {'authorization': sha512.encode('hex'), 'port': 12345}
        else:
            raise RuntimeError('negotiate_server_raw: multiple unchoke')

    def collect(self, stream, request_body):
        ''' Invoked when we must save the result of a session '''
        sha512 = self._stream_to_sha512(stream)
        if sha512 not in self.peers:
            raise RuntimeError('negotiate_server_raw: not authorized')
        else:
            result = self.peers[sha512]
            # Note: no more than one collect per session
            del self.peers[sha512]
            logging.debug('negotiate_server_raw: del sha512 OK: %s',
              sha512.encode('hex'))
            complete_result = {'client': request_body, 'server': result}

            BACKEND.store_raw(complete_result)