Skip to content

Instantly share code, notes, and snippets.

@mhgrove
Last active December 21, 2015 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhgrove/6295318 to your computer and use it in GitHub Desktop.
Save mhgrove/6295318 to your computer and use it in GitHub Desktop.
Modified LocalEcho class which demonstrates the name collision in LocalAddress
package netty;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.local.LocalAddress;
import io.netty.channel.local.LocalChannel;
import io.netty.channel.local.LocalEventLoopGroup;
import io.netty.channel.local.LocalServerChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class LocalEcho {
private final String port;
public LocalEcho(String port) {
this.port = port;
}
public void run() throws Exception {
// Address to bind on / connect to.
final LocalAddress addr = new LocalAddress(port);
EventLoopGroup serverGroup = new LocalEventLoopGroup();
final EventLoopGroup clientGroup = new NioEventLoopGroup(); // NIO event loops are also OK
try {
// Note that we can use any event loop to ensure certain local channels
// are handled by the same event loop thread which drives a certain socket channel
// to reduce the communication latency between socket channels and local channels.
ServerBootstrap sb = new ServerBootstrap();
sb.group(serverGroup)
.channel(LocalServerChannel.class)
.handler(new ChannelInitializer<LocalServerChannel>() {
@Override
public void initChannel(LocalServerChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
}
})
.childHandler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(
new LoggingHandler(LogLevel.INFO),
new LocalEchoServerHandler());
}
});
// Start the server.
sb.bind(addr).sync();
Runnable r = new Runnable() {
public void run() {
try {
Bootstrap cb = new Bootstrap();
cb.group(clientGroup)
.channel(LocalChannel.class)
.handler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(
new LoggingHandler(LogLevel.INFO),
new LocalEchoClientHandler());
}
});
// Start the client.
Channel ch = cb.connect(addr).sync().channel();
// Read commands from the stdin.
System.out.println("Enter text (quit to end)");
ChannelFuture lastWriteFuture = null;
BufferedReader in = new BufferedReader(new InputStreamReader(new ByteArrayInputStream("foo\nbar\nbaz".getBytes())));
for (;;) {
String line = in.readLine();
if (line == null || "quit".equalsIgnoreCase(line)) {
break;
}
// Sends the received line to the server.
lastWriteFuture = ch.writeAndFlush(line);
}
// Wait until all messages are flushed before closing the channel.
if (lastWriteFuture != null) {
lastWriteFuture.awaitUninterruptibly();
}
}
catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
};
List<Thread> aList = new ArrayList<Thread>();
for (int i = 0; i < 50; i++) {
Thread t = new Thread(r);
t.start();
aList.add(t);
}
for (Thread t : aList) {
t.join();
}
} finally {
serverGroup.shutdownGracefully();
clientGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
new LocalEcho("1").run();
}
public class LocalEchoClientHandler extends SimpleChannelInboundHandler<Object> {
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
// Print as received
System.out.println(msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
public class LocalEchoServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// Write back as received
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