Search in sources :

Example 51 with LocalChannel

use of io.netty.channel.local.LocalChannel in project netty by netty.

the class LocalEcho method main.

public static void main(String[] args) throws Exception {
    // Address to bind on / connect to.
    final LocalAddress addr = new LocalAddress(PORT);
    EventLoopGroup serverGroup = new DefaultEventLoopGroup();
    // NIO event loops are also OK
    EventLoopGroup clientGroup = new NioEventLoopGroup();
    try {
        // Note that we can use any event loop to ensure certain local channels
        // are handled by the same event loop thread which drives a certain socket channel
        // to reduce the communication latency between socket channels and local channels.
        ServerBootstrap sb = new ServerBootstrap();
        sb.group(serverGroup).channel(LocalServerChannel.class).handler(new ChannelInitializer<LocalServerChannel>() {

            @Override
            public void initChannel(LocalServerChannel ch) throws Exception {
                ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
            }
        }).childHandler(new ChannelInitializer<LocalChannel>() {

            @Override
            public void initChannel(LocalChannel ch) throws Exception {
                ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new LocalEchoServerHandler());
            }
        });
        Bootstrap cb = new Bootstrap();
        cb.group(clientGroup).channel(LocalChannel.class).handler(new ChannelInitializer<LocalChannel>() {

            @Override
            public void initChannel(LocalChannel ch) throws Exception {
                ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new LocalEchoClientHandler());
            }
        });
        // Start the server.
        sb.bind(addr).sync();
        // Start the client.
        Channel ch = cb.connect(addr).sync().channel();
        // Read commands from the stdin.
        System.out.println("Enter text (quit to end)");
        ChannelFuture lastWriteFuture = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        for (; ; ) {
            String line = in.readLine();
            if (line == null || "quit".equalsIgnoreCase(line)) {
                break;
            }
            // Sends the received line to the server.
            lastWriteFuture = ch.writeAndFlush(line);
        }
        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.awaitUninterruptibly();
        }
    } finally {
        serverGroup.shutdownGracefully();
        clientGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LoggingHandler(io.netty.handler.logging.LoggingHandler) LocalAddress(io.netty.channel.local.LocalAddress) InputStreamReader(java.io.InputStreamReader) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) BufferedReader(java.io.BufferedReader) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 52 with LocalChannel

use of io.netty.channel.local.LocalChannel in project netty by netty.

the class FixedChannelPoolTest method testAcquireBoundQueue.

@Test(expected = IllegalStateException.class)
public void testAcquireBoundQueue() throws Exception {
    EventLoopGroup group = new LocalEventLoopGroup();
    LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID);
    Bootstrap cb = new Bootstrap();
    cb.remoteAddress(addr);
    cb.group(group).channel(LocalChannel.class);
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
        }
    });
    // Start server
    Channel sc = sb.bind(addr).syncUninterruptibly().channel();
    ChannelPoolHandler handler = new TestChannelPoolHandler();
    ChannelPool pool = new FixedChannelPool(cb, handler, 1, 1);
    Channel channel = pool.acquire().syncUninterruptibly().getNow();
    Future<Channel> future = pool.acquire();
    assertFalse(future.isDone());
    try {
        pool.acquire().syncUninterruptibly();
    } finally {
        sc.close().syncUninterruptibly();
        channel.close().syncUninterruptibly();
        group.shutdownGracefully();
    }
}
Also used : LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) TimeoutException(java.util.concurrent.TimeoutException) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 53 with LocalChannel

use of io.netty.channel.local.LocalChannel in project netty by netty.

the class SimpleChannelPoolTest method testBoundedChannelPoolSegment.

@Test
public void testBoundedChannelPoolSegment() throws Exception {
    EventLoopGroup group = new LocalEventLoopGroup();
    LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID);
    Bootstrap cb = new Bootstrap();
    cb.remoteAddress(addr);
    cb.group(group).channel(LocalChannel.class);
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
        }
    });
    // Start server
    Channel sc = sb.bind(addr).sync().channel();
    CountingChannelPoolHandler handler = new CountingChannelPoolHandler();
    ChannelPool pool = new SimpleChannelPool(cb, handler, ChannelHealthChecker.ACTIVE) {

        private final Queue<Channel> queue = new LinkedBlockingQueue<Channel>(1);

        @Override
        protected Channel pollChannel() {
            return queue.poll();
        }

        @Override
        protected boolean offerChannel(Channel ch) {
            return queue.offer(ch);
        }
    };
    Channel channel = pool.acquire().sync().getNow();
    Channel channel2 = pool.acquire().sync().getNow();
    pool.release(channel).syncUninterruptibly().getNow();
    try {
        pool.release(channel2).syncUninterruptibly();
        fail();
    } catch (IllegalStateException e) {
    // expected
    }
    channel2.close().sync();
    assertEquals(2, handler.channelCount());
    assertEquals(0, handler.acquiredCount());
    assertEquals(1, handler.releasedCount());
    sc.close().sync();
    channel.close().sync();
    channel2.close().sync();
    group.shutdownGracefully();
}
Also used : LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ExpectedException(org.junit.rules.ExpectedException) EventLoopGroup(io.netty.channel.EventLoopGroup) LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Queue(java.util.Queue) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Aggregations

LocalChannel (io.netty.channel.local.LocalChannel)53 Test (org.junit.Test)49 LocalServerChannel (io.netty.channel.local.LocalServerChannel)16 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)14 LocalAddress (io.netty.channel.local.LocalAddress)14 Bootstrap (io.netty.bootstrap.Bootstrap)13 Channel (io.netty.channel.Channel)12 EventLoopGroup (io.netty.channel.EventLoopGroup)12 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)11 LocalEventLoopGroup (io.netty.channel.local.LocalEventLoopGroup)11 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)8 DefaultEventExecutorGroup (io.netty.util.concurrent.DefaultEventExecutorGroup)7 EventExecutorGroup (io.netty.util.concurrent.EventExecutorGroup)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 TimeoutException (java.util.concurrent.TimeoutException)7 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 ExpectedException (org.junit.rules.ExpectedException)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)3 OioSocketChannel (io.netty.channel.socket.oio.OioSocketChannel)3