Example usage for io.netty.util ReferenceCountUtil release

List of usage examples for io.netty.util ReferenceCountUtil release

Introduction

In this page you can find the example usage for io.netty.util ReferenceCountUtil release.

Prototype

public static boolean release(Object msg) 

Source Link

Document

Try to call ReferenceCounted#release() if the specified message implements ReferenceCounted .

Usage

From source file:lunarion.db.local.Test.LunarServerHandlerTest.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {/*from w w  w .j  a v a 2s . com*/
        ByteBuf buf = (ByteBuf) msg;
        byte[] req = new byte[buf.readableBytes()];
        buf.readBytes(req);
        String body = new String(req, "UTF-8");
        System.out.println("LunarNode receive msg: " + body);

        //ByteBuf resp = Unpooled.copiedBuffer(body.getBytes());
        int len = body.getBytes().length;
        ByteBuf response = Unpooled.buffer(4 + len);
        response.writeInt(len);
        response.writeBytes(body.getBytes());
        ctx.write(response);
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:lunarion.node.LunarDBServerHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    try {/*from w w  w . j ava2 s  . c o m*/
        ByteBuf buf = (ByteBuf) msg;

        MessageRequest request = new MessageRequest();
        request.read(buf);

        /*
        System.out.println("LunarNode received command: "+ request.getCMD());
        System.out.println("LunarNode received UUID: "+ request.getUUID());
        for(int i=0;i<request.getParams().length;i++)
        {
           System.out.println("LunarNode received: "+ request.getParams()[i]);
        }
        */
        //MessageResponse response = new MessageResponse();
        //TaskHandlingMessage recvTask = new TaskHandlingMessage(request, response, node_tc, ctx);
        TaskHandlingMessage recvTask = new TaskHandlingMessage(request, node_ec, ctx);

        node_ec.getActiveServer().submit(recvTask);

        /*
         MessageSource response = new MessageSource();
          response.setCMD(request.getCMD());
          response.setUUID(request.getUUID());
          response.setParams(request.getParams());
         int len = response.size();
          ByteBuf response_buff = Unpooled.buffer(4+len);
          response_buff.writeInt(len);
          response.write(response_buff);
                
         ctx.writeAndFlush(response_buff);
         */
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:lunarion.node.requester.ClientHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    try {//w  ww .j  a v a 2s .c  o m
        ByteBuf buf = (ByteBuf) msg;
        MessageResponse from_svr = null;
        if (internal) {
            from_svr = new MessageResponse();
            from_svr.read(buf);
        } else {
            from_svr = new MessageResponseForDriver();
            from_svr.read(buf);
        }
        /* 
         System.out.println("LunarNode responded command: "+ from_svr.getCMD());
        System.out.println("LunarNode responded UUID: "+ from_svr.getUUID());
        System.out.println("LunarNode responded suceed: "+ from_svr.isSucceed());
        for(int i=0;i< from_svr.getParams().length;i++)
        {
        System.out.println("LunarNode responded: "+ from_svr.getParams()[i]);
        } 
        */

        String message_id = from_svr.getUUID();
        MessageClientWatcher callBack = watcher_map.get(message_id);
        if (callBack != null) {
            watcher_map.remove(message_id);
            callBack.finish(from_svr);
        }

        /*
         * if do not call this, this connection will keep alive, and not close.
         * we need it keeping connected.
         */
        //ctx.close();
    } finally {
        ReferenceCountUtil.release(msg);
    }

}

From source file:main.java.ch.epfl.lpd.ServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    try {//from   w  w  w. j a va2 s .  c om
        App.client.run((String) msg, ctx);
    } catch (Exception e) {
        //
    } finally {
        ReferenceCountUtil.release(msg);
    }

}

From source file:net.epsilony.utils.codec.modbus.handler.ModbusSlaveResponseEncoderTest.java

License:Open Source License

