@Override @SuppressWarnings("unchecked") public DatagramSocket send(Buffer packet, int port, String host, Handler<AsyncResult<DatagramSocket>> handler) { Objects.requireNonNull(packet, "no null packet accepted"); Objects.requireNonNull(host, "no null host accepted"); if (port < 0 || port > 65535) { throw new IllegalArgumentException("port out of range:" + port); } context.owner().resolveAddress(host, res -> { if (res.succeeded()) { doSend(packet, new InetSocketAddress(res.result(), port), handler); } else { handler.handle(Future.failedFuture(res.cause())); } }); if (metrics != null) { metrics.bytesWritten(null, new SocketAddressImpl(port, host), packet.length()); } return this; }
private DatagramSocket listen(SocketAddress local, Handler<AsyncResult<DatagramSocket>> handler) { Objects.requireNonNull(handler, "no null handler accepted"); context.owner().resolveAddress(local.host(), res -> { if (res.succeeded()) { ChannelFuture future = channel.bind(new InetSocketAddress(res.result(), local.port())); addListener(future, ar -> { if (metrics != null && ar.succeeded()) { metrics.listening(local.host(), localAddress()); } handler.handle(ar); }); } else { handler.handle(Future.failedFuture(res.cause())); } }); return this; }
} else { checkPort(socketAddress.port()); vertx.resolveAddress(socketAddress.host(), res -> { if (res.succeeded()) {
@Test public void testResolveFromFile() { File f = new File(new File(new File(new File("src"), "test"), "resources"), "hosts_config.txt"); VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsPath(f.getAbsolutePath()))); vertx.resolveAddress("server.net", onSuccess(addr -> { assertEquals("192.168.0.15", addr.getHostAddress()); assertEquals("server.net", addr.getHostName()); testComplete(); })); await(); }
final ProxyType proxyType = proxyOptions.getType(); vertx.resolveAddress(proxyHost, dnsRes -> { if (dnsRes.succeeded()) { InetAddress address = dnsRes.result();
@Test public void testAddressResolver() { VertxInternal vertx = (VertxInternal) super.vertx; vertx.resolveAddress("localhost", onSuccess(v -> testComplete())); await(); }
)); CompletableFuture<Void> test1 = new CompletableFuture<>(); vertx.resolveAddress("localhost", ar -> { if (ar.succeeded()) { InetAddress resolved = ar.result(); vertx.resolveAddress("LOCALHOST", ar -> { if (ar.succeeded()) { InetAddress resolved = ar.result();
@Test public void testSearchDomainWithNdots2() throws Exception { Map<String, String> records = new HashMap<>(); records.put("host1.sub.foo.com", "127.0.0.1"); records.put("host2.sub.foo.com", "127.0.0.2"); records.put("host2.sub", "127.0.0.3"); dnsServer.testResolveA(records); VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions( new AddressResolverOptions(). addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()). setOptResourceEnabled(false). addSearchDomain("foo.com"). setNdots(2) )); CountDownLatch latch1 = new CountDownLatch(1); vertx.resolveAddress("host1.sub", onSuccess(resolved -> { assertEquals("127.0.0.1", resolved.getHostAddress()); latch1.countDown(); })); awaitLatch(latch1); // "host2.sub" is resolved with the foo.com search domain as ndots = 2 CountDownLatch latch2 = new CountDownLatch(1); vertx.resolveAddress("host2.sub", onSuccess(resolved -> { assertEquals("127.0.0.2", resolved.getHostAddress()); latch2.countDown(); })); awaitLatch(latch2); }
@Test public void testSearchDomainWithNdots0() throws Exception { Map<String, String> records = new HashMap<>(); records.put("host1", "127.0.0.2"); records.put("host1.foo.com", "127.0.0.3"); dnsServer.testResolveA(records); VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions( new AddressResolverOptions(). addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()). setOptResourceEnabled(false). addSearchDomain("foo.com"). setNdots(0) )); // "host1" resolves directly as ndots = 0 CountDownLatch latch1 = new CountDownLatch(1); vertx.resolveAddress("host1", onSuccess(resolved -> { assertEquals("127.0.0.2", resolved.getHostAddress()); latch1.countDown(); })); awaitLatch(latch1); // "host1.foo.com" resolves to host1.foo.com CountDownLatch latch2 = new CountDownLatch(1); vertx.resolveAddress("host1.foo.com", onSuccess(resolved -> { assertEquals("127.0.0.3", resolved.getHostAddress()); latch2.countDown(); })); awaitLatch(latch2); }
vertx.resolveAddress("host1", onSuccess(resolved -> { assertEquals(addr_host1_foo_com, resolved.getHostAddress()); latch1.countDown(); vertx.resolveAddress("host1.", onSuccess(resolved -> { assertEquals(addr_host1, resolved.getHostAddress()); latch2.countDown(); vertx.resolveAddress("host2", onFailure(cause -> { assertTrue(cause instanceof UnknownHostException); latch3.countDown(); vertx.resolveAddress("host3", onSuccess(cause -> { assertEquals(addr_host3, cause.getHostAddress()); latch4.countDown(); vertx.resolveAddress("host3.", onSuccess(resolved -> { assertEquals(addr_host3, resolved.getHostAddress()); latch5.countDown(); vertx.resolveAddress("host4.sub", onSuccess(resolved -> { assertEquals(addr_host4_sub_foo_com, resolved.getHostAddress()); latch6.countDown(); vertx.resolveAddress("host5.sub", onSuccess(resolved -> { assertEquals(addr_host5_sub_foo_com, resolved.getHostAddress()); latch7.countDown(); vertx.resolveAddress("host6.sub.sub", onSuccess(resolved -> {
vertx.resolveAddress("host1", onSuccess(resolved -> { assertEquals("127.0.0.1", resolved.getHostAddress()); latch1.countDown(); vertx.resolveAddress("host2", onSuccess(resolved -> { assertEquals("127.0.0.2", resolved.getHostAddress()); latch2.countDown(); vertx.resolveAddress("host3", onSuccess(resolved -> { assertEquals("127.0.0.4", resolved.getHostAddress()); latch3.countDown(); vertx.resolveAddress("host4", onFailure(cause -> { assertTrue(cause instanceof UnknownHostException); testComplete();
@Test public void testResolveFromBuffer() { VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsValue(Buffer.buffer("192.168.0.15 server.net")))); vertx.resolveAddress("server.net", onSuccess(addr -> { assertEquals("192.168.0.15", addr.getHostAddress()); assertEquals("server.net", addr.getHostName()); testComplete(); })); await(); }
@Test public void testCaseInsensitiveResolveFromHosts() { VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsPath("hosts_config.txt"))); vertx.resolveAddress("SERVER.NET", onSuccess(addr -> { assertEquals("192.168.0.15", addr.getHostAddress()); assertEquals("server.net", addr.getHostName()); testComplete(); })); await(); }
@Test public void testResolveFromClasspath() { VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsPath("hosts_config.txt"))); vertx.resolveAddress("server.net", onSuccess(addr -> { assertEquals("192.168.0.15", addr.getHostAddress()); assertEquals("server.net", addr.getHostName()); testComplete(); })); await(); }
private DatagramSocket listen(SocketAddress local, Handler<AsyncResult<DatagramSocket>> handler) { Objects.requireNonNull(handler, "no null handler accepted"); context.owner().resolveAddress(local.host(), res -> { if (res.succeeded()) { ChannelFuture future = channel.bind(new InetSocketAddress(res.result(), local.port())); addListener(future, ar -> { if (metrics != null && ar.succeeded()) { metrics.listening(local.host(), localAddress()); } handler.handle(ar); }); } else { handler.handle(Future.failedFuture(res.cause())); } }); return this; }
@Test public void testResolveFromFile() { File f = new File(new File(new File(new File("src"), "test"), "resources"), "hosts_config.txt"); VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsPath(f.getAbsolutePath()))); vertx.resolveAddress("server.net", onSuccess(addr -> { assertEquals("192.168.0.15", addr.getHostAddress()); assertEquals("server.net", addr.getHostName()); testComplete(); })); await(); }
@Test public void testAddressResolver() { VertxInternal vertx = (VertxInternal) super.vertx; vertx.resolveAddress("localhost", onSuccess(v -> testComplete())); await(); }
@Test public void testResolveFromBuffer() { VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsValue(Buffer.buffer("192.168.0.15 server.net")))); vertx.resolveAddress("server.net", onSuccess(addr -> { assertEquals("192.168.0.15", addr.getHostAddress()); assertEquals("server.net", addr.getHostName()); testComplete(); })); await(); }
@Test public void testResolveFromClasspath() { VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsPath("hosts_config.txt"))); vertx.resolveAddress("server.net", onSuccess(addr -> { assertEquals("192.168.0.15", addr.getHostAddress()); assertEquals("server.net", addr.getHostName()); testComplete(); })); await(); }
@Test public void testCaseInsensitiveResolveFromHosts() { VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsPath("hosts_config.txt"))); vertx.resolveAddress("SERVER.NET", onSuccess(addr -> { assertEquals("192.168.0.15", addr.getHostAddress()); assertEquals("server.net", addr.getHostName()); testComplete(); })); await(); }