Example usage for io.netty.buffer ByteBuf readInt

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

Introduction

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

Prototype

public abstract int readInt();

Source Link

Document

Gets a 32-bit integer at the current readerIndex and increases the readerIndex by 4 in this buffer.

Usage

From source file:at.yawk.accordion.simulation.Simulation.java

License:Mozilla Public License

public static void main(String[] args) throws UnknownHostException, InterruptedException {
    Logger.getRootLogger().addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
    Logger.getRootLogger().setLevel(Level.DEBUG);

    Simulation simulation = new Simulation();
    simulation.populate();/*from w w  w.j a  v  a  2  s  . c o  m*/
    TimeUnit.SECONDS.sleep(1);

    simulation.nodes.get(simulation.tiers[0][0]).getConnectionManager().getChannel("test").subscribe(msg -> {
        int len = msg.readInt();
        byte[] blob = new byte[len];
        msg.readBytes(blob);
        Log.getDefaultLogger().info("Received: " + new String(blob));
    });
    TimeUnit.SECONDS.sleep(1);

    ByteBuf msg = Unpooled.buffer();
    byte[] text = "ping".getBytes();
    msg.writeInt(text.length);
    msg.writeBytes(text);

    simulation.nodes.get(simulation.tiers[0][1]).getConnectionManager().getChannel("test").publish(msg);
}

From source file:bftsmart.communication.client.netty.NettyTOMMessageDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object> list) throws Exception {

    // Wait until the length prefix is available.
    if (buffer.readableBytes() < Integer.BYTES) {
        return;/*from ww w. j a  v a2 s  .c  o m*/
    }

    int dataLength = buffer.getInt(buffer.readerIndex());

    //Logger.println("Receiving message with "+dataLength+" bytes.");

    // Wait until the whole data is available.
    if (buffer.readableBytes() < dataLength + Integer.BYTES) {
        return;
    }

    // Skip the length field because we know it already.
    buffer.skipBytes(Integer.BYTES);

    int size = buffer.readInt();
    byte[] data = new byte[size];
    buffer.readBytes(data);

    byte[] signature = null;
    size = buffer.readInt();

    if (size > 0) {
        signature = new byte[size];
        buffer.readBytes(signature);
    }

    DataInputStream dis = null;
    TOMMessage sm = null;

    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        dis = new DataInputStream(bais);
        sm = new TOMMessage();
        sm.rExternal(dis);
        sm.serializedMessage = data;

        if (signature != null) {
            sm.serializedMessageSignature = signature;
            sm.signed = true;
        }

        if (!isClient) {
            rl.readLock().lock();
            if (!sessionTable.containsKey(sm.getSender())) {
                rl.readLock().unlock();

                NettyClientServerSession cs = new NettyClientServerSession(context.channel(), sm.getSender());

                rl.writeLock().lock();
                sessionTable.put(sm.getSender(), cs);
                logger.debug("Active clients: " + sessionTable.size());
                rl.writeLock().unlock();

            } else {
                rl.readLock().unlock();
            }
        }
        logger.debug("Decoded reply from " + sm.getSender() + " with sequence number " + sm.getSequence());
        list.add(sm);
    } catch (Exception ex) {

        logger.error("Failed to decode TOMMessage", ex);
    }
    return;
}

From source file:blazingcache.network.netty.DodoMessageUtils.java

License:Apache License

private static String readUTF8String(ByteBuf buf) {
    int len = buf.readInt();
    byte[] s = new byte[len];
    buf.readBytes(s);/*from   w w  w .jav  a  2  s .co  m*/
    return new String(s, StandardCharsets.UTF_8);
}

From source file:blazingcache.network.netty.DodoMessageUtils.java

License:Apache License

private static Object readEncodedSimpleValue(ByteBuf encoded) {
    byte _opcode = encoded.readByte();
    switch (_opcode) {
    case OPCODE_NULL_VALUE:
        return null;
    case OPCODE_STRING_VALUE:
        return readUTF8String(encoded);
    case OPCODE_INT_VALUE:
        return encoded.readInt();
    case OPCODE_MAP_VALUE: {
        int len = encoded.readInt();
        Map<Object, Object> ret = new HashMap<>();
        for (int i = 0; i < len; i++) {
            Object mapkey = readEncodedSimpleValue(encoded);
            Object value = readEncodedSimpleValue(encoded);
            ret.put(mapkey, value);//ww  w. j  a  v  a  2s.co  m
        }
        return ret;
    }
    case OPCODE_SET_VALUE: {
        int len = encoded.readInt();
        Set<Object> ret = new HashSet<>();
        for (int i = 0; i < len; i++) {
            Object o = readEncodedSimpleValue(encoded);
            ret.add(o);
        }
        return ret;
    }
    case OPCODE_LIST_VALUE: {
        int len = encoded.readInt();
        List<Object> ret = new ArrayList<>(len);
        for (int i = 0; i < len; i++) {
            Object o = readEncodedSimpleValue(encoded);
            ret.add(o);
        }
        return ret;
    }
    case OPCODE_BYTEARRAY_VALUE: {
        int len = encoded.readInt();
        byte[] ret = new byte[len];
        encoded.readBytes(ret);
        return ret;
    }
    case OPCODE_LONG_VALUE:
        return encoded.readLong();
    default:
        throw new RuntimeException("invalid opcode: " + _opcode);
    }
}

