Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save raja-reddy/e4ff241b9afb47d3815061ae28e265f4 to your computer and use it in GitHub Desktop.

Select an option

Save raja-reddy/e4ff241b9afb47d3815061ae28e265f4 to your computer and use it in GitHub Desktop.

Revisions

  1. @Topher-the-Geek Topher-the-Geek created this gist Sep 27, 2013.
    29 changes: 29 additions & 0 deletions PingPongChannelInitializer.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    package example;

    import io.netty.channel.ChannelHandler;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
    import io.netty.handler.codec.LengthFieldPrepender;
    import io.netty.handler.codec.string.StringDecoder;
    import io.netty.handler.codec.string.StringEncoder;
    import io.netty.util.CharsetUtil;

    class PingPongChannelInitializer extends ChannelInitializer<SocketChannel> {

    private ChannelHandler handler;

    public PingPongChannelInitializer (ChannelHandler handler) {
    this.handler = handler;
    }

    @Override
    public void initChannel (SocketChannel ch) throws Exception {
    ch.pipeline().addLast (
    new LengthFieldBasedFrameDecoder (Integer.MAX_VALUE, 0, 2),
    new LengthFieldPrepender (2),
    new StringDecoder (CharsetUtil.UTF_8),
    new StringEncoder (CharsetUtil.UTF_8),
    handler);
    }
    }
    42 changes: 42 additions & 0 deletions PingPongClient.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    package example;

    import io.netty.bootstrap.Bootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelOption;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioSocketChannel;

    public class PingPongClient {

    public static void main(String[] args) throws Exception {

    String host;
    if (args.length > 0) {
    host = args[0];
    } else {
    host = "localhost";
    }

    int port;
    if (args.length > 1) {
    port = Integer.parseInt(args[1]);
    } else {
    port = 8080;
    }

    EventLoopGroup workerGroup = new NioEventLoopGroup ();

    try {
    Bootstrap b = new Bootstrap();
    ChannelFuture f = b.group (workerGroup)
    .channel (NioSocketChannel.class)
    .option (ChannelOption.SO_KEEPALIVE, true)
    .handler (new PingPongChannelInitializer (new PingPongClientHandler ()))
    .connect (host, port).sync ();
    f.channel().closeFuture().sync ();
    } finally {
    workerGroup.shutdownGracefully();
    }
    }
    }
    45 changes: 45 additions & 0 deletions PingPongClientHandler.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    package example;

    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundHandlerAdapter;

    class PingPongClientHandler extends ChannelInboundHandlerAdapter {

    final int M = 1000, N = 10000;

    long start = System.currentTimeMillis();
    int i = M, j = N;

    @Override
    public void channelActive (ChannelHandlerContext ctx) {
    ctx.writeAndFlush ("Hello World.\n");
    j--;
    }

    @Override
    public void channelRead (ChannelHandlerContext ctx, Object msg) {
    ctx.write ("Hello World.\n");
    j--;
    if (j == 0) {
    long end = System.currentTimeMillis();
    double ms = (double)(end - start) / (double)(N);
    double qps = 1.0 / ms * 1000.0;
    System.out.println (String.format ("%d trials: %10.3f ms, %10.0f rps", N, ms, qps));
    j = N;
    i--;
    if (i == 0)
    ctx.close();
    }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    ctx.flush();
    }

    @Override
    public void exceptionCaught (ChannelHandlerContext ctx, Throwable cause) {
    cause.printStackTrace();
    ctx.close();
    }
    }
    45 changes: 45 additions & 0 deletions PingPongServer.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    package example;

    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelOption;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioServerSocketChannel;

    public class PingPongServer {

    private int port;

    public PingPongServer (int port) {
    this.port = port;
    }

    public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
    ServerBootstrap b = new ServerBootstrap();
    ChannelFuture f = b.group (bossGroup, workerGroup)
    .channel (NioServerSocketChannel.class)
    .childHandler (new PingPongChannelInitializer (new PingPongServerHandler ()))
    .option (ChannelOption.SO_BACKLOG, 128)
    .childOption (ChannelOption.SO_KEEPALIVE, true)
    .bind (port) .sync ();
    f.channel().closeFuture().sync();
    } finally {
    workerGroup.shutdownGracefully();
    bossGroup.shutdownGracefully();
    }
    }

    public static void main(String[] args) throws Exception {
    int port;
    if (args.length > 0) {
    port = Integer.parseInt(args[0]);
    } else {
    port = 8080;
    }
    new PingPongServer (port).run();
    }
    }
    25 changes: 25 additions & 0 deletions PingPongServerHandler.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    package example;

    import io.netty.channel.ChannelHandler;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundHandlerAdapter;

    @ChannelHandler.Sharable
    class PingPongServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead (ChannelHandlerContext ctx, Object msg) {
    ctx.write (msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    ctx.flush();
    }

    @Override
    public void exceptionCaught (ChannelHandlerContext ctx, Throwable cause) {
    cause.printStackTrace();
    ctx.close();
    }
    }