Skip to content

Instantly share code, notes, and snippets.

@Topher-the-Geek
Created September 27, 2013 14:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Topher-the-Geek/6729681 to your computer and use it in GitHub Desktop.
Save Topher-the-Geek/6729681 to your computer and use it in GitHub Desktop.
Ping Pong in Netty 4.0.
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);
}
}
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();
}
}
}
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();
}
}
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();
}
}
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment