From 3920158bc0c50fdce81d4db7012338b89af514bd Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 23 Jul 2009 09:05:53 +0000 Subject: [PATCH] Polished all examples --- .../netty/example/discard/DiscardClient.java | 19 ++--- .../netty/example/discard/DiscardServer.java | 15 ++-- .../jboss/netty/example/echo/EchoClient.java | 17 ++--- .../jboss/netty/example/echo/EchoHandler.java | 2 +- .../jboss/netty/example/echo/EchoServer.java | 15 ++-- .../example/factorial/BigIntegerDecoder.java | 1 + .../example/factorial/FactorialClient.java | 20 +++--- .../example/factorial/FactorialServer.java | 14 ++-- .../http/file/HttpStaticFileServer.java | 1 + .../netty/example/http/snoop/HttpClient.java | 21 +++--- .../netty/example/http/snoop/HttpServer.java | 13 ++-- .../tunnel/HttpTunnelingClientExample.java | 12 +++- .../netty/example/local/LocalExample.java | 63 ++++++++--------- ...ple.java => LocalExampleMultthreaded.java} | 70 ++++++++----------- .../local/LocalServerPipelineFactory.java | 27 +++---- .../example/localtime/LocalTimeClient.java | 16 ++--- .../example/localtime/LocalTimeServer.java | 14 ++-- .../example/objectecho/ObjectEchoClient.java | 14 ++-- .../example/objectecho/ObjectEchoServer.java | 14 ++-- .../netty/example/proxy/HexDumpProxy.java | 2 + .../example/securechat/SecureChatClient.java | 17 ++--- .../example/securechat/SecureChatServer.java | 12 ++-- .../netty/example/telnet/TelnetClient.java | 17 ++--- .../netty/example/telnet/TelnetServer.java | 13 ++-- 24 files changed, 179 insertions(+), 250 deletions(-) rename src/main/java/org/jboss/netty/example/local/{LocalExampleMultiple.java => LocalExampleMultthreaded.java} (60%) diff --git a/src/main/java/org/jboss/netty/example/discard/DiscardClient.java b/src/main/java/org/jboss/netty/example/discard/DiscardClient.java index d9fa3a0d07..f831f9b857 100644 --- a/src/main/java/org/jboss/netty/example/discard/DiscardClient.java +++ b/src/main/java/org/jboss/netty/example/discard/DiscardClient.java @@ -26,7 +26,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ClientBootstrap; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; @@ -53,7 +52,6 @@ public class DiscardClient { String host = args[0]; int port = Integer.parseInt(args[1]); int firstMessageSize; - if (args.length == 3) { firstMessageSize = Integer.parseInt(args[2]); } else { @@ -61,19 +59,14 @@ public class DiscardClient { } // Configure the client. - ChannelFactory factory = - new NioClientSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); + ClientBootstrap bootstrap = new ClientBootstrap( + new NioClientSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); - ClientBootstrap bootstrap = new ClientBootstrap(factory); + // Set up the default event pipeline. DiscardClientHandler handler = new DiscardClientHandler(firstMessageSize); - - //bootstrap.getPipeline().addLast("executor", new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(16, 0, 0))); bootstrap.getPipeline().addLast("handler", handler); - bootstrap.setOption("tcpNoDelay", true); - bootstrap.setOption("keepAlive", true); - //bootstrap.setOption("bufferFactory", DirectChannelBufferFactory.getInstance()); // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); @@ -82,6 +75,6 @@ public class DiscardClient { future.getChannel().getCloseFuture().awaitUninterruptibly(); // Shut down thread pools to exit. - factory.releaseExternalResources(); + bootstrap.releaseExternalResources(); } } diff --git a/src/main/java/org/jboss/netty/example/discard/DiscardServer.java b/src/main/java/org/jboss/netty/example/discard/DiscardServer.java index 230b9554f8..8c6e86c1d6 100644 --- a/src/main/java/org/jboss/netty/example/discard/DiscardServer.java +++ b/src/main/java/org/jboss/netty/example/discard/DiscardServer.java @@ -26,7 +26,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; /** @@ -41,18 +40,14 @@ public class DiscardServer { public static void main(String[] args) throws Exception { // Configure the server. - ChannelFactory factory = - new NioServerSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); + ServerBootstrap bootstrap = new ServerBootstrap( + new NioServerSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); - ServerBootstrap bootstrap = new ServerBootstrap(factory); + // Set up the default event pipeline. DiscardServerHandler handler = new DiscardServerHandler(); - bootstrap.getPipeline().addLast("handler", handler); - bootstrap.setOption("child.tcpNoDelay", true); - bootstrap.setOption("child.keepAlive", true); - //bootstrap.setOption("child.bufferFactory", DirectChannelBufferFactory.getInstance()); // Bind and start to accept incoming connections. bootstrap.bind(new InetSocketAddress(8080)); diff --git a/src/main/java/org/jboss/netty/example/echo/EchoClient.java b/src/main/java/org/jboss/netty/example/echo/EchoClient.java index d53791f542..b63c4e8373 100644 --- a/src/main/java/org/jboss/netty/example/echo/EchoClient.java +++ b/src/main/java/org/jboss/netty/example/echo/EchoClient.java @@ -26,7 +26,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ClientBootstrap; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; @@ -55,7 +54,6 @@ public class EchoClient { String host = args[0]; int port = Integer.parseInt(args[1]); int firstMessageSize; - if (args.length == 3) { firstMessageSize = Integer.parseInt(args[2]); } else { @@ -63,17 +61,14 @@ public class EchoClient { } // Configure the client. - ChannelFactory factory = - new NioClientSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); + ClientBootstrap bootstrap = new ClientBootstrap( + new NioClientSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); - ClientBootstrap bootstrap = new ClientBootstrap(factory); + // Set up the default event pipeline. EchoHandler handler = new EchoHandler(firstMessageSize); - bootstrap.getPipeline().addLast("handler", handler); - bootstrap.setOption("tcpNoDelay", true); - bootstrap.setOption("keepAlive", true); // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); @@ -82,6 +77,6 @@ public class EchoClient { future.getChannel().getCloseFuture().awaitUninterruptibly(); // Shut down thread pools to exit. - factory.releaseExternalResources(); + bootstrap.releaseExternalResources(); } } diff --git a/src/main/java/org/jboss/netty/example/echo/EchoHandler.java b/src/main/java/org/jboss/netty/example/echo/EchoHandler.java index 3e805ff805..e33ccbb4d4 100644 --- a/src/main/java/org/jboss/netty/example/echo/EchoHandler.java +++ b/src/main/java/org/jboss/netty/example/echo/EchoHandler.java @@ -83,7 +83,7 @@ public class EchoHandler extends SimpleChannelUpstreamHandler { ChannelHandlerContext ctx, ChannelStateEvent e) { // Send the first message. Server will not send anything here // because the firstMessage's capacity is 0. - //e.getChannel().write(firstMessage); + e.getChannel().write(firstMessage); } @Override diff --git a/src/main/java/org/jboss/netty/example/echo/EchoServer.java b/src/main/java/org/jboss/netty/example/echo/EchoServer.java index 5a3dcab60d..bf6792217e 100644 --- a/src/main/java/org/jboss/netty/example/echo/EchoServer.java +++ b/src/main/java/org/jboss/netty/example/echo/EchoServer.java @@ -26,7 +26,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; /** @@ -42,21 +41,19 @@ public class EchoServer { public static void main(String[] args) throws Exception { // Configure the server. - ChannelFactory factory = - new NioServerSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); + ServerBootstrap bootstrap = new ServerBootstrap( + new NioServerSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); - ServerBootstrap bootstrap = new ServerBootstrap(factory); + // Set up the default event pipeline. EchoHandler handler = new EchoHandler(); - bootstrap.getPipeline().addLast("handler", handler); - bootstrap.setOption("backlog", 4096); // Bind and start to accept incoming connections. bootstrap.bind(new InetSocketAddress(8080)); // Start performance monitor. - //new ThroughputMonitor(handler).start(); + new ThroughputMonitor(handler).start(); } } diff --git a/src/main/java/org/jboss/netty/example/factorial/BigIntegerDecoder.java b/src/main/java/org/jboss/netty/example/factorial/BigIntegerDecoder.java index 90efdb068b..ebde8c83ee 100644 --- a/src/main/java/org/jboss/netty/example/factorial/BigIntegerDecoder.java +++ b/src/main/java/org/jboss/netty/example/factorial/BigIntegerDecoder.java @@ -48,6 +48,7 @@ public class BigIntegerDecoder extends FrameDecoder { if (buffer.readableBytes() < 4) { return null; } + int dataLength = buffer.getInt(buffer.readerIndex()); // Wait until the whole data is available. diff --git a/src/main/java/org/jboss/netty/example/factorial/FactorialClient.java b/src/main/java/org/jboss/netty/example/factorial/FactorialClient.java index e62bb2aa91..7a93364b67 100644 --- a/src/main/java/org/jboss/netty/example/factorial/FactorialClient.java +++ b/src/main/java/org/jboss/netty/example/factorial/FactorialClient.java @@ -27,7 +27,6 @@ import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; @@ -59,17 +58,14 @@ public class FactorialClient { throw new IllegalArgumentException("count must be a positive integer."); } - // Set up. - ChannelFactory factory = - new NioClientSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); - - ClientBootstrap bootstrap = new ClientBootstrap(factory); + // Configure the client. + ClientBootstrap bootstrap = new ClientBootstrap( + new NioClientSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); + // Set up the event pipeline factory. bootstrap.setPipelineFactory(new FactorialClientPipelineFactory(count)); - bootstrap.setOption("tcpNoDelay", true); - bootstrap.setOption("keepAlive", true); // Make a new connection. ChannelFuture connectFuture = @@ -83,10 +79,10 @@ public class FactorialClient { (FactorialClientHandler) channel.getPipeline().getLast(); // Print out the answer. - System.out.format( + System.err.format( "Factorial of %,d is: %,d", count, handler.getFactorial()); // Shut down all thread pools to exit. - factory.releaseExternalResources(); + bootstrap.releaseExternalResources(); } } diff --git a/src/main/java/org/jboss/netty/example/factorial/FactorialServer.java b/src/main/java/org/jboss/netty/example/factorial/FactorialServer.java index 5a1a12719b..b171551d4d 100644 --- a/src/main/java/org/jboss/netty/example/factorial/FactorialServer.java +++ b/src/main/java/org/jboss/netty/example/factorial/FactorialServer.java @@ -26,7 +26,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; /** @@ -42,16 +41,13 @@ public class FactorialServer { public static void main(String[] args) throws Exception { // Configure the server. - ChannelFactory factory = - new NioServerSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); - - ServerBootstrap bootstrap = new ServerBootstrap(factory); + ServerBootstrap bootstrap = new ServerBootstrap( + new NioServerSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); + // Set up the event pipeline factory. bootstrap.setPipelineFactory(new FactorialServerPipelineFactory()); - bootstrap.setOption("child.tcpNoDelay", true); - bootstrap.setOption("child.keepAlive", true); // Bind and start to accept incoming connections. bootstrap.bind(new InetSocketAddress(8080)); diff --git a/src/main/java/org/jboss/netty/example/http/file/HttpStaticFileServer.java b/src/main/java/org/jboss/netty/example/http/file/HttpStaticFileServer.java index ac8aefe433..009f33fb12 100644 --- a/src/main/java/org/jboss/netty/example/http/file/HttpStaticFileServer.java +++ b/src/main/java/org/jboss/netty/example/http/file/HttpStaticFileServer.java @@ -40,6 +40,7 @@ public class HttpStaticFileServer { Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); + // Set up the event pipeline factory. bootstrap.setPipelineFactory(new HttpStaticFileServerPipelineFactory()); // Bind and start to accept incoming connections. diff --git a/src/main/java/org/jboss/netty/example/http/snoop/HttpClient.java b/src/main/java/org/jboss/netty/example/http/snoop/HttpClient.java index e03e78dc76..b4c3d0a2b9 100644 --- a/src/main/java/org/jboss/netty/example/http/snoop/HttpClient.java +++ b/src/main/java/org/jboss/netty/example/http/snoop/HttpClient.java @@ -28,7 +28,6 @@ import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.handler.codec.http.CookieEncoder; @@ -68,12 +67,12 @@ public class HttpClient { } // Configure the client. - ChannelFactory factory = - new NioClientSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); + ClientBootstrap bootstrap = new ClientBootstrap( + new NioClientSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); - ClientBootstrap bootstrap = new ClientBootstrap(factory); + // Set up the event pipeline factory. bootstrap.setPipelineFactory(new HttpClientPipelineFactory()); // Start the connection attempt. @@ -83,25 +82,25 @@ public class HttpClient { Channel channel = future.awaitUninterruptibly().getChannel(); if (!future.isSuccess()) { future.getCause().printStackTrace(); - factory.releaseExternalResources(); + bootstrap.releaseExternalResources(); return; } // Send the HTTP request. HttpRequest request = new DefaultHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toASCIIString()); - request.addHeader(HttpHeaders.Names.HOST, host); - request.addHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); + request.setHeader(HttpHeaders.Names.HOST, host); + request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); CookieEncoder httpCookieEncoder = new CookieEncoder(false); httpCookieEncoder.addCookie("my-cookie", "foo"); httpCookieEncoder.addCookie("another-cookie", "bar"); - request.addHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode()); + request.setHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode()); channel.write(request); // Wait for the server to close the connection. channel.getCloseFuture().awaitUninterruptibly(); // Shut down executor threads to exit. - factory.releaseExternalResources(); + bootstrap.releaseExternalResources(); } } \ No newline at end of file diff --git a/src/main/java/org/jboss/netty/example/http/snoop/HttpServer.java b/src/main/java/org/jboss/netty/example/http/snoop/HttpServer.java index 47d3093528..9982ab9551 100644 --- a/src/main/java/org/jboss/netty/example/http/snoop/HttpServer.java +++ b/src/main/java/org/jboss/netty/example/http/snoop/HttpServer.java @@ -26,7 +26,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; /** @@ -39,15 +38,13 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; public class HttpServer { public static void main(String[] args) { // Configure the server. - ChannelFactory factory = - new NioServerSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); + ServerBootstrap bootstrap = new ServerBootstrap( + new NioServerSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); - ServerBootstrap bootstrap = new ServerBootstrap(factory); + // Set up the event pipeline factory. bootstrap.setPipelineFactory(new HttpServerPipelineFactory()); - bootstrap.setOption("child.tcpNoDelay", true); - bootstrap.setOption("child.keepAlive", true); // Bind and start to accept incoming connections. bootstrap.bind(new InetSocketAddress(8080)); diff --git a/src/main/java/org/jboss/netty/example/http/tunnel/HttpTunnelingClientExample.java b/src/main/java/org/jboss/netty/example/http/tunnel/HttpTunnelingClientExample.java index 6acb20cfdf..d310372ac7 100644 --- a/src/main/java/org/jboss/netty/example/http/tunnel/HttpTunnelingClientExample.java +++ b/src/main/java/org/jboss/netty/example/http/tunnel/HttpTunnelingClientExample.java @@ -64,13 +64,17 @@ public class HttpTunnelingClientExample { URI uri = new URI(args[0]); String scheme = uri.getScheme() == null? "http" : uri.getScheme(); + // Configure the client. ClientBootstrap b = new ClientBootstrap( new HttpTunnelingClientSocketChannelFactory( new OioClientSocketChannelFactory(Executors.newCachedThreadPool()))); + + // Set up the default event pipeline. b.getPipeline().addLast("decoder", new StringDecoder()); b.getPipeline().addLast("encoder", new StringEncoder()); b.getPipeline().addLast("handler", new LoggingHandler(InternalLogLevel.INFO)); + // Set additional options required by the HTTP tunneling transport. b.setOption("serverName", uri.getHost()); b.setOption("serverPath", uri.getRawPath()); @@ -83,10 +87,13 @@ public class HttpTunnelingClientExample { return; } - ChannelFuture channelFuture = b.connect(new InetSocketAddress(uri.getHost(), uri.getPort())); + // Make the connection attempt. + ChannelFuture channelFuture = b.connect( + new InetSocketAddress(uri.getHost(), uri.getPort())); channelFuture.awaitUninterruptibly(); - System.out.println("Enter text ('quit' to exit)"); + // Read commands from the stdin. + System.out.println("Enter text ('quit' to exit)"); ChannelFuture lastWriteFuture = null; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); for (; ;) { @@ -108,6 +115,7 @@ public class HttpTunnelingClientExample { // Wait until the connection is closed or the connection attempt fails. channelFuture.getChannel().getCloseFuture().awaitUninterruptibly(); + // Shut down all threads. b.releaseExternalResources(); } } diff --git a/src/main/java/org/jboss/netty/example/local/LocalExample.java b/src/main/java/org/jboss/netty/example/local/LocalExample.java index 42fb54a6f6..c1a1c7f3c7 100644 --- a/src/main/java/org/jboss/netty/example/local/LocalExample.java +++ b/src/main/java/org/jboss/netty/example/local/LocalExample.java @@ -27,19 +27,15 @@ import java.io.InputStreamReader; import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelDownstreamHandler; -import org.jboss.netty.channel.ChannelEvent; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelFuture; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.channel.ChannelPipelineCoverage; -import org.jboss.netty.channel.ChannelUpstreamHandler; import org.jboss.netty.channel.local.DefaultLocalClientChannelFactory; import org.jboss.netty.channel.local.DefaultLocalServerChannelFactory; import org.jboss.netty.channel.local.LocalAddress; import org.jboss.netty.example.echo.EchoHandler; import org.jboss.netty.handler.codec.string.StringDecoder; import org.jboss.netty.handler.codec.string.StringEncoder; +import org.jboss.netty.handler.logging.LoggingHandler; +import org.jboss.netty.logging.InternalLogLevel; /** * @author The Netty Project (netty-dev@lists.jboss.org) @@ -48,24 +44,35 @@ import org.jboss.netty.handler.codec.string.StringEncoder; */ public class LocalExample { public static void main(String[] args) throws Exception { - ChannelFactory factory = new DefaultLocalServerChannelFactory(); - ServerBootstrap bootstrap = new ServerBootstrap(factory); - EchoHandler handler = new EchoHandler(); + // Address to bind on / connect to. LocalAddress socketAddress = new LocalAddress("1"); - bootstrap.getPipeline().addLast("handler", handler); - bootstrap.bind(socketAddress); - ChannelFactory channelFactory = new DefaultLocalClientChannelFactory(); - ClientBootstrap clientBootstrap = new ClientBootstrap(channelFactory); - clientBootstrap.getPipeline().addLast("decoder", new StringDecoder()); - clientBootstrap.getPipeline().addLast("encoder", new StringEncoder()); - clientBootstrap.getPipeline().addLast("handler", new PrintHandler()); - ChannelFuture channelFuture = clientBootstrap.connect(socketAddress); + // Configure the server. + ServerBootstrap sb = new ServerBootstrap( + new DefaultLocalServerChannelFactory()); + + // Set up the default server-side event pipeline. + EchoHandler handler = new EchoHandler(); + sb.getPipeline().addLast("handler", handler); + + // Start up the server. + sb.bind(socketAddress); + + // Configure the client. + ClientBootstrap cb = new ClientBootstrap( + new DefaultLocalClientChannelFactory()); + + // Set up the default client-side event pipeline. + cb.getPipeline().addLast("decoder", new StringDecoder()); + cb.getPipeline().addLast("encoder", new StringEncoder()); + cb.getPipeline().addLast("handler", new LoggingHandler(InternalLogLevel.INFO)); + + // Make the connection attempt to the server. + ChannelFuture channelFuture = cb.connect(socketAddress); channelFuture.awaitUninterruptibly(); - System.out.println("Enter text (quit to end)"); - // Read commands from the stdin. + System.out.println("Enter text (quit to end)"); ChannelFuture lastWriteFuture = null; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); for (; ;) { @@ -83,22 +90,12 @@ public class LocalExample { lastWriteFuture.awaitUninterruptibly(); } channelFuture.getChannel().close(); + // Wait until the connection is closed or the connection attempt fails. channelFuture.getChannel().getCloseFuture().awaitUninterruptibly(); - } - @ChannelPipelineCoverage("all") - static class PrintHandler implements ChannelUpstreamHandler, ChannelDownstreamHandler { - public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) - throws Exception { - System.err.println(e); - ctx.sendUpstream(e); - } - - public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) - throws Exception { - System.err.println(e); - ctx.sendDownstream(e); - } + // Release all resources used by the local transport. + cb.releaseExternalResources(); + sb.releaseExternalResources(); } } diff --git a/src/main/java/org/jboss/netty/example/local/LocalExampleMultiple.java b/src/main/java/org/jboss/netty/example/local/LocalExampleMultthreaded.java similarity index 60% rename from src/main/java/org/jboss/netty/example/local/LocalExampleMultiple.java rename to src/main/java/org/jboss/netty/example/local/LocalExampleMultthreaded.java index 92c1951e83..fb10d81c57 100644 --- a/src/main/java/org/jboss/netty/example/local/LocalExampleMultiple.java +++ b/src/main/java/org/jboss/netty/example/local/LocalExampleMultthreaded.java @@ -26,50 +26,50 @@ import java.util.concurrent.TimeUnit; import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelDownstreamHandler; -import org.jboss.netty.channel.ChannelEvent; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelFuture; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.channel.ChannelPipelineCoverage; -import org.jboss.netty.channel.ChannelUpstreamHandler; import org.jboss.netty.channel.local.DefaultLocalClientChannelFactory; import org.jboss.netty.channel.local.DefaultLocalServerChannelFactory; import org.jboss.netty.channel.local.LocalAddress; import org.jboss.netty.handler.codec.string.StringDecoder; import org.jboss.netty.handler.codec.string.StringEncoder; import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor; +import org.jboss.netty.handler.logging.LoggingHandler; +import org.jboss.netty.logging.InternalLogLevel; /** * @author The Netty Project (netty-dev@lists.jboss.org) * @author Andy Taylor (andy.taylor@jboss.org) - * @version $Rev: 1482 $, $Date: 2009-06-19 19:48:17 +0200 (ven., 19 juin 2009) $ + * @author Frederic Bregier (fredbregier@free.fr) + * @version $Rev$, $Date$ */ -public class LocalExampleMultiple { +public class LocalExampleMultthreaded { + public static void main(String[] args) throws Exception { - OrderedMemoryAwareThreadPoolExecutor orderedMemoryAwareThreadPoolExecutor = + LocalAddress socketAddress = new LocalAddress("1"); + + OrderedMemoryAwareThreadPoolExecutor eventExecutor = new OrderedMemoryAwareThreadPoolExecutor( 5, 1000000, 10000000, 100, TimeUnit.MILLISECONDS); - ChannelFactory factory = new DefaultLocalServerChannelFactory(); - ServerBootstrap bootstrap = new ServerBootstrap(factory); - bootstrap.setPipelineFactory(new LocalServerPipelineFactory(orderedMemoryAwareThreadPoolExecutor)); - LocalAddress socketAddress = new LocalAddress("1"); - bootstrap.bind(socketAddress); - ChannelFactory channelFactory = new DefaultLocalClientChannelFactory(); - ClientBootstrap clientBootstrap = new ClientBootstrap(channelFactory); - clientBootstrap.getPipeline().addLast("decoder", new StringDecoder()); - clientBootstrap.getPipeline().addLast("encoder", new StringEncoder()); - clientBootstrap.getPipeline().addLast("handler", new PrintHandler()); + ServerBootstrap sb = new ServerBootstrap( + new DefaultLocalServerChannelFactory()); + + sb.setPipelineFactory(new LocalServerPipelineFactory(eventExecutor)); + sb.bind(socketAddress); + + ClientBootstrap cb = new ClientBootstrap( + new DefaultLocalClientChannelFactory()); + + cb.getPipeline().addLast("decoder", new StringDecoder()); + cb.getPipeline().addLast("encoder", new StringEncoder()); + cb.getPipeline().addLast("handler", new LoggingHandler(InternalLogLevel.INFO)); // Read commands from array - String []commands = { - "First", "Second", "Third", "quit" - }; + String[] commands = { "First", "Second", "Third", "quit" }; for (int j = 0; j < 5 ; j++) { System.err.println("Start "+j); - ChannelFuture channelFuture = clientBootstrap.connect(socketAddress); + ChannelFuture channelFuture = cb.connect(socketAddress); channelFuture.awaitUninterruptibly(); if (! channelFuture.isSuccess()) { System.err.println("CANNOT CONNECT"); @@ -91,24 +91,10 @@ public class LocalExampleMultiple { channelFuture.getChannel().getCloseFuture().awaitUninterruptibly(); System.err.println("End "+j); } - clientBootstrap.releaseExternalResources(); - bootstrap.releaseExternalResources(); - orderedMemoryAwareThreadPoolExecutor.shutdownNow(); + + // Release all resources + cb.releaseExternalResources(); + sb.releaseExternalResources(); + eventExecutor.shutdownNow(); } - - @ChannelPipelineCoverage("all") - static class PrintHandler implements ChannelUpstreamHandler, ChannelDownstreamHandler { - public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) - throws Exception { - System.err.println(e); - ctx.sendUpstream(e); - } - - public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) - throws Exception { - System.err.println(e); - ctx.sendDownstream(e); - } - } - } diff --git a/src/main/java/org/jboss/netty/example/local/LocalServerPipelineFactory.java b/src/main/java/org/jboss/netty/example/local/LocalServerPipelineFactory.java index 91591aa125..afc2c70fbc 100644 --- a/src/main/java/org/jboss/netty/example/local/LocalServerPipelineFactory.java +++ b/src/main/java/org/jboss/netty/example/local/LocalServerPipelineFactory.java @@ -22,6 +22,8 @@ */ package org.jboss.netty.example.local; +import java.util.concurrent.Executor; + import org.jboss.netty.channel.ChannelDownstreamHandler; import org.jboss.netty.channel.ChannelEvent; import org.jboss.netty.channel.ChannelHandlerContext; @@ -34,24 +36,26 @@ import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.handler.codec.string.StringDecoder; import org.jboss.netty.handler.codec.string.StringEncoder; import org.jboss.netty.handler.execution.ExecutionHandler; -import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor; /** - * @author frederic - * + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Andy Taylor (andy.taylor@jboss.org) + * @author Frederic Bregier (fredbregier@free.fr) + * @version $Rev$, $Date$ */ public class LocalServerPipelineFactory implements ChannelPipelineFactory { - OrderedMemoryAwareThreadPoolExecutor orderedMemoryAwareThreadPoolExecutor; - public LocalServerPipelineFactory(OrderedMemoryAwareThreadPoolExecutor orderedMemoryAwareThreadPoolExecutor) { - this.orderedMemoryAwareThreadPoolExecutor = - orderedMemoryAwareThreadPoolExecutor; + + private final Executor eventExecutor; + + public LocalServerPipelineFactory(Executor eventExecutor) { + this.eventExecutor = eventExecutor; } + public ChannelPipeline getPipeline() throws Exception { final ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); - pipeline.addLast("pipelineExecutor", new ExecutionHandler( - orderedMemoryAwareThreadPoolExecutor)); + pipeline.addLast("executor", new ExecutionHandler(eventExecutor)); pipeline.addLast("handler", new EchoCloseServerHandler()); return pipeline; } @@ -64,7 +68,6 @@ public class LocalServerPipelineFactory implements ChannelPipelineFactory { final MessageEvent evt = (MessageEvent) e; String msg = (String) evt.getMessage(); if (msg.equalsIgnoreCase("quit")) { - // TRY COMMENT HERE, then it works Channels.close(e.getChannel()); return; } @@ -72,13 +75,11 @@ public class LocalServerPipelineFactory implements ChannelPipelineFactory { ctx.sendUpstream(e); } - public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) - throws Exception { + public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) { if (e instanceof MessageEvent) { final MessageEvent evt = (MessageEvent) e; String msg = (String) evt.getMessage(); if (msg.equalsIgnoreCase("quit")) { - // COMMENT OR NOT, NO PROBLEM Channels.close(e.getChannel()); return; } diff --git a/src/main/java/org/jboss/netty/example/localtime/LocalTimeClient.java b/src/main/java/org/jboss/netty/example/localtime/LocalTimeClient.java index f445c49113..35b0c98617 100644 --- a/src/main/java/org/jboss/netty/example/localtime/LocalTimeClient.java +++ b/src/main/java/org/jboss/netty/example/localtime/LocalTimeClient.java @@ -31,7 +31,6 @@ import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; @@ -62,16 +61,13 @@ public class LocalTimeClient { } // Set up. - ChannelFactory factory = - new NioClientSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); - - ClientBootstrap bootstrap = new ClientBootstrap(factory); + ClientBootstrap bootstrap = new ClientBootstrap( + new NioClientSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); + // Configure the event pipeline factory. bootstrap.setPipelineFactory(new LocalTimeClientPipelineFactory()); - bootstrap.setOption("tcpNoDelay", true); - bootstrap.setOption("keepAlive", true); // Make a new connection. ChannelFuture connectFuture = @@ -90,7 +86,7 @@ public class LocalTimeClient { channel.close().awaitUninterruptibly(); // Shut down all thread pools to exit. - factory.releaseExternalResources(); + bootstrap.releaseExternalResources(); // Print the response at last but not least. Iterator i1 = cities.iterator(); diff --git a/src/main/java/org/jboss/netty/example/localtime/LocalTimeServer.java b/src/main/java/org/jboss/netty/example/localtime/LocalTimeServer.java index 7ff85155c7..49cb7472a7 100644 --- a/src/main/java/org/jboss/netty/example/localtime/LocalTimeServer.java +++ b/src/main/java/org/jboss/netty/example/localtime/LocalTimeServer.java @@ -26,7 +26,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; /** @@ -42,16 +41,13 @@ public class LocalTimeServer { public static void main(String[] args) throws Exception { // Configure the server. - ChannelFactory factory = - new NioServerSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); - - ServerBootstrap bootstrap = new ServerBootstrap(factory); + ServerBootstrap bootstrap = new ServerBootstrap( + new NioServerSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); + // Set up the event pipeline factory. bootstrap.setPipelineFactory(new LocalTimeServerPipelineFactory()); - bootstrap.setOption("child.tcpNoDelay", true); - bootstrap.setOption("child.keepAlive", true); // Bind and start to accept incoming connections. bootstrap.bind(new InetSocketAddress(8080)); diff --git a/src/main/java/org/jboss/netty/example/objectecho/ObjectEchoClient.java b/src/main/java/org/jboss/netty/example/objectecho/ObjectEchoClient.java index cf8aea53b6..e917906dca 100644 --- a/src/main/java/org/jboss/netty/example/objectecho/ObjectEchoClient.java +++ b/src/main/java/org/jboss/netty/example/objectecho/ObjectEchoClient.java @@ -26,7 +26,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ClientBootstrap; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.example.echo.EchoClient; @@ -61,17 +60,14 @@ public class ObjectEchoClient { } // Configure the client. - ChannelFactory factory = - new NioClientSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); + ClientBootstrap bootstrap = new ClientBootstrap( + new NioClientSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); - ClientBootstrap bootstrap = new ClientBootstrap(factory); + // Set up the default event pipeline. ObjectEchoHandler handler = new ObjectEchoHandler(firstMessageSize); - bootstrap.getPipeline().addLast("handler", handler); - bootstrap.setOption("tcpNoDelay", true); - bootstrap.setOption("keepAlive", true); // Start the connection attempt. bootstrap.connect(new InetSocketAddress(host, port)); diff --git a/src/main/java/org/jboss/netty/example/objectecho/ObjectEchoServer.java b/src/main/java/org/jboss/netty/example/objectecho/ObjectEchoServer.java index 656954f933..5a53c1751d 100644 --- a/src/main/java/org/jboss/netty/example/objectecho/ObjectEchoServer.java +++ b/src/main/java/org/jboss/netty/example/objectecho/ObjectEchoServer.java @@ -26,7 +26,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.example.echo.EchoServer; @@ -42,17 +41,14 @@ public class ObjectEchoServer { public static void main(String[] args) throws Exception { // Configure the server. - ChannelFactory factory = - new NioServerSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); + ServerBootstrap bootstrap = new ServerBootstrap( + new NioServerSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); - ServerBootstrap bootstrap = new ServerBootstrap(factory); + // Set up the default event pipeline. ObjectEchoHandler handler = new ObjectEchoHandler(); - bootstrap.getPipeline().addLast("handler", handler); - bootstrap.setOption("child.tcpNoDelay", true); - bootstrap.setOption("child.keepAlive", true); // Bind and start to accept incoming connections. bootstrap.bind(new InetSocketAddress(8080)); diff --git a/src/main/java/org/jboss/netty/example/proxy/HexDumpProxy.java b/src/main/java/org/jboss/netty/example/proxy/HexDumpProxy.java index 8dacdae30a..aebf6ae456 100644 --- a/src/main/java/org/jboss/netty/example/proxy/HexDumpProxy.java +++ b/src/main/java/org/jboss/netty/example/proxy/HexDumpProxy.java @@ -60,6 +60,8 @@ public class HexDumpProxy { Executor executor = Executors.newCachedThreadPool(); ServerBootstrap sb = new ServerBootstrap( new NioServerSocketChannelFactory(executor, executor)); + + // Set up the event pipeline factory. ClientSocketChannelFactory cf = new NioClientSocketChannelFactory(executor, executor); diff --git a/src/main/java/org/jboss/netty/example/securechat/SecureChatClient.java b/src/main/java/org/jboss/netty/example/securechat/SecureChatClient.java index 8bcd949394..b6d5380247 100644 --- a/src/main/java/org/jboss/netty/example/securechat/SecureChatClient.java +++ b/src/main/java/org/jboss/netty/example/securechat/SecureChatClient.java @@ -29,7 +29,6 @@ import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.example.telnet.TelnetClient; @@ -59,17 +58,13 @@ public class SecureChatClient { int port = Integer.parseInt(args[1]); // Configure the client. - ChannelFactory factory = - new NioClientSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); + ClientBootstrap bootstrap = new ClientBootstrap( + new NioClientSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); - ClientBootstrap bootstrap = new ClientBootstrap(factory); SecureChatClientHandler handler = new SecureChatClientHandler(); - bootstrap.setPipelineFactory(new SecureChatPipelineFactory(handler)); - bootstrap.setOption("tcpNoDelay", true); - bootstrap.setOption("keepAlive", true); // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); @@ -78,7 +73,7 @@ public class SecureChatClient { Channel channel = future.awaitUninterruptibly().getChannel(); if (!future.isSuccess()) { future.getCause().printStackTrace(); - factory.releaseExternalResources(); + bootstrap.releaseExternalResources(); return; } @@ -112,6 +107,6 @@ public class SecureChatClient { channel.close().awaitUninterruptibly(); // Shut down all thread pools to exit. - factory.releaseExternalResources(); + bootstrap.releaseExternalResources(); } } diff --git a/src/main/java/org/jboss/netty/example/securechat/SecureChatServer.java b/src/main/java/org/jboss/netty/example/securechat/SecureChatServer.java index 0ee189cb94..f73479882a 100644 --- a/src/main/java/org/jboss/netty/example/securechat/SecureChatServer.java +++ b/src/main/java/org/jboss/netty/example/securechat/SecureChatServer.java @@ -26,7 +26,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.example.telnet.TelnetServer; @@ -42,17 +41,14 @@ public class SecureChatServer { public static void main(String[] args) throws Exception { // Configure the server. - ChannelFactory factory = - new NioServerSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); + ServerBootstrap bootstrap = new ServerBootstrap( + new NioServerSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); - ServerBootstrap bootstrap = new ServerBootstrap(factory); SecureChatServerHandler handler = new SecureChatServerHandler(); bootstrap.setPipelineFactory(new SecureChatPipelineFactory(handler)); - bootstrap.setOption("child.tcpNoDelay", true); - bootstrap.setOption("child.keepAlive", true); // Bind and start to accept incoming connections. bootstrap.bind(new InetSocketAddress(8080)); diff --git a/src/main/java/org/jboss/netty/example/telnet/TelnetClient.java b/src/main/java/org/jboss/netty/example/telnet/TelnetClient.java index e156854168..4e1a4de6ae 100644 --- a/src/main/java/org/jboss/netty/example/telnet/TelnetClient.java +++ b/src/main/java/org/jboss/netty/example/telnet/TelnetClient.java @@ -29,7 +29,6 @@ import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; @@ -57,17 +56,13 @@ public class TelnetClient { int port = Integer.parseInt(args[1]); // Configure the client. - ChannelFactory factory = - new NioClientSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); + ClientBootstrap bootstrap = new ClientBootstrap( + new NioClientSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); - ClientBootstrap bootstrap = new ClientBootstrap(factory); TelnetClientHandler handler = new TelnetClientHandler(); - bootstrap.setPipelineFactory(new TelnetPipelineFactory(handler)); - bootstrap.setOption("tcpNoDelay", true); - bootstrap.setOption("keepAlive", true); // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); @@ -76,7 +71,7 @@ public class TelnetClient { Channel channel = future.awaitUninterruptibly().getChannel(); if (!future.isSuccess()) { future.getCause().printStackTrace(); - factory.releaseExternalResources(); + bootstrap.releaseExternalResources(); return; } @@ -110,6 +105,6 @@ public class TelnetClient { channel.close().awaitUninterruptibly(); // Shut down all thread pools to exit. - factory.releaseExternalResources(); + bootstrap.releaseExternalResources(); } } diff --git a/src/main/java/org/jboss/netty/example/telnet/TelnetServer.java b/src/main/java/org/jboss/netty/example/telnet/TelnetServer.java index 5a0d302e9a..05113bfd42 100644 --- a/src/main/java/org/jboss/netty/example/telnet/TelnetServer.java +++ b/src/main/java/org/jboss/netty/example/telnet/TelnetServer.java @@ -26,7 +26,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; /** @@ -41,17 +40,13 @@ public class TelnetServer { public static void main(String[] args) throws Exception { // Configure the server. - ChannelFactory factory = - new NioServerSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); + ServerBootstrap bootstrap = new ServerBootstrap( + new NioServerSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); - ServerBootstrap bootstrap = new ServerBootstrap(factory); TelnetServerHandler handler = new TelnetServerHandler(); - bootstrap.setPipelineFactory(new TelnetPipelineFactory(handler)); - bootstrap.setOption("child.tcpNoDelay", true); - bootstrap.setOption("child.keepAlive", true); // Bind and start to accept incoming connections. bootstrap.bind(new InetSocketAddress(8080));