def initValue(self, value): arr = bytearray() arr = util.addInt(arr, value) mf = bytearray(1) mf += arr[0:3] self.messageFormat = util.getInt(mf) self.version = util.getByte(arr[3:4], 0) & 0xff
def encode(self): arr = bytearray() mf = bytearray() mf = util.addInt(mf, self.getMessageFormat()) arr += mf[1:4] arr = util.addByte(arr, self.getVersion()) return np.int64(util.getInt(arr))
def getBytes(self): constructorBytes = self.constructor.getBytes() sizeBytes = bytearray() if self.width == 1: sizeBytes = util.addByte(sizeBytes, self.size) elif self.width == 4: sizeBytes = util.addInt(sizeBytes, self.size) countBytes = bytearray() if self.width == 1: countBytes = util.addByte(countBytes, self.count) elif self.width != 0 and self.width == 4: countBytes = util.addInt(countBytes, self.count) elementConstructorBytes = self.elementConstructor.getBytes() valueBytes = bytearray() pos = 0 for tlv in self.values: if isinstance(tlv, TLVAmqp): tlvBytes = tlv.getBytes() valueBytes += tlvBytes[1:len(tlvBytes)] pos += len(tlvBytes) - len(str(elementConstructorBytes)) - 1 data = bytearray() data.append(constructorBytes) if self.size > 0: data += sizeBytes data += countBytes data = util.addByte(data, elementConstructorBytes) data += valueBytes return data
def getBytes(self): constructorBytes = self.constructor.getBytes() sizeBytes = bytearray() if self.width == 1: sizeBytes = util.addByte(sizeBytes, self.size) else: sizeBytes = util.addInt(sizeBytes, self.size) countBytes = bytearray() if self.width == 1: countBytes = util.addByte(countBytes, self.count * 2) else: countBytes = util.addInt(countBytes, self.count * 2) valueBytes = bytearray() pos = 0 if isinstance(self.map, dict): for key, value in self.map.items(): if isinstance(key, TLVAmqp) and isinstance(value, TLVAmqp): keyBytes = key.getBytes() valBytes = value.getBytes() valueBytes.append(keyBytes) pos += len(keyBytes) valueBytes.append(valBytes) pos += len(valBytes) data = bytearray() data.append(constructorBytes) if self.size > 0: data += sizeBytes data += countBytes data += valueBytes return data
def getBytes(self): data = bytearray() data = util.addString(data, self.protocol) data = util.addByte(self.protocolId) data = util.addByte(self.versionMajor) data = util.addByte(self.versionMinor) data = util.addByte(self.versionRevision) return data
def convertUInt(self, i): data = bytearray() if i == 0: return data elif i > 0 and i <= 255: data = util.addByte(data, i) return data else: data = util.addInt(data, i) return data
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 getConstructor(self, buf): code = None constructor = None codeByte = util.getByte(buf, self.index) self.index += 1 if codeByte == 0: descriptor = self.getTlv(buf) code = AMQPType(util.getByte(buf, self.index) & 0xff) self.index += 1 constructor = DescribedConstructor(code, descriptor) else: code = AMQPType(codeByte & 0xff) constructor = SimpleConstructor(code) return constructor
def unwrapDouble(self, tlv): if isinstance(tlv, TLVAmqp): code = tlv.getCode() if code != AMQPType.DOUBLE: raise ValueError('Error trying to parse DOUBLE: received ' + str(tlv.getCode())) return util.getDouble(tlv.getValue())
def unwrapChar(self, tlv): if isinstance(tlv, TLVAmqp): code = tlv.getCode() if code != AMQPType.CHAR: raise ValueError('Error trying to parse CHAR: received ' + str(tlv.getCode())) return util.getInt(tlv.getValue())
def wrapUShort(self, sh): if sh < 0: raise ValueError('negative value of ' + str(sh) + ' cannot be assignet to UShort type') data = bytearray() data = util.addShort(data, sh) return TLVFixed(AMQPType.USHORT, data)
def unwrapSymbol(self, tlv): if isinstance(tlv, TLVAmqp): code = tlv.getCode() if code not in (AMQPType.SYMBOL_8, AMQPType.SYMBOL_32): raise ValueError('Error trying to parse SYMBOL: received ' + str(tlv.getCode())) return AMQPSymbol(str(util.getString(tlv.getValue())))
def unwrapFloat(self, tlv): if isinstance(tlv, TLVAmqp): code = tlv.getCode() if code != AMQPType.FLOAT: raise ValueError('Error trying to parse FLOAT: received ' + str(tlv.getCode())) return util.getFloat(tlv.getValue())
def unwrapTimastamp(self, tlv): if isinstance(tlv, TLVAmqp): code = tlv.getCode() if code != AMQPType.TIMESTAMP: raise ValueError('Error trying to parse TIMESTAMP: received ' + str(tlv.getCode())) return util.getLong(tlv.getValue())
def wrapTimestamp(self, stamp): if stamp is None: raise ValueError('Wrapper cannot wrap timestamp null') data = bytearray() ts = (stamp - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64( 1, 's') data = util.addLong(data, ts) return TLVFixed(AMQPType.TIMESTAMP, data)
def convertULong(self, l): data = bytearray() if l == 0: return data elif l > 0 and l <= 255: return l else: data = util.addLong(data, l) return data
def convertInt(self, i): data = bytearray() if i == 0: return data elif i >= -128 and i <= 127: return i else: data = util.addInt(data, i) return data
def convertLong(self, l): data = bytearray() if l == 0: return data elif l >= -128 and l <= 127: return l else: data = util.addLong(data, l) return data
def next(self, data, index): length = util.getInt(data[self.index:self.index + 4]) & 0xffffffff #print('Parser.next length= ' + str(length) + ' index= ' + str(index)) self.index = index + 4 if length == 1095586128: protocolId = util.getByte(data, self.index) self.index += 1 versionMajor = util.getByte(data, self.index) self.index += 1 versionMinor = util.getByte(data, self.index) self.index += 1 versionRevision = util.getByte(data, self.index) self.index += 1 if (protocolId == 0 or protocolId == 3) and versionMajor == 1 and versionMinor == 0 and versionRevision == 0: self.index = 0 return data[index: 8] self.index = 0 return data[index:length]
def getBytes(self): constructorBytes = self.constructor.getBytes() sizeBytes = bytearray() if self.width == 1: sizeBytes = util.addByte(sizeBytes, self.size) elif self.width != 0: sizeBytes = util.addInt(sizeBytes, self.size) countBytes = bytearray() if self.width == 1: countBytes = util.addByte(countBytes, self.count) elif self.width != 0: countBytes = util.addInt(countBytes, self.count) valueBytes = bytearray() pos = 0 if self.values is not None: for tlv in self.values: if isinstance(tlv, TLVAmqp): tlvBytes = tlv.getBytes() if isinstance(tlvBytes, int): valueBytes = util.addByte(valueBytes, tlvBytes) pos += 1 else: valueBytes += tlvBytes pos += len(tlvBytes) data = bytearray() data += constructorBytes if self.size > 0: data += sizeBytes data += countBytes data += valueBytes return data
def getBytes(self): constructorBytes = self.constructor.getBytes() widthBytes = bytearray() if self.width == 1: widthBytes.append(len(self.value)) elif self.width == 4: widthBytes = util.addInt(widthBytes, len(self.value)) data = bytearray() if isinstance(self.constructor, DescribedConstructor): data += constructorBytes else: data.append(constructorBytes) data += widthBytes if len(self.value) > 0: data += self.value return data
def __init__(self, value): data = bytearray() if isinstance(value, bytearray): data = value if isinstance(value, np.int8): data = util.addByte(data, value) if isinstance(value, np.int16): data = util.addShort(data, value) if isinstance(value, np.int32): data = util.addInt(data, value) if isinstance(value, np.int64): data = util.addLong(data, value) if isinstance(value, np.float32): data = util.addFloat(data, value) if isinstance(value, np.complex128): data = util.addDouble(data, value) self.value = data
def wrapShort(self, sh): data = bytearray() data = util.addShort(data, sh) return TLVFixed(AMQPType.USHORT, data)
def getElement(self, constructor, buf): tlv = None code = constructor.getCode() if isinstance(code, AMQPType): if code == AMQPType.NULL: tlv = TLVNull() elif code in (AMQPType.BOOLEAN_TRUE, AMQPType.BOOLEAN_FALSE, AMQPType.UINT_0, AMQPType.ULONG_0): tlv = TLVFixed(code, bytearray()) elif code in (AMQPType.BOOLEAN, AMQPType.UBYTE, AMQPType.BYTE, AMQPType.SMALL_UINT, AMQPType.SMALL_INT, AMQPType.SMALL_ULONG, AMQPType.SMALL_LONG): value1 = util.getByte(buf, self.index) self.index += 1 tlv = TLVFixed(code, value1) elif code in (AMQPType.SHORT, AMQPType.USHORT): value2 = buf[self.index:self.index + 2] self.index += 2 tlv = TLVFixed(code, value2) elif code in (AMQPType.UINT, AMQPType.INT, AMQPType.FLOAT, AMQPType.DECIMAL_32, AMQPType.CHAR): value4 = buf[self.index:self.index + 4] self.index += 4 tlv = TLVFixed(code, value4) elif code in (AMQPType.ULONG, AMQPType.LONG, AMQPType.DECIMAL_64, AMQPType.DOUBLE, AMQPType.TIMESTAMP): value8 = buf[self.index:self.index + 8] self.index += 8 tlv = TLVFixed(code, value8) elif code in (AMQPType.DECIMAL_128, AMQPType.UUID): value16 = buf[self.index:self.index + 16] self.index += 16 tlv = TLVFixed(code, value16) elif code in (AMQPType.STRING_8, AMQPType.SYMBOL_8, AMQPType.BINARY_8): varlen = util.getByte(buf, self.index) & 0xff self.index += 1 varValue8 = buf[self.index:self.index + int(varlen)] self.index += int(varlen) tlv = TLVVariable(code, varValue8) elif code in (AMQPType.STRING_32, AMQPType.SYMBOL_32, AMQPType.BINARY_32): var32len = util.getInt(buf[self.index:self.index + 4]) self.index += 4 varValue32 = buf[self.index:self.index + int(var32len)] self.index += int(var32len) tlv = TLVVariable(code, varValue32) elif code is AMQPType.LIST_0: tlv = TLVList(None, None) elif code is AMQPType.LIST_8: list8size = util.getByte(buf, self.index) & 0xff self.index += 1 list8count = util.getByte(buf, self.index) & 0xff self.index += 1 list8values = [] for i in range(0, list8count): entity = self.getTlv(buf) list8values.append(entity) tlv = TLVList(code, list8values) elif code is AMQPType.LIST_32: list32size = util.getInt(buf[self.index:self.index + 4]) self.index += 4 list32count = util.getInt(buf[self.index:self.index + 4]) self.index += 4 list32values = [] for i in range(0, list32count): list32values.append(self.getTlv(buf)) tlv = TLVList(code, list32values) elif code is AMQPType.MAP_8: map8size = util.getByte(buf, self.index) & 0xff self.index += 1 map8count = util.getByte(buf, self.index) & 0xff self.index += 1 stop8 = self.index + map8size - 1 map8 = TLVMap(None, None) while self.index < stop8: map8.putElement(self.getTlv(buf), self.getTlv(buf)) tlv = TLVMap(code, map8) elif code is AMQPType.MAP_32: map32size = util.getInt(buf[self.index:self.index + 4]) self.index += 4 map32count = util.getInt(buf[self.index:self.index + 4]) self.index += 4 stop32 = self.index + map32size - 4 map32 = TLVMap(None, None) while self.index < stop32: map32.putElement(self.getTlv(buf), self.getTlv(buf)) tlv = TLVMap(code, map32) elif code is AMQPType.ARRAY_8: array8size = util.getByte(buf, self.index) & 0xff self.index += 1 array8count = util.getByte(buf, self.index) & 0xff self.index += 1 arr8 = [] arr8constructor = self.getConstructor(buf) for i in range(0, array8count): arr8.append(self.getElement(arr8constructor, buf)) tlv = TLVArray(code, arr8) elif code is AMQPType.ARRAY_32: arr32size = util.getInt(buf[self.index:self.index + 4]) self.index += 4 arr32count = util.getInt(buf[self.index:self.index + 4]) self.index += 4 arr32 = [] arr32constructor = self.getConstructor(buf) for i in range(0, arr32count): arr32.append(self.getElement(arr32constructor, buf)) tlv = TLVArray(code, arr32) if isinstance(constructor, DescribedConstructor): tlv.setConstructor(constructor) return tlv
def wrapFloat(self, f): data = bytearray() data = util.addFloat(data, f) return TLVFixed(AMQPType.FLOAT, data)
def getFloat(self): return util.getFloat(self.value)
def getDouble(self): return util.getDouble(self.value)
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 wrapChar(self, ch): data = bytearray() data = util.addInt(data, ch) return TLVFixed(AMQPType.CHAR, data)
def wrapDouble(self, db): data = bytearray() data = util.addDouble(data, db) return TLVFixed(AMQPType.DOUBLE, data)