Example usage for io.netty.buffer ByteBuf readUnsignedShort

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

Introduction

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

Prototype

public abstract int readUnsignedShort();

Source Link

Document

Gets an unsigned 16-bit short integer at the current readerIndex and increases the readerIndex by 2 in this buffer.

Usage

From source file:org.apache.cassandra.transport.CBUtil.java

License:Apache License

public static Pair<List<String>, List<ByteBuffer>> readNameAndValueList(ByteBuf cb, int protocolVersion) {
    int size = cb.readUnsignedShort();
    if (size == 0)
        return Pair.create(Collections.<String>emptyList(), Collections.<ByteBuffer>emptyList());

    List<String> s = new ArrayList<>(size);
    List<ByteBuffer> l = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        s.add(readString(cb));/*from w  ww . j  av a  2s  .  co m*/
        l.add(readBoundValue(cb, protocolVersion));
    }
    return Pair.create(s, l);
}

From source file:org.apache.cassandra.transport.DataType.java

License:Apache License

public Object readValue(ByteBuf cb, int version) {
    switch (this) {
    case CUSTOM:/* w w  w  .  j  a  v  a  2s .com*/
        return CBUtil.readString(cb);
    case LIST:
        return DataType.toType(codec.decodeOne(cb, version));
    case SET:
        return DataType.toType(codec.decodeOne(cb, version));
    case MAP:
        List<AbstractType> l = new ArrayList<AbstractType>(2);
        l.add(DataType.toType(codec.decodeOne(cb, version)));
        l.add(DataType.toType(codec.decodeOne(cb, version)));
        return l;
    case UDT:
        String ks = CBUtil.readString(cb);
        ByteBuffer name = UTF8Type.instance.decompose(CBUtil.readString(cb));
        int n = cb.readUnsignedShort();
        List<ByteBuffer> fieldNames = new ArrayList<>(n);
        List<AbstractType<?>> fieldTypes = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            fieldNames.add(UTF8Type.instance.decompose(CBUtil.readString(cb)));
            fieldTypes.add(DataType.toType(codec.decodeOne(cb, version)));
        }
        return new UserType(ks, name, fieldNames, fieldTypes);
    case TUPLE:
        n = cb.readUnsignedShort();
        List<AbstractType<?>> types = new ArrayList<>(n);
        for (int i = 0; i < n; i++)
            types.add(DataType.toType(codec.decodeOne(cb, version)));
        return new TupleType(types);
    default:
        return null;
    }
}

From source file:org.apache.cassandra.transport.OptionCodec.java

License:Apache License

public Map<T, Object> decode(ByteBuf body, int version) {
    EnumMap<T, Object> options = new EnumMap<T, Object>(klass);
    int n = body.readUnsignedShort();
    for (int i = 0; i < n; i++) {
        T opt = fromId(body.readUnsignedShort());
        Object value = opt.readValue(body, version);
        if (options.containsKey(opt))
            throw new ProtocolException(String.format("Duplicate option %s in message", opt.name()));
        options.put(opt, value);//from   ww w . j  a  v  a 2  s  . c  o m
    }
    return options;
}

From source file:org.apache.cassandra.transport.OptionCodec.java

License:Apache License

public Pair<T, Object> decodeOne(ByteBuf body, int version) {
    T opt = fromId(body.readUnsignedShort());
    Object value = opt.readValue(body, version);
    return Pair.create(opt, value);
}

From source file:org.bgp4j.netty.protocol.BGPv4PacketDecoder.java

License:Apache License

/**
 * decode the NOTIFICATION network packet for error code "Message Header Error". 
 * /*  www  . j av  a2  s  .c o  m*/
 * @param buffer the buffer containing the data. 
 * @return
 */
private NotificationPacket decodeMessageHeaderNotificationPacket(ByteBuf buffer, int errorSubcode) {
    NotificationPacket packet = null;

    switch (errorSubcode) {
    case MessageHeaderErrorNotificationPacket.SUBCODE_CONNECTION_NOT_SYNCHRONIZED:
        packet = new ConnectionNotSynchronizedNotificationPacket();
        break;
    case MessageHeaderErrorNotificationPacket.SUBCODE_BAD_MESSAGE_LENGTH:
        packet = new BadMessageLengthNotificationPacket(buffer.readUnsignedShort());
        break;
    case MessageHeaderErrorNotificationPacket.SUBCODE_BAD_MESSAGE_TYPE:
        packet = new BadMessageTypeNotificationPacket(buffer.readUnsignedByte());
        break;
    }

    return packet;
}

From source file:org.bgp4j.netty.protocol.BGPv4PacketDecoder.java

License:Apache License

