Search in sources :

Example 1 with LoopResources

use of reactor.ipc.netty.resources.LoopResources in project reactor-netty by reactor.

the class TcpResourcesTest method before.

@Before
public void before() {
    loopDisposed = new AtomicBoolean();
    poolDisposed = new AtomicBoolean();
    loopResources = new LoopResources() {

        @Override
        public EventLoopGroup onServer(boolean useNative) {
            return null;
        }

        @Override
        public Mono<Void> disposeLater() {
            return Mono.<Void>empty().doOnSuccess(c -> loopDisposed.set(true));
        }

        @Override
        public boolean isDisposed() {
            return loopDisposed.get();
        }
    };
    poolResources = new PoolResources() {

        @Override
        public ChannelPool selectOrCreate(SocketAddress address, Supplier<? extends Bootstrap> bootstrap, Consumer<? super Channel> onChannelCreate, EventLoopGroup group) {
            return null;
        }

        public Mono<Void> disposeLater() {
            return Mono.<Void>empty().doOnSuccess(c -> poolDisposed.set(true));
        }

        @Override
        public boolean isDisposed() {
            return poolDisposed.get();
        }
    };
    tcpResources = new TcpResources(loopResources, poolResources);
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) SocketAddress(java.net.SocketAddress) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) Supplier(java.util.function.Supplier) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) Bootstrap(io.netty.bootstrap.Bootstrap) Flux(reactor.core.publisher.Flux) Duration(java.time.Duration) NettyContext(reactor.ipc.netty.NettyContext) ChannelPool(io.netty.channel.pool.ChannelPool) SocketUtils(reactor.ipc.netty.SocketUtils) LoopResources(reactor.ipc.netty.resources.LoopResources) Before(org.junit.Before) ChannelPool(io.netty.channel.pool.ChannelPool) Mono(reactor.core.publisher.Mono) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EventLoopGroup(io.netty.channel.EventLoopGroup) PoolResources(reactor.ipc.netty.resources.PoolResources) LoopResources(reactor.ipc.netty.resources.LoopResources) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Before(org.junit.Before)

Example 2 with LoopResources

use of reactor.ipc.netty.resources.LoopResources in project reactor-netty by reactor.

the class UdpClientTest method smokeTest.

@Test
public void smokeTest() throws Exception {
    LoopResources resources = LoopResources.create("test");
    CountDownLatch latch = new CountDownLatch(4);
    NettyContext server = UdpServer.create(ops -> ops.port(0).loopResources(resources)).newHandler((in, out) -> in.receiveObject().map(o -> {
        if (o instanceof DatagramPacket) {
            DatagramPacket received = (DatagramPacket) o;
            System.out.println("Server received " + received.content().toString(CharsetUtil.UTF_8));
            ByteBuf buf1 = Unpooled.copiedBuffer("echo ", CharsetUtil.UTF_8);
            ByteBuf buf2 = Unpooled.copiedBuffer(buf1, received.content().retain());
            return new DatagramPacket(buf2, received.sender());
        } else {
            return Mono.error(new Exception());
        }
    }).flatMap(p -> out.sendObject(p))).block(Duration.ofSeconds(30));
    NettyContext client1 = UdpClient.create(ops -> ops.port(server.address().getPort()).loopResources(resources)).newHandler((in, out) -> {
        in.receive().subscribe(b -> {
            System.out.println("Client1 received " + b.toString(CharsetUtil.UTF_8));
            latch.countDown();
        });
        return out.sendString(Mono.just("ping1")).then(out.sendString(Mono.just("ping2"))).neverComplete();
    }).block(Duration.ofSeconds(30));
    NettyContext client2 = UdpClient.create(ops -> ops.port(server.address().getPort()).loopResources(resources)).newHandler((in, out) -> {
        in.receive().subscribe(b -> {
            System.out.println("Client2 received " + b.toString(CharsetUtil.UTF_8));
            latch.countDown();
        });
        return out.sendString(Mono.just("ping3")).then(out.sendString(Mono.just("ping4"))).neverComplete();
    }).block(Duration.ofSeconds(30));
    assertTrue(latch.await(30, TimeUnit.SECONDS));
    server.dispose();
    client1.dispose();
    client2.dispose();
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Unpooled(io.netty.buffer.Unpooled) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) Duration(java.time.Duration) DatagramPacket(io.netty.channel.socket.DatagramPacket) CharsetUtil(io.netty.util.CharsetUtil) NettyContext(reactor.ipc.netty.NettyContext) LoopResources(reactor.ipc.netty.resources.LoopResources) LoopResources(reactor.ipc.netty.resources.LoopResources) DatagramPacket(io.netty.channel.socket.DatagramPacket) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 3 with LoopResources