@Test
public void test() {
    ModbusSlaveResponseEncoder encoder = new ModbusSlaveResponseEncoder();
    EmbeddedChannel channel = new EmbeddedChannel(encoder);

    ModbusResponse response = new ModbusResponse() {
        {/*from w  w  w. jav a  2 s. co m*/
            transectionId = 0xabcd;
            unitId = 0x83;
        }

        @Override
        public void writePduCore(ByteBuf out) {
            out.writeShort(0xabcd);
            out.writeShort(0xcdef);
            transectionId++;

        }

        @Override
        public int getPduCoreLength() {
            return 4;
        }

        @Override
        public int getFunctionCode() {
            // TODO Auto-generated method stub
            return 6;
        }
    };

    ByteBuf buf = null;
    for (int i = 0; i < 3; i++) {
        channel.writeOutbound(response);
        buf = (ByteBuf) channel.readOutbound();
        int[] buffer = new int[] { 0xab, 0xcd + i, 0x00, 0x00, 0x00, 0x06, 0x83, 0x06, 0xab, 0xcd, 0xcd, 0xef };
        assertEquals(buffer.length, buf.readableBytes());
        for (int b : buffer) {
            assertEquals(b, buf.readUnsignedByte());
        }
        assertTrue(!buf.isReadable());
        ReferenceCountUtil.release(buf);
    }

    for (int i = 0; i < 3; i++) {
        channel.writeOutbound(response);
    }

    for (int i = 0; i < 3; i++) {
        buf = (ByteBuf) channel.readOutbound();
        int[] buffer = new int[] { 0xab, 0xcd + i + 3, 0x00, 0x00, 0x00, 0x06, 0x83, 0x06, 0xab, 0xcd, 0xcd,
                0xef };
        assertEquals(buffer.length, buf.readableBytes());
        for (int b : buffer) {
            assertEquals(b, buf.readUnsignedByte());
        }
        assertTrue(!buf.isReadable());
        ReferenceCountUtil.release(buf);
    }

    encoder.setWithCheckSum(true);
    for (int i = 0; i < 3; i++) {
        channel.writeOutbound(response);
    }
    for (int i = 0; i < 3; i++) {
        buf = (ByteBuf) channel.readOutbound();
        int[] buffer = new int[] { 0xab, 0xcd + i + 6, 0x00, 0x00, 0x00, 0x06, 0x83, 0x06, 0xab, 0xcd, 0xcd,
                0xef };
        assertEquals(buffer.length, buf.readableBytes() - 2);
        for (int b : buffer) {
            assertEquals(b, buf.readUnsignedByte());
        }
        int calcCrc = Utils.crc(buf, buf.readerIndex() - buffer.length, buffer.length);
        assertEquals(calcCrc, buf.readUnsignedShort());
        assertTrue(!buf.isReadable());
        ReferenceCountUtil.release(buf);
    }
}

From source file:network.example.discard.DiscardServerHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    // discard//from   www. j ava  2 s .  com
    ByteBuf in = (ByteBuf) msg;
    try {
        while (in.isReadable()) { // (1)
            System.out.print((char) in.readByte());
            System.out.flush();
        }
    } finally {
        ReferenceCountUtil.release(msg); // (2)
    }
}

From source file:org.acmsl.katas.antlr4netty.InterpreterServerChannelHandler.java

License:Open Source License

/**
 * {@inheritDoc}// w w w  .  j av  a 2 s  . co m
 */
@Override
public void channelRead(@NotNull final ChannelHandlerContext ctx, @NotNull final Object msg) throws Exception {
    @NotNull
    final String input;

    @NotNull
    final ByteBuf buffer = (ByteBuf) msg;

    @NotNull
    final byte[] aux = new byte[buffer.readableBytes()];

    for (int index = 0; index < aux.length; index++) {
        aux[index] = buffer.readByte();
    }

    ReferenceCountUtil.release(msg);

    input = new String(aux, CharsetUtil.UTF_8);

    @NotNull
    final BigDecimal output = calculate(input);

    @NotNull
    final ByteBuf out = Unpooled.copiedBuffer(("" + output + "\n").getBytes(CharsetUtil.UTF_8));

    ctx.writeAndFlush(out);
}