From source file:blazingcache.network.netty.DodoMessageUtils.java

License:Apache License

public static Message decodeMessage(ByteBuf encoded) {
    byte version = encoded.readByte();
    if (version != VERSION) {
        throw new RuntimeException("bad protocol version " + version);
    }//w w w. j a v  a 2 s  .  co  m
    int type = encoded.readInt();
    String messageId = readUTF8String(encoded);
    String replyMessageId = null;
    String workerProcessId = null;
    Map<String, Object> params = new HashMap<>();
    while (encoded.isReadable()) {
        byte opcode = encoded.readByte();
        switch (opcode) {
        case OPCODE_REPLYMESSAGEID:
            replyMessageId = readUTF8String(encoded);
            break;
        case OPCODE_WORKERPROCESSID:
            workerProcessId = readUTF8String(encoded);
            break;
        case OPCODE_PARAMETERS:
            int size = encoded.readInt();
            for (int i = 0; i < size; i++) {
                Object key = readEncodedSimpleValue(encoded);
                Object value = readEncodedSimpleValue(encoded);
                params.put((String) key, value);
            }
            break;
        default:
            throw new RuntimeException("invalid opcode: " + opcode);
        }
    }
    Message m = new Message(workerProcessId, type, params);
    if (replyMessageId != null) {
        m.replyMessageId = replyMessageId;
    }
    m.messageId = messageId;
    return m;

}

From source file:blazingcache.network.netty.MessageUtils.java

License:Apache License

private static Object readEncodedSimpleValue(ByteBuf encoded) {
    byte _opcode = encoded.readByte();
    switch (_opcode) {
    case OPCODE_NULL_VALUE:
        return null;
    case OPCODE_STRING_VALUE:
        return RawString.of(readUTF8String(encoded));
    case OPCODE_INT_VALUE:
        return encoded.readInt();
    case OPCODE_MAP_VALUE: {
        int len = encoded.readInt();
        Map<Object, Object> ret = new HashMap<>();
        for (int i = 0; i < len; i++) {
            Object mapkey = readEncodedSimpleValue(encoded);
            Object value = readEncodedSimpleValue(encoded);
            ret.put(mapkey, value);//from ww  w.j  a v a 2  s.c  o m
        }
        return ret;
    }
    case OPCODE_SET_VALUE: {
        int len = encoded.readInt();
        Set<Object> ret = new HashSet<>();
        for (int i = 0; i < len; i++) {
            Object o = readEncodedSimpleValue(encoded);
            ret.add(o);
        }
        return ret;
    }
    case OPCODE_LIST_VALUE: {
        int len = encoded.readInt();
        List<Object> ret = new ArrayList<>(len);
        for (int i = 0; i < len; i++) {
            Object o = readEncodedSimpleValue(encoded);
            ret.add(o);
        }
        return ret;
    }
    case OPCODE_BYTEARRAY_VALUE: {
        int len = encoded.readInt();
        byte[] ret = new byte[len];
        encoded.readBytes(ret);
        return ret;
    }
    case OPCODE_LONG_VALUE:
        return encoded.readLong();
    default:
        throw new RuntimeException("invalid opcode: " + _opcode);
    }
}

From source file:blazingcache.network.netty.MessageUtils.java

License:Apache License

public static Message decodeMessage(ByteBuf encoded) {
    byte version = encoded.readByte();
    if (version != VERSION) {
        throw new RuntimeException("bad protocol version " + version);
    }//w  ww.j  a v a2  s.c  om
    int type = encoded.readInt();
    String messageId = readUTF8String(encoded);
    String replyMessageId = null;
    String workerProcessId = null;
    Map<String, Object> params = new HashMap<>();
    while (encoded.isReadable()) {
        byte opcode = encoded.readByte();
        switch (opcode) {
        case OPCODE_REPLYMESSAGEID:
            replyMessageId = readUTF8String(encoded);
            break;
        case OPCODE_WORKERPROCESSID:
            workerProcessId = readUTF8String(encoded);
            break;
        case OPCODE_PARAMETERS:
            int size = encoded.readInt();
            for (int i = 0; i < size; i++) {
                Object key = readEncodedSimpleValue(encoded);
                Object value = readEncodedSimpleValue(encoded);
                params.put(((RawString) key).toString(), value);
            }
            break;
        default:
            throw new RuntimeException("invalid opcode: " + opcode);
        }
    }
    Message m = new Message(workerProcessId, type, params);
    if (replyMessageId != null) {
        m.replyMessageId = replyMessageId;
    }
    m.messageId = messageId;
    return m;

}

From source file:blusunrize.immersiveengineering.common.util.network.MessageBirthdayParty.java

@Override
public void fromBytes(ByteBuf buf) {
    this.entityId = buf.readInt();
}

From source file:blusunrize.immersiveengineering.common.util.network.MessageMagnetEquip.java

@Override
public void fromBytes(ByteBuf buf) {
    this.fetchSlot = buf.readInt();
}

From source file:blusunrize.immersiveengineering.common.util.network.MessageMinecartShaderSync.java

@Override
public void fromBytes(ByteBuf buf) {
    this.entityID = buf.readInt();
    this.request = buf.readBoolean();
    if (!request)
        this.shader = ByteBufUtils.readItemStack(buf);
}