Remove Bootstrap operations that require a promise and add various ad-hoc bind() and connect() operations

- Update examples to use the newly added bind() and connect()
  operations.
This commit is contained in:
Trustin Lee 2013-01-30 20:06:40 +09:00
parent 291293a6dc
commit 788d7e9b8b
51 changed files with 199 additions and 271 deletions

View File

@ -42,13 +42,13 @@ public class AppletDiscardServer extends JApplet {
try { try {
bootstrap = new ServerBootstrap(); bootstrap = new ServerBootstrap();
bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class) bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class)
.localAddress(9999).childHandler(new ChannelInitializer<SocketChannel>() { .childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new DiscardServerHandler()); ch.pipeline().addLast(new DiscardServerHandler());
} }
}); });
ChannelFuture f = bootstrap.bind().sync(); ChannelFuture f = bootstrap.bind(9999).sync();
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();

View File

@ -40,11 +40,10 @@ public class DiscardClient {
try { try {
b.group(new NioEventLoopGroup()) b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.remoteAddress(host, port)
.handler(new DiscardClientHandler(firstMessageSize)); .handler(new DiscardClientHandler(firstMessageSize));
// Make the connection attempt. // Make the connection attempt.
ChannelFuture f = b.connect().sync(); ChannelFuture f = b.connect(host, port).sync();
// Wait until the connection is closed. // Wait until the connection is closed.
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();

View File

@ -38,7 +38,6 @@ public class DiscardServer {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(new ChannelInitializer<SocketChannel>() { .childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
@ -47,7 +46,7 @@ public class DiscardServer {
}); });
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
ChannelFuture f = b.bind().sync(); ChannelFuture f = b.bind(port).sync();
// Wait until the server socket is closed. // Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully // In this example, this does not happen, but you can do that to gracefully

View File

@ -25,8 +25,6 @@ import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler; import io.netty.handler.logging.LoggingHandler;
import java.net.InetSocketAddress;
/** /**
* Sends one message when a connection is open and echoes back any received * Sends one message when a connection is open and echoes back any received
* data to the server. Simply put, the echo client initiates the ping-pong * data to the server. Simply put, the echo client initiates the ping-pong
@ -52,7 +50,6 @@ public class EchoClient {
b.group(new NioEventLoopGroup()) b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.TCP_NODELAY, true)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>() { .handler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
@ -63,7 +60,7 @@ public class EchoClient {
}); });
// Start the client. // Start the client.
ChannelFuture f = b.connect().sync(); ChannelFuture f = b.connect(host, port).sync();
// Wait until the connection is closed. // Wait until the connection is closed.
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();

View File

@ -25,8 +25,6 @@ import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler; import io.netty.handler.logging.LoggingHandler;
import java.net.InetSocketAddress;
/** /**
* Echoes back any received data from a client. * Echoes back any received data from a client.
*/ */
@ -45,7 +43,6 @@ public class EchoServer {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100) .option(ChannelOption.SO_BACKLOG, 100)
.localAddress(new InetSocketAddress(port))
.childOption(ChannelOption.TCP_NODELAY, true) .childOption(ChannelOption.TCP_NODELAY, true)
.handler(new LoggingHandler(LogLevel.INFO)) .handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() { .childHandler(new ChannelInitializer<SocketChannel>() {
@ -58,7 +55,7 @@ public class EchoServer {
}); });
// Start the server. // Start the server.
ChannelFuture f = b.bind().sync(); ChannelFuture f = b.bind(port).sync();
// Wait until the server socket is closed. // Wait until the server socket is closed.
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();

View File

@ -41,11 +41,10 @@ public class FactorialClient {
try { try {
b.group(new NioEventLoopGroup()) b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.remoteAddress(host, port)
.handler(new FactorialClientInitializer(count)); .handler(new FactorialClientInitializer(count));
// Make a new connection. // Make a new connection.
ChannelFuture f = b.connect().sync(); ChannelFuture f = b.connect(host, port).sync();
// Get the handler instance to retrieve the answer. // Get the handler instance to retrieve the answer.
FactorialClientHandler handler = FactorialClientHandler handler =

View File

@ -36,10 +36,9 @@ public class FactorialServer {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(new FactorialServerInitializer()); .childHandler(new FactorialServerInitializer());
b.bind().sync().channel().closeFuture().sync(); b.bind(port).sync().channel().closeFuture().sync();
} finally { } finally {
b.shutdown(); b.shutdown();
} }

View File

@ -34,7 +34,6 @@ import io.netty.util.CharsetUtil;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.net.InetSocketAddress;
/** /**
* Server that accept the path of a file an echo back its content. * Server that accept the path of a file an echo back its content.
@ -54,7 +53,6 @@ public class FileServer {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100) .option(ChannelOption.SO_BACKLOG, 100)
.localAddress(new InetSocketAddress(port))
.childOption(ChannelOption.TCP_NODELAY, true) .childOption(ChannelOption.TCP_NODELAY, true)
.handler(new LoggingHandler(LogLevel.INFO)) .handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() { .childHandler(new ChannelInitializer<SocketChannel>() {
@ -69,7 +67,7 @@ public class FileServer {
}); });
// Start the server. // Start the server.
ChannelFuture f = b.bind().sync(); ChannelFuture f = b.bind(port).sync();
// Wait until the server socket is closed. // Wait until the server socket is closed.
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();

View File

@ -32,10 +32,9 @@ public class HttpStaticFileServer {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(new HttpStaticFileServerInitializer()); .childHandler(new HttpStaticFileServerInitializer());
b.bind().sync().channel().closeFuture().sync(); b.bind(port).sync().channel().closeFuture().sync();
} finally { } finally {
b.shutdown(); b.shutdown();
} }

View File

@ -27,7 +27,6 @@ import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.codec.http.HttpVersion;
import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
/** /**
@ -66,11 +65,10 @@ public class HttpSnoopClient {
try { try {
b.group(new NioEventLoopGroup()) b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.handler(new HttpSnoopClientInitializer(ssl)) .handler(new HttpSnoopClientInitializer(ssl));
.remoteAddress(new InetSocketAddress(host, port));
// Make the connection attempt. // Make the connection attempt.
Channel ch = b.connect().sync().channel(); Channel ch = b.connect(host, port).sync().channel();
// Prepare the HTTP request. // Prepare the HTTP request.
HttpRequest request = new DefaultHttpRequest( HttpRequest request = new DefaultHttpRequest(

View File

@ -20,8 +20,6 @@ import io.netty.channel.Channel;
import io.netty.channel.socket.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.net.InetSocketAddress;
/** /**
* An HTTP server that sends back the content of the received HTTP request * An HTTP server that sends back the content of the received HTTP request
* in a pretty plaintext form. * in a pretty plaintext form.
@ -41,10 +39,9 @@ public class HttpSnoopServer {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.childHandler(new HttpSnoopServerInitializer()) .childHandler(new HttpSnoopServerInitializer());
.localAddress(new InetSocketAddress(port));
Channel ch = b.bind().sync().channel(); Channel ch = b.bind(port).sync().channel();
ch.closeFuture().sync(); ch.closeFuture().sync();
} finally { } finally {
b.shutdown(); b.shutdown();

View File

@ -40,7 +40,6 @@ import io.netty.logging.InternalLoggerFactory;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.List; import java.util.List;
@ -156,8 +155,7 @@ public class HttpUploadClient {
URI uriSimple) throws Exception { URI uriSimple) throws Exception {
// Start the connection attempt. // Start the connection attempt.
// No use of HttpPostRequestEncoder since not a POST // No use of HttpPostRequestEncoder since not a POST
bootstrap.remoteAddress(new InetSocketAddress(host, port)); Channel channel = bootstrap.connect(host, port).sync().channel();
Channel channel = bootstrap.connect().sync().channel();
// Prepare the HTTP request. // Prepare the HTTP request.
QueryStringEncoder encoder = new QueryStringEncoder(get); QueryStringEncoder encoder = new QueryStringEncoder(get);
@ -214,8 +212,7 @@ public class HttpUploadClient {
File file, HttpDataFactory factory, List<Entry<String, String>> headers) throws Exception { File file, HttpDataFactory factory, List<Entry<String, String>> headers) throws Exception {
// Start the connection attempt // Start the connection attempt
bootstrap.remoteAddress(new InetSocketAddress(host, port)); Channel channel = bootstrap.connect(host, port).sync().channel();
Channel channel = bootstrap.connect().sync().channel();
// Prepare the HTTP request. // Prepare the HTTP request.
FullHttpRequest request = FullHttpRequest request =
@ -299,8 +296,7 @@ public class HttpUploadClient {
throws Exception { throws Exception {
// Start the connection attempt // Start the connection attempt
bootstrap.remoteAddress(new InetSocketAddress(host, port)); Channel channel = bootstrap.connect(host, port).sync().channel();
Channel channel = bootstrap.connect().sync().channel();
// Prepare the HTTP request. // Prepare the HTTP request.
FullHttpRequest request = FullHttpRequest request =

View File

@ -36,9 +36,9 @@ public class HttpUploadServer {
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap b = new ServerBootstrap();
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class) b.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class)
.localAddress(port).childHandler(new HttpUploadServerInitializer()); .childHandler(new HttpUploadServerInitializer());
Channel ch = b.bind().sync().channel(); Channel ch = b.bind(port).sync().channel();
System.out.println("HTTP Upload Server at port " + port + '.'); System.out.println("HTTP Upload Server at port " + port + '.');
System.out.println("Open your browser and navigate to http://localhost:" + port + '/'); System.out.println("Open your browser and navigate to http://localhost:" + port + '/');

View File

@ -37,10 +37,9 @@ public class AutobahnServer {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(new AutobahnServerInitializer()); .childHandler(new AutobahnServerInitializer());
ChannelFuture f = b.bind().sync(); ChannelFuture f = b.bind(port).sync();
System.out.println("Web Socket Server started at port " + port); System.out.println("Web Socket Server started at port " + port);
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();
} finally { } finally {

View File

@ -86,7 +86,6 @@ public class WebSocketClient {
b.group(new NioEventLoopGroup()) b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.remoteAddress(uri.getHost(), uri.getPort())
.handler(new ChannelInitializer<SocketChannel>() { .handler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
@ -99,7 +98,7 @@ public class WebSocketClient {
}); });
System.out.println("WebSocket Client connecting"); System.out.println("WebSocket Client connecting");
Channel ch = b.connect().sync().channel(); Channel ch = b.connect(uri.getHost(), uri.getPort()).sync().channel();
handler.handshakeFuture().sync(); handler.handshakeFuture().sync();
// Send 10 messages and wait for responses // Send 10 messages and wait for responses

View File

@ -26,8 +26,6 @@ import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder; import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import java.net.InetSocketAddress;
/** /**
* A WebSocket Server that respondes to requests at: * A WebSocket Server that respondes to requests at:
* *
@ -63,7 +61,6 @@ public class WebSocketServer {
try { try {
sb.group(new NioEventLoopGroup(), new NioEventLoopGroup()) sb.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() { .childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(final SocketChannel ch) throws Exception { public void initChannel(final SocketChannel ch) throws Exception {
@ -76,7 +73,7 @@ public class WebSocketServer {
} }
}); });
final Channel ch = sb.bind().sync().channel(); final Channel ch = sb.bind(port).sync().channel();
System.out.println("Web socket server started at port " + port); System.out.println("Web socket server started at port " + port);
ch.closeFuture().sync(); ch.closeFuture().sync();

View File

@ -52,10 +52,9 @@ public class WebSocketServer {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(new WebSocketServerInitializer()); .childHandler(new WebSocketServerInitializer());
Channel ch = b.bind().sync().channel(); Channel ch = b.bind(port).sync().channel();
System.out.println("Web socket server started at port " + port + '.'); System.out.println("Web socket server started at port " + port + '.');
System.out.println("Open your browser and navigate to http://localhost:" + port + '/'); System.out.println("Open your browser and navigate to http://localhost:" + port + '/');

View File

@ -51,10 +51,9 @@ public class WebSocketSslServer {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(new WebSocketSslServerInitializer()); .childHandler(new WebSocketSslServerInitializer());
Channel ch = b.bind().sync().channel(); Channel ch = b.bind(port).sync().channel();
System.out.println("Web socket server started at port " + port + '.'); System.out.println("Web socket server started at port " + port + '.');
System.out.println("Open your browser and navigate to https://localhost:" + port + '/'); System.out.println("Open your browser and navigate to https://localhost:" + port + '/');
ch.closeFuture().sync(); ch.closeFuture().sync();

View File

@ -51,7 +51,6 @@ public class LocalEcho {
// to reduce the communication latency between socket channels and local channels. // to reduce the communication latency between socket channels and local channels.
sb.group(new LocalEventLoopGroup()) sb.group(new LocalEventLoopGroup())
.channel(LocalServerChannel.class) .channel(LocalServerChannel.class)
.localAddress(addr)
.handler(new ChannelInitializer<LocalServerChannel>() { .handler(new ChannelInitializer<LocalServerChannel>() {
@Override @Override
public void initChannel(LocalServerChannel ch) throws Exception { public void initChannel(LocalServerChannel ch) throws Exception {
@ -69,7 +68,6 @@ public class LocalEcho {
cb.group(new NioEventLoopGroup()) // NIO event loops are also OK cb.group(new NioEventLoopGroup()) // NIO event loops are also OK
.channel(LocalChannel.class) .channel(LocalChannel.class)
.remoteAddress(addr)
.handler(new ChannelInitializer<LocalChannel>() { .handler(new ChannelInitializer<LocalChannel>() {
@Override @Override
public void initChannel(LocalChannel ch) throws Exception { public void initChannel(LocalChannel ch) throws Exception {
@ -80,10 +78,10 @@ public class LocalEcho {
}); });
// Start the server. // Start the server.
sb.bind().sync(); sb.bind(addr).sync();
// Start the client. // Start the client.
Channel ch = cb.connect().sync().channel(); Channel ch = cb.connect(addr).sync().channel();
// Read commands from the stdin. // Read commands from the stdin.
System.out.println("Enter text (quit to end)"); System.out.println("Enter text (quit to end)");

View File

@ -48,11 +48,10 @@ public class LocalTimeClient {
try { try {
b.group(new NioEventLoopGroup()) b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.remoteAddress(host, port)
.handler(new LocalTimeClientInitializer()); .handler(new LocalTimeClientInitializer());
// Make a new connection. // Make a new connection.
Channel ch = b.connect().sync().channel(); Channel ch = b.connect(host, port).sync().channel();
// Get the handler instance to initiate the request. // Get the handler instance to initiate the request.
LocalTimeClientHandler handler = LocalTimeClientHandler handler =

View File

@ -36,10 +36,9 @@ public class LocalTimeServer {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(new LocalTimeServerInitializer()); .childHandler(new LocalTimeServerInitializer());
b.bind().sync().channel().closeFuture().sync(); b.bind(port).sync().channel().closeFuture().sync();
} finally { } finally {
b.shutdown(); b.shutdown();
} }

View File

@ -45,7 +45,6 @@ public class ObjectEchoClient {
try { try {
b.group(new NioEventLoopGroup()) b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.remoteAddress(host, port)
.handler(new ChannelInitializer<SocketChannel>() { .handler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
@ -57,7 +56,7 @@ public class ObjectEchoClient {
}); });
// Start the connection attempt. // Start the connection attempt.
b.connect().sync().channel().closeFuture().sync(); b.connect(host, port).sync().channel().closeFuture().sync();
} finally { } finally {
b.shutdown(); b.shutdown();
} }

View File

@ -41,7 +41,6 @@ public class ObjectEchoServer {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(new ChannelInitializer<SocketChannel>() { .childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
@ -53,7 +52,7 @@ public class ObjectEchoServer {
}); });
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
b.bind().sync().channel().closeFuture().sync(); b.bind(port).sync().channel().closeFuture().sync();
} finally { } finally {
b.shutdown(); b.shutdown();
} }

View File

@ -41,7 +41,6 @@ public class PortUnificationServer {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(new ChannelInitializer<SocketChannel>() { .childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
@ -50,7 +49,7 @@ public class PortUnificationServer {
}); });
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
b.bind().sync().channel().closeFuture().sync(); b.bind(port).sync().channel().closeFuture().sync();
} finally { } finally {
b.shutdown(); b.shutdown();
} }

View File

@ -41,10 +41,9 @@ public class HexDumpProxy {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(localPort)
.childHandler(new HexDumpProxyInitializer(remoteHost, remotePort)); .childHandler(new HexDumpProxyInitializer(remoteHost, remotePort));
b.bind().sync().channel().closeFuture().sync(); b.bind(localPort).sync().channel().closeFuture().sync();
} finally { } finally {
b.shutdown(); b.shutdown();
} }

View File

@ -46,10 +46,9 @@ public class HexDumpProxyFrontendHandler extends ChannelInboundByteHandlerAdapte
Bootstrap b = new Bootstrap(); Bootstrap b = new Bootstrap();
b.group(inboundChannel.eventLoop()) b.group(inboundChannel.eventLoop())
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.remoteAddress(remoteHost, remotePort)
.handler(new HexDumpProxyBackendHandler(inboundChannel)); .handler(new HexDumpProxyBackendHandler(inboundChannel));
ChannelFuture f = b.connect(); ChannelFuture f = b.connect(remoteHost, remotePort);
outboundChannel = f.channel(); outboundChannel = f.channel();
f.addListener(new ChannelFutureListener() { f.addListener(new ChannelFutureListener() {
@Override @Override

View File

@ -45,11 +45,10 @@ public class QuoteOfTheMomentClient {
try { try {
b.group(new NioEventLoopGroup()) b.group(new NioEventLoopGroup())
.channel(NioDatagramChannel.class) .channel(NioDatagramChannel.class)
.localAddress(new InetSocketAddress(0))
.option(ChannelOption.SO_BROADCAST, true) .option(ChannelOption.SO_BROADCAST, true)
.handler(new QuoteOfTheMomentClientHandler()); .handler(new QuoteOfTheMomentClientHandler());
Channel ch = b.bind().sync().channel(); Channel ch = b.bind(0).sync().channel();
// Broadcast the QOTM request to port 8080. // Broadcast the QOTM request to port 8080.
ch.write(new DatagramPacket( ch.write(new DatagramPacket(

View File

@ -20,8 +20,6 @@ import io.netty.channel.ChannelOption;
import io.netty.channel.socket.nio.NioDatagramChannel; import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioEventLoopGroup;
import java.net.InetSocketAddress;
/** /**
* A UDP server that responds to the QOTM (quote of the moment) request to a * A UDP server that responds to the QOTM (quote of the moment) request to a
* {@link QuoteOfTheMomentClient}. * {@link QuoteOfTheMomentClient}.
@ -41,11 +39,10 @@ public class QuoteOfTheMomentServer {
try { try {
b.group(new NioEventLoopGroup()) b.group(new NioEventLoopGroup())
.channel(NioDatagramChannel.class) .channel(NioDatagramChannel.class)
.localAddress(new InetSocketAddress(port))
.option(ChannelOption.SO_BROADCAST, true) .option(ChannelOption.SO_BROADCAST, true)
.handler(new QuoteOfTheMomentServerHandler()); .handler(new QuoteOfTheMomentServerHandler());
b.bind().sync().channel().closeFuture().await(); b.bind(port).sync().channel().closeFuture().await();
} finally { } finally {
b.shutdown(); b.shutdown();
} }

View File

@ -18,12 +18,12 @@ package io.netty.example.rxtx;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.rxtx.RxtxChannel;
import io.netty.channel.rxtx.RxtxDeviceAddress;
import io.netty.channel.socket.oio.OioEventLoopGroup; import io.netty.channel.socket.oio.OioEventLoopGroup;
import io.netty.handler.codec.LineBasedFrameDecoder; import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder; import io.netty.handler.codec.string.StringEncoder;
import io.netty.channel.rxtx.RxtxChannel;
import io.netty.channel.rxtx.RxtxDeviceAddress;
/** /**
* Sends one message to a serial device * Sends one message to a serial device
@ -35,7 +35,6 @@ public final class RxtxClient {
try { try {
b.group(new OioEventLoopGroup()) b.group(new OioEventLoopGroup())
.channel(RxtxChannel.class) .channel(RxtxChannel.class)
.remoteAddress(new RxtxDeviceAddress("/dev/ttyUSB0"))
.handler(new ChannelInitializer<RxtxChannel>() { .handler(new ChannelInitializer<RxtxChannel>() {
@Override @Override
public void initChannel(RxtxChannel ch) throws Exception { public void initChannel(RxtxChannel ch) throws Exception {
@ -48,7 +47,7 @@ public final class RxtxClient {
} }
}); });
ChannelFuture f = b.connect().sync(); ChannelFuture f = b.connect(new RxtxDeviceAddress("/dev/ttyUSB0")).sync();
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();
} finally { } finally {

View File

@ -20,13 +20,11 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.sctp.SctpChannel; import io.netty.channel.sctp.SctpChannel;
import io.netty.channel.sctp.SctpChannelOption; import io.netty.channel.sctp.SctpChannelOption;
import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.channel.sctp.nio.NioSctpChannel; import io.netty.channel.sctp.nio.NioSctpChannel;
import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler; import io.netty.handler.logging.LoggingHandler;
import java.net.InetSocketAddress;
/** /**
* Sends one message when a connection is open and echoes back any received * Sends one message when a connection is open and echoes back any received
* data to the server over SCTP connection. * data to the server over SCTP connection.
@ -54,7 +52,6 @@ public class NioSctpEchoClient {
b.group(new NioEventLoopGroup()) b.group(new NioEventLoopGroup())
.channel(NioSctpChannel.class) .channel(NioSctpChannel.class)
.option(SctpChannelOption.SCTP_NODELAY, true) .option(SctpChannelOption.SCTP_NODELAY, true)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SctpChannel>() { .handler(new ChannelInitializer<SctpChannel>() {
@Override @Override
public void initChannel(SctpChannel ch) throws Exception { public void initChannel(SctpChannel ch) throws Exception {
@ -65,7 +62,7 @@ public class NioSctpEchoClient {
}); });
// Start the client. // Start the client.
ChannelFuture f = b.connect().sync(); ChannelFuture f = b.connect(host, port).sync();
// Wait until the connection is closed. // Wait until the connection is closed.
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();

View File

@ -21,13 +21,11 @@ import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.channel.sctp.SctpChannel; import io.netty.channel.sctp.SctpChannel;
import io.netty.channel.sctp.SctpChannelOption; import io.netty.channel.sctp.SctpChannelOption;
import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.channel.sctp.nio.NioSctpServerChannel; import io.netty.channel.sctp.nio.NioSctpServerChannel;
import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler; import io.netty.handler.logging.LoggingHandler;
import java.net.InetSocketAddress;
/** /**
* Echoes back any received data from a SCTP client. * Echoes back any received data from a SCTP client.
*/ */
@ -46,7 +44,6 @@ public class NioSctpEchoServer {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioSctpServerChannel.class) .channel(NioSctpServerChannel.class)
.option(ChannelOption.SO_BACKLOG, 100) .option(ChannelOption.SO_BACKLOG, 100)
.localAddress(new InetSocketAddress(port))
.childOption(SctpChannelOption.SCTP_NODELAY, true) .childOption(SctpChannelOption.SCTP_NODELAY, true)
.handler(new LoggingHandler(LogLevel.INFO)) .handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SctpChannel>() { .childHandler(new ChannelInitializer<SctpChannel>() {
@ -59,7 +56,7 @@ public class NioSctpEchoServer {
}); });
// Start the server. // Start the server.
ChannelFuture f = b.bind().sync(); ChannelFuture f = b.bind(port).sync();
// Wait until the server socket is closed. // Wait until the server socket is closed.
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();

View File

@ -20,13 +20,11 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.sctp.SctpChannel; import io.netty.channel.sctp.SctpChannel;
import io.netty.channel.sctp.SctpChannelOption; import io.netty.channel.sctp.SctpChannelOption;
import io.netty.channel.socket.oio.OioEventLoopGroup;
import io.netty.channel.sctp.oio.OioSctpChannel; import io.netty.channel.sctp.oio.OioSctpChannel;
import io.netty.channel.socket.oio.OioEventLoopGroup;
import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler; import io.netty.handler.logging.LoggingHandler;
import java.net.InetSocketAddress;
/** /**
* Sends one message when a connection is open and echoes back any received * Sends one message when a connection is open and echoes back any received
* data to the server over SCTP connection. * data to the server over SCTP connection.
@ -54,7 +52,6 @@ public class OioSctpEchoClient {
b.group(new OioEventLoopGroup()) b.group(new OioEventLoopGroup())
.channel(OioSctpChannel.class) .channel(OioSctpChannel.class)
.option(SctpChannelOption.SCTP_NODELAY, true) .option(SctpChannelOption.SCTP_NODELAY, true)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SctpChannel>() { .handler(new ChannelInitializer<SctpChannel>() {
@Override @Override
public void initChannel(SctpChannel ch) throws Exception { public void initChannel(SctpChannel ch) throws Exception {
@ -65,7 +62,7 @@ public class OioSctpEchoClient {
}); });
// Start the client. // Start the client.
ChannelFuture f = b.connect().sync(); ChannelFuture f = b.connect(host, port).sync();
// Wait until the connection is closed. // Wait until the connection is closed.
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();

View File

@ -21,13 +21,11 @@ import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.channel.sctp.SctpChannel; import io.netty.channel.sctp.SctpChannel;
import io.netty.channel.sctp.SctpChannelOption; import io.netty.channel.sctp.SctpChannelOption;
import io.netty.channel.socket.oio.OioEventLoopGroup;
import io.netty.channel.sctp.oio.OioSctpServerChannel; import io.netty.channel.sctp.oio.OioSctpServerChannel;
import io.netty.channel.socket.oio.OioEventLoopGroup;
import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler; import io.netty.handler.logging.LoggingHandler;
import java.net.InetSocketAddress;
/** /**
* Echoes back any received data from a SCTP client. * Echoes back any received data from a SCTP client.
*/ */
@ -46,7 +44,6 @@ public class OioSctpEchoServer {
b.group(new OioEventLoopGroup(), new OioEventLoopGroup()) b.group(new OioEventLoopGroup(), new OioEventLoopGroup())
.channel(OioSctpServerChannel.class) .channel(OioSctpServerChannel.class)
.option(ChannelOption.SO_BACKLOG, 100) .option(ChannelOption.SO_BACKLOG, 100)
.localAddress(new InetSocketAddress(port))
.childOption(SctpChannelOption.SCTP_NODELAY, true) .childOption(SctpChannelOption.SCTP_NODELAY, true)
.handler(new LoggingHandler(LogLevel.INFO)) .handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SctpChannel>() { .childHandler(new ChannelInitializer<SctpChannel>() {
@ -59,7 +56,7 @@ public class OioSctpEchoServer {
}); });
// Start the server. // Start the server.
ChannelFuture f = b.bind().sync(); ChannelFuture f = b.bind(port).sync();
// Wait until the server socket is closed. // Wait until the server socket is closed.
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();

View File

@ -43,11 +43,10 @@ public class SecureChatClient {
try { try {
b.group(new NioEventLoopGroup()) b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.remoteAddress(host, port)
.handler(new SecureChatClientInitializer()); .handler(new SecureChatClientInitializer());
// Start the connection attempt. // Start the connection attempt.
Channel ch = b.connect().sync().channel(); Channel ch = b.connect(host, port).sync().channel();
// Read commands from the stdin. // Read commands from the stdin.
ChannelFuture lastWriteFuture = null; ChannelFuture lastWriteFuture = null;

View File

@ -36,10 +36,9 @@ public class SecureChatServer {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(new SecureChatServerInitializer()); .childHandler(new SecureChatServerInitializer());
b.bind().sync().channel().closeFuture().sync(); b.bind(port).sync().channel().closeFuture().sync();
} finally { } finally {
b.shutdown(); b.shutdown();
} }

View File

@ -33,9 +33,8 @@ public final class SocksServer {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(localPort)
.childHandler(new SocksServerInitializer()); .childHandler(new SocksServerInitializer());
b.bind().sync().channel().closeFuture().sync(); b.bind(localPort).sync().channel().closeFuture().sync();
} finally { } finally {
b.shutdown(); b.shutdown();
} }

View File

@ -72,9 +72,9 @@ public final class SocksServerConnectHandler extends ChannelInboundMessageHandle
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
.option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.SO_KEEPALIVE, true)
.handler(new DirectClientInitializer(cb)) .handler(new DirectClientInitializer(cb));
.remoteAddress(request.host(), request.port());
b.connect(); b.connect(request.host(), request.port());
} }
@Override @Override

View File

@ -42,11 +42,10 @@ public class TelnetClient {
try { try {
b.group(new NioEventLoopGroup()) b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.remoteAddress(host, port)
.handler(new TelnetClientInitializer()); .handler(new TelnetClientInitializer());
// Start the connection attempt. // Start the connection attempt.
Channel ch = b.connect().sync().channel(); Channel ch = b.connect(host, port).sync().channel();
// Read commands from the stdin. // Read commands from the stdin.
ChannelFuture lastWriteFuture = null; ChannelFuture lastWriteFuture = null;

View File

@ -35,10 +35,9 @@ public class TelnetServer {
try { try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(new TelnetServerPipelineFactory()); .childHandler(new TelnetServerPipelineFactory());
b.bind().sync().channel().closeFuture().sync(); b.bind(port).sync().channel().closeFuture().sync();
} finally { } finally {
b.shutdown(); b.shutdown();
} }

View File

@ -19,20 +19,18 @@ import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.channel.udt.UdtChannel;
import io.netty.channel.udt.nio.NioUdtProvider;
import io.netty.example.udt.util.UtilConsoleReporter; import io.netty.example.udt.util.UtilConsoleReporter;
import io.netty.example.udt.util.UtilThreadFactory; import io.netty.example.udt.util.UtilThreadFactory;
import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler; import io.netty.handler.logging.LoggingHandler;
import io.netty.channel.udt.UdtChannel;
import io.netty.channel.udt.nio.NioUdtProvider;
import java.net.InetSocketAddress;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
/** /**
* UDT Byte Stream Client * UDT Byte Stream Client
* <p> * <p>
@ -66,8 +64,6 @@ public class ByteEchoClient {
try { try {
boot.group(connectGroup) boot.group(connectGroup)
.channelFactory(NioUdtProvider.BYTE_CONNECTOR) .channelFactory(NioUdtProvider.BYTE_CONNECTOR)
.localAddress("localhost", 0)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<UdtChannel>() { .handler(new ChannelInitializer<UdtChannel>() {
@Override @Override
public void initChannel(final UdtChannel ch) public void initChannel(final UdtChannel ch)
@ -78,7 +74,7 @@ public class ByteEchoClient {
} }
}); });
// Start the client. // Start the client.
final ChannelFuture f = boot.connect().sync(); final ChannelFuture f = boot.connect(host, port).sync();
// Wait until the connection is closed. // Wait until the connection is closed.
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();
} finally { } finally {

View File

@ -20,17 +20,16 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.channel.socket.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.channel.udt.UdtChannel;
import io.netty.channel.udt.nio.NioUdtProvider;
import io.netty.example.udt.util.UtilThreadFactory; import io.netty.example.udt.util.UtilThreadFactory;
import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler; import io.netty.handler.logging.LoggingHandler;
import io.netty.channel.udt.UdtChannel;
import io.netty.channel.udt.nio.NioUdtProvider;
import java.util.concurrent.ThreadFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.concurrent.ThreadFactory;
/** /**
* UDT Byte Stream Server * UDT Byte Stream Server
* <p> * <p>
@ -60,7 +59,6 @@ public class ByteEchoServer {
boot.group(acceptGroup, connectGroup) boot.group(acceptGroup, connectGroup)
.channelFactory(NioUdtProvider.BYTE_ACCEPTOR) .channelFactory(NioUdtProvider.BYTE_ACCEPTOR)
.option(ChannelOption.SO_BACKLOG, 10) .option(ChannelOption.SO_BACKLOG, 10)
.localAddress("localhost", port)
.handler(new LoggingHandler(LogLevel.INFO)) .handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<UdtChannel>() { .childHandler(new ChannelInitializer<UdtChannel>() {
@Override @Override
@ -72,7 +70,7 @@ public class ByteEchoServer {
} }
}); });
// Start the server. // Start the server.
final ChannelFuture future = boot.bind().sync(); final ChannelFuture future = boot.bind(port).sync();
// Wait until the server socket is closed. // Wait until the server socket is closed.
future.channel().closeFuture().sync(); future.channel().closeFuture().sync();
} finally { } finally {

View File

@ -19,20 +19,18 @@ import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.channel.udt.UdtChannel;
import io.netty.channel.udt.nio.NioUdtProvider;
import io.netty.example.udt.util.UtilConsoleReporter; import io.netty.example.udt.util.UtilConsoleReporter;
import io.netty.example.udt.util.UtilThreadFactory; import io.netty.example.udt.util.UtilThreadFactory;
import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler; import io.netty.handler.logging.LoggingHandler;
import io.netty.channel.udt.UdtChannel;
import io.netty.channel.udt.nio.NioUdtProvider;
import java.net.InetSocketAddress;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
/** /**
* UDT Message Flow client * UDT Message Flow client
* <p> * <p>
@ -66,8 +64,6 @@ public class MsgEchoClient {
try { try {
boot.group(connectGroup) boot.group(connectGroup)
.channelFactory(NioUdtProvider.MESSAGE_CONNECTOR) .channelFactory(NioUdtProvider.MESSAGE_CONNECTOR)
.localAddress("localhost", 0)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<UdtChannel>() { .handler(new ChannelInitializer<UdtChannel>() {
@Override @Override
public void initChannel(final UdtChannel ch) public void initChannel(final UdtChannel ch)
@ -78,7 +74,7 @@ public class MsgEchoClient {
} }
}); });
// Start the client. // Start the client.
final ChannelFuture f = boot.connect().sync(); final ChannelFuture f = boot.connect(host, port).sync();
// Wait until the connection is closed. // Wait until the connection is closed.
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();
} finally { } finally {

View File

@ -20,17 +20,16 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.channel.socket.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.channel.udt.UdtChannel;
import io.netty.channel.udt.nio.NioUdtProvider;
import io.netty.example.udt.util.UtilThreadFactory; import io.netty.example.udt.util.UtilThreadFactory;
import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler; import io.netty.handler.logging.LoggingHandler;
import io.netty.channel.udt.UdtChannel;
import io.netty.channel.udt.nio.NioUdtProvider;
import java.util.concurrent.ThreadFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.concurrent.ThreadFactory;
/** /**
* UDT Message Flow Server * UDT Message Flow Server
* <p> * <p>
@ -60,7 +59,6 @@ public class MsgEchoServer {
boot.group(acceptGroup, connectGroup) boot.group(acceptGroup, connectGroup)
.channelFactory(NioUdtProvider.MESSAGE_ACCEPTOR) .channelFactory(NioUdtProvider.MESSAGE_ACCEPTOR)
.option(ChannelOption.SO_BACKLOG, 10) .option(ChannelOption.SO_BACKLOG, 10)
.localAddress("localhost", port)
.handler(new LoggingHandler(LogLevel.INFO)) .handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<UdtChannel>() { .childHandler(new ChannelInitializer<UdtChannel>() {
@Override @Override
@ -72,7 +70,7 @@ public class MsgEchoServer {
} }
}); });
// Start the server. // Start the server.
final ChannelFuture future = boot.bind().sync(); final ChannelFuture future = boot.bind(port).sync();
// Wait until the server socket is closed. // Wait until the server socket is closed.
future.channel().closeFuture().sync(); future.channel().closeFuture().sync();
} finally { } finally {

View File

@ -60,7 +60,6 @@ public abstract class MsgEchoPeerBase {
try { try {
boot.group(connectGroup) boot.group(connectGroup)
.channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS) .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS)
.localAddress(self).remoteAddress(peer)
.handler(new ChannelInitializer<UdtChannel>() { .handler(new ChannelInitializer<UdtChannel>() {
@Override @Override
public void initChannel(final UdtChannel ch) public void initChannel(final UdtChannel ch)
@ -71,7 +70,7 @@ public abstract class MsgEchoPeerBase {
} }
}); });
// Start the peer. // Start the peer.
final ChannelFuture f = boot.connect().sync(); final ChannelFuture f = boot.connect(peer, self).sync();
// Wait until the connection is closed. // Wait until the connection is closed.
f.channel().closeFuture().sync(); f.channel().closeFuture().sync();
} finally { } finally {

View File

@ -21,7 +21,6 @@ import io.netty.channel.ChannelException;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoopGroup; import io.netty.channel.EventLoopGroup;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
@ -37,6 +36,7 @@ import java.util.concurrent.ConcurrentHashMap;
* *
*/ */
public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> { public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> {
private EventLoopGroup group; private EventLoopGroup group;
private ChannelFactory factory; private ChannelFactory factory;
private SocketAddress localAddress; private SocketAddress localAddress;
@ -174,7 +174,7 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> {
* Validate all the parameters. Sub-classes may override this, but should * Validate all the parameters. Sub-classes may override this, but should
* call the super method in that case. * call the super method in that case.
*/ */
protected void validate() { public void validate() {
if (group == null) { if (group == null) {
throw new IllegalStateException("group not set"); throw new IllegalStateException("group not set");
} }
@ -183,22 +183,52 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> {
} }
} }
protected final void validate(ChannelFuture future) {
if (future == null) {
throw new NullPointerException("future");
}
validate();
}
/** /**
* Create a new {@link Channel} and bind it. * Create a new {@link Channel} and bind it.
*/ */
public ChannelFuture bind() { public ChannelFuture bind() {
validate(); validate();
Channel channel = factory().newChannel(); SocketAddress localAddress = this.localAddress;
return bind(channel.newPromise()); if (localAddress == null) {
throw new IllegalStateException("localAddress not set");
}
return doBind(localAddress);
} }
/**
* Create a new {@link Channel} and bind it.
*/
public ChannelFuture bind(int inetPort) {
return bind(new InetSocketAddress(inetPort));
}
/**
* Create a new {@link Channel} and bind it.
*/
public ChannelFuture bind(String inetHost, int inetPort) {
return bind(new InetSocketAddress(inetHost, inetPort));
}
/**
* Create a new {@link Channel} and bind it.
*/
public ChannelFuture bind(InetAddress inetHost, int inetPort) {
return bind(new InetSocketAddress(inetHost, inetPort));
}
/**
* Create a new {@link Channel} and bind it.
*/
public ChannelFuture bind(SocketAddress localAddress) {
validate();
if (localAddress == null) {
throw new NullPointerException("localAddress");
}
return doBind(localAddress);
}
abstract ChannelFuture doBind(SocketAddress localAddress);
/** /**
* the {@link ChannelHandler} to use for serving the requests. * the {@link ChannelHandler} to use for serving the requests.
*/ */
@ -211,42 +241,27 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> {
return (B) this; return (B) this;
} }
protected static boolean ensureOpen(ChannelPromise future) { final SocketAddress localAddress() {
if (!future.channel().isOpen()) {
// Registration was successful but the channel was closed due to some failure in
// handler.
future.setFailure(new ChannelException("initialization failure"));
return false;
}
return true;
}
/**
* Bind the {@link Channel} of the given {@link ChannelFactory}.
*/
public abstract ChannelFuture bind(ChannelPromise future);
protected final SocketAddress localAddress() {
return localAddress; return localAddress;
} }
protected final ChannelFactory factory() { final ChannelFactory factory() {
return factory; return factory;
} }
protected final ChannelHandler handler() { final ChannelHandler handler() {
return handler; return handler;
} }
protected final EventLoopGroup group() { final EventLoopGroup group() {
return group; return group;
} }
protected final Map<ChannelOption<?>, Object> options() { final Map<ChannelOption<?>, Object> options() {
return options; return options;
} }
protected final Map<AttributeKey<?>, Object> attrs() { final Map<AttributeKey<?>, Object> attrs() {
return attrs; return attrs;
} }

View File

@ -20,7 +20,6 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.logging.InternalLogger; import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory; import io.netty.logging.InternalLoggerFactory;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
@ -28,7 +27,6 @@ import io.netty.util.AttributeKey;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;
import java.util.Map.Entry; import java.util.Map.Entry;
/** /**
@ -67,24 +65,16 @@ public class Bootstrap extends AbstractBootstrap<Bootstrap> {
} }
@Override @Override
public ChannelFuture bind(ChannelPromise future) { ChannelFuture doBind(SocketAddress localAddress) {
validate(future); Channel channel = factory().newChannel();
if (localAddress() == null) {
throw new IllegalStateException("localAddress not set");
}
try { try {
init(future.channel()); init(channel);
} catch (Throwable t) { } catch (Throwable t) {
future.setFailure(t); channel.close();
return future; return channel.newFailedFuture(t);
} }
if (!ensureOpen(future)) { return channel.bind(localAddress).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
return future;
}
return future.channel().bind(localAddress(), future).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
} }
/** /**
@ -92,50 +82,76 @@ public class Bootstrap extends AbstractBootstrap<Bootstrap> {
*/ */
public ChannelFuture connect() { public ChannelFuture connect() {
validate(); validate();
Channel channel = factory().newChannel(); SocketAddress remoteAddress = this.remoteAddress;
return connect(channel.newPromise()); if (remoteAddress == null) {
throw new IllegalStateException("remoteAddress not set");
}
return doConnect(remoteAddress, localAddress());
}
/**
* Connect a {@link Channel} to the remote peer.
*/
public ChannelFuture connect(String inetHost, int inetPort) {
return connect(new InetSocketAddress(inetHost, inetPort));
}
/**
* Connect a {@link Channel} to the remote peer.
*/
public ChannelFuture connect(InetAddress inetHost, int inetPort) {
return connect(new InetSocketAddress(inetHost, inetPort));
}
/**
* Connect a {@link Channel} to the remote peer.
*/
public ChannelFuture connect(SocketAddress remoteAddress) {
if (remoteAddress == null) {
throw new NullPointerException("remoteAddress");
}
validate();
return doConnect(remoteAddress, localAddress());
}
/**
* Connect a {@link Channel} to the remote peer.
*/
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) {
if (remoteAddress == null) {
throw new NullPointerException("remoteAddress");
}
validate();
return doConnect(remoteAddress, localAddress);
} }
/** /**
* @see {@link #connect()} * @see {@link #connect()}
*/ */
public ChannelFuture connect(ChannelPromise future) { private ChannelFuture doConnect(SocketAddress remoteAddress, SocketAddress localAddress) {
validate(future); final Channel channel = factory().newChannel();
if (remoteAddress == null) {
throw new IllegalStateException("remoteAddress not set");
}
try { try {
init(future.channel()); init(channel);
} catch (Throwable t) { } catch (Throwable t) {
future.setFailure(t); channel.close();
return future; return channel.newFailedFuture(t);
} }
if (!ensureOpen(future)) { final ChannelFuture future;
return future; if (localAddress == null) {
} future = channel.connect(remoteAddress);
if (localAddress() == null) {
future.channel().connect(remoteAddress, future);
} else { } else {
future.channel().connect(remoteAddress, localAddress(), future); future = channel.connect(remoteAddress, localAddress);
} }
return future.addListener(ChannelFutureListener.CLOSE_ON_FAILURE); return future.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void init(Channel channel) throws Exception { private void init(Channel channel) throws Exception {
if (channel.isActive()) {
throw new IllegalStateException("channel already active:: " + channel);
}
if (channel.isRegistered()) {
throw new IllegalStateException("channel already registered: " + channel);
}
if (!channel.isOpen()) {
throw new ClosedChannelException();
}
ChannelPipeline p = channel.pipeline(); ChannelPipeline p = channel.pipeline();
p.addLast(handler()); p.addLast(handler());
@ -157,7 +173,7 @@ public class Bootstrap extends AbstractBootstrap<Bootstrap> {
} }
@Override @Override
protected void validate() { public void validate() {
super.validate(); super.validate();
if (handler() == null) { if (handler() == null) {
throw new IllegalStateException("handler not set"); throw new IllegalStateException("handler not set");

View File

@ -27,7 +27,6 @@ import io.netty.channel.ChannelInboundMessageHandler;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoopGroup; import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel; import io.netty.channel.ServerChannel;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
@ -35,7 +34,7 @@ import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory; import io.netty.logging.InternalLoggerFactory;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
import java.nio.channels.ClosedChannelException; import java.net.SocketAddress;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -145,27 +144,14 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
} }
@Override @Override
public ChannelFuture bind(ChannelPromise future) { ChannelFuture doBind(SocketAddress localAddress) {
validate(future); Channel channel = factory().newChannel();
Channel channel = future.channel();
if (channel.isActive()) {
future.setFailure(new IllegalStateException("channel already bound: " + channel));
return future;
}
if (channel.isRegistered()) {
future.setFailure(new IllegalStateException("channel already registered: " + channel));
return future;
}
if (!channel.isOpen()) {
future.setFailure(new ClosedChannelException());
return future;
}
try { try {
channel.config().setOptions(options()); channel.config().setOptions(options());
} catch (Exception e) { } catch (Exception e) {
future.setFailure(e); channel.close();
return future; return channel.newFailedFuture(e);
} }
for (Entry<AttributeKey<?>, Object> e: attrs().entrySet()) { for (Entry<AttributeKey<?>, Object> e: attrs().entrySet()) {
@ -174,7 +160,7 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
channel.attr(key).set(e.getValue()); channel.attr(key).set(e.getValue());
} }
ChannelPipeline p = future.channel().pipeline(); ChannelPipeline p = channel.pipeline();
if (handler() != null) { if (handler() != null) {
p.addLast(handler()); p.addLast(handler());
} }
@ -182,17 +168,10 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
ChannelFuture f = group().register(channel).awaitUninterruptibly(); ChannelFuture f = group().register(channel).awaitUninterruptibly();
if (!f.isSuccess()) { if (!f.isSuccess()) {
future.setFailure(f.cause()); return f;
return future;
} }
if (!ensureOpen(future)) { return channel.bind(localAddress).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
return future;
}
channel.bind(localAddress(), future).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
return future;
} }
@Override @Override
@ -204,14 +183,11 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
} }
@Override @Override
protected void validate() { public void validate() {
super.validate(); super.validate();
if (childHandler == null) { if (childHandler == null) {
throw new IllegalStateException("childHandler not set"); throw new IllegalStateException("childHandler not set");
} }
if (localAddress() == null) {
throw new IllegalStateException("localAddress not set");
}
if (childGroup == null) { if (childGroup == null) {
logger.warn("childGroup is not set. Using parentGroup instead."); logger.warn("childGroup is not set. Using parentGroup instead.");
childGroup = group(); childGroup = group();

View File

@ -20,8 +20,6 @@ import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import org.junit.Test; import org.junit.Test;
import java.net.InetSocketAddress;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public abstract class AbstractEventLoopTest { public abstract class AbstractEventLoopTest {
@ -37,7 +35,7 @@ public abstract class AbstractEventLoopTest {
ServerBootstrap bootstrap = new ServerBootstrap(); ServerBootstrap bootstrap = new ServerBootstrap();
ChannelFuture future = bootstrap.channel(newChannel()).group(group) ChannelFuture future = bootstrap.channel(newChannel()).group(group)
.localAddress(new InetSocketAddress(0)).childHandler(new ChannelInitializer<SocketChannel>() { .childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
@ -50,7 +48,7 @@ public abstract class AbstractEventLoopTest {
} }
}) })
.bind().awaitUninterruptibly(); .bind(0).awaitUninterruptibly();
EventExecutor executor = future.channel().pipeline().context(TestChannelHandler2.class).executor(); EventExecutor executor = future.channel().pipeline().context(TestChannelHandler2.class).executor();
EventExecutor executor1 = future.channel().pipeline().context(TestChannelHandler.class).executor(); EventExecutor executor1 = future.channel().pipeline().context(TestChannelHandler.class).executor();

View File

@ -44,12 +44,10 @@ public class LocalChannelRegistryTest {
cb.group(new LocalEventLoopGroup()) cb.group(new LocalEventLoopGroup())
.channel(LocalChannel.class) .channel(LocalChannel.class)
.remoteAddress(addr)
.handler(new TestHandler()); .handler(new TestHandler());
sb.group(new LocalEventLoopGroup()) sb.group(new LocalEventLoopGroup())
.channel(LocalServerChannel.class) .channel(LocalServerChannel.class)
.localAddress(addr)
.childHandler(new ChannelInitializer<LocalChannel>() { .childHandler(new ChannelInitializer<LocalChannel>() {
@Override @Override
public void initChannel(LocalChannel ch) throws Exception { public void initChannel(LocalChannel ch) throws Exception {
@ -59,10 +57,10 @@ public class LocalChannelRegistryTest {
// Start server // Start server
Channel sc = sb.bind().sync().channel(); Channel sc = sb.bind(addr).sync().channel();
// Connect to the server // Connect to the server
Channel cc = cb.connect().sync().channel(); Channel cc = cb.connect(addr).sync().channel();
// Send a message event up the pipeline. // Send a message event up the pipeline.
cc.pipeline().inboundMessageBuffer().add("Hello, World"); cc.pipeline().inboundMessageBuffer().add("Hello, World");

View File

@ -58,7 +58,6 @@ public class LocalTransportThreadModelTest {
sb = new ServerBootstrap(); sb = new ServerBootstrap();
sb.group(new LocalEventLoopGroup()) sb.group(new LocalEventLoopGroup())
.channel(LocalServerChannel.class) .channel(LocalServerChannel.class)
.localAddress(LocalAddress.ANY)
.childHandler(new ChannelInitializer<LocalChannel>() { .childHandler(new ChannelInitializer<LocalChannel>() {
@Override @Override
public void initChannel(LocalChannel ch) throws Exception { public void initChannel(LocalChannel ch) throws Exception {
@ -71,7 +70,7 @@ public class LocalTransportThreadModelTest {
} }
}); });
localAddr = (LocalAddress) sb.bind().syncUninterruptibly().channel().localAddress(); localAddr = (LocalAddress) sb.bind(LocalAddress.ANY).syncUninterruptibly().channel().localAddress();
} }
@AfterClass @AfterClass

View File

@ -43,7 +43,6 @@ public class LocalTransportThreadModelTest2 {
serverBootstrap serverBootstrap
.group(new LocalEventLoopGroup(), new LocalEventLoopGroup()) .group(new LocalEventLoopGroup(), new LocalEventLoopGroup())
.channel(LocalServerChannel.class) .channel(LocalServerChannel.class)
.localAddress(new LocalAddress(LOCAL_CHANNEL))
.childHandler(serverHandler); .childHandler(serverHandler);
Bootstrap clientBootstrap = new Bootstrap(); Bootstrap clientBootstrap = new Bootstrap();
@ -53,7 +52,7 @@ public class LocalTransportThreadModelTest2 {
.channel(LocalChannel.class) .channel(LocalChannel.class)
.remoteAddress(new LocalAddress(LOCAL_CHANNEL)).handler(clientHandler); .remoteAddress(new LocalAddress(LOCAL_CHANNEL)).handler(clientHandler);
serverBootstrap.bind().sync(); serverBootstrap.bind(new LocalAddress(LOCAL_CHANNEL)).sync();
int count = 100; int count = 100;
for (int i = 1; i < count + 1; i ++) { for (int i = 1; i < count + 1; i ++) {