Java Code Examples for org.jboss.netty.buffer.ChannelBuffer#readUnsignedInt()

The following examples show how to use org.jboss.netty.buffer.ChannelBuffer#readUnsignedInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BgpLinkAttrIsIsAdminstGrp.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the BGP link attributes of ISIS administrative group area.
 *
 * @param cb Channel buffer
 * @return object of type BgpLinkAttrIsIsAdminstGrp
 * @throws BgpParseException while parsing BgpLinkAttrIsIsAdminstGrp
 */
public static BgpLinkAttrIsIsAdminstGrp read(ChannelBuffer cb)
        throws BgpParseException {
    long isisAdminGrp;
    short lsAttrLength = cb.readShort();

    if ((lsAttrLength != ISIS_ADMIN_DATA_LEN)
            || (cb.readableBytes() < lsAttrLength)) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
                               BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                               lsAttrLength);
    }

    isisAdminGrp = cb.readUnsignedInt();

    return BgpLinkAttrIsIsAdminstGrp.of(isisAdminGrp);
}
 
Example 2
Source File: BgpUpdate.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Parses BGP UPDATE Attribute Type MULTI_EXIT_DISC.
 *
 * @param bgpSession the BGP Session to use
 * @param ctx the Channel Handler Context
 * @param attrTypeCode the attribute type code
 * @param attrLen the attribute length (in octets)
 * @param attrFlags the attribute flags
 * @param message the message to parse
 * @return the parsed MULTI_EXIT_DISC value
 * @throws BgpMessage.BgpParseException
 */
private static long parseAttributeTypeMultiExitDisc(
                            BgpSession bgpSession,
                            ChannelHandlerContext ctx,
                            int attrTypeCode,
                            int attrLen,
                            int attrFlags,
                            ChannelBuffer message)
    throws BgpMessage.BgpParseException {

    // Check the Attribute Length
    if (attrLen != BgpConstants.Update.MultiExitDisc.LENGTH) {
        // ERROR: Attribute Length Error
        actionsBgpUpdateAttributeLengthError(
            bgpSession, ctx, attrTypeCode, attrLen, attrFlags, message);
        String errorMsg = "Attribute Length Error";
        throw new BgpMessage.BgpParseException(errorMsg);
    }

    long multiExitDisc = message.readUnsignedInt();
    return multiExitDisc;
}
 
Example 3
Source File: BgpUpdate.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Parses BGP UPDATE Attribute Type LOCAL_PREF.
 *
 * @param bgpSession the BGP Session to use
 * @param ctx the Channel Handler Context
 * @param attrTypeCode the attribute type code
 * @param attrLen the attribute length (in octets)
 * @param attrFlags the attribute flags
 * @param message the message to parse
 * @return the parsed LOCAL_PREF value
 * @throws BgpMessage.BgpParseException
 */
private static long parseAttributeTypeLocalPref(
                            BgpSession bgpSession,
                            ChannelHandlerContext ctx,
                            int attrTypeCode,
                            int attrLen,
                            int attrFlags,
                            ChannelBuffer message)
    throws BgpMessage.BgpParseException {

    // Check the Attribute Length
    if (attrLen != BgpConstants.Update.LocalPref.LENGTH) {
        // ERROR: Attribute Length Error
        actionsBgpUpdateAttributeLengthError(
            bgpSession, ctx, attrTypeCode, attrLen, attrFlags, message);
        String errorMsg = "Attribute Length Error";
        throw new BgpMessage.BgpParseException(errorMsg);
    }

    long localPref = message.readUnsignedInt();
    return localPref;
}
 
