Polished all examples

This commit is contained in:
Trustin Lee 2009-07-23 09:05:53 +00:00
parent 6321bb406f
commit 3920158bc0
24 changed files with 179 additions and 250 deletions

View File

@ -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();
}
}

View File

@ -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));

View File

@ -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();
}
}

View File

@ -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

View File

@ -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();
}
}

View File

@ -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.

View File

@ -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();
}
}

View File

@ -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));

View File

@ -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.

View File

@ -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();
}
}

View File

@ -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));

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}

View File

@ -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<String> i1 = cities.iterator();

View File

@ -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));

View File

@ -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));

View File

@ -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));

View File

@ -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);

View File

@ -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();
}
}

View File

@ -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));

View File

@ -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();
}
}

View File

@ -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));