/**
 * decode the NOTIFICATION network packet for error code "Cease". 
 * //from w w w .j  av  a 2s  . c o  m
 * @param buffer the buffer containing the data. 
 * @return
 */
private NotificationPacket decodeCeaseNotificationPacket(ByteBuf buffer, int errorSubcode) {
    NotificationPacket packet = null;

    switch (errorSubcode) {
    default:
        log.info("cannot handle cease notification subcode {}", errorSubcode);
    case CeaseNotificationPacket.SUBCODE_UNSPECIFIC:
        packet = new UnspecifiedCeaseNotificationPacket();
        break;
    case CeaseNotificationPacket.SUBCODE_MAXIMUM_NUMBER_OF_PREFIXES_REACHED:
        packet = new MaximumNumberOfPrefixesReachedNotificationPacket();

        try {
            AddressFamily afi = AddressFamily.fromCode(buffer.readUnsignedShort());
            SubsequentAddressFamily safi = SubsequentAddressFamily.fromCode(buffer.readUnsignedByte());
            int prefixUpperBounds = (int) buffer.readUnsignedInt();

            packet = new MaximumNumberOfPrefixesReachedNotificationPacket(afi, safi, prefixUpperBounds);
        } catch (RuntimeException e) {
            log.info("cannot decode specific reason for CEASE maximum number of prefixes reached notification",
                    e);
        }
        break;
    case CeaseNotificationPacket.SUBCODE_ADMINSTRATIVE_SHUTDOWN:
        packet = new AdministrativeShutdownNotificationPacket();
        break;
    case CeaseNotificationPacket.SUBCODE_PEER_DECONFIGURED:
        packet = new PeerDeconfiguredNotificationPacket();
        break;
    case CeaseNotificationPacket.SUBCODE_ADMINSTRATIVE_RESET:
        packet = new AdministrativeResetNotificationPacket();
        break;
    case CeaseNotificationPacket.SUBCODE_CONNECTION_REJECTED:
        packet = new ConnectionRejectedNotificationPacket();
        break;
    case CeaseNotificationPacket.SUBCODE_OTHER_CONFIGURATION_CHANGE:
        packet = new OtherConfigurationChangeNotificationPacket();
        break;
    case CeaseNotificationPacket.SUBCODE_CONNECTION_COLLISION_RESOLUTION:
        packet = new ConnectionCollisionResolutionNotificationPacket();
        break;
    case CeaseNotificationPacket.SUBCODE_OUT_OF_RESOURCES:
        packet = new OutOfResourcesNotificationPacket();
        break;
    }

    return packet;
}

From source file:org.bgp4j.netty.protocol.open.CapabilityCodec.java

License:Apache License

private static Capability decodeOutboundRouteFilteringCapability(ByteBuf buffer) {
    OutboundRouteFilteringCapability cap = new OutboundRouteFilteringCapability();

    assertMinimalLength(buffer, 5); // 2 octest AFI + 1 octet reserved + 1 octet SAFI + 1 octet number of (ORF type, Send/Receive) tuples

    cap.setAddressFamily(AddressFamily.fromCode(buffer.readUnsignedShort()));
    buffer.readByte();// ww w  .  ja va 2 s .  co m
    cap.setSubsequentAddressFamily(SubsequentAddressFamily.fromCode(buffer.readUnsignedByte()));

    int orfs = buffer.readUnsignedByte();

    if (buffer.readableBytes() != 2 * orfs)
        throw new UnspecificOpenPacketException(
                "Expected " + (2 * orfs) + " octets parameter, got " + buffer.readableBytes() + " octets");

    try {
        cap.getFilters().put(ORFType.fromCode(buffer.readUnsignedByte()),
                ORFSendReceive.fromCode(buffer.readUnsignedByte()));
    } catch (IllegalArgumentException e) {
        throw new UnspecificOpenPacketException(e);
    }
    return cap;
}

From source file:org.bgp4j.netty.protocol.open.OpenPacketDecoder.java

License:Apache License

/**
 * decode the OPEN network packet. The passed channel buffer MUST point to the first packet octet AFTER the type octet.
 * //from  w  w w  .j a va  2s.  c  o  m
 * @param buffer the buffer containing the data. 
 * @return
 */
