Search in sources :

Example 1 with NioServerSocketChannel

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);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) ClientToProxyConnection(io.github.tesla.gateway.netty.transmit.connection.ClientToProxyConnection) ServerChannel(io.netty.channel.ServerChannel) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer)

Example 2 with NioServerSocketChannel

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();
    }
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) DefaultSelectStrategyFactory(io.netty.channel.DefaultSelectStrategyFactory) SelectStrategyFactory(io.netty.channel.SelectStrategyFactory) EventLoopGroup(io.netty.channel.EventLoopGroup) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) IntSupplier(io.netty.util.IntSupplier) SelectStrategy(io.netty.channel.SelectStrategy) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) IOException(java.io.IOException) Selector(java.nio.channels.Selector) AbstractEventLoopTest(io.netty.channel.AbstractEventLoopTest) Test(org.junit.jupiter.api.Test)

Example 3 with NioServerSocketChannel

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();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) Selector(java.nio.channels.Selector) AbstractEventLoopTest(io.netty.channel.AbstractEventLoopTest) Test(org.junit.jupiter.api.Test)

Example 4 with NioServerSocketChannel

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();
    }
}
Also used : ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SelectionKey(java.nio.channels.SelectionKey) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractEventLoopTest(io.netty.channel.AbstractEventLoopTest) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 5 with NioServerSocketChannel

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();
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) AbstractEventLoopTest(io.netty.channel.AbstractEventLoopTest) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

Channel (io.netty.channel.Channel)5 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)5 AbstractEventLoopTest (io.netty.channel.AbstractEventLoopTest)4 ServerSocketChannel (io.netty.channel.socket.ServerSocketChannel)4 SocketChannel (java.nio.channels.SocketChannel)4 Test (org.junit.jupiter.api.Test)4 EventLoopGroup (io.netty.channel.EventLoopGroup)2 InetSocketAddress (java.net.InetSocketAddress)2 Selector (java.nio.channels.Selector)2 Timeout (org.junit.jupiter.api.Timeout)2 ClientToProxyConnection (io.github.tesla.gateway.netty.transmit.connection.ClientToProxyConnection)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 DefaultSelectStrategyFactory (io.netty.channel.DefaultSelectStrategyFactory)1 SelectStrategy (io.netty.channel.SelectStrategy)1 SelectStrategyFactory (io.netty.channel.SelectStrategyFactory)1 ServerChannel (io.netty.channel.ServerChannel)1 IntSupplier (io.netty.util.IntSupplier)1