-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
340 additions
and
13 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/generated/io/vertx/serviceresolver/dns/DnsResolverOptionsConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.vertx.serviceresolver.dns; | ||
|
||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.core.json.JsonArray; | ||
import io.vertx.core.json.impl.JsonUtil; | ||
import java.time.Instant; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Base64; | ||
|
||
/** | ||
* Converter and mapper for {@link io.vertx.serviceresolver.dns.DnsResolverOptions}. | ||
* NOTE: This class has been automatically generated from the {@link io.vertx.serviceresolver.dns.DnsResolverOptions} original class using Vert.x codegen. | ||
*/ | ||
public class DnsResolverOptionsConverter { | ||
|
||
|
||
private static final Base64.Decoder BASE64_DECODER = JsonUtil.BASE64_DECODER; | ||
private static final Base64.Encoder BASE64_ENCODER = JsonUtil.BASE64_ENCODER; | ||
|
||
static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, DnsResolverOptions obj) { | ||
for (java.util.Map.Entry<String, Object> member : json) { | ||
switch (member.getKey()) { | ||
case "host": | ||
if (member.getValue() instanceof String) { | ||
obj.setHost((String)member.getValue()); | ||
} | ||
break; | ||
case "port": | ||
if (member.getValue() instanceof Number) { | ||
obj.setPort(((Number)member.getValue()).intValue()); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
|
||
static void toJson(DnsResolverOptions obj, JsonObject json) { | ||
toJson(obj, json.getMap()); | ||
} | ||
|
||
static void toJson(DnsResolverOptions obj, java.util.Map<String, Object> json) { | ||
if (obj.getHost() != null) { | ||
json.put("host", obj.getHost()); | ||
} | ||
json.put("port", obj.getPort()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/vertx/serviceresolver/dns/DnsResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (c) 2011-2023 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
* which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
*/ | ||
package io.vertx.serviceresolver.dns; | ||
|
||
import io.vertx.serviceresolver.ServiceResolver; | ||
import io.vertx.serviceresolver.dns.impl.DnsResolverImpl; | ||
import io.vertx.serviceresolver.impl.ServiceResolverImpl; | ||
|
||
public interface DnsResolver { | ||
|
||
static ServiceResolver create(DnsResolverOptions options) { | ||
return new ServiceResolverImpl((vertx, lookup) -> new DnsResolverImpl(vertx, options, lookup.loadBalancer)); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/io/vertx/serviceresolver/dns/DnsResolverOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2011-2023 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
* which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
*/ | ||
package io.vertx.serviceresolver.dns; | ||
|
||
import io.vertx.codegen.annotations.DataObject; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.serviceresolver.srv.SrvResolverOptionsConverter; | ||
|
||
@DataObject(generateConverter = true, publicConverter = false) | ||
public class DnsResolverOptions { | ||
|
||
private String host; | ||
private int port; | ||
|
||
public DnsResolverOptions() { | ||
} | ||
|
||
public DnsResolverOptions(DnsResolverOptions other) { | ||
this.host = other.host; | ||
this.port = other.port; | ||
} | ||
|
||
public DnsResolverOptions(JsonObject json) { | ||
DnsResolverOptionsConverter.fromJson(json, this); | ||
} | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public DnsResolverOptions setHost(String host) { | ||
this.host = host; | ||
return this; | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
public DnsResolverOptions setPort(int port) { | ||
this.port = port; | ||
return this; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/io/vertx/serviceresolver/dns/impl/DnsResolverImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) 2011-2023 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
* which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
*/ | ||
package io.vertx.serviceresolver.dns.impl; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.Promise; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.dns.AddressResolverOptions; | ||
import io.vertx.core.impl.AddressResolver; | ||
import io.vertx.core.impl.VertxInternal; | ||
import io.vertx.core.net.Address; | ||
import io.vertx.core.net.SocketAddress; | ||
import io.vertx.serviceresolver.ServiceAddress; | ||
import io.vertx.serviceresolver.dns.DnsResolverOptions; | ||
import io.vertx.serviceresolver.impl.ResolverBase; | ||
import io.vertx.serviceresolver.loadbalancing.LoadBalancer; | ||
import io.vertx.serviceresolver.srv.SrvResolver; | ||
|
||
import java.util.Collections; | ||
|
||
public class DnsResolverImpl extends ResolverBase<SocketAddress, SocketAddress, DnsServiceState> implements SrvResolver { | ||
|
||
private AddressResolver dnsResolver; | ||
|
||
public DnsResolverImpl(Vertx vertx, DnsResolverOptions options, LoadBalancer loadBalancer) { | ||
super(vertx, loadBalancer); | ||
|
||
AddressResolverOptions o = new AddressResolverOptions(); | ||
o.setServers(Collections.singletonList(options.getHost() + ":" + options.getPort())); | ||
dnsResolver = new AddressResolver(vertx, o); | ||
} | ||
|
||
@Override | ||
public SocketAddress tryCast(Address address) { | ||
return address instanceof ServiceAddress ? (SocketAddress) address : null; | ||
} | ||
|
||
@Override | ||
public SocketAddress addressOfEndpoint(SocketAddress endpoint) { | ||
return endpoint; | ||
} | ||
|
||
@Override | ||
public Future<DnsServiceState> resolve(SocketAddress address) { | ||
Promise<DnsServiceState> promise = Promise.promise(); | ||
dnsResolver.resolveHostnameAll(address.host(), ar -> { | ||
if (ar.succeeded()) { | ||
promise.complete(new DnsServiceState(address, 100, ar.result(), loadBalancer)); | ||
} else { | ||
promise.fail(ar.cause()); | ||
} | ||
}); | ||
return promise.future(); | ||
} | ||
|
||
@Override | ||
public void dispose(DnsServiceState state) { | ||
|
||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/io/vertx/serviceresolver/dns/impl/DnsServiceState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2011-2023 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
* which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
*/ | ||
package io.vertx.serviceresolver.dns.impl; | ||
|
||
import io.vertx.core.net.SocketAddress; | ||
import io.vertx.serviceresolver.impl.ServiceState; | ||
import io.vertx.serviceresolver.loadbalancing.LoadBalancer; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.List; | ||
|
||
class DnsServiceState extends ServiceState<SocketAddress> { | ||
|
||
final long timestamp; | ||
|
||
|
||
DnsServiceState(SocketAddress address, long timestamp, List<InetSocketAddress> addresses, LoadBalancer loadBalancer) { | ||
super(address.hostName(), loadBalancer); | ||
|
||
for (InetSocketAddress addr : addresses) { | ||
add(SocketAddress.inetSocketAddress(address.port(), addr.getAddress().getHostAddress())); | ||
} | ||
|
||
this.timestamp = timestamp; | ||
} | ||
|
||
@Override | ||
protected boolean isValid() { | ||
// long now = System.currentTimeMillis(); | ||
// for (EndpointImpl<SrvRecord> endpoint : endpoints()) { | ||
// if (now > endpoint.get().ttl() * 1000 + timestamp) { | ||
// return false; | ||
// } | ||
// } | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.