public void doBeforeReceive() throws Exception { HornetQBuffer body = message.getBodyBuffer(); if (body != null) { body.resetReaderIndex(); } }
/* * Copy constructor */ protected MessageImpl(final MessageImpl other, TypedProperties properties) { messageID = other.getMessageID(); userID = other.getUserID(); address = other.getAddress(); type = other.getType(); durable = other.isDurable(); expiration = other.getExpiration(); timestamp = other.getTimestamp(); priority = other.getPriority(); this.properties = new TypedProperties(properties); // This MUST be synchronized using the monitor on the other message to prevent it running // concurrently // with getEncodedBuffer(), otherwise can introduce race condition when delivering concurrently // to // many subscriptions and bridging to other nodes in a cluster synchronized (other) { bufferValid = other.bufferValid; endOfBodyPosition = other.endOfBodyPosition; endOfMessagePosition = other.endOfMessagePosition; copied = other.copied; if (other.buffer != null) { other.bufferUsed = true; // We need to copy the underlying buffer too, since the different messsages thereafter might // have different // properties set on them, making their encoding different buffer = other.buffer.copy(0, other.buffer.writerIndex()); buffer.setIndex(other.buffer.readerIndex(), buffer.capacity()); } } }
public void decode(final HornetQBuffer buffer) { channelID = buffer.readLong(); decodeRest(buffer); size = buffer.readerIndex(); }
/** * Debug Helper!!!! * * <p>I'm leaving this message here without any callers for a reason: During debugs it's important * eventually to identify what's on the bodies, and this method will give you a good idea about * them. Add the message.bodyToString() to the Watch variables on the debugger view and this will * show up like a charm!!! * * @return */ public String bodyToString() { getEndOfBodyPosition(); int readerIndex1 = this.buffer.readerIndex(); buffer.readerIndex(0); byte[] buffer1 = new byte[buffer.writerIndex()]; buffer.readBytes(buffer1); buffer.readerIndex(readerIndex1); byte[] buffer2 = null; if (bodyBuffer != null) { int readerIndex2 = this.bodyBuffer.readerIndex(); bodyBuffer.readerIndex(0); buffer2 = new byte[bodyBuffer.writerIndex() - bodyBuffer.readerIndex()]; bodyBuffer.readBytes(buffer2); bodyBuffer.readerIndex(readerIndex2); } return "ServerMessage@" + Integer.toHexString(System.identityHashCode(this)) + "[" + ",bodyStart=" + getEndOfBodyPosition() + " buffer=" + ByteUtil.bytesToHex(buffer1, 1) + ", bodyBuffer=" + ByteUtil.bytesToHex(buffer2, 1); }
public void readBytes(final HornetQBuffer dst, final int length) { if (length > dst.writableBytes()) { throw new IndexOutOfBoundsException(); } readBytes(dst, dst.writerIndex(), length); dst.writerIndex(dst.writerIndex() + length); }
public void createBody(final int initialMessageBufferSize) { buffer = HornetQBuffers.dynamicBuffer(initialMessageBufferSize); // There's a bug in netty which means a dynamic buffer won't resize until you write a byte buffer.writeByte((byte) 0); buffer.setIndex(BODY_OFFSET, BODY_OFFSET); }
public synchronized HornetQBuffer getBodyBufferCopy() { // Must copy buffer before sending it HornetQBuffer newBuffer = buffer.copy(0, buffer.capacity()); newBuffer.setIndex(0, getEndOfBodyPosition()); return new ResetLimitWrappedHornetQBuffer(BODY_OFFSET, newBuffer, null); }
/** * Large message version of {@link #assertMessageBody(int, ClientMessage)}. * * @param i * @param message */ protected static void assertLargeMessageBody(final int i, final ClientMessage message) { HornetQBuffer buffer = message.getBodyBuffer(); for (int j = 0; j < LARGE_MESSAGE_SIZE; j++) { Assert.assertTrue( "msg " + i + ", expecting " + LARGE_MESSAGE_SIZE + " bytes, got " + j, buffer.readable()); Assert.assertEquals("equal at " + j, UnitTestCase.getSamplebyte(j), buffer.readByte()); } }
protected ClientMessage writeBodyToMessage(CharSequence payload, ClientMessage message) { message.getBodyBuffer().resetReaderIndex(); HornetQBuffer buff = message.getBodyBuffer(); buff.clear(); buff.writeString(payload.toString()); return message; }
private void decode() { endOfBodyPosition = buffer.getInt(BUFFER_HEADER_SPACE); buffer.readerIndex(endOfBodyPosition + DataConstants.SIZE_INT); decodeHeadersAndProperties(buffer); endOfMessagePosition = buffer.readerIndex(); bufferValid = true; }
@Override public void decodeRest(final HornetQBuffer buffer) { // Buffer comes in after having read standard headers and positioned at Beginning of body part message.decodeFromBuffer(buffer); int ri = buffer.readerIndex(); requiresResponse = buffer.readBoolean(); buffer.readerIndex(ri); }
private void forceCopy() { // Must copy buffer before sending it buffer = buffer.copy(0, buffer.capacity()); buffer.setIndex(0, getEndOfBodyPosition()); if (bodyBuffer != null) { bodyBuffer.setBuffer(buffer); } bufferUsed = false; }
public static TransportConfiguration decode(HornetQBuffer buffer) { String name = BufferHelper.readNullableSimpleStringAsString(buffer); String factoryClassName = buffer.readSimpleString().toString(); int paramSize = buffer.readInt(); Map<String, Object> params = new HashMap<String, Object>(); for (int i = 0; i < paramSize; i++) { String key = buffer.readSimpleString().toString(); String value = buffer.readSimpleString().toString(); params.put(key, value); } TransportConfiguration config = new TransportConfiguration(factoryClassName, params, name); return config; }
public synchronized void write(final PagedMessage message) throws Exception { if (!file.isOpen()) { return; } ByteBuffer buffer = fileFactory.newBuffer(message.getEncodeSize() + Page.SIZE_RECORD); HornetQBuffer wrap = HornetQBuffers.wrappedBuffer(buffer); wrap.clear(); wrap.writeByte(Page.START_BYTE); wrap.writeInt(0); int startIndex = wrap.writerIndex(); message.encode(wrap); int endIndex = wrap.writerIndex(); wrap.setInt(1, endIndex - startIndex); // The encoded length wrap.writeByte(Page.END_BYTE); buffer.rewind(); file.writeDirect(buffer, false); if (pageCache != null) { pageCache.addLiveMessage(message); } numberOfMessages.incrementAndGet(); size.addAndGet(buffer.limit()); storageManager.pageWrite(message, pageId); }
/** Write pending output into file */ public void flush() throws Exception { if (writingChannel != null) { sequentialFile.position(0); // To Fix the size of the file writingChannel.writerIndex(writingChannel.capacity()); sequentialFile.writeInternal(writingChannel.toByteBuffer()); sequentialFile.close(); newDataFiles.add(currentFile); } writingChannel = null; }
public static void encodeConfigs( HornetQBuffer buffer, List<Pair<TransportConfiguration, TransportConfiguration>> configs) { buffer.writeInt(configs == null ? 0 : configs.size()); if (configs != null) { for (Pair<TransportConfiguration, TransportConfiguration> pair : configs) { encode(buffer, pair.getA()); boolean backup = (pair.getB() != null); buffer.writeBoolean(backup); if (backup) { encode(buffer, pair.getB()); } } } }
@Override public HornetQBuffer encode(final RemotingConnection connection) { HornetQBuffer buffer = message.getEncodedBuffer(); // Sanity check if (buffer.writerIndex() != message.getEndOfMessagePosition()) { throw new IllegalStateException("Wrong encode position"); } buffer.writeBoolean(requiresResponse); size = buffer.writerIndex(); // Write standard headers int len = size - DataConstants.SIZE_INT; buffer.setInt(0, len); buffer.setByte(DataConstants.SIZE_INT, getType()); buffer.setLong(DataConstants.SIZE_INT + DataConstants.SIZE_BYTE, channelID); // Position reader for reading by Netty buffer.readerIndex(0); message.resetCopied(); return buffer; }
public void encodeHeadersAndProperties(final HornetQBuffer buffer) { buffer.writeLong(messageID); buffer.writeNullableSimpleString(address); if (userID == null) { buffer.writeByte(DataConstants.NULL); } else { buffer.writeByte(DataConstants.NOT_NULL); buffer.writeBytes(userID.asBytes()); } buffer.writeByte(type); buffer.writeBoolean(durable); buffer.writeLong(expiration); buffer.writeLong(timestamp); buffer.writeByte(priority); properties.encode(buffer); }
public static void encode(HornetQBuffer buffer, TransportConfiguration config) { BufferHelper.writeAsNullableSimpleString(buffer, config.getName()); BufferHelper.writeAsSimpleString(buffer, config.getFactoryClassName()); buffer.writeInt(config.getParams().size()); for (Entry<String, Object> param : config.getParams().entrySet()) { BufferHelper.writeAsSimpleString(buffer, param.getKey()); BufferHelper.writeAsSimpleString(buffer, param.getValue().toString()); } }
public static List<Pair<TransportConfiguration, TransportConfiguration>> decodeConfigs( HornetQBuffer buffer) { int size = buffer.readInt(); List<Pair<TransportConfiguration, TransportConfiguration>> configs = new ArrayList<Pair<TransportConfiguration, TransportConfiguration>>(size); for (int i = 0; i < size; i++) { TransportConfiguration live = decode(buffer); boolean hasBackup = buffer.readBoolean(); TransportConfiguration backup = null; if (hasBackup) { backup = decode(buffer); } configs.add(new Pair<TransportConfiguration, TransportConfiguration>(live, backup)); } return configs; }
public void decodeHeadersAndProperties(final HornetQBuffer buffer) { messageID = buffer.readLong(); address = buffer.readNullableSimpleString(); if (buffer.readByte() == DataConstants.NOT_NULL) { byte[] bytes = new byte[16]; buffer.readBytes(bytes); userID = new UUID(UUID.TYPE_TIME_BASED, bytes); } else { userID = null; } type = buffer.readByte(); durable = buffer.readBoolean(); expiration = buffer.readLong(); timestamp = buffer.readLong(); priority = buffer.readByte(); properties.decode(buffer); }
protected String readLine(HornetQBuffer in, int maxLength, String errorMessage) throws IOException { byte b; ByteArrayOutputStream baos = new ByteArrayOutputStream(maxLength); while ((b = in.readByte()) != '\n') { if (baos.size() > maxLength) { throw new StompException(errorMessage, true); } baos.write(b); } byte[] sequence = baos.toByteArray(); return new String(sequence, "UTF-8"); }
public HornetQBuffer toHornetQBuffer() throws Exception { if (buffer == null) { if (bytesBody != null) { buffer = HornetQBuffers.dynamicBuffer(bytesBody.length + 512); } else { buffer = HornetQBuffers.dynamicBuffer(512); } if (isPing()) { buffer.writeByte((byte) 10); return buffer; } StringBuffer head = new StringBuffer(); head.append(command); head.append(Stomp.NEWLINE); // Output the headers. for (Map.Entry<String, String> header : headers.entrySet()) { head.append(header.getKey()); head.append(Stomp.Headers.SEPARATOR); head.append(header.getValue()); head.append(Stomp.NEWLINE); } // Add a newline to separate the headers from the content. head.append(Stomp.NEWLINE); buffer.writeBytes(head.toString().getBytes("UTF-8")); if (bytesBody != null) { buffer.writeBytes(bytesBody); } buffer.writeBytes(END_OF_FRAME); size = buffer.writerIndex(); } return buffer; }
@Override public void encodeRest(final HornetQBuffer buffer) { buffer.writeBoolean(synchronizationIsFinished); buffer.writeBoolean(allowsAutoFailBack); buffer.writeString(nodeID); if (synchronizationIsFinished) return; buffer.writeByte(dataType.code); buffer.writeInt(ids.length); for (long id : ids) { buffer.writeLong(id); } }
public static void assertEqualsBuffers( final int size, final HornetQBuffer expected, final HornetQBuffer actual) { // assertEquals(expected.length, actual.length); expected.readerIndex(0); actual.readerIndex(0); for (int i = 0; i < size; i++) { byte b1 = expected.readByte(); byte b2 = actual.readByte(); Assert.assertEquals("byte at index " + i, b1, b2); } expected.resetReaderIndex(); actual.resetReaderIndex(); }
@Override public void decodeRest(final HornetQBuffer buffer) { synchronizationIsFinished = buffer.readBoolean(); allowsAutoFailBack = buffer.readBoolean(); nodeID = buffer.readString(); if (synchronizationIsFinished) { return; } dataType = SyncDataType.getDataType(buffer.readByte()); int length = buffer.readInt(); ids = new long[length]; for (int i = 0; i < length; i++) { ids[i] = buffer.readLong(); } }
// Decode from journal or paging public void decode(final HornetQBuffer buff) { int start = buff.readerIndex(); endOfBodyPosition = buff.readInt(); endOfMessagePosition = buff.getInt(endOfBodyPosition - BUFFER_HEADER_SPACE + start); int length = endOfMessagePosition - BUFFER_HEADER_SPACE; buffer.setIndex(0, BUFFER_HEADER_SPACE); buffer.writeBytes(buff, start, length); decode(); buff.readerIndex(start + length); }
public synchronized HornetQBuffer getEncodedBuffer() { HornetQBuffer buff = encodeToBuffer(); if (bufferUsed) { HornetQBuffer copied = buff.copy(0, buff.capacity()); copied.setIndex(0, endOfMessagePosition); return copied; } else { buffer.setIndex(0, endOfMessagePosition); bufferUsed = true; return buffer; } }
// This must be synchronized as it can be called concurrently id the message is being delivered // concurrently to // many queues - the first caller in this case will actually encode it private synchronized HornetQBuffer encodeToBuffer() { if (!bufferValid) { if (bufferUsed) { // Cannot use same buffer - must copy forceCopy(); } int bodySize = getEndOfBodyPosition(); // Clebert: I've started sending this on encoding due to conversions between protocols // and making sure we are not losing the buffer start position between protocols this.endOfBodyPosition = bodySize; // write it buffer.setInt(BUFFER_HEADER_SPACE, bodySize); // Position at end of body and skip past the message end position int. // check for enough room in the buffer even though it is dynamic if ((bodySize + 4) > buffer.capacity()) { buffer.setIndex(0, bodySize); buffer.writeInt(0); } else { buffer.setIndex(0, bodySize + DataConstants.SIZE_INT); } encodeHeadersAndProperties(buffer); // Write end of message position endOfMessagePosition = buffer.writerIndex(); buffer.setInt(bodySize, endOfMessagePosition); bufferValid = true; } return buffer; }
public HornetQBuffer encode(final RemotingConnection connection) { HornetQBuffer buffer = connection.createBuffer(PacketImpl.INITIAL_PACKET_SIZE); // The standard header fields buffer.writeInt(0); // The length gets filled in at the end buffer.writeByte(type); buffer.writeLong(channelID); encodeRest(buffer); size = buffer.writerIndex(); // The length doesn't include the actual length byte int len = size - DataConstants.SIZE_INT; buffer.setInt(0, len); return buffer; }