Example usage for io.netty.channel ChannelPipeline addLast

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

Introduction

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

Prototype

ChannelPipeline addLast(EventExecutorGroup group, ChannelHandler... handlers);

Source Link

Document

Inserts ChannelHandler s at the last position of this pipeline.

Usage

From source file:com.relayrides.pushy.apns.FeedbackServiceClient.java

License:Open Source License

/**
 * <p>Retrieves a list of expired tokens from the APNs feedback service. Be warned that this is a
 * <strong>destructive operation</strong>. According to Apple's documentation:</p>
 *
 * <blockquote>The feedback service's list is cleared after you read it. Each time you connect to the feedback
 * service, the information it returns lists only the failures that have happened since you last
 * connected.</blockquote>//w ww.j a  v  a  2 s.com
 *
 * @param timeout the time after the last received data after which the connection to the feedback service should
 * be closed
 * @param timeoutUnit the unit of time in which the given {@code timeout} is measured
 *
 * @return a list of tokens that have expired since the last connection to the feedback service
 *
 * @throws InterruptedException if interrupted while waiting for a response from the feedback service
 * @throws FeedbackConnectionException if the connection to the feedback service failed for any reason
 */
public synchronized List<ExpiredToken> getExpiredTokens(final long timeout, final TimeUnit timeoutUnit)
        throws InterruptedException, FeedbackConnectionException {

    this.expiredTokens.clear();

    final Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(this.eventLoopGroup);
    bootstrap.channel(NioSocketChannel.class);

    final FeedbackServiceClient feedbackClient = this;
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(final SocketChannel channel) throws Exception {
            final ChannelPipeline pipeline = channel.pipeline();

            final SSLEngine sslEngine = feedbackClient.sslContext.createSSLEngine();
            sslEngine.setUseClientMode(true);

            pipeline.addLast("ssl", new SslHandler(sslEngine));
            pipeline.addLast("readTimeoutHandler", new ReadTimeoutHandler(timeout, timeoutUnit));
            pipeline.addLast("decoder", new ExpiredTokenDecoder());
            pipeline.addLast("handler", new FeedbackClientHandler(feedbackClient));
        }

    });

    final ChannelFuture connectFuture = bootstrap
            .connect(this.environment.getFeedbackHost(), this.environment.getFeedbackPort()).await();

    if (connectFuture.isSuccess()) {
        log.debug("Connected to feedback service.");

        final SslHandler sslHandler = connectFuture.channel().pipeline().get(SslHandler.class);

        if (sslHandler != null) {
            final Future<Channel> handshakeFuture = sslHandler.handshakeFuture().await();

            if (handshakeFuture.isSuccess()) {
                log.debug("Completed TLS handshake with feedback service.");

                // The feedback service will send us a list of device tokens as soon as we complete the SSL
                // handshake, then hang up. While we're waiting to sync with the connection closure, we'll be
                // receiving messages from the feedback service from another thread.
                connectFuture.channel().closeFuture().await();
            } else {
                log.debug("Failed to complete TLS handshake with feedback service.", handshakeFuture.cause());

                connectFuture.channel().close().await();
                throw new FeedbackConnectionException(handshakeFuture.cause());
            }
        } else {
            log.warn("Feedback client failed to get SSL handler and could not wait for TLS handshake.");

            connectFuture.channel().close().await();
            throw new FeedbackConnectionException(null);
        }
    } else {
        log.debug("Failed to connect to feedback service.", connectFuture.cause());
        throw new FeedbackConnectionException(connectFuture.cause());
    }

    return new ArrayList<ExpiredToken>(this.expiredTokens);
}

From source file:com.relayrides.pushy.apns.FeedbackServiceConnection.java

License:Open Source License

/**
 * <p>Connects to the APNs feedback service and waits for expired tokens to arrive. Be warned that this is a
 * <strong>destructive operation</strong>. According to Apple's documentation:</p>
 *
 * <blockquote>The feedback service's list is cleared after you read it. Each time you connect to the feedback
 * service, the information it returns lists only the failures that have happened since you last
 * connected.</blockquote>/* w w w .ja v a  2  s . c om*/
 */
