Example usage for io.netty.buffer ByteBuf getUnsignedShort

List of usage examples for io.netty.buffer ByteBuf getUnsignedShort

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf getUnsignedShort.

Prototype

public abstract int getUnsignedShort(int index);

Source Link

Document

Gets an unsigned 16-bit short integer at the specified absolute index in this buffer.

Usage

From source file:org.opendaylight.openflowjava.protocol.impl.clients.UdpSimpleClientFramer.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext chc, DatagramPacket msg, List<Object> list) throws Exception {
    ByteBuf bb = msg.content();
    if (bb.readableBytes() < LENGTH_OF_HEADER) {
        LOG.debug("skipping bb - too few data for header: {}", bb.readableBytes());
        return;/*from  ww  w.j  a va 2s.c o m*/
    }

    int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
    if (bb.readableBytes() < length) {
        LOG.debug("skipping bb - too few data for msg: {} < {}", bb.readableBytes(), length);
        return;
    }
    LOG.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));

    ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
    list.add(messageBuffer);
    messageBuffer.retain();
    bb.skipBytes(length);
}

From source file:org.opendaylight.openflowjava.protocol.impl.core.OFDatagramPacketHandler.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
    LOG.debug("OFDatagramPacketFramer");
    MessageConsumer consumer = UdpConnectionMap.getMessageConsumer(msg.sender());
    if (consumer == null) {
        ConnectionFacade connectionFacade = adapterFactory.createConnectionFacade(ctx.channel(), msg.sender(),
                false);/*from  w  ww  . j a  v  a 2  s  . c  o  m*/
        connectionHandler.onSwitchConnected(connectionFacade);
        connectionFacade.checkListeners();
        UdpConnectionMap.addConnection(msg.sender(), connectionFacade);
    }
    ByteBuf bb = msg.content();
    int readableBytes = bb.readableBytes();
    if (readableBytes < LENGTH_OF_HEADER) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("skipping bytebuf - too few bytes for header: {} < {}", readableBytes, LENGTH_OF_HEADER);
            LOG.debug("bb: {}", ByteBufUtils.byteBufToHexString(bb));
        }
        return;
    }

    int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
    LOG.debug("length of actual message: {}", length);

    if (readableBytes < length) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("skipping bytebuf - too few bytes for msg: {} < {}", readableBytes, length);
            LOG.debug("bytebuffer: {}", ByteBufUtils.byteBufToHexString(bb));
        }
        return;
    }
    LOG.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));

    byte version = bb.readByte();
    if ((version == EncodeConstants.OF13_VERSION_ID) || (version == EncodeConstants.OF10_VERSION_ID)) {
        LOG.debug("detected version: {}", version);
        ByteBuf messageBuffer = bb.slice();
        out.add(new VersionMessageUdpWrapper(version, messageBuffer, msg.sender()));
        messageBuffer.retain();
    } else {
        LOG.warn("detected version: {} - currently not supported", version);
    }
    bb.skipBytes(bb.readableBytes());
}

From source file:org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
    int readableBytes = bb.readableBytes();
    if (readableBytes < LENGTH_OF_HEADER) {
        LOGGER.debug("skipping bb - too few data for header: " + readableBytes);
        return;/* w w  w  .  j  a v  a  2s  .com*/
    }

    int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
    LOGGER.debug("length of actual message: {}", length);

    if (readableBytes < length) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("skipping bb - too few data for msg: " + readableBytes + " < " + length);
            LOGGER.debug("bb: " + ByteBufUtils.byteBufToHexString(bb));
        }
        return;
    }
    LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));

    ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
    list.add(messageBuffer);
    messageBuffer.retain();
    bb.skipBytes(length);
}

From source file:org.opendaylight.openflowjava.protocol.impl.deserialization.action.OF13SetFieldActionDeserializer.java

License:Open Source License

