Esempio n. 1
0
class TrackerResource(Resource):

    def __init__(self, peer_manager):
        Resource.__init__(self)
        self._peer_manager = peer_manager
        self._encoder = PixtreamJSONEncoder()

    def _encode_peers(self):
        return self._encoder.encode(self._peer_manager)

    def _encode_failure(self, reason):
        answer = dict(failure_reason = reason)
        return self._encoder.encode(answer)

    def _encode_success(self):
        return self._encoder.encode('success')
Esempio n. 2
0
    def test_encoder(self):
        pm = PeerManager(peer_timeout=100)
        pm.peers = dict(
            ("id{0}".format(i), Peer("id{0}".format(i), "192.168.1.{0}".format(i), 20000 + i, time.time()))
            for i in range(10)
        )

        encoder = PixtreamJSONEncoder()
        output = encoder.encode(pm)

        result = json.loads(output)

        self.assertEqual(result["request_interval"], 100)

        peers = result["peers"]
        peers = sorted(peers, key=lambda x: x["id"])

        for i, peer in enumerate(peers):
            self.assertEqual(peer["id"], "id{0}".format(i))
            self.assertEqual(peer["ip"], "192.168.1.{0}".format(i))
            self.assertEqual(peer["port"], 20000 + i)