Ejemplo n.º 1
0
    def decode(self, data):
        #print('index= ' + str(self.index))
        length = util.getInt(data[self.index:self.index+4]) & 0xffffffff
        self.index += 4
        doff = util.getByte(data,self.index) & 0xff
        self.index += 1
        type = util.getByte(data,self.index) & 0xff
        self.index += 1
        channel = util.getShort(data[self.index:self.index+2]) & 0xffff
        self.index += 2

        if length == 8 and doff == 2 and (type == 0 or type == 1) and channel == 0:
            if self.index >= len(data):
                self.index = 0
                return AMQPPing()
            else:
                raise ValueError("Received malformed ping-header with invalid length")

        if length == 1095586128 and (doff == 3 or doff == 0) and type == 1 and channel == 0:
            if self.index >= len(data):
                self.index = 0
                return AMQPProtoHeader(doff)
            else:
                raise ValueError("Received malformed protocol-header with invalid length")

        header = None
        headerFactory = HeaderFactory(self.index)
        if type == 0:
            header = headerFactory.getAMQP(data)
        elif type == 1:
            header = headerFactory.getSASL(data)
        else:
            raise ValueError("Received malformed header with invalid type: " + type)

        self.index = headerFactory.getIndex()

        if isinstance(header, AMQPHeader):
            header.setDoff(doff)
            header.setType(type)
            header.setChannel(channel)

        if header.getCode() == HeaderCode.TRANSFER:
            if isinstance(header,AMQPTransfer):
                header.setSections({})
            while self.index < len(data):
                headerFactory.setIndex(self.index)
                section = headerFactory.getSection(data)
                header.sections[section.getCode()] = section
                self.index = headerFactory.getIndex()

        self.index = 0
        return header
 def unwrapShort(self, tlv):
     if isinstance(tlv, TLVAmqp):
         if tlv.getCode() != AMQPType.SHORT:
             raise ValueError('Error trying to parse SHORT: received ' +
                              str(tlv.getCode()))
     return util.getShort(tlv.getValue())
 def getShort(self):
     return util.getShort(self.value)