Example 4
Source File: RPCRecordDecoder.java    From nfs-client-java with Apache License 2.0 5 votes vote down vote up
protected Object decode(ChannelHandlerContext channelHandlerContext, Channel channel, ChannelBuffer channelBuffer) throws Exception {
    // Wait until the length prefix is available.
    if (channelBuffer.readableBytes() < 4) {
        // If null is returned, it means there is not enough data yet.
        // FrameDecoder will call again when there is a sufficient amount of data available.
        return null;
    }

    //marking the current reading position
    channelBuffer.markReaderIndex();

    //get the fragment size and wait until the entire fragment is available.
    long fragSize = channelBuffer.readUnsignedInt();
    boolean lastFragment = RecordMarkingUtil.isLastFragment(fragSize);
    fragSize = RecordMarkingUtil.maskFragmentSize(fragSize);
    if (channelBuffer.readableBytes() < fragSize) {
        channelBuffer.resetReaderIndex();
        return null;
    }

    //seek to the beginning of the next fragment
    channelBuffer.skipBytes((int) fragSize);

    _recordLength += 4 + (int) fragSize;

    //check the last fragment
    if (!lastFragment) {
        //not the last fragment, the data is put in an internally maintained cumulative buffer
        return null;
    }

    byte[] rpcResponse = new byte[_recordLength];
    channelBuffer.readerIndex(channelBuffer.readerIndex() - _recordLength);
    channelBuffer.readBytes(rpcResponse, 0, _recordLength);

    _recordLength = 0;
    return rpcResponse;
}
 
Example 5
Source File: MemcachedBinaryCommandDecoder.java    From fqueue with Apache License 2.0 4 votes vote down vote up
protected Object decode(ChannelHandlerContext channelHandlerContext, Channel channel, ChannelBuffer channelBuffer) throws Exception {

        // need at least 24 bytes, to get header
        if (channelBuffer.readableBytes() < 24) return null;

        // get the header
        channelBuffer.markReaderIndex();
        ChannelBuffer headerBuffer = ChannelBuffers.buffer(ByteOrder.BIG_ENDIAN, 24);
        channelBuffer.readBytes(headerBuffer);

        short magic = headerBuffer.readUnsignedByte();

        // magic should be 0x80
        if (magic != 0x80) {
            headerBuffer.resetReaderIndex();

            throw new MalformedCommandException("binary request payload is invalid, magic byte incorrect");
        }

        short opcode = headerBuffer.readUnsignedByte();
        short keyLength = headerBuffer.readShort();
        short extraLength = headerBuffer.readUnsignedByte();
        short dataType = headerBuffer.readUnsignedByte();   // unused
        short reserved = headerBuffer.readShort(); // unused
        int totalBodyLength = headerBuffer.readInt();
        int opaque = headerBuffer.readInt();
        long cas = headerBuffer.readLong();

        // we want the whole of totalBodyLength; otherwise, keep waiting.
        if (channelBuffer.readableBytes() < totalBodyLength) {
            channelBuffer.resetReaderIndex();
            return null;
        }

        // This assumes correct order in the enum. If that ever changes, we will have to scan for 'code' field.
        BinaryCommand bcmd = BinaryCommand.values()[opcode];

        Command cmdType = bcmd.correspondingCommand;
        CommandMessage cmdMessage = CommandMessage.command(cmdType);
        cmdMessage.noreply = bcmd.noreply;
        cmdMessage.cas_key = cas;
        cmdMessage.opaque = opaque;
        cmdMessage.addKeyToResponse = bcmd.addKeyToResponse;

        // get extras. could be empty.
        ChannelBuffer extrasBuffer = ChannelBuffers.buffer(ByteOrder.BIG_ENDIAN, extraLength);
        channelBuffer.readBytes(extrasBuffer);

        // get the key if any
        if (keyLength != 0) {
            ChannelBuffer keyBuffer = ChannelBuffers.buffer(ByteOrder.BIG_ENDIAN, keyLength);
            channelBuffer.readBytes(keyBuffer);

            ArrayList<String> keys = new ArrayList<String>();
            String key = keyBuffer.toString(USASCII);
            keys.add(key); // TODO this or UTF-8? ISO-8859-1?

            cmdMessage.keys = keys;


            if (cmdType == Command.ADD ||
                    cmdType == Command.SET ||
                    cmdType == Command.REPLACE ||
                    cmdType == Command.APPEND ||
                    cmdType == Command.PREPEND)
            {
                // TODO these are backwards from the spec, but seem to be what spymemcached demands -- which has the mistake?!
                short expire = (short) (extrasBuffer.capacity() != 0 ? extrasBuffer.readUnsignedShort() : 0);
                short flags = (short) (extrasBuffer.capacity() != 0 ? extrasBuffer.readUnsignedShort() : 0);

                // the remainder of the message -- that is, totalLength - (keyLength + extraLength) should be the payload
                int size = totalBodyLength - keyLength - extraLength;

                cmdMessage.element = new LocalCacheElement(key, flags, expire != 0 && expire < CacheElement.THIRTY_DAYS ? LocalCacheElement.Now() + expire : expire, 0L);
                cmdMessage.element.setData(new byte[size]);
                channelBuffer.readBytes(cmdMessage.element.getData(), 0, size);
            } else if (cmdType == Command.INCR || cmdType == Command.DECR) {
                long initialValue = extrasBuffer.readUnsignedInt();
                long amount = extrasBuffer.readUnsignedInt();
                long expiration = extrasBuffer.readUnsignedInt();

                cmdMessage.incrAmount = (int) amount;
                cmdMessage.incrDefault = (int) initialValue;
                cmdMessage.incrExpiry = (int) expiration;
            }
        }

        return cmdMessage;
    }
 
