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:com.linkedin.r2.transport.http.client.Http2InitializerHandler.java

License:Apache License

/**
 * Sets up HTTP/2 over TCP through protocol upgrade (h2c) pipeline
 *//*from   w  w w .ja  v  a 2s.  c o m*/
private void configureHttpPipeline(ChannelHandlerContext ctx, Request request) throws Exception {
    Http2StreamCodec http2Codec = new Http2StreamCodecBuilder().connection(_connection)
            .maxContentLength(_maxResponseSize).maxHeaderSize(_maxHeaderSize)
            .gracefulShutdownTimeoutMillis(_gracefulShutdownTimeout).streamingTimeout(_streamingTimeout)
            .scheduler(_scheduler).build();
    HttpClientCodec sourceCodec = new HttpClientCodec(MAX_INITIAL_LINE_LENGTH, _maxHeaderSize, _maxChunkSize);
    Http2ClientUpgradeCodec targetCodec = new Http2ClientUpgradeCodec(http2Codec);
    HttpClientUpgradeHandler upgradeCodec = new HttpClientUpgradeHandler(sourceCodec, targetCodec,
            MAX_CLIENT_UPGRADE_CONTENT_LENGTH);
    Http2SchemeHandler schemeHandler = new Http2SchemeHandler(HttpScheme.HTTP.toString());

    String host = request.getURI().getAuthority();
    int port = request.getURI().getPort();
    String path = request.getURI().getPath();

    Http2UpgradeHandler upgradeHandler = new Http2UpgradeHandler(host, port, path);
    Http2StreamResponseHandler responseHandler = new Http2StreamResponseHandler();
    Http2ChannelPoolHandler channelPoolHandler = new Http2ChannelPoolHandler();

    ctx.pipeline().addBefore(ctx.name(), "sourceCodec", sourceCodec);
    ctx.pipeline().addBefore(ctx.name(), "upgradeCodec", upgradeCodec);
    ctx.pipeline().addBefore(ctx.name(), "upgradeHandler", upgradeHandler);
    ctx.pipeline().addBefore(ctx.name(), "schemeHandler", schemeHandler);
    ctx.pipeline().addBefore(ctx.name(), "responseHandler", responseHandler);
    ctx.pipeline().addBefore(ctx.name(), "channelHandler", channelPoolHandler);

    _setupComplete = true;
}

From source file:com.linkedin.r2.transport.http.client.Http2InitializerHandler.java

License:Apache License

/**
 * Sets up HTTP/2 over TLS through ALPN (h2) pipeline
 *///from   w  ww. j av  a2 s  .  c  om
private void configureHttpsPipeline(ChannelHandlerContext ctx) throws Exception {
    JdkSslContext context = new JdkSslContext(_sslContext, IS_CLIENT,
            Arrays.asList(_sslParameters.getCipherSuites()), IdentityCipherSuiteFilter.INSTANCE,
            new ApplicationProtocolConfig(ApplicationProtocolConfig.Protocol.ALPN,
                    ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE,
                    ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT,
                    ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1),
            _sslParameters.getNeedClientAuth() ? ClientAuth.REQUIRE : ClientAuth.OPTIONAL);
    SslHandler sslHandler = context.newHandler(ctx.alloc());

    Http2StreamCodec http2Codec = new Http2StreamCodecBuilder().connection(_connection)
            .maxContentLength(_maxResponseSize).maxHeaderSize(_maxHeaderSize)
            .gracefulShutdownTimeoutMillis(_gracefulShutdownTimeout).streamingTimeout(_streamingTimeout)
            .scheduler(_scheduler).build();

    Http2AlpnHandler alpnHandler = new Http2AlpnHandler(sslHandler, http2Codec);
    Http2SchemeHandler schemeHandler = new Http2SchemeHandler(HttpScheme.HTTPS.toString());
    Http2StreamResponseHandler responseHandler = new Http2StreamResponseHandler();
    Http2ChannelPoolHandler channelPoolHandler = new Http2ChannelPoolHandler();

    ctx.pipeline().addBefore(ctx.name(), "alpnHandler", alpnHandler);
    ctx.pipeline().addBefore(ctx.name(), "schemeHandler", schemeHandler);
    ctx.pipeline().addBefore(ctx.name(), "responseHandler", responseHandler);
    ctx.pipeline().addBefore(ctx.name(), "channelHandler", channelPoolHandler);

    _setupComplete = true;
}

From source file:com.otcdlink.chiron.downend.Http11ProxyHandler.java

License:Apache License

@Override
protected void addCodec(ChannelHandlerContext ctx) throws Exception {
    ChannelPipeline p = ctx.pipeline();//from w  w  w.ja  va2s .  c o  m
    String name = ctx.name();
    p.addBefore(name, null, codec);
}

