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);
}
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();
}
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();
}
}
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);
}
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));
}
}
Aggregations