Example usage for io.netty.buffer ByteBuf getLong

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

Introduction

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

Prototype

public abstract long getLong(int index);

Source Link

Document

Gets a 64-bit long integer at the specified absolute index in this buffer.

Usage

From source file:org.dcache.xrootd.protocol.messages.ReadRequest.java

License:Open Source License

public ReadRequest(ByteBuf buffer) {
    super(buffer, kXR_read);

    fhandle = buffer.getInt(4);
    offset = buffer.getLong(8);
    rlen = buffer.getInt(16);
}

From source file:org.dcache.xrootd.protocol.messages.SigverRequest.java

License:Open Source License

public SigverRequest(ByteBuf buffer) {
    super(buffer, kXR_sigver);

    expectrid = buffer.getShort(4);//from  w w w.  ja  v  a2s.com
    version = buffer.getByte(6);
    flags = buffer.getByte(7); // should == kXR_nodata if this is a write
    seqno = buffer.getLong(8);
    crypto = buffer.getByte(16);

    /*
     * skip reserved [bytes 17-19]
     */

    int dlen = buffer.getInt(20);
    signature = new byte[dlen];
    buffer.getBytes(24, signature);
}

From source file:org.dcache.xrootd.protocol.messages.WriteRequest.java

License:Open Source License

public WriteRequest(ByteBuf buffer) {
    super(buffer, kXR_write);

    fhandle = buffer.getInt(4);/*from w ww  .ja v  a 2  s . c  om*/
    offset = buffer.getLong(8);
    dlen = buffer.getInt(20);
    data = buffer.alloc().ioBuffer(dlen); // Most likely this will be written to disk
    buffer.getBytes(24, data);
}

From source file:org.elasticsearch.transport.netty4.ESLoggingHandler.java

License:Apache License

private String format(final ChannelHandlerContext ctx, final String eventName, final ByteBuf arg)
        throws IOException {
    final int readableBytes = arg.readableBytes();
    if (readableBytes == 0) {
        return super.format(ctx, eventName, arg);
    } else if (readableBytes >= 2) {
        final StringBuilder sb = new StringBuilder();
        sb.append(ctx.channel().toString());
        final int offset = arg.readerIndex();
        // this might be an ES message, check the header
        if (arg.getByte(offset) == (byte) 'E' && arg.getByte(offset + 1) == (byte) 'S') {
            if (readableBytes == TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE) {
                final int length = arg.getInt(offset + MESSAGE_LENGTH_OFFSET);
                if (length == TcpTransport.PING_DATA_SIZE) {
                    sb.append(" [ping]").append(' ').append(eventName).append(": ").append(readableBytes)
                            .append('B');
                    return sb.toString();
                }//from w w w .  j  a v  a  2 s . co  m
            } else if (readableBytes >= TcpHeader.HEADER_SIZE) {
                // we are going to try to decode this as an ES message
                final int length = arg.getInt(offset + MESSAGE_LENGTH_OFFSET);
                final long requestId = arg.getLong(offset + REQUEST_ID_OFFSET);
                final byte status = arg.getByte(offset + STATUS_OFFSET);
                final boolean isRequest = TransportStatus.isRequest(status);
                final String type = isRequest ? "request" : "response";
                final String version = Version.fromId(arg.getInt(offset + VERSION_ID_OFFSET)).toString();
                sb.append(" [length: ").append(length);
                sb.append(", request id: ").append(requestId);
                sb.append(", type: ").append(type);
                sb.append(", version: ").append(version);
                if (isRequest) {
                    // it looks like an ES request, try to decode the action
                    final int remaining = readableBytes - ACTION_OFFSET;
                    final ByteBuf slice = arg.slice(offset + ACTION_OFFSET, remaining);
                    // the stream might be compressed
                    try (StreamInput in = in(status, slice, remaining)) {
                        // the first bytes in the message is the context headers
                        try (ThreadContext context = new ThreadContext(Settings.EMPTY)) {
                            context.readHeaders(in);
                        }
                        // now we can decode the action name
                        sb.append(", action: ").append(in.readString());
                    }
                }
                sb.append(']');
                sb.append(' ').append(eventName).append(": ").append(readableBytes).append('B');
                return sb.toString();
            }
        }
    }
    // we could not decode this as an ES message, use the default formatting
    return super.format(ctx, eventName, arg);
}

