@Override public Optional<ServerTransport> buildServer(URI uri) { Objects.requireNonNull(uri, "uri must not be null"); if (!SCHEME.equals(uri.getScheme())) { return Optional.empty(); } return Optional.of( TcpServerTransport.create(TcpServer.create().host(uri.getHost()).port(uri.getPort()))); } }
/** * Creates a new instance binding to localhost * * @param port the port to bind to * @return a new instance */ public static TcpServerTransport create(int port) { TcpServer server = TcpServer.create().port(port); return create(server); }
/** * Creates a new instance * * @param bindAddress the address to bind to * @param port the port to bind to * @return a new instance * @throws NullPointerException if {@code bindAddress} is {@code null} */ public static TcpServerTransport create(String bindAddress, int port) { Objects.requireNonNull(bindAddress, "bindAddress must not be null"); TcpServer server = TcpServer.create().host(bindAddress).port(port); return create(server); }
@DisplayName("creates client with TcpServer") @Test void createTcpClient() { assertThat(TcpServerTransport.create(TcpServer.create())).isNotNull(); }
@Override public Optional<ServerTransport> buildServer(URI uri) { Objects.requireNonNull(uri, "uri must not be null"); if (!SCHEME.equals(uri.getScheme())) { return Optional.empty(); } return Optional.of( TcpServerTransport.create(TcpServer.create().host(uri.getHost()).port(uri.getPort()))); } }
/** * Creates a new instance binding to localhost * * @param port the port to bind to * @return a new instance */ public static TcpServerTransport create(int port) { TcpServer server = TcpServer.create().port(port); return create(server); }
/** * Creates a new instance * * @param bindAddress the address to bind to * @param port the port to bind to * @return a new instance * @throws NullPointerException if {@code bindAddress} is {@code null} */ public static TcpServerTransport create(String bindAddress, int port) { Objects.requireNonNull(bindAddress, "bindAddress must not be null"); TcpServer server = TcpServer.create().host(bindAddress).port(port); return create(server); }
@Test(timeout = 2000) public void startAndAwait() throws InterruptedException { AtomicReference<DisposableServer> conn = new AtomicReference<>(); CountDownLatch startLatch = new CountDownLatch(1); Thread t = new Thread(() -> TcpServer.create() .handle((in, out) -> out.sendString(Mono.just("foo"))) .bindUntilJavaShutdown(Duration.ofMillis(200), c -> { conn.set(c); startLatch.countDown(); })); t.start(); //let the server initialize startLatch.await(); //check nothing happens for 200ms t.join(200); Assertions.assertThat(t.isAlive()).isTrue(); //check that stopping the bnc stops the server conn.get().disposeNow(); t.join(); Assertions.assertThat(t.isAlive()).isFalse(); }
@Override public Mono<InetSocketAddress> bind(int port, ServiceMethodRegistry methodRegistry) { return Mono.defer( () -> { TcpServer tcpServer = TcpServer.create() .runOn(loopResources) .addressSupplier(() -> new InetSocketAddress(port)) .doOnConnection( connection -> { LOGGER.info("Accepted connection on {}", connection.channel()); connection.onDispose( () -> { LOGGER.info("Connection closed on {}", connection.channel()); connections.remove(connection); }); connections.add(connection); }); return RSocketFactory.receive() .frameDecoder( frame -> ByteBufPayload.create( frame.sliceData().retain(), frame.sliceMetadata().retain())) .acceptor(new RSocketServiceAcceptor(codec, methodRegistry)) .transport(() -> TcpServerTransport.create(tcpServer)) .start() .map(server -> this.server = server) .map(CloseableChannel::address); }); }
@Override public Mono<InetSocketAddress> bind(int port, ServiceMethodRegistry methodRegistry) { return Mono.defer( () -> { TcpServer tcpServer = TcpServer.create() .runOn(loopResources) .addressSupplier(() -> new InetSocketAddress(port)) .doOnConnection( connection -> { LOGGER.info("Accepted connection on {}", connection.channel()); connection.onDispose( () -> { LOGGER.info("Connection closed on {}", connection.channel()); connections.remove(connection); }); connections.add(connection); }); return RSocketFactory.receive() .frameDecoder( frame -> ByteBufPayload.create( frame.sliceData().retain(), frame.sliceMetadata().retain())) .acceptor(new RSocketServiceAcceptor(codec, methodRegistry)) .transport(() -> TcpServerTransport.create(tcpServer)) .start() .map(server -> this.server = server) .map(CloseableChannel::address); }); }
@Override public Mono<InetSocketAddress> bind(int port, ServiceMethodRegistry methodRegistry) { return Mono.defer( () -> { TcpServer tcpServer = TcpServer.create() .runOn(loopResources) .addressSupplier(() -> new InetSocketAddress(port)) .doOnConnection( connection -> { LOGGER.info("Accepted connection on {}", connection.channel()); connection.onDispose( () -> { LOGGER.info("Connection closed on {}", connection.channel()); connections.remove(connection); }); connections.add(connection); }); return RSocketFactory.receive() .frameDecoder( frame -> ByteBufPayload.create( frame.sliceData().retain(), frame.sliceMetadata().retain())) .acceptor(new RSocketServiceAcceptor(codec, methodRegistry)) .transport(() -> TcpServerTransport.create(tcpServer)) .start() .map(server -> this.server = server) .map(CloseableChannel::address); }); }
/** * Creates TcpServer. * * @return tcp server */ private TcpServer newTcpServer() { return TcpServer.create() .runOn(loopResources) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.SO_REUSEADDR, true) .addressSupplier(() -> new InetSocketAddress(config.getPort())) .bootstrap(b -> BootstrapHandlers.updateConfiguration(b, "inbound", channelInitializer)); }
@Test public void gettingOptionsDuplicates() { TcpServer server = TcpServer.create().host("foo").port(123); Assertions.assertThat(server.configure()) .isNotSameAs(TcpServerBind.INSTANCE.serverBootstrap) .isNotSameAs(server.configure()); }
@Test public void testIssue462() throws InterruptedException { final CountDownLatch countDownLatch = new CountDownLatch(1); DisposableServer server = TcpServer.create() .port(0) .handle((in, out) -> { in.receive() .log("channel") .subscribe(trip -> countDownLatch.countDown()); return Flux.never(); }) .wiretap(true) .bindNow(); assertNotNull(server); System.out.println("PORT +" + server.address() .getPort()); Connection client = TcpClient.create() .port(server.address() .getPort()) .handle((in, out) -> out.sendString(Flux.just("test"))) .wiretap(true) .connectNow(); assertNotNull(client); client.disposeNow(); server.disposeNow(); assertThat("Latch was counted down", countDownLatch.await(5, TimeUnit.SECONDS)); }
final CountDownLatch latch = new CountDownLatch(2); DisposableServer server = TcpServer.create() .port(port) .handle((in, out) -> {
@Test public void prematureCancel() { DirectProcessor<Void> signal = DirectProcessor.create(); DisposableServer x = TcpServer.create() .host("localhost") .port(0) .handle((in, out) -> { signal.onComplete(); return out.withConnection(c -> c.addHandlerFirst(new HttpResponseEncoder())) .sendObject(Mono.delay(Duration.ofSeconds(2)) .map(t -> new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.PROCESSING))) .neverComplete(); }) .wiretap(true) .bindNow(Duration.ofSeconds(30)); StepVerifier.create( createHttpClientForContextWithAddress(x) .get() .uri("/") .responseContent() .timeout(signal)) .verifyError(TimeoutException.class); }
@Test public void exposesRemoteAddress() throws InterruptedException { final int port = SocketUtils.findAvailableTcpPort(); final CountDownLatch latch = new CountDownLatch(1); DisposableServer server = TcpServer.create() .port(port) .handle((in, out) -> { in.withConnection(c -> { InetSocketAddress addr = c.address(); assertNotNull("remote address is not null", addr.getAddress()); latch.countDown(); }); return Flux.never(); }) .wiretap(true) .bindNow(); assertNotNull(server); Connection client = TcpClient.create().port(port) .handle((in, out) -> out.sendString(Flux.just("Hello World!"))) .wiretap(true) .connectNow(); assertNotNull(client); assertTrue("Latch was counted down", latch.await(5, TimeUnit.SECONDS)); client.disposeNow(); server.disposeNow(); }
@Test public void abort() { DisposableServer x = TcpServer.create() .port(0) .handle((in, out) ->
public void pipelined() { DisposableServer x = TcpServer.create() .host("localhost") .port(0)
}; TcpServer server = TcpServer.create() .doOnConnection(c -> c.addHandlerLast("codec", new LineBasedFrameDecoder(8 * 1024)))