Example usage for io.netty.buffer ByteBuf retain

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

Introduction

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

Prototype

@Override
    public abstract ByteBuf retain();

Source Link

Usage

From source file:com.hazelcast.simulator.protocol.handler.ForwardToWorkerHandler.java

License:Open Source License

private void forwardSimulatorMessage(ChannelHandlerContext ctx, ByteBuf buffer, int workerAddressIndex) {
    long messageId = SimulatorMessageCodec.getMessageId(buffer);

    Response response = new Response(messageId, getSourceAddress(buffer));
    if (workerAddressIndex == 0) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(format("[%d] %s forwarding message to all workers", messageId, addressLevel));
        }/*from www.j  a  va2 s.  com*/
        List<ResponseFuture> futureList = new ArrayList<ResponseFuture>();
        for (ClientConnector clientConnector : clientConnectorManager.getClientConnectors()) {
            buffer.retain();
            futureList.add(clientConnector.writeAsync(buffer));
        }
        try {
            for (ResponseFuture future : futureList) {
                response.addResponse(future.get());
            }
        } catch (InterruptedException e) {
            throw new SimulatorProtocolException("ResponseFuture.get() got interrupted!", e);
        }
    } else {
        ClientConnector clientConnector = clientConnectorManager.get(workerAddressIndex);
        if (clientConnector == null) {
            LOGGER.error(format("[%d] %s Worker %d not found!", messageId, addressLevel, workerAddressIndex));
            response.addResponse(localAddress, FAILURE_WORKER_NOT_FOUND);
            ctx.writeAndFlush(response);
            return;
        }
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(format("[%d] %s forwarding message to Worker %d", messageId, addressLevel,
                    workerAddressIndex));
        }
        buffer.retain();
        response.addResponse(clientConnector.write(buffer));
    }
    ctx.writeAndFlush(response);
}

From source file:com.hazelcast.simulator.protocol.handler.ForwardToWorkerHandler.java

License:Open Source License

private void forwardResponse(ChannelHandlerContext ctx, ByteBuf buffer, int workerAddressIndex) {
    long messageId = ResponseCodec.getMessageId(buffer);

    ClientConnector clientConnector = clientConnectorManager.get(workerAddressIndex);
    if (clientConnector == null) {
        LOGGER.error(format("[%d] %s Worker %d not found!", messageId, addressLevel, workerAddressIndex));
        ctx.writeAndFlush(new Response(messageId, localAddress, localAddress, FAILURE_WORKER_NOT_FOUND));
        return;//w  w w.  j  a va 2s  .c  om
    }

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(format("[%d] %s forwarding response to Worker %d", messageId, addressLevel,
                workerAddressIndex));
    }
    buffer.retain();
    clientConnector.forwardToChannel(buffer);
}

From source file:com.hazelcast.simulator.protocol.handler.SimulatorProtocolDecoder.java

License:Open Source License

private void decodeSimulatorMessage(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) {
    long messageId = SimulatorMessageCodec.getMessageId(buffer);
    AddressLevel dstAddressLevel = AddressLevel
            .fromInt(SimulatorMessageCodec.getDestinationAddressLevel(buffer));
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(format("[%d] %s %s received a message for addressLevel %s", messageId, addressLevel,
                localAddress, dstAddressLevel));
    }//from   w  w w  .  j  av a 2 s.  c o m

    if (dstAddressLevel == addressLevel) {
        SimulatorMessage message = SimulatorMessageCodec.decodeSimulatorMessage(buffer);
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(format("[%d] %s %s will consume %s", messageId, addressLevel, localAddress, message));
        }
        out.add(message);
    } else {
        int addressIndex = SimulatorMessageCodec.getChildAddressIndex(buffer, addressLevelValue);
        ctx.attr(forwardAddressIndex).set(addressIndex);

        out.add(buffer.duplicate());
        buffer.readerIndex(buffer.readableBytes());
        buffer.retain();
    }
}

From source file:com.hazelcast.simulator.protocol.handler.SimulatorProtocolDecoder.java

License:Open Source License

private void decodeResponse(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) {
    long messageId = ResponseCodec.getMessageId(buffer);
    AddressLevel dstAddressLevel = AddressLevel.fromInt(ResponseCodec.getDestinationAddressLevel(buffer));
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(format("[%d] %s %s received a response for addressLevel %s", messageId, addressLevel,
                localAddress, dstAddressLevel));
    }/*from   w  w w . jav  a  2s .  co m*/

    if (dstAddressLevel == addressLevel || dstAddressLevel.isParentAddressLevel(addressLevel)) {
        Response response = ResponseCodec.decodeResponse(buffer);
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(format("[%d] %s %s received %s", response.getMessageId(), addressLevel, localAddress,
                    response));
        }
        if (workerJvmManager != null) {
            workerJvmManager.updateLastSeenTimestamp(response);
        }

        out.add(response);
    } else {
        int addressIndex = ResponseCodec.getChildAddressIndex(buffer, addressLevelValue);
        ctx.attr(forwardAddressIndex).set(addressIndex);

        out.add(buffer.duplicate());
        buffer.readerIndex(buffer.readableBytes());
        buffer.retain();
    }
}