public NotificationPacket decodeOpenNotificationPacket(ByteBuf buffer, int errorSubcode) {
    NotificationPacket packet = null;

    switch (errorSubcode) {
    case OpenNotificationPacket.SUBCODE_BAD_BGP_IDENTIFIER:
        packet = new BadBgpIdentifierNotificationPacket();
        break;
    case OpenNotificationPacket.SUBCODE_BAD_PEER_AS:
        packet = new BadPeerASNotificationPacket();
        break;
    case OpenNotificationPacket.SUBCODE_UNACCEPTABLE_HOLD_TIMER:
        packet = new UnacceptableHoldTimerNotificationPacket();
        break;
    case OpenNotificationPacket.SUBCODE_UNSPECIFIC:
        packet = new UnspecificOpenNotificationPacket();
        break;
    case OpenNotificationPacket.SUBCODE_UNSUPPORTED_OPTIONAL_PARAMETER:
        packet = new UnsupportedOptionalParameterNotificationPacket();
        break;
    case OpenNotificationPacket.SUBCODE_UNSUPPORTED_VERSION_NUMBER:
        packet = new UnsupportedVersionNumberNotificationPacket(buffer.readUnsignedShort());
        break;
    case OpenNotificationPacket.SUBCODE_UNSUPPORTED_CAPABILITY:
        packet = new CapabilityListUnsupportedCapabilityNotificationPacket(
                CapabilityCodec.decodeCapabilities(buffer));
        break;
    }

    return packet;
}

From source file:org.bgp4j.netty.protocol.open.OpenPacketDecoder.java

License:Apache License

/**
 * decode the OPEN network packet. The passed channel buffer MUST point to the first packet octet AFTER the packet type and the buffer 
 * must be at least 9 octets large at this point.
 * /*from ww w . ja  va 2  s.c om*/
 * @param buffer the buffer containing the data. 
 * @return
 */
public OpenPacket decodeOpenPacket(ByteBuf buffer) {
    OpenPacket packet = new OpenPacket();

    ProtocolPacketUtils.verifyPacketSize(buffer, BGPv4Constants.BGP_PACKET_MIN_SIZE_OPEN, -1);

    packet.setProtocolVersion(buffer.readUnsignedByte());
    if (packet.getProtocolVersion() != BGPv4Constants.BGP_VERSION)
        throw new UnsupportedVersionNumberException(BGPv4Constants.BGP_VERSION);
    packet.setAutonomousSystem(buffer.readUnsignedShort());
    packet.setHoldTime(buffer.readUnsignedShort());
    packet.setBgpIdentifier(buffer.readUnsignedInt());
    if ((packet.getBgpIdentifier() & IPV4_MULTICAST_MASK) == IPV4_MULTICAST_MASK)
        throw new BadBgpIdentifierException();

    int parameterLength = buffer.readUnsignedByte();

    if (parameterLength > 0) {
        while (buffer.isReadable()) {
            int parameterType = buffer.readUnsignedByte();
            int paramLength = buffer.readUnsignedByte();

            ByteBuf valueBuffer = buffer.readBytes(paramLength);

            switch (parameterType) {
            case BGPv4Constants.BGP_OPEN_PARAMETER_TYPE_AUTH:
                break;
            case BGPv4Constants.BGP_OPEN_PARAMETER_TYPE_CAPABILITY:
                packet.getCapabilities().addAll(CapabilityCodec.decodeCapabilities(valueBuffer));
                break;
            default:
                throw new UnsupportedOptionalParameterException();
            }
        }
    }

    return packet;
}

From source file:org.bgp4j.netty.protocol.refresh.RouteRefreshPacketDecoder.java

License:Apache License

/**
 * decode the UPDATE network packet. The passed channel buffer MUST point to the first packet octet AFTER the type octet.
 * //w ww  .j a  v a2s .  co  m
 * @param buffer the buffer containing the data. 
 * @return the decoded packet or null on decoding problems. Neither RFC2918 nor RFC5291 nor RFC4271 describe an error
 * handling procedure, so best advise is to ignore invalid packets for now.
 */
public BGPv4Packet decodeRouteRefreshPacket(ByteBuf buffer) {
    RouteRefreshPacket packet = null;

    try {
        AddressFamily af = AddressFamily.fromCode(buffer.readUnsignedShort());

        buffer.readByte(); // swallow reserved octet

        SubsequentAddressFamily saf = SubsequentAddressFamily.fromCode(buffer.readUnsignedByte());

        packet = new RouteRefreshPacket(af, saf);

        if (buffer.isReadable()) {
            // we have outbound router filter rules here
            OutboundRouteFilter orf = new OutboundRouteFilter(af, saf);

            orf.setRefreshType(ORFRefreshType.fromCode(buffer.readUnsignedByte()));

            while (buffer.isReadable()) {
                ORFType orfType = ORFType.fromCode(buffer.readUnsignedByte());
                ByteBuf entriesBuffer = buffer.readSlice(buffer.readUnsignedShort());

                buffer.readBytes(entriesBuffer);
                orf.addAllORFEntries(decodeORFEntries(entriesBuffer, orfType));
            }

            packet.setOutboundRouteFilter(orf);
        }
    } catch (Exception e) {
        log.error("cannot decode ROUTE_REFRESH packet, suppressing it from further processing", e);

        packet = null;
    }

    return packet;
}