Allow easier use of AIO transport via bootstrap. Related to [#725]

This commit is contained in:
Norman Maurer 2012-11-10 20:43:53 +01:00
parent 1cc104e1c0
commit fa805c4c13
2 changed files with 33 additions and 0 deletions

View File

@ -20,6 +20,8 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.aio.AioEventLoopGroup;
import io.netty.channel.socket.aio.AioSocketChannel;
import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory;
import io.netty.util.AttributeKey;
@ -177,4 +179,22 @@ public class Bootstrap extends AbstractBootstrap<Bootstrap> {
b.attrs().putAll(attrs());
return b;
}
@Override
public Bootstrap channel(Class<? extends Channel> channelClass) {
if (channelClass == null) {
throw new NullPointerException("channelClass");
}
if (channelClass == AioSocketChannel.class) {
return channelFactory(new AioSocketChannelFactory());
}
return super.channel(channelClass);
}
private final class AioSocketChannelFactory implements ChannelFactory {
@Override
public Channel newChannel() {
return new AioSocketChannel((AioEventLoopGroup) group());
}
}
}

View File

@ -30,6 +30,8 @@ import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.aio.AioEventLoopGroup;
import io.netty.channel.socket.aio.AioServerSocketChannel;
import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory;
import io.netty.util.AttributeKey;
@ -98,6 +100,9 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
if (!ServerChannel.class.isAssignableFrom(channelClass)) {
throw new IllegalArgumentException();
}
if (channelClass == AioServerSocketChannel.class) {
channelFactory(new AioServerSocketChannelFactory());
}
return super.channel(channelClass);
}
@ -258,4 +263,12 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
}
}
}
private final class AioServerSocketChannelFactory implements ChannelFactory {
@Override
public Channel newChannel() {
return new AioServerSocketChannel((AioEventLoopGroup) group(), (AioEventLoopGroup) childGroup);
}
}
}