use of reactor.ipc.netty.resources.LoopResources in project reactor-netty by reactor.

the class UdpServerTests method supportsUdpMulticast.

@Test
public void supportsUdpMulticast() throws Exception {
    final int port = SocketUtils.findAvailableUdpPort();
    final CountDownLatch latch = new CountDownLatch(Schedulers.DEFAULT_POOL_SIZE);
    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
    final InetAddress multicastGroup = InetAddress.getByName("230.0.0.1");
    final NetworkInterface multicastInterface = findMulticastEnabledIPv4Interface();
    log.info("Using network interface '{}' for multicast", multicastInterface);
    final Collection<NettyContext> servers = new ArrayList<>();
    LoopResources resources = LoopResources.create("test");
    for (int i = 0; i < 4; i++) {
        NettyContext server = UdpServer.create(opts -> opts.option(ChannelOption.SO_REUSEADDR, true).connectAddress(() -> new InetSocketAddress(port)).protocolFamily(InternetProtocolFamily.IPv4).loopResources(resources)).newHandler((in, out) -> {
            Flux.<NetworkInterface>generate(s -> {
                if (ifaces.hasMoreElements()) {
                    s.next(ifaces.nextElement());
                } else {
                    s.complete();
                }
            }).flatMap(iface -> {
                if (isMulticastEnabledIPv4Interface(iface)) {
                    return in.join(multicastGroup, iface);
                }
                return Flux.empty();
            }).thenMany(in.receive().asByteArray()).log().subscribe(bytes -> {
                if (bytes.length == 1024) {
                    latch.countDown();
                }
            });
            return Flux.never();
        }).block(Duration.ofSeconds(30));
        servers.add(server);
    }
    for (int i = 0; i < Schedulers.DEFAULT_POOL_SIZE; i++) {
        threadPool.submit(() -> {
            try {
                MulticastSocket multicast = new MulticastSocket();
                multicast.joinGroup(new InetSocketAddress(multicastGroup, port), multicastInterface);
                byte[] data = new byte[1024];
                new Random().nextBytes(data);
                multicast.send(new DatagramPacket(data, data.length, multicastGroup, port));
                multicast.close();
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
        }).get(5, TimeUnit.SECONDS);
    }
    latch.await(5, TimeUnit.SECONDS);
    assertThat("latch was not counted down enough: " + latch.getCount() + " left on " + (4 ^ 2), latch.getCount() == 0);
    for (NettyContext s : servers) {
        s.dispose();
    }
}
Also used : ChannelOption(io.netty.channel.ChannelOption) Enumeration(java.util.Enumeration) DatagramChannel(java.nio.channels.DatagramChannel) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) SocketException(java.net.SocketException) Loggers(reactor.util.Loggers) Duration(java.time.Duration) After(org.junit.After) Logger(reactor.util.Logger) InternetProtocolFamily(io.netty.channel.socket.InternetProtocolFamily) Schedulers(reactor.core.scheduler.Schedulers) SocketUtils(reactor.ipc.netty.SocketUtils) LoopResources(reactor.ipc.netty.resources.LoopResources) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Collection(java.util.Collection) NetworkInterface(java.net.NetworkInterface) NetUtil(io.netty.util.NetUtil) IOException(java.io.IOException) Test(org.junit.Test) Inet4Address(java.net.Inet4Address) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) MulticastSocket(java.net.MulticastSocket) NettyContext(reactor.ipc.netty.NettyContext) DatagramPacket(java.net.DatagramPacket) MulticastSocket(java.net.MulticastSocket) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) CountDownLatch(java.util.concurrent.CountDownLatch) SocketException(java.net.SocketException) IOException(java.io.IOException) NettyContext(reactor.ipc.netty.NettyContext) Random(java.util.Random) LoopResources(reactor.ipc.netty.resources.LoopResources) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 4 with LoopResources