From source file:com.shouxun.server.netty.tcp.EchoServerHandler.java

License:Apache License

public void removeFromHashMapClient(ChannelHandlerContext ctxClient) {
    ChannelHandlerContext ctx;
    for (String imei : hashMapClient.keySet()) {
        ctx = hashMapClient.get(imei);//  w w  w  .j a v a2s  .c om
        if (ctx.name().equals(ctxClient.name())) {
            hashMapClient.remove(imei);
            break;
        }
    }
}

From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPCodec.java

License:Apache License

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
        throws Exception {

    // Discard input if handshake failed. It is expected that the user will close the channel.
    if (session.handshakeFuture().isDone()) {
        assert !session.handshakeFuture().isSuccess();
        in.skipBytes(in.readableBytes());
    }/*from   ww  w.j a v a 2  s  . c o  m*/

    // Shake hands
    final ZMTPHandshake handshake;
    try {
        handshake = handshaker.handshake(in, ctx);
        if (handshake == null) {
            // Handshake is not yet done. Await more input.
            return;
        }
    } catch (Exception e) {
        session.handshakeFailure(e);
        ctx.fireUserEventTriggered(new ZMTPHandshakeFailure(session));
        throw e;
    }

    // Handshake is done.
    session.handshakeSuccess(handshake);

    // Replace this handler with the framing encoder and decoder
    if (actualReadableBytes() > 0) {
        out.add(in.readBytes(actualReadableBytes()));
    }
    final ZMTPDecoder decoder = config.decoder().decoder(session);
    final ZMTPEncoder encoder = config.encoder().encoder(session);
    final ZMTPWireFormat wireFormat = ZMTPWireFormats.wireFormat(session.negotiatedVersion());
    final ChannelHandler handler = new CombinedChannelDuplexHandler<ZMTPFramingDecoder, ZMTPFramingEncoder>(
            new ZMTPFramingDecoder(wireFormat, decoder), new ZMTPFramingEncoder(wireFormat, encoder));
    ctx.pipeline().replace(this, ctx.name(), handler);

    // Tell the user that the handshake is complete
    ctx.fireUserEventTriggered(new ZMTPHandshakeSuccess(session, handshake));
}

From source file:de.unipassau.isl.evs.ssh.core.network.ClientHandshakeHandler.java

License:Open Source License

/**
 * Called once the TCP connection is established.
 * Configures the per-connection pipeline that is responsible for handling incoming and outgoing data.
 * After an incoming packet is decrypted, decoded and verified,
 * it will be sent to its target {@link de.unipassau.isl.evs.ssh.core.handler.MessageHandler}
 * by the {@link IncomingDispatcher}./*from   ww  w .j  av  a2s .c  o m*/
 */
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    Log.v(TAG, "channelRegistered " + ctx);
    ctx.attr(ATTR_HANDSHAKE_FINISHED).set(false);

    // Add (de-)serialization Handlers before this Handler
    ctx.pipeline().addBefore(ctx.name(), ObjectEncoder.class.getSimpleName(), new ObjectEncoder());
    ctx.pipeline().addBefore(ctx.name(), ObjectDecoder.class.getSimpleName(),
            new ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(getClass().getClassLoader())));
    ctx.pipeline().addBefore(ctx.name(), LoggingHandler.class.getSimpleName(),
            new LoggingHandler(LogLevel.TRACE));

    // Timeout Handler
    ctx.pipeline().addBefore(ctx.name(), IdleStateHandler.class.getSimpleName(),
            new IdleStateHandler(READER_IDLE_TIME, WRITER_IDLE_TIME, ALL_IDLE_TIME));
    ctx.pipeline().addBefore(ctx.name(), TimeoutHandler.class.getSimpleName(), new TimeoutHandler());

    // Add exception handler
    ctx.pipeline().addAfter(ctx.name(), PipelinePlug.class.getSimpleName(), new PipelinePlug());

    super.channelRegistered(ctx);
    Log.v(TAG, "Pipeline after register: " + ctx.pipeline());
}

From source file:de.unipassau.isl.evs.ssh.core.network.ClientHandshakeHandler.java

License:Open Source License

protected void handshakeSuccessful(ChannelHandlerContext ctx) {
    if (state != State.FINISHED) {
        throw new IllegalStateException("Handshake not finished: " + state);
    }/*w  ww. j ava 2s  .c  o  m*/

    // allow pings
    TimeoutHandler.setPingEnabled(ctx.channel(), true);
    // add Dispatcher
    ctx.pipeline().addBefore(ctx.name(), IncomingDispatcher.class.getSimpleName(),
            container.require(IncomingDispatcher.KEY));
    // Logging is handled by IncomingDispatcher and OutgoingRouter
    ctx.pipeline().remove(LoggingHandler.class.getSimpleName());
    // remove HandshakeHandler
    ctx.pipeline().remove(this);

    Log.v(TAG, "Handshake successful, current Pipeline: " + ctx.pipeline());
    container.require(Client.KEY).notifyClientConnected();
}

