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-09-22 05:05:00 +02:00
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelFutureListener;
|
2012-05-30 01:41:26 +02:00
|
|
|
import io.netty.channel.ChannelHandler;
|
2012-06-07 07:52:33 +02:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
2012-09-11 08:31:20 +02:00
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
2012-09-22 05:05:00 +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;
|
2012-09-22 05:05:00 +02:00
|
|
|
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-09-11 10:04:05 +02:00
|
|
|
import io.netty.channel.socket.SocketChannel;
|
2012-05-14 16:57:23 +02:00
|
|
|
import io.netty.logging.InternalLogger;
|
|
|
|
import io.netty.logging.InternalLoggerFactory;
|
2012-10-26 23:52:50 +02:00
|
|
|
import io.netty.util.AttributeKey;
|
2012-06-09 01:44:56 +02:00
|
|
|
import io.netty.util.NetworkConstants;
|
2012-05-14 16:57:23 +02:00
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.nio.channels.ClosedChannelException;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* {@link Bootstrap} sub-class which allows easy bootstrap of {@link ServerChannel}
|
|
|
|
*
|
|
|
|
*/
|
2012-09-12 14:04:41 +02:00
|
|
|
public class ServerBootstrap extends AbstractBootstrap<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> childOptions = new LinkedHashMap<ChannelOption<?>, Object>();
|
2012-10-26 23:52:50 +02:00
|
|
|
private final Map<AttributeKey<?>, Object> childAttrs = new LinkedHashMap<AttributeKey<?>, Object>();
|
2012-08-10 13:17:18 +02:00
|
|
|
private EventLoopGroup childGroup;
|
2012-06-03 10:00:16 +02:00
|
|
|
private ChannelHandler childHandler;
|
2012-05-14 16:57:23 +02:00
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* Specify the {@link EventLoopGroup} which is used for the parent (acceptor) and the child (client).
|
|
|
|
*/
|
2012-09-11 08:31:20 +02:00
|
|
|
@Override
|
2012-08-10 13:26:04 +02:00
|
|
|
public ServerBootstrap group(EventLoopGroup group) {
|
2012-09-11 08:31:20 +02:00
|
|
|
return group(group, group);
|
2012-08-10 13:26:04 +02:00
|
|
|
}
|
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* Set the {@link EventLoopGroup} for the parent (acceptor) and the child (client). These
|
|
|
|
* {@link EventLoopGroup}'s are used to handle all the events and IO for {@link SocketChannel} and
|
|
|
|
* {@link Channel}'s.
|
|
|
|
*/
|
2012-08-10 13:17:18 +02:00
|
|
|
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
|
2012-09-11 08:31:20 +02:00
|
|
|
super.group(parentGroup);
|
|
|
|
if (childGroup == null) {
|
|
|
|
throw new NullPointerException("childGroup");
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-09-11 08:31:20 +02:00
|
|
|
if (this.childGroup != null) {
|
|
|
|
throw new IllegalStateException("childGroup set already");
|
2012-05-30 01:41:26 +02:00
|
|
|
}
|
2012-08-10 13:17:18 +02:00
|
|
|
this.childGroup = childGroup;
|
2012-05-14 16:57:23 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* The {@link Class} which is used to create the {@link ServerChannel} from (for the acceptor).
|
|
|
|
*/
|
2012-09-11 08:31:20 +02:00
|
|
|
@Override
|
|
|
|
public ServerBootstrap channel(Class<? extends Channel> channelClass) {
|
|
|
|
if (channelClass == null) {
|
|
|
|
throw new NullPointerException("channelClass");
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-09-11 08:31:20 +02:00
|
|
|
if (!ServerChannel.class.isAssignableFrom(channelClass)) {
|
|
|
|
throw new IllegalArgumentException();
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-09-11 08:31:20 +02:00
|
|
|
return super.channel(channelClass);
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* Allow to specify a {@link ChannelOption} which is used for the {@link Channel} instances once they get created
|
|
|
|
* (after the acceptor accepted the {@link Channel}). Use a value of <code>null</code> to remove a previous set
|
|
|
|
* {@link ChannelOption}.
|
|
|
|
*/
|
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-10-26 23:52:50 +02:00
|
|
|
public <T> ServerBootstrap childAttr(AttributeKey<T> childKey, T value) {
|
|
|
|
if (childKey == null) {
|
|
|
|
throw new NullPointerException("childKey");
|
|
|
|
}
|
|
|
|
if (value == null) {
|
|
|
|
childAttrs.remove(childKey);
|
|
|
|
} else {
|
|
|
|
childAttrs.put(childKey, value);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* Set the {@link ChannelHandler} which is used to server the request for the {@link Channel}'s.
|
|
|
|
*/
|
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-09-11 08:31:20 +02:00
|
|
|
@Override
|
2012-05-14 16:57:23 +02:00
|
|
|
public ChannelFuture bind(ChannelFuture future) {
|
2012-05-31 23:54:48 +02:00
|
|
|
validate(future);
|
2012-09-11 08:31:20 +02:00
|
|
|
Channel channel = future.channel();
|
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 {
|
2012-09-11 08:31:20 +02:00
|
|
|
channel.config().setOptions(options());
|
2012-07-07 07:37:44 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
future.setFailure(e);
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2012-10-26 23:52:50 +02:00
|
|
|
for (Entry<AttributeKey<?>, Object> e: attrs().entrySet()) {
|
|
|
|
channel.attr((AttributeKey<Object>) e.getKey()).set(e.getValue());
|
|
|
|
}
|
|
|
|
|
2012-09-11 08:31:20 +02:00
|
|
|
ChannelPipeline p = future.channel().pipeline();
|
2012-10-09 20:55:24 +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-09-11 08:31:20 +02:00
|
|
|
ChannelFuture f = group().register(channel).awaitUninterruptibly();
|
2012-05-14 16:57:23 +02:00
|
|
|
if (!f.isSuccess()) {
|
|
|
|
future.setFailure(f.cause());
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2012-09-11 08:31:20 +02:00
|
|
|
if (!ensureOpen(future)) {
|
2012-05-31 23:54:48 +02:00
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2012-09-11 08:31:20 +02:00
|
|
|
channel.bind(localAddress(), future).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
|
2012-05-14 16:57:23 +02:00
|
|
|
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2012-09-11 08:31:20 +02:00
|
|
|
@Override
|
2012-05-15 06:45:25 +02:00
|
|
|
public void shutdown() {
|
2012-09-11 08:31:20 +02:00
|
|
|
super.shutdown();
|
2012-08-10 13:17:18 +02:00
|
|
|
if (childGroup != null) {
|
|
|
|
childGroup.shutdown();
|
2012-05-15 06:45:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 08:31:20 +02:00
|
|
|
@Override
|
|
|
|
protected void validate() {
|
|
|
|
super.validate();
|
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.");
|
2012-09-11 08:31:20 +02:00
|
|
|
childGroup = group();
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-09-11 08:31:20 +02:00
|
|
|
if (localAddress() == null) {
|
2012-05-14 16:57:23 +02:00
|
|
|
logger.warn("localAddress is not set. Using " + DEFAULT_LOCAL_ADDR + " instead.");
|
2012-09-11 08:31:20 +02:00
|
|
|
localAddress(DEFAULT_LOCAL_ADDR);
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-31 23:54:48 +02:00
|
|
|
|
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
|
|
|
|
2012-09-11 08:31:20 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-26 23:52:50 +02:00
|
|
|
for (Entry<AttributeKey<?>, Object> e: childAttrs.entrySet()) {
|
|
|
|
child.attr((AttributeKey<Object>) e.getKey()).set(e.getValue());
|
|
|
|
}
|
|
|
|
|
2012-05-14 16:57:23 +02:00
|
|
|
try {
|
2012-08-10 13:17:18 +02:00
|
|
|
childGroup.register(child);
|
2012-05-14 16:57:23 +02:00
|
|
|
} catch (Throwable t) {
|
2012-09-22 05:05:00 +02:00
|
|
|
child.unsafe().closeForcibly();
|
2012-05-14 16:57:23 +02:00
|
|
|
logger.warn("Failed to register an accepted channel: " + child, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|