Example usage for io.netty.channel ChannelPipeline lastContext

List of usage examples for io.netty.channel ChannelPipeline lastContext

Introduction

In this page you can find the example usage for io.netty.channel ChannelPipeline lastContext.

Prototype

ChannelHandlerContext lastContext();

Source Link

Document

Returns the context of the last ChannelHandler in this pipeline.

Usage

From source file:com.addthis.hydra.query.loadbalance.NextQueryTask.java

License:Apache License

private static ChannelHandlerContext cleanPipelineAndGetLastContext(ChannelPipeline pipeline) {
    log.trace("pipeline before pruning {}", pipeline);
    ChannelHandlerContext lastContext = pipeline.lastContext();
    while ((lastContext != null) && !"encoder".equals(lastContext.name())) {
        pipeline.removeLast();/* w  w  w .  j av a2s.co  m*/
        lastContext = pipeline.lastContext();
    }
    log.trace("pipeline after pruning {}", pipeline);
    return lastContext;
}

From source file:com.linecorp.armeria.client.http.HttpClientPipelineConfigurator.java

License:Apache License

void addBeforeSessionHandler(ChannelPipeline pipeline, ChannelHandler handler) {
    // Get the name of the HttpSessionHandler so that we can put our handlers before it.
    final ChannelHandlerContext lastContext = pipeline.lastContext();
    assert lastContext.handler().getClass() == HttpSessionHandler.class;

    pipeline.addBefore(lastContext.name(), null, handler);
}

From source file:io.reactivex.netty.client.ClientChannelFactoryImpl.java

License:Apache License

@Override
public ChannelFuture connect(final Subscriber<? super ObservableConnection<I, O>> subscriber,
        RxClient.ServerInfo serverInfo,/*from  w ww . j av a 2s.  c  o  m*/
        final ClientConnectionFactory<I, O, ? extends ObservableConnection<I, O>> connectionFactory) {
    final long startTimeMillis = Clock.newStartTimeMillis();
    eventsSubject.onEvent(ClientMetricsEvent.CONNECT_START);
    final ChannelFuture connectFuture = clientBootstrap.connect(serverInfo.getHost(), serverInfo.getPort());

    subscriber.add(Subscriptions.create(new Action0() {
        @Override
        public void call() {
            if (!connectFuture.isDone()) {
                connectFuture.cancel(true); // Unsubscribe here means, no more connection is required. A close on connection is explicit.
            }
        }
    }));

    connectFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            try {
                if (!future.isSuccess()) {
                    eventsSubject.onEvent(ClientMetricsEvent.CONNECT_FAILED, Clock.onEndMillis(startTimeMillis),
                            future.cause());
                    subscriber.onError(future.cause());
                } else {
                    eventsSubject.onEvent(ClientMetricsEvent.CONNECT_SUCCESS,
                            Clock.onEndMillis(startTimeMillis));
                    ChannelPipeline pipeline = future.channel().pipeline();
                    ChannelHandlerContext ctx = pipeline.lastContext(); // The connection uses the context for write which should always start from the tail.
                    final ObservableConnection<I, O> newConnection = connectionFactory.newConnection(ctx);
                    ChannelHandler lifecycleHandler = pipeline
                            .get(RxRequiredConfigurator.CONN_LIFECYCLE_HANDLER_NAME);
                    if (null == lifecycleHandler) {
                        onNewConnection(newConnection, subscriber);
                    } else {
                        @SuppressWarnings("unchecked")
                        ConnectionLifecycleHandler<I, O> handler = (ConnectionLifecycleHandler<I, O>) lifecycleHandler;
                        SslHandler sslHandler = pipeline.get(SslHandler.class);
                        if (null == sslHandler) {
                            handler.setConnection(newConnection);
                            onNewConnection(newConnection, subscriber);
                        } else {
                            sslHandler.handshakeFuture()
                                    .addListener(new GenericFutureListener<Future<? super Channel>>() {
                                        @Override
                                        public void operationComplete(Future<? super Channel> future)
                                                throws Exception {
                                            onNewConnection(newConnection, subscriber);
                                        }
                                    });
                        }
                    }
                }
            } catch (Throwable throwable) {
                subscriber.onError(throwable);
            }
        }
    });
    return connectFuture;
}