Example usage for io.netty.channel ChannelHandlerContext fireChannelRead

List of usage examples for io.netty.channel ChannelHandlerContext fireChannelRead

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext fireChannelRead.

Prototype

@Override
    ChannelHandlerContext fireChannelRead(Object msg);

Source Link

Usage

From source file:org.apache.giraph.comm.netty.InboundByteCounter.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof ByteBuf) {
        ByteBuf buf = (ByteBuf) msg;//from   w  ww . j a  va 2s.c o  m
        int receivedBytes = delegate.byteBookkeeper(buf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("channelRead: " + ctx.channel().toString() + " buffer " + "size = " + receivedBytes
                    + ", total bytes = " + getBytesReceived());
        }
    }
    ctx.fireChannelRead(msg);
}

From source file:org.apache.hadoop.hbase.security.CryptoAESUnwrapHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
    byte[] bytes = new byte[msg.readableBytes()];
    msg.readBytes(bytes);//from  w  w w .j  av  a2 s  .  co  m
    ctx.fireChannelRead(Unpooled.wrappedBuffer(cryptoAES.unwrap(bytes, 0, bytes.length)));
}

From source file:org.apache.hadoop.hbase.security.SaslClientHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;//from  w  w  w. ja  v a2s. c  o  m

    // If not complete, try to negotiate
    if (!saslClient.isComplete()) {
        while (!saslClient.isComplete() && in.isReadable()) {
            readStatus(in);
            int len = in.readInt();
            if (firstRead) {
                firstRead = false;
                if (len == SaslUtil.SWITCH_TO_SIMPLE_AUTH) {
                    if (!fallbackAllowed) {
                        throw new IOException("Server asks us to fall back to SIMPLE auth, " + "but this "
                                + "client is configured to only allow secure connections.");
                    }
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Server asks us to fall back to simple auth.");
                    }
                    saslClient.dispose();

                    ctx.pipeline().remove(this);
                    successfulConnectHandler.onSuccess(ctx.channel());
                    return;
                }
            }
            saslToken = new byte[len];
            if (LOG.isDebugEnabled()) {
                LOG.debug("Will read input token of size " + saslToken.length
                        + " for processing by initSASLContext");
            }
            in.readBytes(saslToken);

            saslToken = evaluateChallenge(saslToken);
            if (saslToken != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Will send token of size " + saslToken.length + " from initSASLContext.");
                }
                writeSaslToken(ctx, saslToken);
            }
        }

        if (saslClient.isComplete()) {
            String qop = (String) saslClient.getNegotiatedProperty(Sasl.QOP);

            if (LOG.isDebugEnabled()) {
                LOG.debug("SASL client context established. Negotiated QoP: " + qop);
            }

            boolean useWrap = qop != null && !"auth".equalsIgnoreCase(qop);

            if (!useWrap) {
                ctx.pipeline().remove(this);
            }
            successfulConnectHandler.onSuccess(ctx.channel());
        }
    }
    // Normal wrapped reading
    else {
        try {
            int length = in.readInt();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Actual length is " + length);
            }
            saslToken = new byte[length];
            in.readBytes(saslToken);
        } catch (IndexOutOfBoundsException e) {
            return;
        }
        try {
            ByteBuf b = ctx.channel().alloc().buffer(saslToken.length);

            b.writeBytes(saslClient.unwrap(saslToken, 0, saslToken.length));
            ctx.fireChannelRead(b);

        } catch (SaslException se) {
            try {
                saslClient.dispose();
            } catch (SaslException ignored) {
                LOG.debug("Ignoring SASL exception", ignored);
            }
            throw se;
        }
    }
}

From source file:org.apache.hadoop.hbase.security.SaslUnwrapHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
    byte[] bytes = new byte[msg.readableBytes()];
    msg.readBytes(bytes);/*from   ww  w .jav a  2 s. c  o m*/
    ctx.fireChannelRead(Unpooled.wrappedBuffer(saslClient.unwrap(bytes, 0, bytes.length)));
}

From source file:org.apache.helix.ipc.netty.NettyHelixIPCBackPressureHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
    ctx.fireChannelRead(msg);
}

From source file:org.apache.hyracks.http.server.CLFLogger.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;
        clientIp = ((NioSocketChannel) ctx.channel()).remoteAddress().getAddress().toString().substring(1);
        requestTime = Instant.now();
        reqLine = req.method().toString() + " " + req.getUri() + " " + req.getProtocolVersion().toString();
        userAgentRef = headerValueOrDash("Referer", req) + " " + headerValueOrDash("User-Agent", req);
        lastChunk = false;//from   ww w. ja v  a 2  s. c  o m
    }
    ctx.fireChannelRead(msg);
}

From source file:org.apache.spark.network.server.TransportChannelHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object request) throws Exception {
    if (request instanceof RequestMessage) {
        requestHandler.handle((RequestMessage) request);
    } else if (request instanceof ResponseMessage) {
        responseHandler.handle((ResponseMessage) request);
    } else {/*from w w  w  .  ja  v a 2 s.  co  m*/
        ctx.fireChannelRead(request);
    }
}

From source file:org.apache.spark.network.util.TransportFrameDecoderSuite.java

License:Apache License

@Test
public void testRetainedFrames() throws Exception {
    TransportFrameDecoder decoder = new TransportFrameDecoder();

    AtomicInteger count = new AtomicInteger();
    List<ByteBuf> retained = new ArrayList<>();

    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.fireChannelRead(any())).thenAnswer(in -> {
        // Retain a few frames but not others.
        ByteBuf buf = (ByteBuf) in.getArguments()[0];
        if (count.incrementAndGet() % 2 == 0) {
            retained.add(buf);// ww  w.  j a v a  2  s  . c  o  m
        } else {
            buf.release();
        }
        return null;
    });

    ByteBuf data = createAndFeedFrames(100, decoder, ctx);
    try {
        // Verify all retained buffers are readable.
        for (ByteBuf b : retained) {
            byte[] tmp = new byte[b.readableBytes()];
            b.readBytes(tmp);
            b.release();
        }
        verifyAndCloseDecoder(decoder, ctx, data);
    } finally {
        for (ByteBuf b : retained) {
            release(b);
        }
    }
}

From source file:org.apache.spark.network.util.TransportFrameDecoderSuite.java

License:Apache License

private ChannelHandlerContext mockChannelHandlerContext() {
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.fireChannelRead(any())).thenAnswer(in -> {
        ByteBuf buf = (ByteBuf) in.getArguments()[0];
        buf.release();//  w w  w . j ava 2 s. c  o  m
        return null;
    });
    return ctx;
}

From source file:org.apache.tajo.ws.rs.netty.NettyRestHandlerContainer.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    boolean needRelease = true;
    try {/*from  w  w w. j av a2  s  .  c o m*/
        if (msg instanceof FullHttpRequest) {
            FullHttpRequest request = (FullHttpRequest) msg;
            messageReceived(ctx, request);
        } else {
            needRelease = false;
            ctx.fireChannelRead(msg);
        }
    } finally {
        if (needRelease) {
            ReferenceCountUtil.release(msg);
        }
    }
}