Example usage for io.netty.channel ChannelHandlerContext name

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

Introduction

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

Prototype

String name();

Source Link

Document

The unique name of the ChannelHandlerContext .The name was used when then ChannelHandler was added to the ChannelPipeline .

Usage

From source file:io.netty.example.http2.helloworld.frame.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *//*from   w  ww  . j  a  va  2  s  .c  om*/
private 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 channelRead0(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)");
            ChannelPipeline pipeline = ctx.pipeline();
            pipeline.addAfter(ctx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:io.netty.example.http2.helloworld.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *///from  www  . j a  v a2s.c o m
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();
    final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec,
            upgradeCodecFactory);
    final CleartextHttp2ServerUpgradeHandler cleartextHttp2ServerUpgradeHandler = new CleartextHttp2ServerUpgradeHandler(
            sourceCodec, upgradeHandler, new HelloWorldHttp2HandlerBuilder().build());

    p.addLast(cleartextHttp2ServerUpgradeHandler);
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void channelRead0(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)");
            ChannelPipeline pipeline = ctx.pipeline();
            pipeline.addAfter(ctx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:io.nodyn.tcp.AfterConnectEventHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    ctx.pipeline().addAfter(ctx.name(), "emit.data", new DataEventHandler(this.process, this.tcp));
    emit("afterConnect", this.tcp);
    super.channelActive(ctx);
}

From source file:io.nodyn.tcp.ConnectionEventHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    TCPWrap clientHandle = new TCPWrap(this.process, ctx.channel().newSucceededFuture());
    ctx.pipeline().addAfter(ctx.name(), "emit.close", new EOFEventHandler(this.process, clientHandle));
    ctx.pipeline().addAfter(ctx.name(), "emit.data", new DataEventHandler(this.process, clientHandle));
    emit("connection", clientHandle);
    super.channelActive(ctx);
}

From source file:io.reactivesocket.netty.tcp.server.ReactiveSocketServerHandler.java

License:Apache License

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    ChannelPipeline cp = ctx.pipeline();
    if (cp.get(LengthFieldBasedFrameDecoder.class) == null) {
        ctx.pipeline().addBefore(ctx.name(), LengthFieldBasedFrameDecoder.class.getName(),
                new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE >> 1, 0, BitUtil.SIZE_OF_INT,
                        -1 * BitUtil.SIZE_OF_INT, 0));
    }/*from   w  w  w. ja  v a2  s.  c  om*/

}

From source file:io.reactivex.netty.protocol.http.websocket.WebSocketClientHandler.java

License:Apache License

private void finishHandshake(ChannelHandlerContext ctx, FullHttpResponse msg, Channel ch) {
    try {//w  ww .ja v a 2s.c o  m
        handshaker.finishHandshake(ch, msg);
    } catch (WebSocketHandshakeException e) {
        eventsSubject.onEvent(WebSocketClientMetricsEvent.HANDSHAKE_FAILURE,
                Clock.onEndMillis(handshakeStartTime));
        handshakeFuture.setFailure(e);
        ctx.close();
        return;
    }
    eventsSubject.onEvent(WebSocketClientMetricsEvent.HANDSHAKE_SUCCESS, Clock.onEndMillis(handshakeStartTime));

    ChannelPipeline p = ctx.pipeline();
    ChannelHandlerContext nettyDecoderCtx = p.context(WebSocketFrameDecoder.class);
    p.addAfter(nettyDecoderCtx.name(), "websocket-read-metrics", new ClientReadMetricsHandler(eventsSubject));
    ChannelHandlerContext nettyEncoderCtx = p.context(WebSocketFrameEncoder.class);
    p.addAfter(nettyEncoderCtx.name(), "websocket-write-metrics", new ClientWriteMetricsHandler(eventsSubject));
    if (messageAggregation) {
        p.addAfter("websocket-read-metrics", "websocket-frame-aggregator",
                new WebSocketFrameAggregator(maxFramePayloadLength));
    }
    p.remove(HttpObjectAggregator.class);
    p.remove(this);

    handshakeFuture.setSuccess();
}

From source file:io.reactivex.netty.protocol.http.websocket.WebSocketServerHandler.java

License:Apache License

private void updatePipeline(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.pipeline();/* w w  w .j  a  v a  2 s.  com*/
    ChannelHandlerContext nettyEncoderCtx = p.context(WebSocketFrameEncoder.class);
    p.addAfter(nettyEncoderCtx.name(), "websocket-write-metrics", new ServerWriteMetricsHandler(eventsSubject));
    ChannelHandlerContext nettyDecoderCtx = p.context(WebSocketFrameDecoder.class);
    p.addAfter(nettyDecoderCtx.name(), "websocket-read-metrics", new ServerReadMetricsHandler(eventsSubject));
    if (messageAggregator) {
        p.addAfter("websocket-read-metrics", "websocket-frame-aggregator",
                new WebSocketFrameAggregator(maxFramePayloadLength));
    }
    p.remove(this);
}

From source file:io.scalecube.socketio.pipeline.ResourceHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // ?//from  www.  j av  a 2  s.co  m
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;
        QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri());
        String requestPath = queryDecoder.path();
        URL resUrl = resources.get(requestPath);
        if (resUrl != null) {
            if (log.isDebugEnabled())
                log.debug("Received HTTP resource request: {} {} from channel: {}", req.getMethod(),
                        requestPath, ctx.channel());

            URLConnection fileUrl = resUrl.openConnection();
            long lastModified = fileUrl.getLastModified();
            // check if file has been modified since last request
            if (isNotModified(req, lastModified)) {
                sendNotModified(ctx);
                return;
            }
            // create resource input-stream and check existence
            final InputStream is = fileUrl.getInputStream();
            if (is == null) {
                sendError(ctx, HttpResponseStatus.NOT_FOUND);
                return;
            }
            // create ok response
            HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
            // set Content-Length header
            HttpHeaders.setContentLength(res, fileUrl.getContentLengthLong());
            // set Content-Type header
            setContentTypeHeader(res, fileUrl);
            // set Date, Expires, Cache-Control and Last-Modified headers
            setDateAndCacheHeaders(res, lastModified);
            // write initial response header
            ctx.write(res);

            // write the content stream
            ctx.pipeline().addBefore(ctx.name(), "chunked-writer-handler", new ChunkedWriteHandler());
            ChannelFuture writeFuture = ctx.writeAndFlush(new ChunkedStream(is, fileUrl.getContentLength()));
            // add operation complete listener so we can close the channel and the input stream
            writeFuture.addListener(ChannelFutureListener.CLOSE);
            ReferenceCountUtil.release(msg);
            return;
        }
    }
    super.channelRead(ctx, msg);
}

From source file:me.netty.http.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *///from  ww w.j  a  v a  2  s  .  com
private 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 channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null, new Http1Handler());
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:netty.codec.marshalling.SubReqServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println(ctx.name());
    if (msg instanceof SubscribeReq) {
        SubscribeReq req = (SubscribeReq) msg;
        if ("dreamyao".equalsIgnoreCase(req.getUserName())) {
            System.out.println("Service accept client subscrib req : [" + req.toString() + "]");
            ctx.writeAndFlush(resp(req.getSubReqID()));
        }/*from www  .  j a  v  a 2s.  com*/
    }

}