Example #1
0
    def from_bytes(cls, payload):
        payload_length, message_id, piece_index = unpack(
            ">IBI", payload[:cls.total_length])
        if message_id != cls.message_id:
            raise msgexcp.Message_Exception("Not a Have message")

        return Have(piece_index)
Example #2
0
    def from_bytes(cls, payload):
        payload_length, message_id, piece_index, block_offset, block_length = unpack(
            ">IBIII", payload[:cls.total_length])
        if message_id != cls.message_id:
            raise msgexcp.Message_Exception("Not a Cancel message")

        return Cancel(piece_index, block_offset, block_length)
Example #3
0
    def from_bytes(cls, payload):
        payload_length, message_id = unpack(">IB", payload[:cls.total_length])

        if message_id != cls.message_id:
            raise msgexcp.Message_Exception("Not an UnChoke message")

        return UnChoke()
Example #4
0
    def from_bytes(cls, payload):
        payload_length = unpack(">I", payload[:cls.total_length])

        if payload_length != 0:
            raise msgexcp.Message_Exception("Not a Keep Alive message")

        return KeepAlive()
Example #5
0
    def from_bytes(cls, payload):
        payload_length, message_id, listen_port = unpack(
            ">IBI", payload[:cls.total_length])

        if message_id != cls.message_id:
            raise msgexcp.Message_Exception("Not a Port message")

        return Port(listen_port)
Example #6
0
    def from_bytes(cls, payload):
        block_length = len(payload) - 13
        payload_length, message_id, piece_index, block_offset, block = unpack(
            ">IBII{}s".format(block_length), payload[:13 + block_length])

        if message_id != cls.message_id:
            raise msgexcp.Message_Exception("Not a Piece message")

        return Piece(block_length, piece_index, block_offset, block)
Example #7
0
    def dispatch(self):
        try:
            payload_length, message_id, = unpack(">IB", self.payload[:5])
        except Exception as e:
            logging.warning("Error when unpacking message : %s" % e.__str__())
            return None

        map_id_to_message = {
            0: Choke,
            1: UnChoke,
            2: Interested,
            3: NotInterested,
            4: Have,
            5: BitField,
            6: Request,
            7: Piece,
            8: Cancel,
            9: Port
        }

        if message_id not in list(map_id_to_message.keys()):
            raise msgexcp.Message_Exception("Wrong message id")

        return map_id_to_message[message_id].from_bytes(self.payload)
Example #8
0
    def from_bytes(cls, payload):
        payload_length, message_id = unpack(">IB", payload[:cls.total_length])
        if message_id != cls.message_id:
            raise msgexcp.Message_Exception("Not a Non Interested message")

        return Interested()