Rename (Server)ChannelBuilder to (Server)ChannelBootstrap

- It does not build a new Channel but just helps bootstrapping it.
- Added shutdown() method for simpler deinitialization
- ServerChannelBootstrap has shorter method names for the parent channel
This commit is contained in:
Trustin Lee 2012-05-15 13:45:25 +09:00
parent 128851dd09
commit d01d1d0843
4 changed files with 75 additions and 66 deletions

View File

@ -16,7 +16,7 @@
package io.netty.example.echo;
import io.netty.channel.Channel;
import io.netty.channel.ChannelBuilder;
import io.netty.channel.ChannelBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
@ -52,7 +52,7 @@ public class EchoClient {
EventLoop loop = new SelectorEventLoop();
try {
// Configure the client.
ChannelBuilder b = new ChannelBuilder();
ChannelBootstrap b = new ChannelBootstrap();
b.eventLoop(loop)
.channel(new NioSocketChannel())
.option(ChannelOption.TCP_NODELAY, true)

View File

@ -20,8 +20,7 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoop;
import io.netty.channel.ServerChannelBuilder;
import io.netty.channel.ServerChannelBootstrap;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.SelectorEventLoop;
import io.netty.handler.logging.LogLevel;
@ -41,17 +40,13 @@ public class EchoServer {
}
public void run() throws Exception {
// Create the required event loops.
EventLoop parentLoop = new SelectorEventLoop();
EventLoop childLoop = new SelectorEventLoop();
// Configure the server.
ServerChannelBootstrap b = new ServerChannelBootstrap();
try {
// Configure the server.
ServerChannelBuilder b = new ServerChannelBuilder();
b.parentEventLoop(parentLoop)
.parentChannel(new NioServerSocketChannel())
.parentOption(ChannelOption.SO_BACKLOG, 100)
b.eventLoop(new SelectorEventLoop(), new SelectorEventLoop())
.channel(new NioServerSocketChannel())
.option(ChannelOption.SO_BACKLOG, 100)
.localAddress(new InetSocketAddress(port))
.childEventLoop(childLoop)
.childOption(ChannelOption.TCP_NODELAY, true)
.childInitializer(new ChannelInitializer() {
@Override
@ -69,8 +64,7 @@ public class EchoServer {
f.channel().closeFuture().sync();
} finally {
// Shut down all event loops to terminate all threads.
parentLoop.shutdown();
childLoop.shutdown();
b.shutdown();
}
}

View File

@ -9,9 +9,9 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
public class ChannelBuilder {
public class ChannelBootstrap {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ChannelBuilder.class);
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ChannelBootstrap.class);
private final Map<ChannelOption<?>, Object> options = new LinkedHashMap<ChannelOption<?>, Object>();
private EventLoop eventLoop;
@ -20,7 +20,7 @@ public class ChannelBuilder {
private SocketAddress localAddress;
private SocketAddress remoteAddress;
public ChannelBuilder eventLoop(EventLoop eventLoop) {
public ChannelBootstrap eventLoop(EventLoop eventLoop) {
if (eventLoop == null) {
throw new NullPointerException("eventLoop");
}
@ -28,7 +28,7 @@ public class ChannelBuilder {
return this;
}
public ChannelBuilder channel(Channel channel) {
public ChannelBootstrap channel(Channel channel) {
if (channel == null) {
throw new NullPointerException("channel");
}
@ -36,7 +36,7 @@ public class ChannelBuilder {
return this;
}
public <T> ChannelBuilder option(ChannelOption<T> option, T value) {
public <T> ChannelBootstrap option(ChannelOption<T> option, T value) {
if (option == null) {
throw new NullPointerException("option");
}
@ -48,7 +48,7 @@ public class ChannelBuilder {
return this;
}
public ChannelBuilder initializer(ChannelHandler initializer) {
public ChannelBootstrap initializer(ChannelHandler initializer) {
if (initializer == null) {
throw new NullPointerException("initializer");
}
@ -56,12 +56,12 @@ public class ChannelBuilder {
return this;
}
public ChannelBuilder localAddress(SocketAddress localAddress) {
public ChannelBootstrap localAddress(SocketAddress localAddress) {
this.localAddress = localAddress;
return this;
}
public ChannelBuilder remoteAddress(SocketAddress remoteAddress) {
public ChannelBootstrap remoteAddress(SocketAddress remoteAddress) {
this.remoteAddress = remoteAddress;
return this;
}
@ -140,7 +140,13 @@ public class ChannelBuilder {
eventLoop.register(channel).syncUninterruptibly();
}
public void validate() {
public void shutdown() {
if (eventLoop != null) {
eventLoop.shutdown();
}
}
private void validate() {
if (eventLoop == null) {
throw new IllegalStateException("eventLoop not set");
}

View File

@ -13,46 +13,46 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
public class ServerChannelBuilder {
public class ServerChannelBootstrap {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ServerChannelBuilder.class);
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ServerChannelBootstrap.class);
private static final InetSocketAddress DEFAULT_LOCAL_ADDR = new InetSocketAddress(SocketAddresses.LOCALHOST, 0);
private final Acceptor acceptor = new Acceptor();
private final ChannelHandler acceptor = new ChannelInitializer() {
@Override
public void initChannel(Channel ch) throws Exception {
Acceptor acceptor = new Acceptor();
ch.pipeline().addLast(ChannelBootstrap.generateName(acceptor), acceptor);
}
};
private final Map<ChannelOption<?>, Object> parentOptions = new LinkedHashMap<ChannelOption<?>, Object>();
private final Map<ChannelOption<?>, Object> childOptions = new LinkedHashMap<ChannelOption<?>, Object>();
private EventLoop parentEventLoop;
private EventLoop childEventLoop;
private ServerChannel parentChannel;
private ChannelHandler parentInitializer;
private ServerChannel channel;
private ChannelHandler initializer;
private ChannelHandler childInitializer;
private SocketAddress localAddress;
public ServerChannelBuilder parentEventLoop(EventLoop parentEventLoop) {
public ServerChannelBootstrap eventLoop(EventLoop parentEventLoop, EventLoop childEventLoop) {
if (parentEventLoop == null) {
throw new NullPointerException("parentEventLoop");
}
this.parentEventLoop = parentEventLoop;
return this;
}
public ServerChannelBuilder childEventLoop(EventLoop childEventLoop) {
if (childEventLoop == null) {
throw new NullPointerException("childEventLoop");
}
this.childEventLoop = childEventLoop;
return this;
}
public ServerChannelBuilder parentChannel(ServerChannel parentChannel) {
if (parentChannel == null) {
throw new NullPointerException("parentChannel");
public ServerChannelBootstrap channel(ServerChannel channel) {
if (channel == null) {
throw new NullPointerException("channel");
}
this.parentChannel = parentChannel;
this.channel = channel;
return this;
}
public <T> ServerChannelBuilder parentOption(ChannelOption<T> parentOption, T value) {
public <T> ServerChannelBootstrap option(ChannelOption<T> parentOption, T value) {
if (parentOption == null) {
throw new NullPointerException("parentOption");
}
@ -64,7 +64,7 @@ public class ServerChannelBuilder {
return this;
}
public <T> ServerChannelBuilder childOption(ChannelOption<T> childOption, T value) {
public <T> ServerChannelBootstrap childOption(ChannelOption<T> childOption, T value) {
if (childOption == null) {
throw new NullPointerException("childOption");
}
@ -76,12 +76,12 @@ public class ServerChannelBuilder {
return this;
}
public ServerChannelBuilder parentInitializer(ChannelHandler parentInitializer) {
this.parentInitializer = parentInitializer;
public ServerChannelBootstrap initializer(ChannelHandler initializer) {
this.initializer = initializer;
return this;
}
public ServerChannelBuilder childInitializer(ChannelHandler childInitializer) {
public ServerChannelBootstrap childInitializer(ChannelHandler childInitializer) {
if (childInitializer == null) {
throw new NullPointerException("childInitializer");
}
@ -89,7 +89,7 @@ public class ServerChannelBuilder {
return this;
}
public ServerChannelBuilder localAddress(SocketAddress localAddress) {
public ServerChannelBootstrap localAddress(SocketAddress localAddress) {
if (localAddress == null) {
throw new NullPointerException("localAddress");
}
@ -99,53 +99,62 @@ public class ServerChannelBuilder {
public ChannelFuture bind() {
validate();
return bind(parentChannel.newFuture());
return bind(channel.newFuture());
}
public ChannelFuture bind(ChannelFuture future) {
validate();
if (parentChannel.isActive()) {
future.setFailure(new IllegalStateException("parentChannel already bound: " + parentChannel));
if (channel.isActive()) {
future.setFailure(new IllegalStateException("channel already bound: " + channel));
return future;
}
if (parentChannel.isRegistered()) {
future.setFailure(new IllegalStateException("parentChannel already registered: " + parentChannel));
if (channel.isRegistered()) {
future.setFailure(new IllegalStateException("channel already registered: " + channel));
return future;
}
if (!parentChannel.isOpen()) {
if (!channel.isOpen()) {
future.setFailure(new ClosedChannelException());
return future;
}
ChannelPipeline p = parentChannel.pipeline();
if (parentInitializer != null) {
p.addLast(ChannelBuilder.generateName(parentInitializer), parentInitializer);
ChannelPipeline p = channel.pipeline();
if (initializer != null) {
p.addLast(ChannelBootstrap.generateName(initializer), initializer);
}
p.addLast(ChannelBuilder.generateName(acceptor), acceptor);
p.addLast(ChannelBootstrap.generateName(acceptor), acceptor);
ChannelFuture f = parentEventLoop.register(parentChannel).awaitUninterruptibly();
ChannelFuture f = parentEventLoop.register(channel).awaitUninterruptibly();
if (!f.isSuccess()) {
future.setFailure(f.cause());
return future;
}
parentChannel.bind(localAddress, future).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
channel.bind(localAddress, future).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
return future;
}
public void validate() {
if (parentEventLoop == null) {
throw new IllegalStateException("parentEventLoop not set");
public void shutdown() {
if (parentEventLoop != null) {
parentEventLoop.shutdown();
}
if (parentChannel == null) {
throw new IllegalStateException("parentChannel not set");
if (childEventLoop != null) {
childEventLoop.shutdown();
}
}
private void validate() {
if (parentEventLoop == null) {
throw new IllegalStateException("eventLoop not set");
}
if (channel == null) {
throw new IllegalStateException("channel not set");
}
if (childInitializer == null) {
throw new IllegalStateException("childInitializer not set");
}
if (childEventLoop == null) {
logger.warn("childEventLoop is not set. Using parentEventLoop instead.");
logger.warn("childEventLoop is not set. Using eventLoop instead.");
childEventLoop = parentEventLoop;
}
if (localAddress == null) {
@ -169,7 +178,7 @@ public class ServerChannelBuilder {
break;
}
child.pipeline().addLast(ChannelBuilder.generateName(childInitializer), childInitializer);
child.pipeline().addLast(ChannelBootstrap.generateName(childInitializer), childInitializer);
for (Entry<ChannelOption<?>, Object> e: childOptions.entrySet()) {
try {