use of reactor.ipc.netty.resources.LoopResources in project reactor-netty by reactor.

the class HttpResourcesTest method before.

@Before
public void before() {
    loopDisposed = new AtomicBoolean();
    poolDisposed = new AtomicBoolean();
    loopResources = new LoopResources() {

        @Override
        public EventLoopGroup onServer(boolean useNative) {
            return null;
        }

        @Override
        public Mono<Void> disposeLater() {
            return Mono.<Void>empty().doOnSuccess(c -> loopDisposed.set(true));
        }

        @Override
        public boolean isDisposed() {
            return loopDisposed.get();
        }
    };
    poolResources = new PoolResources() {

        @Override
        public ChannelPool selectOrCreate(SocketAddress address, Supplier<? extends Bootstrap> bootstrap, Consumer<? super Channel> onChannelCreate, EventLoopGroup group) {
            return null;
        }

        public Mono<Void> disposeLater() {
            return Mono.<Void>empty().doOnSuccess(c -> poolDisposed.set(true));
        }

        @Override
        public boolean isDisposed() {
            return poolDisposed.get();
        }
    };
    testResources = new HttpResources(loopResources, poolResources);
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) SocketAddress(java.net.SocketAddress) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) PoolResources(reactor.ipc.netty.resources.PoolResources) Supplier(java.util.function.Supplier) Consumer(java.util.function.Consumer) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelPool(io.netty.channel.pool.ChannelPool) LoopResources(reactor.ipc.netty.resources.LoopResources) Before(org.junit.Before) ChannelPool(io.netty.channel.pool.ChannelPool) Mono(reactor.core.publisher.Mono) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EventLoopGroup(io.netty.channel.EventLoopGroup) PoolResources(reactor.ipc.netty.resources.PoolResources) LoopResources(reactor.ipc.netty.resources.LoopResources) SocketAddress(java.net.SocketAddress) Before(org.junit.Before)

Example 5 with LoopResources

use of reactor.ipc.netty.resources.LoopResources in project reactor-netty by reactor.

the class ClientOptions method groupAndChannel.

@SuppressWarnings("unchecked")
final void groupAndChannel(Bootstrap bootstrap) {
    LoopResources loops = Objects.requireNonNull(getLoopResources(), "loopResources");
    boolean useNative = this.protocolFamily == null && preferNative() && !(sslContext() instanceof JdkSslContext);
    EventLoopGroup elg = loops.onClient(useNative);
    if (this.poolResources != null && elg instanceof Supplier) {
        // don't colocate
        bootstrap.group(((Supplier<EventLoopGroup>) elg).get());
    } else {
        bootstrap.group(elg);
    }
    if (useDatagramChannel()) {
        if (useNative) {
            bootstrap.channel(loops.onDatagramChannel(elg));
        } else {
            bootstrap.channelFactory(() -> new NioDatagramChannel(protocolFamily));
        }
    } else {
        bootstrap.channel(loops.onChannel(elg));
    }
}
Also used : JdkSslContext(io.netty.handler.ssl.JdkSslContext) EventLoopGroup(io.netty.channel.EventLoopGroup) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) LoopResources(reactor.ipc.netty.resources.LoopResources) Supplier(java.util.function.Supplier)

Aggregations

LoopResources (reactor.ipc.netty.resources.LoopResources)7 EventLoopGroup (io.netty.channel.EventLoopGroup)4 Duration (java.time.Duration)4 Test (org.junit.Test)4 Mono (reactor.core.publisher.Mono)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 TimeUnit (java.util.concurrent.TimeUnit)3 Supplier (java.util.function.Supplier)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Before (org.junit.Before)3 NettyContext (reactor.ipc.netty.NettyContext)3 PoolResources (reactor.ipc.netty.resources.PoolResources)3 Bootstrap (io.netty.bootstrap.Bootstrap)2 Channel (io.netty.channel.Channel)2 ChannelPool (io.netty.channel.pool.ChannelPool)2 JdkSslContext (io.netty.handler.ssl.JdkSslContext)2 NON_NULL (com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 FAIL_ON_UNKNOWN_PROPERTIES (com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1