From source file:com.heliosapm.streams.onramp.GZipDetector.java

License:Apache License

/**
 * {@inheritDoc}/*w  w w  .ja v  a 2s. c  o m*/
 * @see io.netty.handler.codec.ByteToMessageDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)
 */
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf buff, final List<Object> out)
        throws Exception {
    if (buff.readableBytes() > 4) {
        final int magic1 = buff.getUnsignedByte(buff.readerIndex());
        final int magic2 = buff.getUnsignedByte(buff.readerIndex() + 1);
        if (isGzip(magic1, magic2)) {
            enableGzip(ctx);
        } else {
            ctx.pipeline().remove(this);
        }
        out.add(buff.retain());
    }
}

From source file:com.heliosapm.streams.onramp.TextLineRpcHandler.java

License:Apache License

/**
 * {@inheritDoc}//  ww w  .j  ava2s  .  c  om
 * @see io.netty.handler.codec.MessageToMessageDecoder#decode(io.netty.channel.ChannelHandlerContext, java.lang.Object, java.util.List)
 */
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf buff, final List<Object> out)
        throws Exception {
    final String v = buff.toString(UTF8);
    log.debug("Received Message: [{}]", v);
    mf.send(v);
    out.add(buff.retain());
}

From source file:com.hop.hhxx.example.http2.helloworld.server.HelloWorldHttp2Handler.java

License:Apache License

@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) {
    int processed = data.readableBytes() + padding;
    if (endOfStream) {
        sendResponse(ctx, streamId, data.retain());
    }/*from www  .j a v a2s  . c o  m*/
    return processed;
}

From source file:com.ibm.crail.datanode.netty.NettyDataNode.java

License:Apache License

@Override
public void run() throws Exception {

    int entries = (int) (NettyConstants.DATANODE_NETTY_STORAGE_LIMIT
            / NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE);
    map = new ConcurrentHashMap<Integer, ByteBuf>(entries);
    datanode = null;/*from w w w  .j av  a 2 s.c om*/
    /* we start with stag 1 and increment it constantly */
    stag = 1;
    LOG.info("Booting with " + entries + " nums of " + NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE
            + " byte buffers");

    /* this manages the netty datanode which processes the client requests */
    datanode = new NettyServer(getAddress(), this);
    datanode.start();

    /* now the Namenode Processor communication part */
    long allocated = 0;
    double perc;
    LOG.info("Allocation started for the target of : " + NettyConstants.DATANODE_NETTY_STORAGE_LIMIT);
    while (allocated < NettyConstants.DATANODE_NETTY_STORAGE_LIMIT) {
        /* allocate a new buffer */
        ByteBuf buf = directBuffer((int) NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE,
                (int) NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE);
        /* retain this buffer */
        buf.retain();
        Long address = ((DirectBuffer) buf.nioBuffer()).address();

        /* update entries */
        map.put(this.stag, buf);
        this.setBlock(address, (int) NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE, this.stag);
        LOG.info("MAP entry : " + Long.toHexString(address) + " length : "
                + (int) NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE + " stag : " + this.stag + " refCount: "
                + buf.refCnt());

        /* update counters */
        allocated += NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE;
        perc = allocated * 100 / NettyConstants.DATANODE_NETTY_STORAGE_LIMIT;
        this.stag++;
        LOG.info("Allocation done : " + perc + "% , allocated " + allocated + " / "
                + NettyConstants.DATANODE_NETTY_STORAGE_LIMIT);
    }

    while (true) {
        DataNodeStatistics statistics = this.getDataNode();
        LOG.info("Datanode statistics, freeBlocks " + statistics.getFreeBlockCount());
        Thread.sleep(2000);
    }

    /* now we wait for the other thread */
    //datanode.join();
}

From source file:com.jfastnet.peers.netty.KryoNettyPeer.java

License:Apache License

public ByteBuf getByteBuf(Message message) {
    ByteBuf data = Unpooled.buffer();
    config.serialiser.serialiseWithStream(message, new ByteBufOutputStream(data));

    int length = data.writerIndex();
    log.trace("Message size of {} is {}", message, length);
    //      if (length > config.maximumUdpPacketSize) {
    //         log.error("Message {} exceeds maximum size of {}! Size is {} byte", new Object[]{message, config.maximumUdpPacketSize, length});
    //         return null;
    //      }/* w  w w  .  ja  va 2  s.  c  o m*/
    return data.retain();
}

From source file:com.kixeye.kixmpp.p2p.node.RemoteNode.java

License:Apache License

@Override
public void sendMessage(MessageWrapper wrapper) {
    if (channel != null) {
        ByteBuf buf = wrapper.getSerialized(cluster.getMessageRegistry());
        channel.writeAndFlush(buf.retain());
    }//from   w w  w  .  ja va 2  s.c  o  m
}