- Fixes #1808 - Move all methods in ChannelInboundHandler and ChannelOutboundHandler up to ChannelHandler - Remove ChannelInboundHandler and ChannelOutboundHandler - Deprecate ChannelInboundHandlerAdapter, ChannelOutboundHandlerAdapter, and ChannelDuplexHandler - Replace CombinedChannelDuplexHandler with ChannelHandlerAppender because it's not possible to combine two handlers into one easily now - Introduce 'Skip' annotation to pass events through efficiently - Remove all references to the deprecated types and update Javadoc
384 lines
14 KiB
Java
384 lines
14 KiB
Java
/*
|
|
* 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.
|
|
*/
|
|
package io.netty.bootstrap;
|
|
|
|
import io.netty.channel.Channel;
|
|
import io.netty.channel.ChannelConfig;
|
|
import io.netty.channel.ChannelException;
|
|
import io.netty.channel.ChannelHandler;
|
|
import io.netty.channel.ChannelHandlerAdapter;
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
import io.netty.channel.ChannelInitializer;
|
|
import io.netty.channel.ChannelOption;
|
|
import io.netty.channel.ChannelPipeline;
|
|
import io.netty.channel.EventLoop;
|
|
import io.netty.channel.EventLoopGroup;
|
|
import io.netty.channel.ServerChannel;
|
|
import io.netty.channel.socket.SocketChannel;
|
|
import io.netty.util.AttributeKey;
|
|
import io.netty.util.concurrent.EventExecutorGroup;
|
|
import io.netty.util.internal.StringUtil;
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
|
|
|
import java.lang.reflect.Constructor;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
/**
|
|
* {@link Bootstrap} sub-class which allows easy bootstrap of {@link ServerChannel}
|
|
*
|
|
*/
|
|
public final class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerChannel> {
|
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ServerBootstrap.class);
|
|
|
|
private volatile ServerChannelFactory<? extends ServerChannel> channelFactory;
|
|
private final Map<ChannelOption<?>, Object> childOptions = new LinkedHashMap<ChannelOption<?>, Object>();
|
|
private final Map<AttributeKey<?>, Object> childAttrs = new LinkedHashMap<AttributeKey<?>, Object>();
|
|
private volatile EventLoopGroup childGroup;
|
|
private volatile ChannelHandler childHandler;
|
|
|
|
public ServerBootstrap() { }
|
|
|
|
private ServerBootstrap(ServerBootstrap bootstrap) {
|
|
super(bootstrap);
|
|
channelFactory = bootstrap.channelFactory;
|
|
childGroup = bootstrap.childGroup;
|
|
childHandler = bootstrap.childHandler;
|
|
synchronized (bootstrap.childOptions) {
|
|
childOptions.putAll(bootstrap.childOptions);
|
|
}
|
|
synchronized (bootstrap.childAttrs) {
|
|
childAttrs.putAll(bootstrap.childAttrs);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The {@link Class} which is used to create {@link Channel} instances from.
|
|
* You either use this or {@link #channelFactory(ServerChannelFactory)} if your
|
|
* {@link Channel} implementation has no no-args constructor.
|
|
*/
|
|
public ServerBootstrap channel(Class<? extends ServerChannel> channelClass) {
|
|
if (channelClass == null) {
|
|
throw new NullPointerException("channelClass");
|
|
}
|
|
return channelFactory(new ServerBootstrapChannelFactory<ServerChannel>(channelClass));
|
|
}
|
|
|
|
/**
|
|
* {@link ChannelFactory} which is used to create {@link Channel} instances from
|
|
* when calling {@link #bind()}. This method is usually only used if {@link #channel(Class)}
|
|
* is not working for you because of some more complex needs. If your {@link Channel} implementation
|
|
* has a no-args constructor, its highly recommend to just use {@link #channel(Class)} for
|
|
* simplify your code.
|
|
*/
|
|
public ServerBootstrap channelFactory(ServerChannelFactory<? extends ServerChannel> channelFactory) {
|
|
if (channelFactory == null) {
|
|
throw new NullPointerException("channelFactory");
|
|
}
|
|
if (this.channelFactory != null) {
|
|
throw new IllegalStateException("channelFactory set already");
|
|
}
|
|
|
|
this.channelFactory = channelFactory;
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
Channel createChannel() {
|
|
EventLoop eventLoop = group().next();
|
|
return channelFactory().newChannel(eventLoop, childGroup);
|
|
}
|
|
|
|
ServerChannelFactory<? extends ServerChannel> channelFactory() {
|
|
return channelFactory;
|
|
}
|
|
|
|
/**
|
|
* Specify the {@link EventLoopGroup} which is used for the parent (acceptor) and the child (client).
|
|
*/
|
|
@Override
|
|
public ServerBootstrap group(EventLoopGroup group) {
|
|
return group(group, group);
|
|
}
|
|
|
|
/**
|
|
* Set the {@link EventExecutorGroup} for the parent (acceptor) and the child (client). These
|
|
* {@link EventExecutorGroup}'s are used to handle all the events and IO for {@link SocketChannel} and
|
|
* {@link Channel}'s.
|
|
*/
|
|
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
|
|
super.group(parentGroup);
|
|
if (childGroup == null) {
|
|
throw new NullPointerException("childGroup");
|
|
}
|
|
if (this.childGroup != null) {
|
|
throw new IllegalStateException("childGroup set already");
|
|
}
|
|
this.childGroup = childGroup;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* 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} to remove a previous set
|
|
* {@link ChannelOption}.
|
|
*/
|
|
public <T> ServerBootstrap childOption(ChannelOption<T> childOption, T value) {
|
|
if (childOption == null) {
|
|
throw new NullPointerException("childOption");
|
|
}
|
|
if (value == null) {
|
|
synchronized (childOptions) {
|
|
childOptions.remove(childOption);
|
|
}
|
|
} else {
|
|
synchronized (childOptions) {
|
|
childOptions.put(childOption, value);
|
|
}
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Set the specific {@link AttributeKey} with the given value on every child {@link Channel}. If the value is
|
|
* {@code null} the {@link AttributeKey} is removed
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Set the {@link ChannelHandler} which is used to serve the request for the {@link Channel}'s.
|
|
*/
|
|
public ServerBootstrap childHandler(ChannelHandler childHandler) {
|
|
if (childHandler == null) {
|
|
throw new NullPointerException("childHandler");
|
|
}
|
|
this.childHandler = childHandler;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Return the configured {@link EventLoopGroup} which will be used for the child channels or {@code null}
|
|
* if non is configured yet.
|
|
*/
|
|
public EventLoopGroup childGroup() {
|
|
return childGroup;
|
|
}
|
|
|
|
@Override
|
|
void init(Channel channel) throws Exception {
|
|
final Map<ChannelOption<?>, Object> options = options();
|
|
synchronized (options) {
|
|
channel.config().setOptions(options);
|
|
}
|
|
|
|
final Map<AttributeKey<?>, Object> attrs = attrs();
|
|
synchronized (attrs) {
|
|
for (Entry<AttributeKey<?>, Object> e: attrs.entrySet()) {
|
|
@SuppressWarnings("unchecked")
|
|
AttributeKey<Object> key = (AttributeKey<Object>) e.getKey();
|
|
channel.attr(key).set(e.getValue());
|
|
}
|
|
}
|
|
|
|
ChannelPipeline p = channel.pipeline();
|
|
if (handler() != null) {
|
|
p.addLast(handler());
|
|
}
|
|
|
|
final ChannelHandler currentChildHandler = childHandler;
|
|
final Entry<ChannelOption<?>, Object>[] currentChildOptions;
|
|
final Entry<AttributeKey<?>, Object>[] currentChildAttrs;
|
|
synchronized (childOptions) {
|
|
currentChildOptions = childOptions.entrySet().toArray(newOptionArray(childOptions.size()));
|
|
}
|
|
synchronized (childAttrs) {
|
|
currentChildAttrs = childAttrs.entrySet().toArray(newAttrArray(childAttrs.size()));
|
|
}
|
|
|
|
p.addLast(new ChannelInitializer<Channel>() {
|
|
@Override
|
|
public void initChannel(Channel ch) throws Exception {
|
|
ch.pipeline().addLast(new ServerBootstrapAcceptor(currentChildHandler, currentChildOptions,
|
|
currentChildAttrs));
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public ServerBootstrap validate() {
|
|
super.validate();
|
|
if (childHandler == null) {
|
|
throw new IllegalStateException("childHandler not set");
|
|
}
|
|
if (childGroup == null) {
|
|
logger.warn("childGroup is not set. Using parentGroup instead.");
|
|
childGroup = group();
|
|
}
|
|
return this;
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
private static Entry<ChannelOption<?>, Object>[] newOptionArray(int size) {
|
|
return new Entry[size];
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
private static Entry<AttributeKey<?>, Object>[] newAttrArray(int size) {
|
|
return new Entry[size];
|
|
}
|
|
|
|
private static class ServerBootstrapAcceptor extends ChannelHandlerAdapter {
|
|
|
|
private final ChannelHandler childHandler;
|
|
private final Entry<ChannelOption<?>, Object>[] childOptions;
|
|
private final Entry<AttributeKey<?>, Object>[] childAttrs;
|
|
|
|
ServerBootstrapAcceptor(ChannelHandler childHandler, Entry<ChannelOption<?>, Object>[] childOptions,
|
|
Entry<AttributeKey<?>, Object>[] childAttrs) {
|
|
this.childHandler = childHandler;
|
|
this.childOptions = childOptions;
|
|
this.childAttrs = childAttrs;
|
|
}
|
|
|
|
@Override
|
|
@SuppressWarnings("unchecked")
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
|
Channel child = (Channel) msg;
|
|
|
|
child.pipeline().addLast(childHandler);
|
|
|
|
for (Entry<ChannelOption<?>, Object> e: childOptions) {
|
|
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);
|
|
}
|
|
}
|
|
|
|
for (Entry<AttributeKey<?>, Object> e: childAttrs) {
|
|
child.attr((AttributeKey<Object>) e.getKey()).set(e.getValue());
|
|
}
|
|
|
|
child.unsafe().register(child.newPromise());
|
|
}
|
|
|
|
@Override
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
final ChannelConfig config = ctx.channel().config();
|
|
if (config.isAutoRead()) {
|
|
// stop accept new connections for 1 second to allow the channel to recover
|
|
// See https://github.com/netty/netty/issues/1328
|
|
config.setAutoRead(false);
|
|
ctx.channel().eventLoop().schedule(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
config.setAutoRead(true);
|
|
}
|
|
}, 1, TimeUnit.SECONDS);
|
|
}
|
|
// still let the exceptionCaught event flow through the pipeline to give the user
|
|
// a chance to do something with it
|
|
ctx.fireExceptionCaught(cause);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
@SuppressWarnings("CloneDoesntCallSuperClone")
|
|
public ServerBootstrap clone() {
|
|
return new ServerBootstrap(this);
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
StringBuilder buf = new StringBuilder(super.toString());
|
|
buf.setLength(buf.length() - 1);
|
|
buf.append(", ");
|
|
if (childGroup != null) {
|
|
buf.append("childGroup: ");
|
|
buf.append(StringUtil.simpleClassName(childGroup));
|
|
buf.append(", ");
|
|
}
|
|
synchronized (childOptions) {
|
|
if (!childOptions.isEmpty()) {
|
|
buf.append("childOptions: ");
|
|
buf.append(childOptions);
|
|
buf.append(", ");
|
|
}
|
|
}
|
|
synchronized (childAttrs) {
|
|
if (!childAttrs.isEmpty()) {
|
|
buf.append("childAttrs: ");
|
|
buf.append(childAttrs);
|
|
buf.append(", ");
|
|
}
|
|
}
|
|
if (childHandler != null) {
|
|
buf.append("childHandler: ");
|
|
buf.append(childHandler);
|
|
buf.append(", ");
|
|
}
|
|
if (buf.charAt(buf.length() - 1) == '(') {
|
|
buf.append(')');
|
|
} else {
|
|
buf.setCharAt(buf.length() - 2, ')');
|
|
buf.setLength(buf.length() - 1);
|
|
}
|
|
|
|
return buf.toString();
|
|
}
|
|
|
|
private static final class ServerBootstrapChannelFactory<T extends ServerChannel>
|
|
implements ServerChannelFactory<T> {
|
|
|
|
private final Class<? extends T> clazz;
|
|
|
|
ServerBootstrapChannelFactory(Class<? extends T> clazz) {
|
|
this.clazz = clazz;
|
|
}
|
|
|
|
@Override
|
|
public T newChannel(EventLoop eventLoop, EventLoopGroup childGroup) {
|
|
try {
|
|
Constructor<? extends T> constructor = clazz.getConstructor(EventLoop.class, EventLoopGroup.class);
|
|
return constructor.newInstance(eventLoop, childGroup);
|
|
} catch (Throwable t) {
|
|
throw new ChannelException("Unable to create Channel from class " + clazz, t);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return StringUtil.simpleClassName(clazz) + ".class";
|
|
}
|
|
}
|
|
}
|