Example usage for io.netty.handler.codec.string StringEncoder StringEncoder

List of usage examples for io.netty.handler.codec.string StringEncoder StringEncoder

Introduction

In this page you can find the example usage for io.netty.handler.codec.string StringEncoder StringEncoder.

Prototype

public StringEncoder() 

Source Link

Document

Creates a new instance with the current system character set.

Usage

From source file:SecureChatServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    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 StringDecoder());
    pipeline.addLast(new StringEncoder());

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

From source file:NettyServer.java

License:Open Source License

void start() throws Exception {
    parentGroup = new NioEventLoopGroup(1);
    childGroup = new NioEventLoopGroup();

    ServerBootstrap bootstrap = new ServerBootstrap().group(parentGroup, childGroup)
            .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override/*from w w  w .  j  a va2s.  c  o m*/
                protected void initChannel(SocketChannel channel) throws Exception {
                    ChannelPipeline pipeline = channel.pipeline();

                    pipeline.addLast("lineFrame", new LineBasedFrameDecoder(256));
                    pipeline.addLast("decoder", new StringDecoder());
                    pipeline.addLast("encoder", new StringEncoder());

                    pipeline.addLast("handler", new ChannelInboundHandlerAdapter() {
                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            Channel channel = ctx.channel();
                            channelActiveHandler.accept(channel);
                        }

                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            messageConsumer.accept(msg);
                        }
                    });
                }
            });
    channel = bootstrap.bind(port).channel();
    System.out.println("NettyServer started");
}

From source file:barry.ServerInitializer.java

License:Apache License

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

    pipeline.addLast("logging", new LoggingHandler());

    // ???/*from  w  w w. j  a  va 2 s .com*/
    //      pipelinene.addLast("decoder", new MsgDecoder());
    // ????byte
    //      pipeline.addLast("encoder", new MsgEncoder());

    pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());

    // 
    pipeline.addLast(new IdleStateHandler(0, 0, 90, TimeUnit.SECONDS));

    // 
    pipeline.addLast("handler", serverHandler);
}

From source file:books.netty.ssl.SecureChatClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    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.

    SSLEngine engine = null;/*from w  w  w .  j a  v a  2  s.  c o m*/
    if (SSLMODE.CA.toString().equals(tlsMode)) {
        engine = SecureChatSslContextFactory
                .getClientContext(tlsMode, null,
                        System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/client/cChat.jks")
                .createSSLEngine();
    } else if (SSLMODE.CSA.toString().equals(tlsMode)) {
        engine = SecureChatSslContextFactory
                .getClientContext(tlsMode,
                        System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/twoway/cChat.jks",
                        System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/twoway/cChat.jks")
                .createSSLEngine();

        // engine = SecureChatSslContextFactory
        // .getClientContext(
        // tlsMode,
        // System.getProperty("user.dir")
        // + "/src/com/phei/netty/ssl/conf/client/cChat.jks",
        // System.getProperty("user.dir")
        // + "/src/com/phei/netty/ssl/conf/client/cChat.jks")
        // .createSSLEngine();

    } else {
        System.err.println("ERROR : " + tlsMode);
        System.exit(-1);
    }
    engine.setUseClientMode(true);
    pipeline.addLast("ssl", new SslHandler(engine));

    // On top of the SSL handler, add the text line codec.
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());

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

From source file:books.netty.ssl.SecureChatServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    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.
    ///*from  ww  w.  j a  v  a2  s .co  m*/
    // Read SecureChatSslContextFactory
    // if you need client certificate authentication.

    SSLEngine engine = null;
    if (SSLMODE.CA.toString().equals(tlsMode)) {
        engine = SecureChatSslContextFactory
                .getServerContext(tlsMode,
                        System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/client/sChat.jks", null)
                .createSSLEngine();
    } else if (SSLMODE.CSA.toString().equals(tlsMode)) {
        engine = SecureChatSslContextFactory
                .getServerContext(tlsMode,
                        System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/twoway/sChat.jks",
                        System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/twoway/sChat.jks")
                .createSSLEngine();

        // engine = SecureChatSslContextFactory
        // .getServerContext(
        // tlsMode,
        // System.getProperty("user.dir")
        // + "/src/com/phei/netty/ssl/conf/client/sChat.jks",
        // System.getProperty("user.dir")
        // + "/src/com/phei/netty/ssl/conf/client/sChat.jks")
        // .createSSLEngine();
    } else {
        System.err.println("ERROR : " + tlsMode);
        System.exit(-1);
    }
    engine.setUseClientMode(false);

    // Client auth
    if (SSLMODE.CSA.toString().equals(tlsMode))
        engine.setNeedClientAuth(true);
    pipeline.addLast("ssl", new SslHandler(engine));

    // On top of the SSL handler, add the text line codec.
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());

    // and then business logic.
    pipeline.addLast("handler", new SecureChatServerHandler());
}

From source file:ca.lambtoncollege.hauntedhouse.client.ClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    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.
    if (sslCtx != null)
        pipeline.addLast(sslCtx.newHandler(ch.alloc(), Client.HOST, Client.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());
    // and then business logic.
    pipeline.addLast(new ClientHandler());
}

From source file:ca.lambtoncollege.hauntedhouse.server.ServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    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.
    if (sslCtx != null)
        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 StringDecoder());
    pipeline.addLast(new StringEncoder());
    // and then business logic.
    pipeline.addLast(new ServerHandler());
}

From source file:ca.lambtoncollege.netty.chat.SecureChatClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    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());
    // and then business logic.
    pipeline.addLast(new SecureChatClientHandler());
}

From source file:ca.lambtoncollege.netty.chat.SecureChatServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    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 StringDecoder());
    pipeline.addLast(new StringEncoder());
    // and then business logic.
    pipeline.addLast(new SecureChatServerHandler());
}

From source file:com.carlos.netty.server.ObjectEchoServer.java

License:Apache License

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

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new EchoLoginHandler(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 StringEncoder(), new StringDecoder(), new ObjectEchoServerHandler());
                    }
                });

        // Bind and start to accept incoming connections.
        b.bind(PORT).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}