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 {
bootstrap = new ServerBootstrap();
bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class)
.localAddress(9999).childHandler(new ChannelInitializer<SocketChannel>() {
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new DiscardServerHandler());
}
});
ChannelFuture f = bootstrap.bind().sync();
ChannelFuture f = bootstrap.bind(9999).sync();
f.channel().closeFuture().sync();
} catch (Exception ex) {
ex.printStackTrace();

View File

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

View File

@ -38,7 +38,6 @@ public class DiscardServer {
try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
@ -47,7 +46,7 @@ public class DiscardServer {
});
// 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.
// 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.LoggingHandler;
import java.net.InetSocketAddress;
/**
* 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
@ -52,7 +50,6 @@ public class EchoClient {
b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
@ -63,7 +60,7 @@ public class EchoClient {
});
// Start the client.
ChannelFuture f = b.connect().sync();
ChannelFuture f = b.connect(host, port).sync();
// Wait until the connection is closed.
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.LoggingHandler;
import java.net.InetSocketAddress;
/**
* Echoes back any received data from a client.
*/
@ -45,7 +43,6 @@ public class EchoServer {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.localAddress(new InetSocketAddress(port))
.childOption(ChannelOption.TCP_NODELAY, true)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@ -58,7 +55,7 @@ public class EchoServer {
});
// Start the server.
ChannelFuture f = b.bind().sync();
ChannelFuture f = b.bind(port).sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();

View File

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

View File

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

View File

@ -34,7 +34,6 @@ import io.netty.util.CharsetUtil;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetSocketAddress;
/**
* 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())
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.localAddress(new InetSocketAddress(port))
.childOption(ChannelOption.TCP_NODELAY, true)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@ -69,7 +67,7 @@ public class FileServer {
});
// Start the server.
ChannelFuture f = b.bind().sync();
ChannelFuture f = b.bind(port).sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();

View File

@ -32,10 +32,9 @@ public class HttpStaticFileServer {
try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(new HttpStaticFileServerInitializer());
b.bind().sync().channel().closeFuture().sync();
b.bind(port).sync().channel().closeFuture().sync();
} finally {
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.HttpVersion;
import java.net.InetSocketAddress;
import java.net.URI;
/**
@ -66,11 +65,10 @@ public class HttpSnoopClient {
try {
b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(new HttpSnoopClientInitializer(ssl))
.remoteAddress(new InetSocketAddress(host, port));
.handler(new HttpSnoopClientInitializer(ssl));
// Make the connection attempt.
Channel ch = b.connect().sync().channel();
Channel ch = b.connect(host, port).sync().channel();
// Prepare the HTTP request.
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.NioServerSocketChannel;
import java.net.InetSocketAddress;
/**
* An HTTP server that sends back the content of the received HTTP request
* in a pretty plaintext form.
@ -41,10 +39,9 @@ public class HttpSnoopServer {
try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.childHandler(new HttpSnoopServerInitializer())
.localAddress(new InetSocketAddress(port));
.childHandler(new HttpSnoopServerInitializer());
Channel ch = b.bind().sync().channel();
Channel ch = b.bind(port).sync().channel();
ch.closeFuture().sync();
} finally {
b.shutdown();

View File

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

View File

@ -36,9 +36,9 @@ public class HttpUploadServer {
ServerBootstrap b = new ServerBootstrap();
try {
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("Open your browser and navigate to http://localhost:" + port + '/');

View File

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

View File

@ -86,7 +86,6 @@ public class WebSocketClient {
b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.remoteAddress(uri.getHost(), uri.getPort())
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
@ -99,7 +98,7 @@ public class WebSocketClient {
});
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();
// 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.websocketx.WebSocketServerProtocolHandler;
import java.net.InetSocketAddress;
/**
* A WebSocket Server that respondes to requests at:
*
@ -63,7 +61,6 @@ public class WebSocketServer {
try {
sb.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
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);
ch.closeFuture().sync();

View File

@ -52,10 +52,9 @@ public class WebSocketServer {
try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.localAddress(port)
.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("Open your browser and navigate to http://localhost:" + port + '/');

View File

@ -51,10 +51,9 @@ public class WebSocketSslServer {
try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.localAddress(port)
.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("Open your browser and navigate to https://localhost:" + port + '/');
ch.closeFuture().sync();

View File

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

View File

@ -48,11 +48,10 @@ public class LocalTimeClient {
try {
b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.remoteAddress(host, port)
.handler(new LocalTimeClientInitializer());
// 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.
LocalTimeClientHandler handler =

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -45,11 +45,10 @@ public class QuoteOfTheMomentClient {
try {
b.group(new NioEventLoopGroup())
.channel(NioDatagramChannel.class)
.localAddress(new InetSocketAddress(0))
.option(ChannelOption.SO_BROADCAST, true)
.handler(new QuoteOfTheMomentClientHandler());
Channel ch = b.bind().sync().channel();
Channel ch = b.bind(0).sync().channel();
// Broadcast the QOTM request to port 8080.
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.NioEventLoopGroup;
import java.net.InetSocketAddress;
/**
* A UDP server that responds to the QOTM (quote of the moment) request to a
* {@link QuoteOfTheMomentClient}.
@ -41,11 +39,10 @@ public class QuoteOfTheMomentServer {
try {
b.group(new NioEventLoopGroup())
.channel(NioDatagramChannel.class)
.localAddress(new InetSocketAddress(port))
.option(ChannelOption.SO_BROADCAST, true)
.handler(new QuoteOfTheMomentServerHandler());
b.bind().sync().channel().closeFuture().await();
b.bind(port).sync().channel().closeFuture().await();
} finally {
b.shutdown();
}

View File

@ -18,12 +18,12 @@ package io.netty.example.rxtx;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
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.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
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
@ -35,7 +35,6 @@ public final class RxtxClient {
try {
b.group(new OioEventLoopGroup())
.channel(RxtxChannel.class)
.remoteAddress(new RxtxDeviceAddress("/dev/ttyUSB0"))
.handler(new ChannelInitializer<RxtxChannel>() {
@Override
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();
} finally {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -21,7 +21,6 @@ import io.netty.channel.ChannelException;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoopGroup;
import io.netty.util.AttributeKey;
@ -37,6 +36,7 @@ import java.util.concurrent.ConcurrentHashMap;
*
*/
public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> {
private EventLoopGroup group;
private ChannelFactory factory;
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
* call the super method in that case.
*/
protected void validate() {
public void validate() {
if (group == null) {
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.
*/
public ChannelFuture bind() {
validate();
Channel channel = factory().newChannel();
return bind(channel.newPromise());
SocketAddress localAddress = this.localAddress;
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.
*/
@ -211,42 +241,27 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> {
return (B) this;
}
protected static boolean ensureOpen(ChannelPromise future) {
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() {
final SocketAddress localAddress() {
return localAddress;
}
protected final ChannelFactory factory() {
final ChannelFactory factory() {
return factory;
}
protected final ChannelHandler handler() {
final ChannelHandler handler() {
return handler;
}
protected final EventLoopGroup group() {
final EventLoopGroup group() {
return group;
}
protected final Map<ChannelOption<?>, Object> options() {
final Map<ChannelOption<?>, Object> options() {
return options;
}
protected final Map<AttributeKey<?>, Object> attrs() {
final Map<AttributeKey<?>, Object> attrs() {
return attrs;
}

View File

@ -20,7 +20,6 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory;
import io.netty.util.AttributeKey;
@ -28,7 +27,6 @@ import io.netty.util.AttributeKey;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;
import java.util.Map.Entry;
/**
@ -67,24 +65,16 @@ public class Bootstrap extends AbstractBootstrap<Bootstrap> {
}
@Override
public ChannelFuture bind(ChannelPromise future) {
validate(future);
if (localAddress() == null) {
throw new IllegalStateException("localAddress not set");
}
ChannelFuture doBind(SocketAddress localAddress) {
Channel channel = factory().newChannel();
try {
init(future.channel());
init(channel);
} catch (Throwable t) {
future.setFailure(t);
return future;
channel.close();
return channel.newFailedFuture(t);
}
if (!ensureOpen(future)) {
return future;
}
return future.channel().bind(localAddress(), future).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
return channel.bind(localAddress).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
}
/**
@ -92,50 +82,76 @@ public class Bootstrap extends AbstractBootstrap<Bootstrap> {
*/
public ChannelFuture connect() {
validate();
Channel channel = factory().newChannel();
return connect(channel.newPromise());
SocketAddress remoteAddress = this.remoteAddress;
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()}
*/
public ChannelFuture connect(ChannelPromise future) {
validate(future);
if (remoteAddress == null) {
throw new IllegalStateException("remoteAddress not set");
}
private ChannelFuture doConnect(SocketAddress remoteAddress, SocketAddress localAddress) {
final Channel channel = factory().newChannel();
try {
init(future.channel());
init(channel);
} catch (Throwable t) {
future.setFailure(t);
return future;
channel.close();
return channel.newFailedFuture(t);
}
if (!ensureOpen(future)) {
return future;
}
if (localAddress() == null) {
future.channel().connect(remoteAddress, future);
final ChannelFuture future;
if (localAddress == null) {
future = channel.connect(remoteAddress);
} else {
future.channel().connect(remoteAddress, localAddress(), future);
future = channel.connect(remoteAddress, localAddress);
}
return future.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
}
@SuppressWarnings("unchecked")
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();
p.addLast(handler());
@ -157,7 +173,7 @@ public class Bootstrap extends AbstractBootstrap<Bootstrap> {
}
@Override
protected void validate() {
public void validate() {
super.validate();
if (handler() == null) {
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.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.socket.SocketChannel;
@ -35,7 +34,7 @@ import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory;
import io.netty.util.AttributeKey;
import java.nio.channels.ClosedChannelException;
import java.net.SocketAddress;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
@ -145,27 +144,14 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
}
@Override
public ChannelFuture bind(ChannelPromise future) {
validate(future);
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;
}
ChannelFuture doBind(SocketAddress localAddress) {
Channel channel = factory().newChannel();
try {
channel.config().setOptions(options());
} catch (Exception e) {
future.setFailure(e);
return future;
channel.close();
return channel.newFailedFuture(e);
}
for (Entry<AttributeKey<?>, Object> e: attrs().entrySet()) {
@ -174,7 +160,7 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
channel.attr(key).set(e.getValue());
}
ChannelPipeline p = future.channel().pipeline();
ChannelPipeline p = channel.pipeline();
if (handler() != null) {
p.addLast(handler());
}
@ -182,17 +168,10 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
ChannelFuture f = group().register(channel).awaitUninterruptibly();
if (!f.isSuccess()) {
future.setFailure(f.cause());
return future;
return f;
}
if (!ensureOpen(future)) {
return future;
}
channel.bind(localAddress(), future).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
return future;
return channel.bind(localAddress).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
}
@Override
@ -204,14 +183,11 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
}
@Override
protected void validate() {
public void validate() {
super.validate();
if (childHandler == null) {
throw new IllegalStateException("childHandler not set");
}
if (localAddress() == null) {
throw new IllegalStateException("localAddress not set");
}
if (childGroup == null) {
logger.warn("childGroup is not set. Using parentGroup instead.");
childGroup = group();

View File

@ -20,8 +20,6 @@ import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import org.junit.Test;
import java.net.InetSocketAddress;
import static org.junit.Assert.*;
public abstract class AbstractEventLoopTest {
@ -37,7 +35,7 @@ public abstract class AbstractEventLoopTest {
ServerBootstrap bootstrap = new ServerBootstrap();
ChannelFuture future = bootstrap.channel(newChannel()).group(group)
.localAddress(new InetSocketAddress(0)).childHandler(new ChannelInitializer<SocketChannel>() {
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
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 executor1 = future.channel().pipeline().context(TestChannelHandler.class).executor();

View File

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

View File

@ -58,7 +58,6 @@ public class LocalTransportThreadModelTest {
sb = new ServerBootstrap();
sb.group(new LocalEventLoopGroup())
.channel(LocalServerChannel.class)
.localAddress(LocalAddress.ANY)
.childHandler(new ChannelInitializer<LocalChannel>() {
@Override
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

View File

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