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:netty5.http.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.
 *//*from ww  w. j a v a 2s.c  om*/
private static void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();

    p.addLast(sourceCodec);
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void messageReceived(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
            ctx.pipeline().replace(this, "http-hello-world",
                    new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
            ctx.fireChannelRead(msg);
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:nettyprivateprotocolclientdemo.HeartBeatReqHandler.java

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    NettyMessage message = (NettyMessage) msg;

    if (message.getHeader() != null && message.getHeader().getType() == MessageType.LOGIN_RESP.value()) {
        heartBeat = ctx.executor().scheduleAtFixedRate(new HeartBeatReqHandler.HeartBeatTask(ctx), 0, 5000,
                TimeUnit.MILLISECONDS);
    } else if (message.getHeader() != null
            && message.getHeader().getType() == MessageType.HEARTBEAT_RESP.value()) {
        System.out.println("Client receive server heart beat message : ---> " + message);
    } else {/*from  w w  w.ja  v a2 s  .  co m*/
        ctx.fireChannelRead(msg);
    }
}

From source file:nettyprivateprotocolclientdemo.LoginAuthReqHandler.java

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    NettyMessage message = (NettyMessage) msg;

    if (message.getHeader() != null && message.getHeader().getType() == MessageType.LOGIN_RESP.value()) {
        byte loginResult = (byte) message.getBody();

        if (loginResult != (byte) 0) {
            ctx.close();//w  ww .ja v a 2 s  .  c o m
        } else {
            System.out.println("Login is OK : " + message);
            ctx.fireChannelRead(msg);
        }
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:nettyprivateprotocolserverdemo.LoginAuthRespHandler.java

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    NettyMessage message = (NettyMessage) msg;

    if (message.getHeader() != null && message.getHeader().getType() == MessageType.LOGIN_REQ.value()) {
        String nodeIndex = ctx.channel().remoteAddress().toString();

        NettyMessage loginResp = null;//from   w w  w .j  a v  a2 s.  com
        if (nodeCheck.containsKey(nodeIndex)) {
            loginResp = buildResponse((byte) -1);
        } else {
            InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
            String ip = address.getAddress().getHostAddress();
            boolean isOK = false;

            for (String whiteIP : whiteList) {
                if (whiteIP.equals(ip)) {
                    isOK = true;
                    break;
                }
            }

            if (isOK) {
                loginResp = buildResponse((byte) 0);
                nodeCheck.put(nodeIndex, true);
            } else {
                loginResp = buildResponse((byte) -1);
            }
        }

        System.out.println("The login response is : " + loginResp + " body {" + loginResp.getBody() + "}");
        ctx.writeAndFlush(loginResp);
    } else {
        ctx.fireChannelRead(msg);
    }

}

From source file:network.thunder.core.communication.layer.low.ping.PingHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    lastMessageReceived = System.currentTimeMillis();

    if (msg instanceof Ping) {
        lastPing = System.currentTimeMillis();
        sendPong();//from w  w  w .j a v  a 2 s.  com
    } else if (msg instanceof Pong) {
        lastPong = System.currentTimeMillis();
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:network.thunder.core.communication.nio.handler.low.DumpHexHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("Incoming: " + msg);
    ctx.fireChannelRead(msg);

}

From source file:network.thunder.core.communication.nio.handler.low.NodeConnectionHandler.java

License:Apache License

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

}

From source file:octoteam.tahiti.server.pipeline.RequestRateLimitHandler.java

@Override
protected void messageReceived(ChannelHandlerContext ctx, Message msg) throws Exception {
    if (msg.getDirection() != Message.DirectionCode.REQUEST || msg.getService() != serviceCode) {
        ctx.fireChannelRead(msg);
        return;/*w w w.ja v a 2 s . c om*/
    }
    License quotaLimiter = (License) PipelineHelper.getSession(ctx).get(sessionKey);
    if (quotaLimiter == null) {
        quotaLimiter = this.quotaLimiterFactory.call();
        PipelineHelper.getSession(ctx).put(sessionKey, quotaLimiter);
    }
    if (quotaLimiter.use() == License.Availability.AVAILABLE) {
        ctx.fireChannelRead(msg);
    } else {
        RateLimitExceededEvent evt = new RateLimitExceededEvent(name);
        ctx.fireUserEventTriggered(evt);
        ctx.writeAndFlush(buildExceededMsg(msg));
    }
}

From source file:org.aesh.terminal.http.netty.HttpRequestHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
    if (wsUri.equalsIgnoreCase(request.getUri())) {
        ctx.fireChannelRead(request.retain());
    } else {//from w ww  . j av  a 2  s.c om
        if (HttpHeaders.is100ContinueExpected(request)) {
            send100Continue(ctx);
        }

        HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(),
                HttpResponseStatus.INTERNAL_SERVER_ERROR);

        String path = request.getUri();
        if ("/".equals(path)) {
            path = "/index.html";
        }
        URL res = HttpTtyConnection.class.getResource("/org/aesh/terminal/http" + path);
        try {
            if (res != null) {
                DefaultFullHttpResponse fullResp = new DefaultFullHttpResponse(request.getProtocolVersion(),
                        HttpResponseStatus.OK);
                InputStream in = res.openStream();
                byte[] tmp = new byte[256];
                for (int l = 0; l != -1; l = in.read(tmp)) {
                    fullResp.content().writeBytes(tmp, 0, l);
                }
                int li = path.lastIndexOf('.');
                if (li != -1 && li != path.length() - 1) {
                    String ext = path.substring(li + 1, path.length());
                    String contentType;
                    switch (ext) {
                    case "html":
                        contentType = "text/html";
                        break;
                    case "js":
                        contentType = "application/javascript";
                        break;
                    default:
                        contentType = null;
                        break;
                    }
                    if (contentType != null) {
                        fullResp.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType);
                    }
                }
                response = fullResp;
            } else {
                response.setStatus(HttpResponseStatus.NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ctx.write(response);
            ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }
}

From source file:org.aotorrent.client.InboundHandshakeHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    final ByteBuf buf = (ByteBuf) msg;

    if (handshakeDone.get()) {
        super.channelRead(ctx, msg);
        return;//ww  w .  ja  va 2 s  . c o m
    }

    try {
        HandshakeRequest handshakeRequest = new HandshakeRequest(buf);

        final String hexInfoHash = Hex.encodeHexString(handshakeRequest.getInfoHash());
        final TorrentEngine torrentEngine = client.getEngine(hexInfoHash);

        if (torrentEngine != null) {
            final PeerRequest peerHandshake = new HandshakeRequest(torrentEngine.getInfoHash(),
                    torrentEngine.getPeerId());
            ctx.write(peerHandshake.toTransmit());
            ctx.flush();

            PeerConnection peerConnection = new PeerConnection(torrentEngine, ctx);
            ctx.pipeline().addFirst("trafficCounter", torrentEngine.getCounter());
            ctx.pipeline().addLast("peerConnection", peerConnection);
            ctx.pipeline().remove(this);

            handshakeDone.set(true);
            LOGGER.info("Handshake done");

            ByteBuf out = ((ByteBuf) msg).copy();
            ctx.fireChannelRead(out);
            peerConnection.sendBitField();
        } else {
            LOGGER.info("Peer want to exchange torrent that we haven't");
            ctx.close();
        }
    } finally {
        buf.release();
    }
}