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(ChannelHandler... handlers);

Source Link

Document

Inserts ChannelHandler s at the last position of this pipeline.

Usage

From source file:com.heliosapm.shorthand.caster.broadcast.BroadcastListener.java

License:Open Source License

/**
 * Starts a listener on the passed socket address
 * @param isa The socket address to listen on
 * @param nic The network interface to listen on
 *//*from   ww  w  . jav  a  2  s  .  c o m*/
public void startListener(InetSocketAddress isa, NetworkInterface nic) {
    Channel channel = null;
    if (isa.getAddress().isMulticastAddress()) {
        channel = bootstrap.group(group).channel(NioDatagramChannel.class)
                //                 .option(ChannelOption.SO_BROADCAST, true)
                .option(ChannelOption.IP_MULTICAST_ADDR, isa.getAddress())
                .option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF)
                .handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel channel) throws Exception {
                        ChannelPipeline pipeline = channel.pipeline();
                        pipeline.addLast(new LoggingHandler(BroadcastListener.class, LogLevel.DEBUG));
                        pipeline.addLast(router);
                    }
                }).localAddress(isa).bind(isa.getPort()).syncUninterruptibly().channel();
        ((NioDatagramChannel) channel).joinGroup(isa, NetUtil.LOOPBACK_IF).syncUninterruptibly();

        //.bind(isa.getPort()).syncUninterruptibly().channel();
        log("Bound to Multicast [%s]", isa);
    } else {
        channel = bootstrap.group(group).channel(NioDatagramChannel.class)
                .option(ChannelOption.SO_BROADCAST, true).handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel channel) throws Exception {
                        ChannelPipeline pipeline = channel.pipeline();
                        pipeline.addLast(new LoggingHandler(BroadcastListener.class, LogLevel.DEBUG));
                        pipeline.addLast(router);
                    }
                }).localAddress(isa).bind(isa).syncUninterruptibly().channel();
        log("Bound to Broadcast UDP [%s]", isa);
    }
    boundChannels.add(channel);

    //.bind().syncUninterruptibly().channel();
    boundChannels.add(channel);
    log("Started Broadcast Listener on [%s]", isa);

}

From source file:com.heliosapm.webrpc.server.RPCServer.java

License:Apache License

@Override
protected void initChannel(final SocketChannel ch) throws Exception {
    final ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
    pipeline.addLast(webSockServiceHandler);

}

From source file:com.heren.turtle.entry.channel.MessageReceiveInitializer.java

License:Open Source License

@Override
protected void initChannel(Channel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    pipeline.addLast(new ObjectDecoder(MAX_OBJECT_SIZE,
            ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
    pipeline.addLast(new ObjectEncoder());
    if (needToFilter) {
        pipeline.addLast(new LoginAuthRespHandler());
    }//from  w w w  .  j a v  a 2  s. c o  m
    pipeline.addLast(new MessageReceiverHandler());
}

From source file:com.hipishare.chat.SecureChatClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    LOG.info("channel?");
    ChannelPipeline pipeline = ch.pipeline();

    // Add SSL handler first to encrypt and decrypt everything.
    // In this example, we use a bogus certificate in the server side
    // and accept any invalid certificates in the client side.
    // You will need something more complicated to identify both
    // and server in the real world.
    pipeline.addLast(sslCtx.newHandler(ch.alloc(), SecureChatClient.HOST, SecureChatClient.PORT));

    // On top of the SSL handler, add the text line codec.
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());
    pipeline.addLast(new LoggingHandler(LogLevel.INFO));

    // and then business logic.
    pipeline.addLast(new SecureChatClientHandler());
}

From source file:com.hipishare.chat.securetest.SecureChatClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    LOG.info("channel?");
    ChannelPipeline pipeline = ch.pipeline();

    // Add SSL handler first to encrypt and decrypt everything.
    // In this example, we use a bogus certificate in the server side
    // and accept any invalid certificates in the client side.
    // You will need something more complicated to identify both
    // and server in the real world.
    //        pipeline.addLast(sslCtx.newHandler(ch.alloc(), SecureChatClient.HOST, SecureChatClient.PORT));

    // On top of the SSL handler, add the text line codec.
    //        pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    //        pipeline.addLast(new LengthFieldBasedFrameDecoder(65536, 0, 4, 0, 4));
    pipeline.addLast(new LengthFieldPrepender(4, false));
    /*pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());*/
    pipeline.addLast(new MsgObjectDecoder());
    pipeline.addLast(new MsgObjectEncoder());
    //        pipeline.addLast(new LoggingHandler(LogLevel.INFO));

    // and then business logic.
    pipeline.addLast(new SecureChatClientHandler());
}

From source file:com.hipishare.chat.server.handler.SecureChatInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    // HandlerPipeline?InboundHandler?OutboundHandler?
    ChannelPipeline pipeline = ch.pipeline();

    // Add SSL handler first to encrypt and decrypt everything.
    // In this example, we use a bogus certificate in the server side
    // and accept any invalid certificates in the client side.
    // You will need something more complicated to identify both
    // and server in the real world.
    //        pipeline.addLast(sslCtx.newHandler(ch.alloc()));

    // On top of the SSL handler, add the text line codec.
    //        pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(new LengthFieldBasedFrameDecoder(65536, 0, 4, 0, 4));
    /*pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());*/
    pipeline.addLast(new MsgObjectDecoder());
    pipeline.addLast(new MsgObjectEncoder());

    // /*  w w  w.  j a va  2s .c o  m*/
    pipeline.addLast(new IdleStateHandler(160, 130, 0, TimeUnit.SECONDS));
    pipeline.addLast(new HeartBeatHandler());

    // and then business logic.
    pipeline.addLast(new SecureChatHandler());
}

From source file:com.hipishare.chat.test.EchoClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.git
    final SslContext sslCtx;
    if (SSL) {/*from   w  w w .j  av a 2  s. c o  m*/
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        if (sslCtx != null) {
                            p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                        }
                        p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoClientHandler());
                    }
                });

        // Start the client.
        ChannelFuture f = b.connect(HOST, PORT).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}

From source file:com.hipishare.chat.test.EchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*  w ww .j a v  a  2s . c o  m*/
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        if (sslCtx != null) {
                            p.addLast(sslCtx.newHandler(ch.alloc()));
                        }
                        //p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new IdleStateHandler(30, 10, 0, TimeUnit.SECONDS));
                        p.addLast(new HeartBeatHandler());
                        p.addLast(new EchoServerHandler());
                    }
                });

        // Start the server.
        ChannelFuture f = b.bind(PORT).sync();

        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.hop.hhxx.example.factorial.FactorialClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();

    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc(), io.netty.example.factorial.FactorialClient.HOST,
                io.netty.example.factorial.FactorialClient.PORT));
    }//w  ww . j a v a2 s  .  co m

    // Enable stream compression (you can remove these two if unnecessary)
    pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
    pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

    // Add the number codec first,
    pipeline.addLast(new BigIntegerDecoder());
    pipeline.addLast(new NumberEncoder());

    // and then business logic.
    pipeline.addLast(new FactorialClientHandler());
}

From source file:com.hop.hhxx.example.factorial.FactorialServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();

    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }//from www  . j  av  a2  s .  c  o  m

    // Enable stream compression (you can remove these two if unnecessary)
    pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
    pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

    // Add the number codec first,
    pipeline.addLast(new io.netty.example.factorial.BigIntegerDecoder());
    pipeline.addLast(new NumberEncoder());

    // and then business logic.
    // Please note we create a handler for every new channel
    // because it has stateful properties.
    pipeline.addLast(new FactorialServerHandler());
}