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:c5db.eventLogging.EventLogListener.java

License:Apache License

@Override
protected void doStart() {
    nioEventLoopGroup.next().execute(() -> {

        Bootstrap bootstrap = new Bootstrap();
        try {//from w w  w .  ja  v  a 2 s.c  om
            bootstrap.group(nioEventLoopGroup).channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, true).option(ChannelOption.SO_REUSEADDR, true)
                    .handler(new ChannelInitializer<DatagramChannel>() {
                        @Override
                        protected void initChannel(DatagramChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast("protostuffDecoder",
                                    new UdpProtostuffDecoder<>(EventLogEntry.getSchema(), false));
                            p.addLast("logger", new MsgHandler());
                        }
                    });

            bootstrap.bind(port).addListener(new GenericFutureListener<ChannelFuture>() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    channel = future.channel();
                }
            });

            notifyStarted();
        } catch (Throwable t) {
            notifyFailed(t);
        }
    });
}

From source file:c5db.eventLogging.EventLogService.java

License:Apache License

@Override
protected void doStart() {
    nioEventLoopGroup.next().execute(() -> {
        Bootstrap bootstrap = new Bootstrap();
        try {//ww  w  .j  a  v a 2  s  .co  m
            bootstrap.group(nioEventLoopGroup).channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, true).option(ChannelOption.SO_REUSEADDR, true)
                    .handler(new ChannelInitializer<DatagramChannel>() {
                        @Override
                        protected void initChannel(DatagramChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast("protostuffEncoder",
                                    new UdpProtostuffEncoder<>(EventLogEntry.getSchema(), false));
                        }
                    });

            bootstrap.bind(port).addListener(new GenericFutureListener<ChannelFuture>() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    broadcastChannel = future.channel();
                }
            });

            eventLogChannel.subscribe(fiber, msg -> {
                if (broadcastChannel == null) {
                    LOG.debug("Broadcast channel isn't read yet, dropped message");
                    return;
                }

                LOG.trace("Sending event {}", msg);
                broadcastChannel
                        .writeAndFlush(new UdpProtostuffEncoder.UdpProtostuffMessage<>(sendAddress, msg));
            });

            fiber.start();

            notifyStarted();
        } catch (Throwable t) {
            fiber.dispose();

            notifyFailed(t);
        }
    });
}

From source file:c5db.regionserver.RegionServerService.java

License:Apache License

@Override
protected void doStart() {
    fiber.start();/*from  w ww .j av a 2 s .c  o  m*/

    fiber.execute(() -> {
        // we need the tablet module:
        ListenableFuture<C5Module> f = server.getModule(ModuleType.Tablet);
        Futures.addCallback(f, new FutureCallback<C5Module>() {
            @Override
            public void onSuccess(final C5Module result) {
                tabletModule = (TabletModule) result;
                bootstrap.group(acceptGroup, workerGroup).option(ChannelOption.SO_REUSEADDR, true)
                        .childOption(ChannelOption.TCP_NODELAY, true).channel(NioServerSocketChannel.class)
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception {
                                ChannelPipeline p = ch.pipeline();
                                p.addLast("http-server-codec", new HttpServerCodec());
                                p.addLast("http-agg",
                                        new HttpObjectAggregator(C5ServerConstants.MAX_CALL_SIZE));
                                p.addLast("websocket-agg",
                                        new WebSocketFrameAggregator(C5ServerConstants.MAX_CALL_SIZE));
                                p.addLast("decoder", new WebsocketProtostuffDecoder("/websocket"));
                                p.addLast("encoder", new WebsocketProtostuffEncoder());
                                p.addLast("handler", new RegionServerHandler(RegionServerService.this));
                            }
                        });

                bootstrap.bind(port).addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        if (future.isSuccess()) {
                            listenChannel = future.channel();
                            notifyStarted();
                        } else {
                            LOG.error("Unable to find Region Server to {} {}", port, future.cause());
                            notifyFailed(future.cause());
                        }
                    }
                });
            }

            @Override
            public void onFailure(Throwable t) {
                notifyFailed(t);
            }
        }, fiber);
    });
}

From source file:c5db.replication.ReplicatorService.java

License:Apache License

