2012-06-04 22:31:44 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2012 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2012-05-30 01:41:26 +02:00
|
|
|
package io.netty.bootstrap;
|
|
|
|
|
2012-06-10 05:22:32 +02:00
|
|
|
import io.netty.buffer.MessageBuf;
|
2012-06-11 10:02:00 +02:00
|
|
|
import io.netty.buffer.Unpooled;
|
2012-05-30 01:41:26 +02:00
|
|
|
import io.netty.channel.Channel;
|
2012-05-31 23:54:48 +02:00
|
|
|
import io.netty.channel.ChannelException;
|
2012-05-30 01:41:26 +02:00
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelFutureListener;
|
|
|
|
import io.netty.channel.ChannelHandler;
|
2012-06-07 07:52:33 +02:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
2012-06-10 03:48:11 +02:00
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
2012-06-10 05:22:32 +02:00
|
|
|
import io.netty.channel.ChannelInboundMessageHandler;
|
2012-05-30 01:41:26 +02:00
|
|
|
import io.netty.channel.ChannelInitializer;
|
|
|
|
import io.netty.channel.ChannelOption;
|
|
|
|
import io.netty.channel.ChannelPipeline;
|
2012-08-10 13:17:18 +02:00
|
|
|
import io.netty.channel.EventLoopGroup;
|
2012-05-30 01:41:26 +02:00
|
|
|
import io.netty.channel.ServerChannel;
|
2012-05-14 16:57:23 +02:00
|
|
|
import io.netty.logging.InternalLogger;
|
|
|
|
import io.netty.logging.InternalLoggerFactory;
|
2012-06-09 01:44:56 +02:00
|
|
|
import io.netty.util.NetworkConstants;
|
2012-05-14 16:57:23 +02:00
|
|
|
|
2012-05-31 08:47:15 +02:00
|
|
|
import java.net.InetAddress;
|
2012-05-14 16:57:23 +02:00
|
|
|
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;
|
|
|
|
|
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-06-09 01:44:56 +02:00
|
|
|
private static final InetSocketAddress DEFAULT_LOCAL_ADDR = new InetSocketAddress(NetworkConstants.LOCALHOST, 0);
|
2012-05-14 16:57:23 +02:00
|
|
|
|
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>();
|
2012-08-10 13:17:18 +02:00
|
|
|
private EventLoopGroup parentGroup;
|
|
|
|
private EventLoopGroup childGroup;
|
2012-05-15 06:45:25 +02:00
|
|
|
private ServerChannel channel;
|
2012-06-03 10:00:16 +02:00
|
|
|
private ChannelHandler handler;
|
|
|
|
private ChannelHandler childHandler;
|
2012-05-14 16:57:23 +02:00
|
|
|
private SocketAddress localAddress;
|
|
|
|
|
2012-08-10 13:26:04 +02:00
|
|
|
public ServerBootstrap group(EventLoopGroup group) {
|
|
|
|
if (group == null) {
|
|
|
|
throw new NullPointerException("group");
|
|
|
|
}
|
|
|
|
if (parentGroup != null) {
|
|
|
|
throw new IllegalStateException("parentGroup set already");
|
|
|
|
}
|
|
|
|
parentGroup = group;
|
|
|
|
childGroup = group;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-08-10 13:17:18 +02:00
|
|
|
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
|
|
|
|
if (parentGroup == null) {
|
|
|
|
throw new NullPointerException("parentGroup");
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-08-10 13:17:18 +02:00
|
|
|
if (this.parentGroup != null) {
|
|
|
|
throw new IllegalStateException("parentGroup set already");
|
2012-05-30 01:41:26 +02:00
|
|
|
}
|
2012-08-10 13:17:18 +02:00
|
|
|
this.parentGroup = parentGroup;
|
|
|
|
this.childGroup = childGroup;
|
2012-05-14 16:57:23 +02:00
|
|
|
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-06-03 10:00:16 +02:00
|
|
|
public ServerBootstrap handler(ChannelHandler handler) {
|
|
|
|
this.handler = handler;
|
2012-05-14 16:57:23 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-06-03 10:00:16 +02:00
|
|
|
public ServerBootstrap childHandler(ChannelHandler childHandler) {
|
|
|
|
if (childHandler == null) {
|
|
|
|
throw new NullPointerException("childHandler");
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-06-03 10:00:16 +02:00
|
|
|
this.childHandler = childHandler;
|
2012-05-14 16:57:23 +02:00
|
|
|
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-31 08:47:15 +02:00
|
|
|
public ServerBootstrap localAddress(InetAddress host, int port) {
|
|
|
|
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) {
|
2012-05-31 23:54:48 +02:00
|
|
|
validate(future);
|
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-07-07 07:37:44 +02:00
|
|
|
try {
|
|
|
|
channel.config().setOptions(parentOptions);
|
|
|
|
} catch (Exception e) {
|
|
|
|
future.setFailure(e);
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2012-05-15 06:45:25 +02:00
|
|
|
ChannelPipeline p = channel.pipeline();
|
2012-06-03 10:00:16 +02:00
|
|
|
if (handler != null) {
|
|
|
|
p.addLast(handler);
|
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-08-10 13:17:18 +02:00
|
|
|
ChannelFuture f = parentGroup.register(channel).awaitUninterruptibly();
|
2012-05-14 16:57:23 +02:00
|
|
|
if (!f.isSuccess()) {
|
|
|
|
future.setFailure(f.cause());
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2012-05-31 23:54:48 +02:00
|
|
|
if (!channel.isOpen()) {
|
|
|
|
// Registration was successful but the channel was closed due to some failure in
|
2012-06-03 10:00:16 +02:00
|
|
|
// handler.
|
2012-05-31 23:54:48 +02:00
|
|
|
future.setFailure(new ChannelException("initialization failure"));
|
|
|
|
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() {
|
2012-08-10 13:17:18 +02:00
|
|
|
if (parentGroup != null) {
|
|
|
|
parentGroup.shutdown();
|
2012-05-15 06:45:25 +02:00
|
|
|
}
|
2012-08-10 13:17:18 +02:00
|
|
|
if (childGroup != null) {
|
|
|
|
childGroup.shutdown();
|
2012-05-15 06:45:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void validate() {
|
2012-08-10 13:17:18 +02:00
|
|
|
if (parentGroup == null) {
|
|
|
|
throw new IllegalStateException("parentGroup 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
|
|
|
}
|
2012-06-03 10:00:16 +02:00
|
|
|
if (childHandler == null) {
|
|
|
|
throw new IllegalStateException("childHandler not set");
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-08-10 13:17:18 +02:00
|
|
|
if (childGroup == null) {
|
|
|
|
logger.warn("childGroup is not set. Using parentGroup instead.");
|
|
|
|
childGroup = parentGroup;
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
|
|
|
if (localAddress == null) {
|
|
|
|
logger.warn("localAddress is not set. Using " + DEFAULT_LOCAL_ADDR + " instead.");
|
|
|
|
localAddress = DEFAULT_LOCAL_ADDR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-31 23:54:48 +02:00
|
|
|
private void validate(ChannelFuture future) {
|
|
|
|
if (future == null) {
|
|
|
|
throw new NullPointerException("future");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (future.channel() != channel) {
|
|
|
|
throw new IllegalArgumentException("future.channel() must be the same channel.");
|
|
|
|
}
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2012-06-10 05:22:32 +02:00
|
|
|
private class Acceptor
|
|
|
|
extends ChannelInboundHandlerAdapter implements ChannelInboundMessageHandler<Channel> {
|
|
|
|
|
2012-06-10 03:48:11 +02:00
|
|
|
@Override
|
2012-06-10 05:22:32 +02:00
|
|
|
public MessageBuf<Channel> newInboundBuffer(ChannelHandlerContext ctx) throws Exception {
|
2012-06-11 10:02:00 +02:00
|
|
|
return Unpooled.messageBuffer();
|
2012-06-10 03:48:11 +02:00
|
|
|
}
|
2012-05-14 16:57:23 +02:00
|
|
|
|
|
|
|
@Override
|
2012-06-07 07:52:33 +02:00
|
|
|
public void inboundBufferUpdated(ChannelHandlerContext ctx) {
|
2012-06-12 10:02:00 +02:00
|
|
|
MessageBuf<Channel> in = ctx.inboundMessageBuffer();
|
2012-05-14 16:57:23 +02:00
|
|
|
for (;;) {
|
|
|
|
Channel child = in.poll();
|
|
|
|
if (child == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-06-03 10:00:16 +02:00
|
|
|
child.pipeline().addLast(childHandler);
|
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 {
|
2012-08-10 13:17:18 +02:00
|
|
|
childGroup.register(child);
|
2012-05-14 16:57:23 +02:00
|
|
|
} catch (Throwable t) {
|
|
|
|
logger.warn("Failed to register an accepted channel: " + child, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|