2012-05-30 01:41:26 +02:00
|
|
|
package io.netty.bootstrap;
|
|
|
|
|
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import io.netty.channel.ChannelBufferHolder;
|
|
|
|
import io.netty.channel.ChannelBufferHolders;
|
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelFutureListener;
|
|
|
|
import io.netty.channel.ChannelHandler;
|
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
|
import io.netty.channel.ChannelInboundHandlerContext;
|
|
|
|
import io.netty.channel.ChannelInitializer;
|
|
|
|
import io.netty.channel.ChannelOption;
|
|
|
|
import io.netty.channel.ChannelPipeline;
|
|
|
|
import io.netty.channel.EventLoop;
|
|
|
|
import io.netty.channel.ServerChannel;
|
2012-05-14 16:57:23 +02:00
|
|
|
import io.netty.logging.InternalLogger;
|
|
|
|
import io.netty.logging.InternalLoggerFactory;
|
|
|
|
import io.netty.util.SocketAddresses;
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.net.SocketAddress;
|
|
|
|
import java.nio.channels.ClosedChannelException;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Queue;
|
|
|
|
|
2012-05-30 01:41:26 +02:00
|
|
|
public class ServerBootstrap {
|
2012-05-14 16:57:23 +02:00
|
|
|
|
2012-05-30 01:41:26 +02:00
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ServerBootstrap.class);
|
2012-05-14 16:57:23 +02:00
|
|
|
private static final InetSocketAddress DEFAULT_LOCAL_ADDR = new InetSocketAddress(SocketAddresses.LOCALHOST, 0);
|
|
|
|
|
2012-05-28 10:23:58 +02:00
|
|
|
private final ChannelHandler acceptor = new ChannelInitializer<Channel>() {
|
2012-05-15 06:45:25 +02:00
|
|
|
@Override
|
|
|
|
public void initChannel(Channel ch) throws Exception {
|
2012-05-30 01:41:26 +02:00
|
|
|
ch.pipeline().addLast(new Acceptor());
|
2012-05-15 06:45:25 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-14 16:57:23 +02:00
|
|
|
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;
|
2012-05-15 06:45:25 +02:00
|
|
|
private ServerChannel channel;
|
|
|
|
private ChannelHandler initializer;
|
2012-05-14 16:57:23 +02:00
|
|
|
private ChannelHandler childInitializer;
|
|
|
|
private SocketAddress localAddress;
|
|
|
|
|
2012-05-30 01:41:26 +02:00
|
|
|
public ServerBootstrap eventLoop(EventLoop parentEventLoop, EventLoop childEventLoop) {
|
2012-05-14 16:57:23 +02:00
|
|
|
if (parentEventLoop == null) {
|
|
|
|
throw new NullPointerException("parentEventLoop");
|
|
|
|
}
|
2012-05-30 01:41:26 +02:00
|
|
|
if (this.parentEventLoop != null) {
|
|
|
|
throw new IllegalStateException("eventLoop set already");
|
|
|
|
}
|
2012-05-14 16:57:23 +02:00
|
|
|
this.parentEventLoop = parentEventLoop;
|
|
|
|
this.childEventLoop = childEventLoop;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-05-30 01:41:26 +02:00
|
|
|
public ServerBootstrap channel(ServerChannel channel) {
|
2012-05-15 06:45:25 +02:00
|
|
|
if (channel == null) {
|
|
|
|
throw new NullPointerException("channel");
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-05-30 01:41:26 +02:00
|
|
|
if (this.channel != null) {
|
|
|
|
throw new IllegalStateException("channel set already");
|
|
|
|
}
|
2012-05-15 06:45:25 +02:00
|
|
|
this.channel = channel;
|
2012-05-14 16:57:23 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-05-30 01:41:26 +02:00
|
|
|
public <T> ServerBootstrap option(ChannelOption<T> parentOption, T value) {
|
2012-05-14 16:57:23 +02:00
|
|
|
if (parentOption == null) {
|
|
|
|
throw new NullPointerException("parentOption");
|
|
|
|
}
|
|
|
|
if (value == null) {
|
|
|
|
parentOptions.remove(parentOption);
|
|
|
|
} else {
|
|
|
|
parentOptions.put(parentOption, value);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-05-30 01:41:26 +02:00
|
|
|
public <T> ServerBootstrap childOption(ChannelOption<T> childOption, T value) {
|
2012-05-14 16:57:23 +02:00
|
|
|
if (childOption == null) {
|
|
|
|
throw new NullPointerException("childOption");
|
|
|
|
}
|
|
|
|
if (value == null) {
|
|
|
|
childOptions.remove(childOption);
|
|
|
|
} else {
|
|
|
|
childOptions.put(childOption, value);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-05-30 01:41:26 +02:00
|
|
|
public ServerBootstrap initializer(ChannelHandler initializer) {
|
2012-05-15 06:45:25 +02:00
|
|
|
this.initializer = initializer;
|
2012-05-14 16:57:23 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-05-30 01:41:26 +02:00
|
|
|
public ServerBootstrap childInitializer(ChannelHandler childInitializer) {
|
2012-05-14 16:57:23 +02:00
|
|
|
if (childInitializer == null) {
|
|
|
|
throw new NullPointerException("childInitializer");
|
|
|
|
}
|
|
|
|
this.childInitializer = childInitializer;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-05-30 01:41:26 +02:00
|
|
|
public ServerBootstrap localAddress(SocketAddress localAddress) {
|
2012-05-14 16:57:23 +02:00
|
|
|
if (localAddress == null) {
|
|
|
|
throw new NullPointerException("localAddress");
|
|
|
|
}
|
|
|
|
this.localAddress = localAddress;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-05-30 01:41:26 +02:00
|
|
|
public ServerBootstrap localAddress(int port) {
|
2012-05-28 10:23:58 +02:00
|
|
|
localAddress = new InetSocketAddress(port);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-05-30 01:41:26 +02:00
|
|
|
public ServerBootstrap localAddress(String host, int port) {
|
2012-05-28 10:23:58 +02:00
|
|
|
localAddress = new InetSocketAddress(host, port);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-05-14 16:57:23 +02:00
|
|
|
public ChannelFuture bind() {
|
|
|
|
validate();
|
2012-05-15 06:45:25 +02:00
|
|
|
return bind(channel.newFuture());
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ChannelFuture bind(ChannelFuture future) {
|
|
|
|
validate();
|
2012-05-15 06:45:25 +02:00
|
|
|
if (channel.isActive()) {
|
|
|
|
future.setFailure(new IllegalStateException("channel already bound: " + channel));
|
2012-05-14 16:57:23 +02:00
|
|
|
return future;
|
|
|
|
}
|
2012-05-15 06:45:25 +02:00
|
|
|
if (channel.isRegistered()) {
|
|
|
|
future.setFailure(new IllegalStateException("channel already registered: " + channel));
|
2012-05-14 16:57:23 +02:00
|
|
|
return future;
|
|
|
|
}
|
2012-05-15 06:45:25 +02:00
|
|
|
if (!channel.isOpen()) {
|
2012-05-14 16:57:23 +02:00
|
|
|
future.setFailure(new ClosedChannelException());
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2012-05-15 06:45:25 +02:00
|
|
|
ChannelPipeline p = channel.pipeline();
|
|
|
|
if (initializer != null) {
|
2012-05-30 01:41:26 +02:00
|
|
|
p.addLast(initializer);
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-05-30 01:41:26 +02:00
|
|
|
p.addLast(acceptor);
|
2012-05-14 16:57:23 +02:00
|
|
|
|
2012-05-15 06:45:25 +02:00
|
|
|
ChannelFuture f = parentEventLoop.register(channel).awaitUninterruptibly();
|
2012-05-14 16:57:23 +02:00
|
|
|
if (!f.isSuccess()) {
|
|
|
|
future.setFailure(f.cause());
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2012-05-15 06:45:25 +02:00
|
|
|
channel.bind(localAddress, future).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
|
2012-05-14 16:57:23 +02:00
|
|
|
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2012-05-15 06:45:25 +02:00
|
|
|
public void shutdown() {
|
|
|
|
if (parentEventLoop != null) {
|
|
|
|
parentEventLoop.shutdown();
|
|
|
|
}
|
|
|
|
if (childEventLoop != null) {
|
|
|
|
childEventLoop.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void validate() {
|
2012-05-14 16:57:23 +02:00
|
|
|
if (parentEventLoop == null) {
|
2012-05-15 06:45:25 +02:00
|
|
|
throw new IllegalStateException("eventLoop not set");
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-05-15 06:45:25 +02:00
|
|
|
if (channel == null) {
|
|
|
|
throw new IllegalStateException("channel not set");
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
|
|
|
if (childInitializer == null) {
|
|
|
|
throw new IllegalStateException("childInitializer not set");
|
|
|
|
}
|
|
|
|
if (childEventLoop == null) {
|
2012-05-15 06:45:25 +02:00
|
|
|
logger.warn("childEventLoop is not set. Using eventLoop instead.");
|
2012-05-14 16:57:23 +02:00
|
|
|
childEventLoop = parentEventLoop;
|
|
|
|
}
|
|
|
|
if (localAddress == null) {
|
|
|
|
logger.warn("localAddress is not set. Using " + DEFAULT_LOCAL_ADDR + " instead.");
|
|
|
|
localAddress = DEFAULT_LOCAL_ADDR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class Acceptor extends ChannelInboundHandlerAdapter<Channel> {
|
|
|
|
@Override
|
|
|
|
public ChannelBufferHolder<Channel> newInboundBuffer(ChannelInboundHandlerContext<Channel> ctx) {
|
2012-05-16 16:02:06 +02:00
|
|
|
return ChannelBufferHolders.messageBuffer();
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void inboundBufferUpdated(ChannelInboundHandlerContext<Channel> ctx) {
|
2012-05-29 21:09:29 +02:00
|
|
|
Queue<Channel> in = ctx.inbound().messageBuffer();
|
2012-05-14 16:57:23 +02:00
|
|
|
for (;;) {
|
|
|
|
Channel child = in.poll();
|
|
|
|
if (child == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-05-30 01:41:26 +02:00
|
|
|
child.pipeline().addLast(childInitializer);
|
2012-05-14 16:57:23 +02:00
|
|
|
|
|
|
|
for (Entry<ChannelOption<?>, Object> e: childOptions.entrySet()) {
|
|
|
|
try {
|
|
|
|
if (!child.config().setOption((ChannelOption<Object>) e.getKey(), e.getValue())) {
|
|
|
|
logger.warn("Unknown channel option: " + e);
|
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
|
|
|
logger.warn("Failed to set a channel option: " + child, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
childEventLoop.register(child);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
logger.warn("Failed to register an accepted channel: " + child, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|