List of usage examples for io.netty.buffer ByteBuf readLong
public abstract long readLong();
From source file:org.blockartistry.mod.DynSurround.network.PacketSpeechBubble.java
License:MIT License
@Override public void fromBytes(final ByteBuf buf) { this.entityId = new UUID(buf.readLong(), buf.readLong()); this.message = ByteBufUtils.readUTF8String(buf); }
From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.CollectdPacketDecoder.java
License:Apache License
private long readNumericPartContent(ByteBuf content) { return content.readLong(); }
From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.CollectdPacketDecoder.java
License:Apache License
private Values readValuePartContent(ByteBuf content, int length) { int beginIndex = content.readerIndex(); int total = content.readUnsignedShort(); List<DataType> dataTypes = new ArrayList<>(total); for (int i = 0; i < total; i++) { byte sampleTypeId = content.readByte(); dataTypes.add(DataType.findById(sampleTypeId)); }/* ww w .j a v a2 s .co m*/ List<Number> data = new ArrayList<>(total); for (DataType dataType : dataTypes) { switch (dataType) { case COUNTER: case ABSOLUTE: byte[] valueBytes = new byte[8]; content.readBytes(valueBytes); data.add(new BigInteger(1, valueBytes)); break; case DERIVE: data.add(content.readLong()); break; case GAUGE: data.add(Double.longBitsToDouble(ByteBufUtil.swapLong(content.readLong()))); break; default: logger.debug("Skipping unknown data type: {}", dataType); } } // Skip any additionnal bytes int readCount = content.readerIndex() - beginIndex; if (length > readCount) { content.skipBytes(readCount - length); } return new Values(dataTypes, data); }
From source file:org.helios.octo.client.ResponseHandler.java
License:Open Source License
/** * {@inheritDoc}//w w w .ja va 2 s . c o m * @see io.netty.handler.codec.ByteToMessageDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, io.netty.channel.MessageList) */ @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, MessageList<Object> out) { long bytesAvailable = super.actualReadableBytes(); log.info("Replay Bytes Available:" + bytesAvailable); if (bytesAvailable < 1) return; switch (state()) { case REQUEST_ID: log.info("--------------------------------->Processing [" + state() + "]"); long reqId = in.readLong(); ctx.channel().attr(OctoShared.REQ_ID).set(reqId); checkpoint(RESPONSE_TYPE); log.info("REQUEST_ID:" + reqId); case RESPONSE_TYPE: log.info("--------------------------------->Processing [" + state() + "]"); byte responseType = in.readByte(); log.info("RESPONSE_TYPE:" + responseType); if (responseType == 0) { checkpoint(STREAM_TYPE); } else { // if(!ctx.pipeline().get(name)) ctx.pipeline().addAfter(OctoShared.RESPONSE_HANDLER, OctoShared.OBJECT_DECODER, new ObjectDecoder(ClassResolvers.cacheDisabled(null))); out.add(in.copy(0, super.actualReadableBytes())); checkpoint(FORWARD); return; } case STREAM_TYPE: log.info("--------------------------------->Processing [" + state() + "]"); byte streamType = in.readByte(); ctx.channel().attr(OctoShared.STREAM).set(streamType); log.info("STREAM_TYPE:" + streamType); checkpoint(FORWARD); return; // out.add(in.readBytes(super.actualReadableBytes())); // return; case FORWARD: log.info("--------------------------------->Processing [" + state() + "]"); log.info("Forwarding [" + super.actualReadableBytes() + "]...."); out.add(in.readBytes(super.actualReadableBytes())); log.info("Forward Complete. Remaining: [" + super.actualReadableBytes() + "]"); return; default: log.warn("Unexpected state [" + state() + "]"); break; } }
From source file:org.jupiter.transport.netty.handler.ProtocolDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { switch (state()) { case HEADER_MAGIC: checkMagic(in.readShort()); // MAGIC checkpoint(State.HEADER_SIGN); case HEADER_SIGN: header.sign(in.readByte()); // ?? checkpoint(State.HEADER_STATUS); case HEADER_STATUS: header.status(in.readByte()); // ?? checkpoint(State.HEADER_ID); case HEADER_ID: header.id(in.readLong()); // ?id checkpoint(State.HEADER_BODY_LENGTH); case HEADER_BODY_LENGTH: header.bodyLength(in.readInt()); // ? checkpoint(State.BODY);//from w w w.j av a2s .c om case BODY: switch (header.messageCode()) { case JProtocolHeader.HEARTBEAT: break; case JProtocolHeader.REQUEST: { int length = checkBodyLength(header.bodyLength()); byte[] bytes = new byte[length]; in.readBytes(bytes); JRequestBytes request = new JRequestBytes(header.id()); request.timestamp(SystemClock.millisClock().now()); request.bytes(header.serializerCode(), bytes); out.add(request); break; } case JProtocolHeader.RESPONSE: { int length = checkBodyLength(header.bodyLength()); byte[] bytes = new byte[length]; in.readBytes(bytes); JResponseBytes response = new JResponseBytes(header.id()); response.status(header.status()); response.bytes(header.serializerCode(), bytes); out.add(response); break; } default: throw IoSignals.ILLEGAL_SIGN; } checkpoint(State.HEADER_MAGIC); } }
From source file:org.lanternpowered.pingy.PingyHandler.java
License:MIT License
private void handleStatusPing(ChannelHandlerContext ctx, ByteBuf msg) { sendMessage(ctx, 0x01, buf -> buf.writeLong(msg.readLong())); }
From source file:org.onlab.netty.MessageDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object> out) throws Exception { switch (state()) { case READ_MESSAGE_ID: messageId = buffer.readLong(); checkpoint(DecoderState.READ_SENDER_IP_VERSION); case READ_SENDER_IP_VERSION: ipVersion = buffer.readByte() == 0x0 ? Version.INET : Version.INET6; checkpoint(DecoderState.READ_SENDER_IP); case READ_SENDER_IP: byte[] octects = new byte[IpAddress.byteLength(ipVersion)]; buffer.readBytes(octects);// www. j a v a 2 s . c om senderIp = IpAddress.valueOf(ipVersion, octects); checkpoint(DecoderState.READ_SENDER_PORT); case READ_SENDER_PORT: senderPort = buffer.readInt(); checkpoint(DecoderState.READ_MESSAGE_TYPE_LENGTH); case READ_MESSAGE_TYPE_LENGTH: messageTypeLength = buffer.readInt(); checkpoint(DecoderState.READ_MESSAGE_TYPE); case READ_MESSAGE_TYPE: byte[] messageTypeBytes = new byte[messageTypeLength]; buffer.readBytes(messageTypeBytes); messageType = new String(messageTypeBytes, Charsets.UTF_8); checkpoint(DecoderState.READ_CONTENT_LENGTH); case READ_CONTENT_LENGTH: contentLength = buffer.readInt(); checkpoint(DecoderState.READ_CONTENT); case READ_CONTENT: byte[] payload = new byte[contentLength]; buffer.readBytes(payload); InternalMessage message = new InternalMessage(messageId, new Endpoint(senderIp, senderPort), messageType, payload); out.add(message); checkpoint(DecoderState.READ_MESSAGE_ID); break; default: checkState(false, "Must not be here"); } }
From source file:org.onosproject.lisp.msg.protocols.DefaultLispInfo.java
License:Apache License
public static LispInfo deserialize(ByteBuf byteBuf) throws LispParseError, LispReaderException { if (byteBuf.readerIndex() != 0) { return null; }/*from ww w. j a v a 2 s . c o m*/ // infoReply -> 1 bit boolean infoReplyFlag = ByteOperator.getBit(byteBuf.readByte(), INFO_REPLY_INDEX); // let's skip the reserved field byteBuf.skipBytes(RESERVED_SKIP_LENGTH_1); // nonce -> 64 bits long nonce = byteBuf.readLong(); // keyId -> 16 bits short keyId = byteBuf.readShort(); // authenticationDataLength -> 16 bits short authLength = byteBuf.readShort(); // authData -> depends on the authenticationDataLength byte[] authData = new byte[authLength]; byteBuf.readBytes(authData); // ttl -> 32 bits int ttl = byteBuf.readInt(); // let's skip the reserved field byteBuf.skipBytes(RESERVED_SKIP_LENGTH_2); // mask length -> 8 bits short maskLength = byteBuf.readUnsignedByte(); LispAfiAddress prefix = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf); return new DefaultLispInfo(infoReplyFlag, nonce, keyId, authLength, authData, ttl, (byte) maskLength, prefix); }
From source file:org.onosproject.store.cluster.messaging.impl.MessageDecoder.java
License:Apache License
@Override @SuppressWarnings("squid:S128") // suppress switch fall through warning protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object> out) throws Exception { switch (state()) { case READ_MESSAGE_PREAMBLE: preamble = buffer.readInt();//from w w w. j a v a 2 s . c o m checkpoint(DecoderState.READ_LOGICAL_TIME); case READ_LOGICAL_TIME: logicalTime = buffer.readLong(); checkpoint(DecoderState.READ_LOGICAL_COUNTER); case READ_LOGICAL_COUNTER: logicalCounter = buffer.readLong(); checkpoint(DecoderState.READ_MESSAGE_ID); case READ_MESSAGE_ID: messageId = buffer.readLong(); checkpoint(DecoderState.READ_SENDER_IP_VERSION); case READ_SENDER_IP_VERSION: ipVersion = buffer.readByte() == 0x0 ? Version.INET : Version.INET6; checkpoint(DecoderState.READ_SENDER_IP); case READ_SENDER_IP: byte[] octets = new byte[IpAddress.byteLength(ipVersion)]; buffer.readBytes(octets); senderIp = IpAddress.valueOf(ipVersion, octets); checkpoint(DecoderState.READ_SENDER_PORT); case READ_SENDER_PORT: senderPort = buffer.readInt(); checkpoint(DecoderState.READ_MESSAGE_TYPE_LENGTH); case READ_MESSAGE_TYPE_LENGTH: messageTypeLength = buffer.readInt(); checkpoint(DecoderState.READ_MESSAGE_TYPE); case READ_MESSAGE_TYPE: byte[] messageTypeBytes = new byte[messageTypeLength]; buffer.readBytes(messageTypeBytes); messageType = new String(messageTypeBytes, Charsets.UTF_8); checkpoint(DecoderState.READ_MESSAGE_STATUS); case READ_MESSAGE_STATUS: status = Status.forId(buffer.readInt()); checkpoint(DecoderState.READ_CONTENT_LENGTH); case READ_CONTENT_LENGTH: contentLength = buffer.readInt(); checkpoint(DecoderState.READ_CONTENT); case READ_CONTENT: byte[] payload; if (contentLength > 0) { //TODO Perform a sanity check on the size before allocating payload = new byte[contentLength]; buffer.readBytes(payload); } else { payload = new byte[0]; } InternalMessage message = new InternalMessage(preamble, new HybridLogicalTime(logicalTime, logicalCounter), messageId, new Endpoint(senderIp, senderPort), messageType, payload, status); out.add(message); checkpoint(DecoderState.READ_MESSAGE_PREAMBLE); break; default: checkState(false, "Must not be here"); } }
From source file:org.opendaylight.netide.openflowjava.protocol.impl.serialization.factories.FlowRemovedMessageFactoryTest.java
License:Open Source License
@Test public void testSerialize() { FlowRemovedMessageFactory serializer = new FlowRemovedMessageFactory(); SerializerRegistry registry = new NetIdeSerializerRegistryImpl(); registry.init();/* w w w . j av a2 s . co m*/ serializer.injectSerializerRegistry(registry); ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer(); serializer.serialize(message, serializedBuffer); BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 72); Assert.assertEquals("Wrong cookie", message.getCookie().longValue(), serializedBuffer.readLong()); Assert.assertEquals("Wrong priority", message.getPriority().intValue(), serializedBuffer.readShort()); Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readByte()); Assert.assertEquals("Wrong Table ID", message.getTableId().getValue().intValue(), serializedBuffer.readUnsignedByte()); Assert.assertEquals("Wrong duration sec", message.getDurationSec().intValue(), serializedBuffer.readInt()); Assert.assertEquals("Wrong duration nsec", message.getDurationNsec().intValue(), serializedBuffer.readInt()); Assert.assertEquals("Wrong Idle timeout", message.getIdleTimeout().intValue(), serializedBuffer.readShort()); Assert.assertEquals("Wrong Hard timeout", message.getIdleTimeout().intValue(), serializedBuffer.readShort()); Assert.assertEquals("Wrong Packet count", message.getPacketCount().longValue(), serializedBuffer.readLong()); Assert.assertEquals("Wrong Byte count", message.getByteCount().longValue(), serializedBuffer.readLong()); Assert.assertEquals("Wrong match type", 1, serializedBuffer.readUnsignedShort()); serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES); Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort()); short fieldAndMask = serializedBuffer.readUnsignedByte(); Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1); Assert.assertEquals("Wrong oxm field", 1, fieldAndMask >> 1); serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES); Assert.assertEquals("Wrong oxm value", 42, serializedBuffer.readUnsignedInt()); Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort()); fieldAndMask = serializedBuffer.readUnsignedByte(); Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1); Assert.assertEquals("Wrong oxm field", 9, fieldAndMask >> 1); serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES); Assert.assertEquals("Wrong oxm value", 4, serializedBuffer.readUnsignedByte()); serializedBuffer.skipBytes(7); }