Example 6
Source File: MemcachedBinaryCommandDecoder.java    From fqueue with Apache License 2.0 4 votes vote down vote up
protected Object decode(ChannelHandlerContext channelHandlerContext, Channel channel, ChannelBuffer channelBuffer) throws Exception {

        // need at least 24 bytes, to get header
        if (channelBuffer.readableBytes() < 24) return null;

        // get the header
        channelBuffer.markReaderIndex();
        ChannelBuffer headerBuffer = ChannelBuffers.buffer(ByteOrder.BIG_ENDIAN, 24);
        channelBuffer.readBytes(headerBuffer);

        short magic = headerBuffer.readUnsignedByte();

        // magic should be 0x80
        if (magic != 0x80) {
            headerBuffer.resetReaderIndex();

            throw new MalformedCommandException("binary request payload is invalid, magic byte incorrect");
        }

        short opcode = headerBuffer.readUnsignedByte();
        short keyLength = headerBuffer.readShort();
        short extraLength = headerBuffer.readUnsignedByte();
        short dataType = headerBuffer.readUnsignedByte();   // unused
        short reserved = headerBuffer.readShort(); // unused
        int totalBodyLength = headerBuffer.readInt();
        int opaque = headerBuffer.readInt();
        long cas = headerBuffer.readLong();

        // we want the whole of totalBodyLength; otherwise, keep waiting.
        if (channelBuffer.readableBytes() < totalBodyLength) {
            channelBuffer.resetReaderIndex();
            return null;
        }

        // This assumes correct order in the enum. If that ever changes, we will have to scan for 'code' field.
        BinaryCommand bcmd = BinaryCommand.values()[opcode];

        Command cmdType = bcmd.correspondingCommand;
        CommandMessage cmdMessage = CommandMessage.command(cmdType);
        cmdMessage.noreply = bcmd.noreply;
        cmdMessage.cas_key = cas;
        cmdMessage.opaque = opaque;
        cmdMessage.addKeyToResponse = bcmd.addKeyToResponse;

        // get extras. could be empty.
        ChannelBuffer extrasBuffer = ChannelBuffers.buffer(ByteOrder.BIG_ENDIAN, extraLength);
        channelBuffer.readBytes(extrasBuffer);

        // get the key if any
        if (keyLength != 0) {
            ChannelBuffer keyBuffer = ChannelBuffers.buffer(ByteOrder.BIG_ENDIAN, keyLength);
            channelBuffer.readBytes(keyBuffer);

            ArrayList<String> keys = new ArrayList<String>();
            String key = keyBuffer.toString(USASCII);
            keys.add(key); // TODO this or UTF-8? ISO-8859-1?

            cmdMessage.keys = keys;


            if (cmdType == Command.ADD ||
                    cmdType == Command.SET ||
                    cmdType == Command.REPLACE ||
                    cmdType == Command.APPEND ||
                    cmdType == Command.PREPEND)
            {
                // TODO these are backwards from the spec, but seem to be what spymemcached demands -- which has the mistake?!
                short expire = (short) (extrasBuffer.capacity() != 0 ? extrasBuffer.readUnsignedShort() : 0);
                short flags = (short) (extrasBuffer.capacity() != 0 ? extrasBuffer.readUnsignedShort() : 0);

                // the remainder of the message -- that is, totalLength - (keyLength + extraLength) should be the payload
                int size = totalBodyLength - keyLength - extraLength;

                cmdMessage.element = new LocalCacheElement(key, flags, expire != 0 && expire < CacheElement.THIRTY_DAYS ? LocalCacheElement.Now() + expire : expire, 0L);
                cmdMessage.element.setData(new byte[size]);
                channelBuffer.readBytes(cmdMessage.element.getData(), 0, size);
            } else if (cmdType == Command.INCR || cmdType == Command.DECR) {
                long initialValue = extrasBuffer.readUnsignedInt();
                long amount = extrasBuffer.readUnsignedInt();
                long expiration = extrasBuffer.readUnsignedInt();

                cmdMessage.incrAmount = (int) amount;
                cmdMessage.incrDefault = (int) initialValue;
                cmdMessage.incrExpiry = (int) expiration;
            }
        }

        return cmdMessage;
    }
 