From source file:org.acmsl.queryj.debugging.netty.NettyServerChannelHandler.java

License:Open Source License

/**
 * {@inheritDoc}//from   ww  w. j a v  a2 s  . co  m
 */
@Override
public void channelRead(@NotNull final ChannelHandlerContext ctx, @NotNull final Object msg) {
    try {
        @NotNull
        final ByteBuf buffer = (ByteBuf) msg;

        @NotNull
        final byte[] aux = new byte[buffer.readableBytes()];

        for (int index = 0; index < aux.length; index++) {
            aux[index] = buffer.readByte();
        }

        //            System.out.println("Received " + new String(aux, CharsetUtil.US_ASCII));

        processCommand(new String(aux, CharsetUtil.US_ASCII), this.m__Listener);
    } finally {
        ReferenceCountUtil.release(msg);
    }

    final ChannelFuture future = ctx.writeAndFlush("ACK");
    future.addListener(channelFuture -> {
        System.out.println("Closing context");
        ctx.close();
    });

    /*
    try
    {
    ctx.channel().closeFuture().sync();
    }
    catch (@NotNull final InterruptedException ex)
    {
            
    }*/
}

From source file:org.apache.activemq.artemis.core.protocol.mqtt.MQTTProtocolHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    try {//from w ww.j  a va2 s .  c  om
        if (stopped) {
            disconnect(true);
            return;
        }

        MqttMessage message = (MqttMessage) msg;

        // Disconnect if Netty codec failed to decode the stream.
        if (message.decoderResult().isFailure()) {
            log.debug("Bad Message Disconnecting Client.");
            disconnect(true);
            return;
        }

        connection.dataReceived();

        MQTTUtil.logMessage(session.getState(), message, true);

        this.protocolManager.invokeIncoming(message, this.connection);

        switch (message.fixedHeader().messageType()) {
        case CONNECT:
            handleConnect((MqttConnectMessage) message, ctx);
            break;
        case PUBLISH:
            handlePublish((MqttPublishMessage) message);
            break;
        case PUBACK:
            handlePuback((MqttPubAckMessage) message);
            break;
        case PUBREC:
            handlePubrec(message);
            break;
        case PUBREL:
            handlePubrel(message);
            break;
        case PUBCOMP:
            handlePubcomp(message);
            break;
        case SUBSCRIBE:
            handleSubscribe((MqttSubscribeMessage) message);
            break;
        case UNSUBSCRIBE:
            handleUnsubscribe((MqttUnsubscribeMessage) message);
            break;
        case PINGREQ:
            handlePingreq();
            break;
        case DISCONNECT:
            disconnect(false);
            break;
        case UNSUBACK:
        case SUBACK:
        case PINGRESP:
        case CONNACK: // The server does not instantiate connections therefore any CONNACK received over a connection is an invalid control message.
        default:
            disconnect(true);
        }
    } catch (Exception e) {
        log.debug("Error processing Control Packet, Disconnecting Client", e);
        disconnect(true);
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:org.apache.activemq.artemis.core.remoting.impl.netty.HttpAcceptorHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
    FullHttpRequest request = (FullHttpRequest) msg;
    HttpMethod method = request.getMethod();
    // if we are a post then we send upstream, otherwise we are just being prompted for a response.
    if (method.equals(HttpMethod.POST)) {
        ctx.fireChannelRead(ReferenceCountUtil.retain(((FullHttpRequest) msg).content()));
        // add a new response
        responses.put(new ResponseHolder(System.currentTimeMillis() + responseTime,
                new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)));
        ReferenceCountUtil.release(msg);
        return;//ww w.  j  a v a 2  s  .c  o m
    }
    super.channelRead(ctx, msg);
}