public synchronized void connect() {

    if (this.connectFuture != null) {
        throw new IllegalStateException(String.format("%s already started a connection attempt.", this.name));
    }

    final Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(this.eventLoopGroup);
    bootstrap.channel(NioSocketChannel.class);

    final FeedbackServiceConnection feedbackConnection = this;
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(final SocketChannel channel) throws Exception {
            final ChannelPipeline pipeline = channel.pipeline();

            final SSLEngine sslEngine = feedbackConnection.sslContext.createSSLEngine();
            sslEngine.setUseClientMode(true);

            pipeline.addLast("ssl", new SslHandler(sslEngine));
            pipeline.addLast("readTimeoutHandler",
                    new ReadTimeoutHandler(feedbackConnection.configuration.getReadTimeout()));
            pipeline.addLast("decoder", new ExpiredTokenDecoder());
            pipeline.addLast("handler", new FeedbackClientHandler(feedbackConnection));
        }
    });

    this.connectFuture = bootstrap.connect(this.environment.getFeedbackHost(),
            this.environment.getFeedbackPort());
    this.connectFuture.addListener(new GenericFutureListener<ChannelFuture>() {

        @Override
        public void operationComplete(final ChannelFuture connectFuture) {

            if (connectFuture.isSuccess()) {
                log.debug("{} connected; waiting for TLS handshake.", feedbackConnection.name);

                final SslHandler sslHandler = connectFuture.channel().pipeline().get(SslHandler.class);

                try {
                    sslHandler.handshakeFuture().addListener(new GenericFutureListener<Future<Channel>>() {

                        @Override
                        public void operationComplete(final Future<Channel> handshakeFuture) {
                            if (handshakeFuture.isSuccess()) {
                                log.debug("{} successfully completed TLS handshake.", feedbackConnection.name);

                                if (feedbackConnection.listener != null) {
                                    feedbackConnection.listener.handleConnectionSuccess(feedbackConnection);
                                }

                            } else {
                                log.debug("{} failed to complete TLS handshake with APNs feedback service.",
                                        feedbackConnection.name, handshakeFuture.cause());

                                connectFuture.channel().close();

                                if (feedbackConnection.listener != null) {
                                    feedbackConnection.listener.handleConnectionFailure(feedbackConnection,
                                            handshakeFuture.cause());
                                }
                            }
                        }
                    });
                } catch (NullPointerException e) {
                    log.warn("{} failed to get SSL handler and could not wait for a TLS handshake.",
                            feedbackConnection.name);

                    connectFuture.channel().close();

                    if (feedbackConnection.listener != null) {
                        feedbackConnection.listener.handleConnectionFailure(feedbackConnection, e);
                    }
                }
            } else {
                log.debug("{} failed to connect to APNs feedback service.", feedbackConnection.name,
                        connectFuture.cause());

                if (feedbackConnection.listener != null) {
                    feedbackConnection.listener.handleConnectionFailure(feedbackConnection,
                            connectFuture.cause());
                }
            }
        }
    });
}

From source file:com.replaymod.sponge.recording.spongecommon.SpongeRecorder.java

License:MIT License

public SpongeRecorder(Game game, SpongeConnection connection) {
    super(game, connection);

    ChannelPipeline pipeline = connection.getChannel().get().pipeline();
    pipeline.addLast("recorder_compression_order", new PipelineOrderHandler());
    pipeline.addBefore("encoder", "outbound_recorder", new OutboundPacketRecorder());
    pipeline.addBefore("decoder", "inbound_recorder", new InboundPacketRecorder());
}

From source file:com.seagate.kinetic.client.io.provider.nio.http.HttpChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) throws Exception {

    boolean ssl = Boolean.getBoolean("kinetic.io.https");

    ChannelPipeline p = ch.pipeline();

    // Enable HTTPS if necessary.
    if (ssl) {/*from   w w  w .  j  ava  2 s . c om*/
        SSLEngine engine = SslContextFactory.getClientContext().createSSLEngine();

        engine.setUseClientMode(true);

        p.addLast("ssl", new SslHandler(engine));
    }

    p.addLast("codec", new HttpClientCodec(1024, 4 * 1024, 4 * 1024 * 1024));

    p.addLast("aggregator", new HttpObjectAggregator(4 * 1024 * 1024));

    p.addLast("handler", new HttpMessageServiceHandler(mservice));

    logger.info("http/s channel initialized, use ssl handler=" + ssl);
}