Example 7
Source File: BgpUpdate.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Parses BGP UPDATE Attribute Type AGGREGATOR.
 *
 * @param bgpSession the BGP Session to use
 * @param ctx the Channel Handler Context
 * @param attrTypeCode the attribute type code
 * @param attrLen the attribute length (in octets)
 * @param attrFlags the attribute flags
 * @param message the message to parse
 * @return the parsed AGGREGATOR value: a tuple of <AS-Number, IP-Address>
 * @throws BgpMessage.BgpParseException
 */
private static Pair<Long, Ip4Address> parseAttributeTypeAggregator(
                                            BgpSession bgpSession,
                                            ChannelHandlerContext ctx,
                                            int attrTypeCode,
                                            int attrLen,
                                            int attrFlags,
                                            ChannelBuffer message)
    throws BgpMessage.BgpParseException {
    int expectedAttrLen;

    if (bgpSession.isAs4OctetCapable()) {
        expectedAttrLen = BgpConstants.Update.Aggregator.AS4_LENGTH;
    } else {
        expectedAttrLen = BgpConstants.Update.Aggregator.AS2_LENGTH;
    }

    // Check the Attribute Length
    if (attrLen != expectedAttrLen) {
        // ERROR: Attribute Length Error
        actionsBgpUpdateAttributeLengthError(
            bgpSession, ctx, attrTypeCode, attrLen, attrFlags, message);
        String errorMsg = "Attribute Length Error";
        throw new BgpMessage.BgpParseException(errorMsg);
    }

    // The AGGREGATOR AS number
    long aggregatorAsNumber;
    if (bgpSession.isAs4OctetCapable()) {
        aggregatorAsNumber = message.readUnsignedInt();
    } else {
        aggregatorAsNumber = message.readUnsignedShort();
    }
    // The AGGREGATOR IP address
    Ip4Address aggregatorIpAddress =
        Ip4Address.valueOf((int) message.readUnsignedInt());

    Pair<Long, Ip4Address> aggregator = Pair.of(aggregatorAsNumber,
                                                aggregatorIpAddress);
    return aggregator;
}