From source file:org.graylog.plugins.netflow.utils.ByteBufUtils.java

License:Apache License

public static long getUnsignedInteger(final ByteBuf buf, final int offset, final int length) {
    switch (length) {
    case 1://from   www  . ja  v a 2 s  .  c o  m
        return buf.getUnsignedByte(offset);
    case 2:
        return buf.getUnsignedShort(offset);
    case 3:
        return buf.getUnsignedMedium(offset);
    case 4:
        return buf.getUnsignedInt(offset);
    case 8:
        return buf.getLong(offset) & 0x00000000ffffffffL;
    default:
        return 0L;
    }
}

From source file:org.onosproject.driver.optical.handshaker.OplinkHandshakerUtil.java

License:Apache License

private OplinkPortAdjacency getNeighbor(OFExpPortAdjacency ad) {
    // Check input parameter
    if (ad == null) {
        return null;
    }/*from w ww  .ja v  a2s.co  m*/
    // Get adjacency properties
    for (OFExpPortAdjacencyId adid : ad.getProperties()) {
        List<OFExpExtAdId> otns = adid.getAdId();
        if (otns != null && otns.size() > 0) {
            OFExpPortAdidOtn otn = (OFExpPortAdidOtn) otns.get(0);
            // ITU-T G.7714 ETH MAC Format (in second 16 bytes of the following)
            // |---------------------------------------------------------------------------|
            // | Other format (16 bytes)                                                   |
            // |---------------------------------------------------------------------------|
            // | Header (2 bytes) | ID (4 BITS) | MAC (6 bytes) | Port (4 bytes) | Unused  |
            // |---------------------------------------------------------------------------|
            ByteBuf buffer = Unpooled.buffer(OPSPEC_BYTES);
            otn.getOpspec().write32Bytes(buffer);
            long mac = buffer.getLong(OPSPEC_MAC_POS) << OPSPEC_ID_BITS >>> OPSPEC_MAC_BIT_OFF;
            int port = (int) (buffer.getLong(OPSPEC_PORT_POS) << OPSPEC_ID_BITS >>> OPSPEC_PORT_BIT_OFF);
            // Oplink does not use the 4 most significant bytes of Dpid so Dpid can be
            // constructed from MAC address
            return new OplinkPortAdjacency(DeviceId.deviceId(Dpid.uri(new Dpid(mac))),
                    PortNumber.portNumber(port));
        }
    }
    // Returns null if no properties found
    return null;
}

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

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;

    if (buf.getUnsignedByte(0) == 0xD0) {

        long deviceId = ((Long.reverseBytes(buf.getLong(0))) >> 32) & 0xFFFFFFFFL;
        getDeviceSession(channel, remoteAddress, String.valueOf(deviceId));

        return null;
    } else if (buf.toString(StandardCharsets.US_ASCII).startsWith("$OK:")
            || buf.toString(StandardCharsets.US_ASCII).startsWith("$ERR:")
            || buf.toString(StandardCharsets.US_ASCII).startsWith("$MSG:")) {

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, new Date());
        position.set(Position.KEY_RESULT, buf.toString(StandardCharsets.US_ASCII));

        return position;

    } else {//from   w  w w . j a  va2  s  . c  om

        Parser parser = new Parser(PATTERN, buf.toString(StandardCharsets.US_ASCII));
        if (!parser.matches()) {
            return null;
        }

        Position position = new Position(getProtocolName());

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());

        position.setTime(parser.nextDateTime());

        position.setLongitude(parser.nextDouble(0));
        position.setLatitude(parser.nextDouble(0));
        position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
        position.setCourse(parser.nextDouble(0));
        position.setAltitude(parser.nextDouble(0));

        int satellites = parser.nextInt(0);
        position.setValid(satellites != 0);
        position.set(Position.KEY_SATELLITES, satellites);

        position.set(Position.KEY_EVENT, parser.next());
        position.set(Position.KEY_BATTERY, parser.nextDouble());
        if (parser.hasNext()) {
            position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000);
        }
        position.set(Position.KEY_INPUT, parser.next());
        position.set(Position.PREFIX_ADC + 1, parser.next());
        position.set(Position.PREFIX_ADC + 2, parser.next());
        position.set(Position.KEY_OUTPUT, parser.next());

        return position;

    }

}