From source file:com.seagate.kinetic.client.io.provider.nio.ssl.SslChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) throws Exception {

    if (mservice.getConfiguration().getConnectTimeoutMillis() > 0) {
        ch.config().setConnectTimeoutMillis(mservice.getConfiguration().getConnectTimeoutMillis());
    }// w w  w .  java2s .c o  m

    ChannelPipeline pipeline = ch.pipeline();

    SSLEngine engine = SslContextFactory.getClientContext().createSSLEngine();

    engine.setUseClientMode(true);

    /**
     * enable TLS V1.x protocols.
     */
    TlsUtil.enableSupportedProtocols(engine);

    // add ssl handler
    pipeline.addLast("ssl", new SslHandler(engine));

    // decoder
    pipeline.addLast("decoder", new KineticDecoder());
    // encoder
    pipeline.addLast("encoder", new KineticEncoder());

    pipeline.addLast("handler", new SslMessageServiceHandler(mservice));

    logger.info("ssl channel initialized ... ");
}

From source file:com.seagate.kinetic.client.io.provider.nio.tcp.NioChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) throws Exception {

    if (mservice.getConfiguration().getConnectTimeoutMillis() > 0) {
        ch.config().setConnectTimeoutMillis(mservice.getConfiguration().getConnectTimeoutMillis());
    }/* w  w  w .j ava  2  s .co  m*/

    ChannelPipeline p = ch.pipeline();

    // decoder
    p.addLast("decoder", new KineticDecoder());
    // encoder
    p.addLast("encoder", new KineticEncoder());

    p.addLast("handler", new NioMessageServiceHandler(mservice));

    logger.info("nio channel initialized ...");
}

From source file:com.seagate.kinetic.client.io.provider.nio.udt.UdtClientChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(UdtChannel ch) throws Exception {

    if (mservice.getConfiguration().getConnectTimeoutMillis() > 0) {
        ch.config().setConnectTimeoutMillis(mservice.getConfiguration().getConnectTimeoutMillis());
    }/*from ww w  . j  a v  a2  s. c om*/

    ChannelPipeline p = ch.pipeline();

    p.addLast("handler", new UdtClientMessageServiceHandler(mservice));

    logger.info("UDT client nio channel initilized ...");
}

From source file:com.seagate.kinetic.simulator.io.provider.nio.http.HttpChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) throws Exception {

    ChannelPipeline p = ch.pipeline();

    boolean isSsl = Boolean.getBoolean("kinetic.io.https");

    if (isSsl) {//from  ww w.  j  a va 2  s.  c  o m
        SSLEngine engine = SslContextFactory.getServerContext().createSSLEngine();

        engine.setUseClientMode(false);

        p.addLast("ssl", new SslHandler(engine));

        logger.info("ssl handler added, https is enabled ...");
    }

    p.addLast("decoder", new HttpRequestDecoder(1024, 4 * 1024, 4 * 1024 * 1024));

    p.addLast("encoder", new HttpResponseEncoder());

    p.addLast("aggregator", new HttpObjectAggregator(4 * 1024 * 1024));

    p.addLast("handler", new HttpMessageServiceHandler(lcservice));

    logger.info("http channel initialized. ssl/tls enabled=" + isSsl);
}

From source file:com.seagate.kinetic.simulator.io.provider.nio.ssl.SslChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) throws Exception {

    ChannelPipeline pipeline = ch.pipeline();

    SSLEngine engine = SslContextFactory.getServerContext().createSSLEngine();

    engine.setUseClientMode(false);/*from  w ww  .  j  ava 2s .  co  m*/

    // enable TLS v1.x protocols.
    TlsUtil.enableSupportedProtocols(engine);

    // add ssl handler
    pipeline.addLast("ssl", new SslHandler(engine));

    // decoder
    pipeline.addLast("decoder", new KineticDecoder());
    // encoder
    pipeline.addLast("encoder", new KineticEncoder());

    // pipeline.addLast("handler", new SslMessageServiceHandler(lcservice));

    pipeline.addLast("handler", new NioMessageServiceHandler(lcservice, true));

    logger.info("ssl nio channel initialized ... ");
}

From source file:com.seagate.kinetic.simulator.io.provider.nio.tcp.NioChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) throws Exception {

    ChannelPipeline p = ch.pipeline();

    // decoder//w w  w  . ja  v  a2s.  c  o m
    p.addLast("decoder", new KineticDecoder());
    // encoder
    p.addLast("encoder", new KineticEncoder());

    p.addLast("handler", new NioMessageServiceHandler(lcservice, false));

    logger.info("nio channel initialized., is secure channel=false");
}