Java Code Examples for io.vertx.core.net.SocketAddress#inetSocketAddress()

The following examples show how to use io.vertx.core.net.SocketAddress#inetSocketAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OkapiClientTest.java    From okapi with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpClientLegacy3(TestContext context) {
  Async async = context.async();
  StringBuilder b = new StringBuilder();

  context.assertTrue(server != null);
  HttpClient client = vertx.createHttpClient();
  SocketAddress sa = SocketAddress.inetSocketAddress(PORT, LOCALHOST);
  HttpClientRequest requestAbs = HttpClientLegacy.requestAbs(client,
    HttpMethod.GET, sa, URL + "/test1", res -> {
      b.append("response");
      async.complete();
    });
  requestAbs.exceptionHandler(res -> {
    b.append("exception");
    async.complete();
  });
  requestAbs.end();
  async.await();
  context.assertEquals("response", b.toString());
}
 
Example 2
Source File: OkapiClientTest.java    From okapi with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpClientLegacy4(TestContext context) {
  Async async = context.async();
  StringBuilder b = new StringBuilder();

  context.assertTrue(server != null);
  HttpClient client = vertx.createHttpClient();
  SocketAddress sa = SocketAddress.inetSocketAddress(PORT + 1, LOCALHOST);
  HttpClientRequest requestAbs = HttpClientLegacy.requestAbs(client,
    HttpMethod.GET, sa, URL + "/test1", res -> {
      b.append("response");
      async.complete();
    });
  requestAbs.exceptionHandler(res -> {
    b.append("exception");
    async.complete();
  });
  requestAbs.end();
  async.await();
  context.assertEquals("exception", b.toString());
}
 
Example 3
Source File: DefaultSendReplyService.java    From enode with MIT License 5 votes vote down vote up
public CompletableFuture<Void> sendReply(RemoteReply remoteReply, String replyAddress) {
    String message = JsonTool.serialize(remoteReply) + SysProperties.DELIMITED;
    Address address = RemotingUtil.string2Address(replyAddress);
    SocketAddress socketAddress = SocketAddress.inetSocketAddress(address.getPort(), address.getHost());
    CompletableFuture<NetSocket> future = new CompletableFuture<>();
    if (socketMap.putIfAbsent(replyAddress, future) == null) {
        netClient.connect(socketAddress, res -> {
            if (!res.succeeded()) {
                future.completeExceptionally(res.cause());
                logger.error("Failed to connect NetServer", res.cause());
                return;
            }
            NetSocket socket = res.result();
            socket.endHandler(v -> socket.close()).exceptionHandler(t -> {
                socketMap.remove(replyAddress);
                logger.error("NetSocket occurs unexpected error", t);
                socket.close();
            }).handler(buffer -> {
                String greeting = buffer.toString("UTF-8");
                logger.info("NetClient receiving: {}", greeting);
            }).closeHandler(v -> {
                socketMap.remove(replyAddress);
                logger.info("NetClient socket closed: {}", replyAddress);
            });
            future.complete(socket);
        });
    }
    return socketMap.get(replyAddress).thenAccept(socket -> {
        socket.write(message);
    }).exceptionally(ex -> {
        logger.error("Send command reply has exception, replyAddress: {}", replyAddress, ex);
        return null;
    });
}
 
Example 4
Source File: VertxServerHttpRequestTest.java    From vertx-spring-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetRemoteAddress() {
    SocketAddress original = SocketAddress.inetSocketAddress(8080, "localhost");
    given(mockHttpServerRequest.remoteAddress()).willReturn(original);

    InetSocketAddress expected = new InetSocketAddress("localhost", 8080);
    assertThat(vertxServerHttpRequest.getRemoteAddress()).isEqualTo(expected);
}
 
Example 5
Source File: DefaultWebServer.java    From festival with Apache License 2.0 4 votes vote down vote up
private SocketAddress resolveAddress() {
    String host = resourceReader.readProperty(ServerProperty.SERVER_HOST, String.class, ServerProperty.DEFAULT_HOST);
    Integer port = resourceReader.readProperty(ServerProperty.SERVER_PORT, Integer.class, ServerProperty.DEFAULT_PORT);
    return SocketAddress.inetSocketAddress(port, host);
}
 
Example 6
Source File: TcpServerProperties.java    From jetlinks-community with Apache License 2.0 4 votes vote down vote up
public SocketAddress createSocketAddress() {
    if (StringUtils.isEmpty(host)) {
        host = "localhost";
    }
    return SocketAddress.inetSocketAddress(port, host);
}
 
Example 7
Source File: SqlConnectOptions.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
@GenIgnore
public SocketAddress getSocketAddress() {
  return SocketAddress.inetSocketAddress(getPort(), getHost());
}
 
Example 8
Source File: RedisURI.java    From vertx-redis-client with Apache License 2.0 4 votes vote down vote up
public RedisURI(String connectionString) {
  this.connectionString = connectionString;
  try {
    final URI uri = new URI(connectionString);

    final String host = uri.getHost() == null ? DEFAULT_HOST : uri.getHost();
    final int port = uri.getPort() == -1 ? DEFAULT_PORT : uri.getPort();
    final String path = (uri.getPath() == null || uri.getPath().isEmpty()) ? "/" : uri.getPath();

    // According to https://www.iana.org/assignments/uri-schemes/prov/redis there is no specified order of decision
    // in case if db number or password are given in 2 different ways (e.g. in path and query).
    // Implementation uses the query values as a fallback if another one is missing.
    Map<String, String> query = parseQuery(uri);
    switch (uri.getScheme()) {
      case "rediss":
        ssl = true;
        socketAddress = SocketAddress.inetSocketAddress(port, host);
        if (path.length() > 1) {
          // skip initial slash
          select = Integer.parseInt(uri.getPath().substring(1));
        } else if (query.containsKey("db")) {
          select = Integer.parseInt(query.get("db"));
        } else {
          select = null;
        }
        break;
      case "redis":
        ssl = false;
        socketAddress = SocketAddress.inetSocketAddress(port, host);
        if (path.length() > 1) {
          // skip initial slash
          select = Integer.parseInt(uri.getPath().substring(1));
        } else if (query.containsKey("db")) {
          select = Integer.parseInt(query.get("db"));
        } else {
          select = null;
        }
        break;
      case "unix":
        ssl = false;
        socketAddress = SocketAddress.domainSocketAddress(path);
        if (query.containsKey("db")) {
          select = Integer.parseInt(query.get("db"));
        } else {
          select = null;
        }
        break;
      default:
        throw new IllegalArgumentException("Unsupported Redis connection string scheme [" + uri.getScheme() + "]");
    }

    String userInfo = uri.getUserInfo();
    if (userInfo != null) {
      String[] userInfoArray = userInfo.split(":");
      if (userInfoArray.length > 0) {
        password = userInfoArray[userInfoArray.length - 1];
      } else {
        password = query.getOrDefault("password", null);
      }
    } else {
      password = query.getOrDefault("password", null);
    }

  } catch (URISyntaxException e) {
    throw new IllegalArgumentException("Failed to parse the connection string", e);
  }
}