/**
 * ********** Service startup/registration and shutdown/termination **************
 *///from   w w  w .j  a  va  2s .c  o m
@Override
protected void doStart() {
    // must start the fiber up early.
    fiber = fiberSupplier.getNewFiber(this::failModule);
    setupEventChannelSubscription();
    fiber.start();

    C5Futures.addCallback(getDependedOnModules(), (ignore) -> {
        ChannelInitializer<SocketChannel> initer = new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast("frameDecode", new ProtobufVarint32FrameDecoder());
                p.addLast("pbufDecode", new ProtostuffDecoder<>(ReplicationWireMessage.getSchema()));

                p.addLast("frameEncode", new ProtobufVarint32LengthFieldPrepender());
                p.addLast("pbufEncoder", new ProtostuffEncoder<ReplicationWireMessage>());

                p.addLast(new MessageHandler());
            }
        };

        serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_BACKLOG, 100)
                .childOption(ChannelOption.TCP_NODELAY, true).childHandler(initer);

        //noinspection RedundantCast
        serverBootstrap.bind(port).addListener((ChannelFutureListener) future -> {
            if (future.isSuccess()) {
                LOG.info("successfully bound node {} port {} ", nodeId, port);
                listenChannel = future.channel();
            } else {
                LOG.error("Unable to bind! ", future.cause());
                failModule(future.cause());
            }
        });

        outgoingBootstrap.group(workerGroup).channel(NioSocketChannel.class)
                .option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.TCP_NODELAY, true)
                .handler(initer);

        //noinspection Convert2MethodRef
        outgoingRequests.subscribe(fiber, message -> handleOutgoingMessage(message),
                // Clean up cancelled requests.
                message -> handleCancelledSession(message.getSession()));

        notifyStarted();

    }, (Throwable t) -> {
        LOG.error("ReplicatorService unable to retrieve modules!", t);
        failModule(t);
    }, fiber);
}

From source file:cc.io.lessons.server.HttpUploadServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    // Create a default pipeline implementation.
    ChannelPipeline pipeline = ch.pipeline();

    if (HttpUploadServer.isSSL) {
        SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
        engine.setUseClientMode(false);/*from w  w  w .  j  a  va2 s .  c  om*/
        pipeline.addLast("ssl", new SslHandler(engine));
    }

    pipeline.addLast("decoder", new HttpRequestDecoder());
    pipeline.addLast("encoder", new HttpResponseEncoder());

    // Remove the following line if you don't want automatic content
    // compression.
    //pipeline.addLast("deflater", new HttpContentCompressor());
    pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
    pipeline.addLast("WebSocket", new WebSocketServerHandler());
    pipeline.addLast("handler", new HttpUploadServerHandler());

    //pipeline.addLast("CustomTextFrameHandler",new CustomTextFrameHandler());
}

From source file:cc.ly.mc.client.netty.SocketClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast("decoder", new SocketDecoder());
    pipeline.addLast("encoder", new SocketEncoder());
    pipeline.addLast("handler", new SocketClientHandler());
}

From source file:cc.ly.mc.core.client.io.SocketClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast("decoder", new SocketClientDecoder());
    pipeline.addLast("encoder", new SocketClientEncoder());
    pipeline.addLast("handler", new SocketClientHandler(connectedListeners, disconnectedListeners));
}

From source file:cc.ly.mc.core.server.io.SocketServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast("decoder", new SocketServerDecoder());
    pipeline.addLast("encoder", new SocketServerEncoder());
    pipeline.addLast("handler", new SocketServerHandler(connectedListeners, disconnectedListeners));
}

From source file:cc.ly.mc.server.netty.SocketServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast("idleStateHandler", new IdleStateHandler(0, 3, 0));
    pipeline.addLast("heartbeat", new HeartbeatHandler());
    pipeline.addLast("decoder", new SocketDecoder());
    pipeline.addLast("encoder", new SocketEncoder());
    pipeline.addLast("handler", new SocketServerHandler());
}

From source file:ccwihr.client.t2.Http2ClientInitializer.java

License:Apache License

protected void configureEndOfPipeline(ChannelPipeline pipeline) {
    pipeline.addLast(settingsHandler, responseHandler);
}