use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpServerTests method exposesRemoteAddress.
@Test
public void exposesRemoteAddress() throws InterruptedException {
final int port = SocketUtils.findAvailableTcpPort();
final CountDownLatch latch = new CountDownLatch(1);
NettyContext server = TcpServer.create(port).newHandler((in, out) -> {
InetSocketAddress remoteAddr = in.remoteAddress();
assertNotNull("remote address is not null", remoteAddr.getAddress());
latch.countDown();
return Flux.never();
}).block(Duration.ofSeconds(30));
NettyContext client = TcpClient.create(port).newHandler((in, out) -> out.sendString(Flux.just("Hello World!"))).block(Duration.ofSeconds(30));
assertTrue("latch was counted down", latch.await(5, TimeUnit.SECONDS));
client.dispose();
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpServerTests method tcpServerHandlesJsonPojosOverSsl.
@Test
public void tcpServerHandlesJsonPojosOverSsl() throws Exception {
final CountDownLatch latch = new CountDownLatch(2);
SslContext clientOptions = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
final TcpServer server = TcpServer.create(opts -> opts.host("localhost").sslSelfSigned());
ObjectMapper m = new ObjectMapper();
NettyContext connectedServer = server.newHandler((in, out) -> {
in.receive().asByteArray().map(bb -> {
try {
return m.readValue(bb, Pojo.class);
} catch (IOException io) {
throw Exceptions.propagate(io);
}
}).log("conn").subscribe(data -> {
if ("John Doe".equals(data.getName())) {
latch.countDown();
}
});
return out.sendString(Mono.just("Hi")).neverComplete();
}).block(Duration.ofSeconds(30));
final TcpClient client = TcpClient.create(opts -> opts.host("localhost").port(connectedServer.address().getPort()).sslContext(clientOptions));
NettyContext connectedClient = client.newHandler((in, out) -> {
// in
in.receive().asString().log("receive").subscribe(data -> {
if (data.equals("Hi")) {
latch.countDown();
}
});
// out
return out.send(Flux.just(new Pojo("John" + " Doe")).map(s -> {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
m.writeValue(os, s);
return out.alloc().buffer().writeBytes(os.toByteArray());
} catch (IOException ioe) {
throw Exceptions.propagate(ioe);
}
})).neverComplete();
// return Mono.empty();
}).block(Duration.ofSeconds(30));
assertTrue("Latch was counted down", latch.await(5, TimeUnit.SECONDS));
connectedClient.dispose();
connectedServer.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpServerTests method exposesNettyPipelineConfiguration.
@Test
public void exposesNettyPipelineConfiguration() throws InterruptedException {
final int port = SocketUtils.findAvailableTcpPort();
final CountDownLatch latch = new CountDownLatch(2);
final TcpClient client = TcpClient.create(port);
BiFunction<? super NettyInbound, ? super NettyOutbound, ? extends Publisher<Void>> serverHandler = (in, out) -> {
in.receive().asString().subscribe(data -> {
log.info("data " + data + " on " + in);
latch.countDown();
});
return Flux.never();
};
TcpServer server = TcpServer.create(opts -> opts.afterChannelInit(c -> c.pipeline().addBefore(NettyPipeline.ReactiveBridge, "codec", new LineBasedFrameDecoder(8 * 1024))).port(port));
NettyContext connected = server.newHandler(serverHandler).block(Duration.ofSeconds(30));
NettyContext clientContext = client.newHandler((in, out) -> out.send(Flux.just("Hello World!\n", "Hello 11!\n").map(b -> out.alloc().buffer().writeBytes(b.getBytes())))).block(Duration.ofSeconds(30));
assertTrue("Latch was counted down", latch.await(10, TimeUnit.SECONDS));
connected.dispose();
clientContext.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpServerTests method assertSendFile.
private void assertSendFile(Function<NettyOutbound, NettyOutbound> fn) throws InterruptedException {
NettyContext context = TcpServer.create().newHandler((in, out) -> in.receive().asString().flatMap(word -> "GOGOGO".equals(word) ? fn.apply(out).then() : out.sendString(Mono.just("NOPE")))).block();
MonoProcessor<String> m1 = MonoProcessor.create();
MonoProcessor<String> m2 = MonoProcessor.create();
NettyContext client1 = TcpClient.create(opt -> opt.port(context.address().getPort())).newHandler((in, out) -> {
in.receive().asString().log("-----------------CLIENT1").subscribe(m1::onNext);
return out.sendString(Mono.just("gogogo")).neverComplete();
}).block();
NettyContext client2 = TcpClient.create(opt -> opt.port(context.address().getPort())).newHandler((in, out) -> {
in.receive().asString(StandardCharsets.UTF_8).take(2).reduceWith(String::new, String::concat).log("-----------------CLIENT2").subscribe(m2::onNext);
return out.sendString(Mono.just("GOGOGO")).neverComplete();
}).block();
String client1Response = m1.block();
String client2Response = m2.block();
client1.dispose();
client1.onClose().block();
client2.dispose();
client2.onClose().block();
context.dispose();
context.onClose().block();
Assertions.assertThat(client1Response).isEqualTo("NOPE");
Assertions.assertThat(client2Response).startsWith("This is an UTF-8 file that is larger than 1024 bytes. " + "It contains accents like é.").contains("1024 mark here ->").contains("<- 1024 mark here").endsWith("End of File");
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpServerTests method test5.
@Test
@Ignore
public void test5() throws Exception {
// Hot stream of data, could be injected from anywhere
EmitterProcessor<String> broadcaster = EmitterProcessor.create();
// Get a reference to the tail of the operation pipeline (microbatching + partitioning)
final Processor<List<String>, List<String>> processor = WorkQueueProcessor.<List<String>>builder().autoCancel(false).build();
broadcaster.bufferTimeout(10, Duration.ofSeconds(1)).log("broadcaster").subscribe(processor);
// on a server dispatching data on the default shared dispatcher, and serializing/deserializing as string
// Listen for anything exactly hitting the root URI and route the incoming connection request to the callback
NettyContext s = HttpServer.create(0).newRouter(r -> r.get("/", (request, response) -> {
// prepare a response header to be appended first before any reply
response.addHeader("X-CUSTOM", "12345");
// returning a stream of String from each microbatch merged
return response.sendString(Flux.from(processor).flatMap(Flux::fromIterable).take(Duration.ofSeconds(5)).concatWith(Flux.just("end\n")));
})).block(Duration.ofSeconds(30));
for (int i = 0; i < 50; i++) {
Thread.sleep(500);
broadcaster.onNext(System.currentTimeMillis() + "\n");
}
s.dispose();
}
Aggregations