use of io.netty.channel.socket.nio.NioServerSocketChannel in project tesla by linking12.
the class HttpProxyServer method doStart.
private void doStart() {
ServerBootstrap serverBootstrap = new ServerBootstrap().group(serverGroup.getClientToProxyAcceptorPoolForTransport(), serverGroup.getClientToProxyWorkerPoolForTransport());
ChannelInitializer<Channel> initializer = new ChannelInitializer<Channel>() {
protected void initChannel(Channel ch) throws Exception {
new ClientToProxyConnection(HttpProxyServer.this, ch.pipeline(), globalTrafficShapingHandler);
}
};
serverBootstrap.channelFactory(new ChannelFactory<ServerChannel>() {
public ServerChannel newChannel() {
return new NioServerSocketChannel();
}
});
serverBootstrap.childHandler(initializer);
ChannelFuture future = serverBootstrap.bind(requestedAddress).addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
registerChannel(future.channel());
}
}
}).awaitUninterruptibly();
Throwable cause = future.cause();
if (cause != null) {
throw new RuntimeException(cause);
}
this.boundAddress = ((InetSocketAddress) future.channel().localAddress());
LOG.info("Proxy started at address: " + this.boundAddress);
Runtime.getRuntime().addShutdownHook(jvmShutdownHook);
}
use of io.netty.channel.socket.nio.NioServerSocketChannel in project netty by netty.
the class NioEventLoopTest method testRebuildSelectorOnIOException.
@Test
public void testRebuildSelectorOnIOException() {
SelectStrategyFactory selectStrategyFactory = new SelectStrategyFactory() {
@Override
public SelectStrategy newSelectStrategy() {
return new SelectStrategy() {
private boolean thrown;
@Override
public int calculateStrategy(IntSupplier selectSupplier, boolean hasTasks) throws Exception {
if (!thrown) {
thrown = true;
throw new IOException();
}
return -1;
}
};
}
};
EventLoopGroup group = new NioEventLoopGroup(1, new DefaultThreadFactory("ioPool"), SelectorProvider.provider(), selectStrategyFactory);
final NioEventLoop loop = (NioEventLoop) group.next();
try {
Channel channel = new NioServerSocketChannel();
Selector selector = loop.unwrappedSelector();
loop.register(channel).syncUninterruptibly();
Selector newSelector = ((NioEventLoop) channel.eventLoop()).unwrappedSelector();
assertTrue(newSelector.isOpen());
assertNotSame(selector, newSelector);
assertFalse(selector.isOpen());
channel.close().syncUninterruptibly();
} finally {
group.shutdownGracefully();
}
}
use of io.netty.channel.socket.nio.NioServerSocketChannel in project netty by netty.
the class NioEventLoopTest method testRebuildSelector.
@Test
public void testRebuildSelector() {
EventLoopGroup group = new NioEventLoopGroup(1);
final NioEventLoop loop = (NioEventLoop) group.next();
try {
Channel channel = new NioServerSocketChannel();
loop.register(channel).syncUninterruptibly();
Selector selector = loop.unwrappedSelector();
assertSame(selector, ((NioEventLoop) channel.eventLoop()).unwrappedSelector());
assertTrue(selector.isOpen());
// Submit to the EventLoop so we are sure its really executed in a non-async manner.
loop.submit(new Runnable() {
@Override
public void run() {
loop.rebuildSelector();
}
}).syncUninterruptibly();
Selector newSelector = ((NioEventLoop) channel.eventLoop()).unwrappedSelector();
assertTrue(newSelector.isOpen());
assertNotSame(selector, newSelector);
assertFalse(selector.isOpen());
channel.close().syncUninterruptibly();
} finally {
group.shutdownGracefully();
}
}
use of io.netty.channel.socket.nio.NioServerSocketChannel in project netty by netty.
the class NioEventLoopTest method testSelectableChannel.
@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testSelectableChannel() throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup(1);
NioEventLoop loop = (NioEventLoop) group.next();
try {
Channel channel = new NioServerSocketChannel();
loop.register(channel).syncUninterruptibly();
channel.bind(new InetSocketAddress(0)).syncUninterruptibly();
SocketChannel selectableChannel = SocketChannel.open();
selectableChannel.configureBlocking(false);
selectableChannel.connect(channel.localAddress());
final CountDownLatch latch = new CountDownLatch(1);
loop.register(selectableChannel, SelectionKey.OP_CONNECT, new NioTask<SocketChannel>() {
@Override
public void channelReady(SocketChannel ch, SelectionKey key) {
latch.countDown();
}
@Override
public void channelUnregistered(SocketChannel ch, Throwable cause) {
}
});
latch.await();
selectableChannel.close();
channel.close().syncUninterruptibly();
} finally {
group.shutdownGracefully();
}
}
use of io.netty.channel.socket.nio.NioServerSocketChannel in project netty by netty.
the class NioEventLoopTest method testChannelsRegistered.
@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testChannelsRegistered() throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup(1);
final NioEventLoop loop = (NioEventLoop) group.next();
try {
final Channel ch1 = new NioServerSocketChannel();
final Channel ch2 = new NioServerSocketChannel();
assertEquals(0, registeredChannels(loop));
assertTrue(loop.register(ch1).syncUninterruptibly().isSuccess());
assertTrue(loop.register(ch2).syncUninterruptibly().isSuccess());
assertEquals(2, registeredChannels(loop));
assertTrue(ch1.deregister().syncUninterruptibly().isSuccess());
int registered;
// times before we see the right number of registered chanels.
while ((registered = registeredChannels(loop)) == 2) {
Thread.sleep(50);
}
assertEquals(1, registered);
} finally {
group.shutdownGracefully();
}
}
Aggregations