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

The following examples show how to use org.jboss.netty.buffer.ChannelBuffer#readUnsignedShort() . 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: OspfMessageReader.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the OSPF packet Header.
 *
 * @param channelBuffer channel buffer instance.
 * @return Ospf Header instance.
 */
private OspfPacketHeader getOspfHeader(ChannelBuffer channelBuffer) {
    OspfPacketHeader ospfPacketHeader = new OspfPacketHeader();

    // Determine OSPF version & Packet Type
    int version = channelBuffer.readByte(); //byte 1 is ospf version
    int packetType = channelBuffer.readByte(); //byte 2 is ospf packet type

    // byte 3 & 4 combine is packet length.
    int packetLength = channelBuffer.readShort();

    byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
    channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
    Ip4Address routerId = Ip4Address.valueOf(tempByteArray);

    tempByteArray = new byte[OspfUtil.FOUR_BYTES];
    channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
    Ip4Address areaId = Ip4Address.valueOf(tempByteArray);

    int checkSum = channelBuffer.readUnsignedShort();
    int auType = channelBuffer.readUnsignedShort();
    int authentication = (int) channelBuffer.readLong();

    ospfPacketHeader.setOspfVer(version);
    ospfPacketHeader.setOspftype(packetType);
    ospfPacketHeader.setOspfPacLength(packetLength);
    ospfPacketHeader.setRouterId(routerId);
    ospfPacketHeader.setAreaId(areaId);
    ospfPacketHeader.setChecksum(checkSum);
    ospfPacketHeader.setAuthType(auType);
    ospfPacketHeader.setAuthentication(authentication);

    return ospfPacketHeader;
}
 
Example 2
Source File: FpmFrameDecoder.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
        throws Exception {

    if (!channel.isConnected()) {
        return null;
    }

    if (buffer.readableBytes() < FpmHeader.FPM_HEADER_LENGTH) {
        return null;
    }

    buffer.markReaderIndex();

    short version = buffer.readUnsignedByte();
    short type = buffer.readUnsignedByte();
    int length = buffer.readUnsignedShort();

    buffer.resetReaderIndex();

    if (buffer.readableBytes() < length) {
        // Not enough bytes to read a whole message
        return null;
    }

    byte[] fpmMessage = new byte[length];
    buffer.readBytes(fpmMessage);

    return FpmHeader.decode(fpmMessage, 0, fpmMessage.length);
}
 
Example 3
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 4
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 5
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;
}