From source file:de.unipassau.isl.evs.ssh.master.network.ServerHandshakeHandler.java

License:Open Source License

/**
 * Configures the per-connection pipeline that is responsible for handling incoming and outgoing data.
 * After an incoming packet is decrypted, decoded and verified,
 * it will be sent to its target {@link MessageHandler}
 * by the {@link IncomingDispatcher}.// ww  w.  ja v  a2 s . c  om
 */
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    Log.v(TAG, "channelRegistered " + ctx);
    if (container == null) {
        //Do not accept new connections after the Server has been shut down
        Log.v(TAG, "channelRegistered:closed");
        ctx.close();
        return;
    }

    // Add (de-)serialization Handlers before this Handler
    ctx.pipeline().addBefore(ctx.name(), ObjectEncoder.class.getSimpleName(), new ObjectEncoder());
    ctx.pipeline().addBefore(ctx.name(), ObjectDecoder.class.getSimpleName(),
            new ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(getClass().getClassLoader())));
    ctx.pipeline().addBefore(ctx.name(), LoggingHandler.class.getSimpleName(),
            new LoggingHandler(LogLevel.TRACE));

    // Timeout Handler
    ctx.pipeline().addBefore(ctx.name(), IdleStateHandler.class.getSimpleName(),
            new IdleStateHandler(READER_IDLE_TIME, WRITER_IDLE_TIME, ALL_IDLE_TIME));
    ctx.pipeline().addBefore(ctx.name(), TimeoutHandler.class.getSimpleName(), new TimeoutHandler());

    // Add exception handler
    ctx.pipeline().addLast(PipelinePlug.class.getSimpleName(), new PipelinePlug());

    super.channelRegistered(ctx);
    Log.v(TAG, "Pipeline after register: " + ctx.pipeline());
}

From source file:de.unipassau.isl.evs.ssh.master.network.ServerHandshakeHandler.java

License:Open Source License

protected void handshakeSuccessful(ChannelHandlerContext ctx) {
    final State state = getState(ctx);
    if (state != State.FINISHED) {
        throw new IllegalStateException("Handshake not finished: " + state);
    }/*w  w  w . j  a v  a2 s.c o  m*/
    final DeviceID deviceID = ctx.channel().attr(ATTR_PEER_ID).get();

    // allow pings
    TimeoutHandler.setPingEnabled(ctx.channel(), true);
    // add Dispatcher
    ctx.pipeline().addBefore(ctx.name(), IncomingDispatcher.class.getSimpleName(),
            container.require(IncomingDispatcher.KEY));
    // Logging is handled by IncomingDispatcher and OutgoingRouter
    ctx.pipeline().remove(LoggingHandler.class.getSimpleName());
    // remove HandshakeHandler
    ctx.pipeline().remove(this);

    // Register connection
    server.getActiveChannels().add(ctx.channel());
    Log.i(TAG, "Handshake with " + deviceID + " successful, current Pipeline: " + ctx.pipeline());

    Message message = new Message(
            new DeviceConnectedPayload(deviceID, ctx.channel(), ctx.attr(ATTR_LOCAL_CONNECTION).get()));
    container.require(OutgoingRouter.KEY).sendMessageLocal(RoutingKeys.MASTER_DEVICE_CONNECTED, message);

    ctx.channel().closeFuture().addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            for (Server.ServerConnectionListener listener : server.listeners) {
                listener.onClientConnected(future.channel());
            }
        }
    });

    for (Server.ServerConnectionListener listener : server.listeners) {
        listener.onClientConnected(ctx.channel());
    }
}

From source file:diskCacheV111.doors.NettyLineBasedDoor.java

License:Open Source License

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    ChannelPipeline pipeline = ctx.pipeline();
    String self = ctx.name();

    if (expectProxyProtocol) {
        pipeline.addBefore("door", "haproxy", new HAProxyMessageDecoder());
    }/*from  ww w.j av  a  2s. c  o m*/

    // Decoders
    pipeline.addBefore(self, "frameDecoder", new LineBasedFrameDecoder(KiB.toBytes(64)));
    pipeline.addBefore(self, "stringDecoder", new StringDecoder(charset));

    // Encoder
    pipeline.addBefore(self, "lineEncoder", new LineEncoder(lineSeparator, charset));

    pipeline.addBefore(self, "logger", new LoggingHandler());
}