Esempio n. 1
0
 def test_bitencoding2(self):
     pieces = set([0])
     message = specs.PieceBitFieldMessage.create(pieces)
     code = message.pack()
     object = Message.parse(code)
     self.assert_(isinstance(object, PieceBitFieldMessage))
     self.assertEqual(object.pieces, pieces)
Esempio n. 2
0
    def test_identity(self):
        peer_id = ''.join(random.choice(string.letters) for _ in range(20))

        handshake = specs.HandshakeMessage.create(peer_id)

        bytes = handshake.pack()
        new_handshake = Message.parse(bytes)

        self.assert_(isinstance(new_handshake, specs.HandshakeMessage))
Esempio n. 3
0
    def test_iomessage(self):
        peer_id = ''.join(random.choice(string.letters) for _ in range(20))

        handshake = specs.HandshakeMessage.create(peer_id)

        bytes = handshake.pack()
        new_handshake = Message.parse(bytes)

        self.assert_(new_handshake.is_valid())
        self.assertEqual(new_handshake.peer_id, peer_id)
Esempio n. 4
0
    def stringReceived(self, message):
        """Overrides method to receive a message without the prefix """

        logging.debug('Received: {0} from {1}'.format(repr(message[0]),
                                                      self.partner_address))

        if self.handshaked and self.choked:
            logging.info('Ignoring message because the peer is choked')
            return

        try:
            message_object = Message.parse(message)
        except MessageException as error:
            logging.error('Decoding error ' + str(error))
            self.drop()
            return

        handler = self.handlers.get(type(message_object), self.receive_default)
        handler(message_object)