@Override
public Action deserialize(ByteBuf input) {
    org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder builder = new ActionBuilder();
    int startIndex = input.readerIndex();
    input.skipBytes(2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
    SetFieldCaseBuilder caseBuilder = new SetFieldCaseBuilder();
    SetFieldActionBuilder actionBuilder = new SetFieldActionBuilder();
    int oxmClass = input.getUnsignedShort(input.readerIndex());
    // get oxm_field & hasMask byte and extract the field value
    int oxmField = input.getUnsignedByte(input.readerIndex() + EncodeConstants.SIZE_OF_SHORT_IN_BYTES) >>> 1;
    MatchEntryDeserializerKey key = new MatchEntryDeserializerKey(EncodeConstants.OF13_VERSION_ID, oxmClass,
            oxmField);/* w  w  w .  j  a va2s.  c  o m*/
    if (oxmClass == EncodeConstants.EXPERIMENTER_VALUE) {
        long expId = input.getUnsignedInt(input.readerIndex() + EncodeConstants.SIZE_OF_SHORT_IN_BYTES
                + 2 * EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
        key.setExperimenterId(expId);
    }
    OFDeserializer<MatchEntry> matchDeserializer = registry.getDeserializer(key);
    List<MatchEntry> entry = new ArrayList<>();
    entry.add(matchDeserializer.deserialize(input));
    actionBuilder.setMatchEntry(entry);
    caseBuilder.setSetFieldAction(actionBuilder.build());
    builder.setActionChoice(caseBuilder.build());
    int paddingRemainder = (input.readerIndex() - startIndex) % EncodeConstants.PADDING;
    if (paddingRemainder != 0) {
        input.skipBytes(EncodeConstants.PADDING - paddingRemainder);
    }
    return builder.build();
}

From source file:org.opendaylight.openflowjava.protocol.impl.util.CodeKeyMakerFactory.java

License:Open Source License

/**
 * @param version/*from  ww w .j a va2 s.  c  o m*/
 * @return
 */
public static CodeKeyMaker createMatchEntriesKeyMaker(short version) {
    return new AbstractCodeKeyMaker(version) {
        @Override
        public MessageCodeKey make(ByteBuf input) {
            int oxmClass = input.getUnsignedShort(input.readerIndex());
            int oxmField = input
                    .getUnsignedByte(input.readerIndex() + EncodeConstants.SIZE_OF_SHORT_IN_BYTES) >>> 1;
            MatchEntryDeserializerKey key = new MatchEntryDeserializerKey(getVersion(), oxmClass, oxmField);
            if (oxmClass == EncodeConstants.EXPERIMENTER_VALUE) {
                long expId = input.getUnsignedInt(input.readerIndex() + EncodeConstants.SIZE_OF_SHORT_IN_BYTES
                        + 2 * EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
                key.setExperimenterId(expId);
                return key;
            }
            key.setExperimenterId(null);
            return key;
        }
    };
}

From source file:org.opendaylight.openflowjava.protocol.impl.util.CodeKeyMakerFactory.java

License:Open Source License

/**
 * @param version/*from w w  w. j  a v  a 2  s.  c o  m*/
 * @return
 */
public static CodeKeyMaker createActionsKeyMaker(short version) {
    return new AbstractCodeKeyMaker(version) {
        @Override
        public MessageCodeKey make(ByteBuf input) {
            int type = input.getUnsignedShort(input.readerIndex());
            if (type == EncodeConstants.EXPERIMENTER_VALUE) {
                Long expId = input
                        .getUnsignedInt(input.readerIndex() + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
                return new ExperimenterActionDeserializerKey(getVersion(), expId);
            }
            ActionDeserializerKey actionDeserializerKey = new ActionDeserializerKey(getVersion(), type, null);
            return actionDeserializerKey;
        }
    };
}

From source file:org.opendaylight.openflowjava.protocol.impl.util.CodeKeyMakerFactory.java

License:Open Source License

/**
 * @param version//from  w w w .ja va2 s .c  o m
 * @return
 */
public static CodeKeyMaker createInstructionsKeyMaker(short version) {
    return new AbstractCodeKeyMaker(version) {
        @Override
        public MessageCodeKey make(ByteBuf input) {
            int type = input.getUnsignedShort(input.readerIndex());
            if (type == EncodeConstants.EXPERIMENTER_VALUE) {
                Long expId = input
                        .getUnsignedInt(input.readerIndex() + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
                return new ExperimenterInstructionDeserializerKey(getVersion(), expId);
            }
            return new InstructionDeserializerKey(getVersion(), type, null);
        }
    };
}

From source file:org.opendaylight.protocol.bmp.impl.message.PeerUpHandler.java

License:Open Source License

private static int getBgpMessageLength(final ByteBuf buffer) {
    return buffer.getUnsignedShort(buffer.readerIndex() + MessageUtil.MARKER_LENGTH);
}

From source file:org.traccar.protocol.AlematicsFrameDecoder.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf buf) throws Exception {

    if (buf.readableBytes() < MESSAGE_MINIMUM_LENGTH) {
        return null;
    }//w  ww  . j  ava 2  s .  c o m

    if (buf.getUnsignedShort(buf.readerIndex()) == 0xFAF8) {
        ByteBuf heartbeat = buf.readRetainedSlice(12);
        if (ctx != null && ctx.channel() != null) {
            ctx.channel().writeAndFlush(new NetworkMessage(heartbeat, ctx.channel().remoteAddress()));
        }
    }

    return super.decode(ctx, buf);
}

From source file:org.traccar.protocol.AplicomFrameDecoder.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {

    // Skip Alive message
    while (buf.isReadable() && Character.isDigit(buf.getByte(buf.readerIndex()))) {
        buf.readByte();/*from  www.j  av a  2  s  .  c  om*/
    }

    // Check minimum length
    if (buf.readableBytes() < 11) {
        return null;
    }

    // Read flags
    int version = buf.getUnsignedByte(buf.readerIndex() + 1);
    int offset = 1 + 1 + 3;
    if ((version & 0x80) != 0) {
        offset += 4;
    }

    // Get data length
    int length = buf.getUnsignedShort(buf.readerIndex() + offset);
    offset += 2;
    if ((version & 0x40) != 0) {
        offset += 3;
    }
    length += offset; // add header

    // Return buffer
    if (buf.readableBytes() >= length) {
        return buf.readRetainedSlice(length);
    }

    return null;
}