|
|
|
@ -22,6 +22,7 @@ import org.hyperledger.besu.util.NetworkUtility; |
|
|
|
|
import java.net.InetAddress; |
|
|
|
|
import java.net.URI; |
|
|
|
|
import java.util.Objects; |
|
|
|
|
import java.util.Optional; |
|
|
|
|
import java.util.OptionalInt; |
|
|
|
|
import java.util.regex.Matcher; |
|
|
|
|
import java.util.regex.Pattern; |
|
|
|
@ -40,22 +41,18 @@ public class EnodeURL { |
|
|
|
|
|
|
|
|
|
private final Bytes nodeId; |
|
|
|
|
private final InetAddress ip; |
|
|
|
|
private final OptionalInt listeningPort; |
|
|
|
|
private final OptionalInt discoveryPort; |
|
|
|
|
private final Optional<Integer> listeningPort; |
|
|
|
|
private final Optional<Integer> discoveryPort; |
|
|
|
|
|
|
|
|
|
private EnodeURL( |
|
|
|
|
final Bytes nodeId, |
|
|
|
|
final InetAddress address, |
|
|
|
|
final OptionalInt listeningPort, |
|
|
|
|
final OptionalInt discoveryPort) { |
|
|
|
|
final Optional<Integer> listeningPort, |
|
|
|
|
final Optional<Integer> discoveryPort) { |
|
|
|
|
checkArgument( |
|
|
|
|
nodeId.size() == NODE_ID_SIZE, "Invalid node id. Expected id of length: 64 bytes."); |
|
|
|
|
checkArgument( |
|
|
|
|
!listeningPort.isPresent() || NetworkUtility.isValidPort(listeningPort.getAsInt()), |
|
|
|
|
"Invalid listening port. Port should be between 1 - 65535."); |
|
|
|
|
checkArgument( |
|
|
|
|
!discoveryPort.isPresent() || NetworkUtility.isValidPort(discoveryPort.getAsInt()), |
|
|
|
|
"Invalid discovery port. Port should be between 1 - 65535."); |
|
|
|
|
listeningPort.ifPresent(port -> NetworkUtility.checkPort(port, "listening")); |
|
|
|
|
discoveryPort.ifPresent(port -> NetworkUtility.checkPort(port, "discovery")); |
|
|
|
|
|
|
|
|
|
this.nodeId = nodeId; |
|
|
|
|
this.ip = address; |
|
|
|
@ -98,17 +95,16 @@ public class EnodeURL { |
|
|
|
|
int tcpPort = uri.getPort(); |
|
|
|
|
|
|
|
|
|
// Parse discport if it exists
|
|
|
|
|
OptionalInt discoveryPort = OptionalInt.empty(); |
|
|
|
|
Optional<Integer> discoveryPort = Optional.empty(); |
|
|
|
|
String query = uri.getQuery(); |
|
|
|
|
if (query != null) { |
|
|
|
|
final Matcher discPortMatcher = DISCPORT_QUERY_STRING_REGEX.matcher(query); |
|
|
|
|
if (discPortMatcher.matches()) { |
|
|
|
|
Integer discPort = Ints.tryParse(discPortMatcher.group(1)); |
|
|
|
|
discoveryPort = discPort == null ? discoveryPort : OptionalInt.of(discPort); |
|
|
|
|
discoveryPort = Optional.ofNullable(Ints.tryParse(discPortMatcher.group(1))); |
|
|
|
|
} |
|
|
|
|
checkArgument(discoveryPort.isPresent(), "Invalid discovery port: '" + query + "'."); |
|
|
|
|
} else { |
|
|
|
|
discoveryPort = OptionalInt.of(tcpPort); |
|
|
|
|
discoveryPort = Optional.of(tcpPort); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return builder() |
|
|
|
@ -208,7 +204,7 @@ public class EnodeURL { |
|
|
|
|
return discoveryPort.isPresent(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public OptionalInt getListeningPort() { |
|
|
|
|
public Optional<Integer> getListeningPort() { |
|
|
|
|
return listeningPort; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -216,7 +212,7 @@ public class EnodeURL { |
|
|
|
|
return listeningPort.orElse(0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public OptionalInt getDiscoveryPort() { |
|
|
|
|
public Optional<Integer> getDiscoveryPort() { |
|
|
|
|
return discoveryPort; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -252,11 +248,11 @@ public class EnodeURL { |
|
|
|
|
public static class Builder { |
|
|
|
|
|
|
|
|
|
private Bytes nodeId; |
|
|
|
|
private OptionalInt listeningPort; |
|
|
|
|
private OptionalInt discoveryPort; |
|
|
|
|
private Optional<Integer> listeningPort; |
|
|
|
|
private Optional<Integer> discoveryPort; |
|
|
|
|
private InetAddress ip; |
|
|
|
|
|
|
|
|
|
private Builder() {}; |
|
|
|
|
private Builder() {} |
|
|
|
|
|
|
|
|
|
public EnodeURL build() { |
|
|
|
|
validate(); |
|
|
|
@ -315,12 +311,12 @@ public class EnodeURL { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Builder disableListening() { |
|
|
|
|
this.listeningPort = OptionalInt.empty(); |
|
|
|
|
this.listeningPort = Optional.empty(); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Builder disableDiscovery() { |
|
|
|
|
this.discoveryPort = OptionalInt.empty(); |
|
|
|
|
this.discoveryPort = Optional.empty(); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -333,16 +329,12 @@ public class EnodeURL { |
|
|
|
|
* An optional listening port value. If the value is empty of equal to 0, the listening port |
|
|
|
|
* will be empty - indicating the corresponding node is not listening. |
|
|
|
|
* |
|
|
|
|
* @param listeningPort If non-empty represents the port to listen on, if empty means the node |
|
|
|
|
* is not listening |
|
|
|
|
* @param maybeListeningPort If non-empty represents the port to listen on, if empty means the |
|
|
|
|
* node is not listening |
|
|
|
|
* @return The modified builder |
|
|
|
|
*/ |
|
|
|
|
public Builder listeningPort(final OptionalInt listeningPort) { |
|
|
|
|
if (listeningPort.isPresent() && listeningPort.getAsInt() == 0) { |
|
|
|
|
this.listeningPort = OptionalInt.empty(); |
|
|
|
|
} else { |
|
|
|
|
this.listeningPort = listeningPort; |
|
|
|
|
} |
|
|
|
|
public Builder listeningPort(final Optional<Integer> maybeListeningPort) { |
|
|
|
|
this.listeningPort = maybeListeningPort.filter(port -> port != 0); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -354,29 +346,19 @@ public class EnodeURL { |
|
|
|
|
* @return The modified builder |
|
|
|
|
*/ |
|
|
|
|
public Builder listeningPort(final int listeningPort) { |
|
|
|
|
if (listeningPort == 0) { |
|
|
|
|
this.listeningPort = OptionalInt.empty(); |
|
|
|
|
} else { |
|
|
|
|
this.listeningPort = OptionalInt.of(listeningPort); |
|
|
|
|
} |
|
|
|
|
return this; |
|
|
|
|
return listeningPort(Optional.of(listeningPort)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* The port on which to listen for discovery messages. A value that is empty or equal to 0, |
|
|
|
|
* indicates that the node is not listening for discovery messages. |
|
|
|
|
* |
|
|
|
|
* @param discoveryPort If non-empty and non-zero, represents the port on which to listen for |
|
|
|
|
* discovery messages. Otherwise, indicates that the node is not running discovery. |
|
|
|
|
* @param maybeDiscoveryPort If non-empty and non-zero, represents the port on which to listen |
|
|
|
|
* for discovery messages. Otherwise, indicates that the node is not running discovery. |
|
|
|
|
* @return The modified builder |
|
|
|
|
*/ |
|
|
|
|
public Builder discoveryPort(final OptionalInt discoveryPort) { |
|
|
|
|
if (discoveryPort.isPresent() && discoveryPort.getAsInt() == 0) { |
|
|
|
|
this.discoveryPort = OptionalInt.empty(); |
|
|
|
|
} else { |
|
|
|
|
this.discoveryPort = discoveryPort; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Builder discoveryPort(final Optional<Integer> maybeDiscoveryPort) { |
|
|
|
|
this.discoveryPort = maybeDiscoveryPort.filter(port -> port != 0); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -389,12 +371,7 @@ public class EnodeURL { |
|
|
|
|
* @return The modified builder |
|
|
|
|
*/ |
|
|
|
|
public Builder discoveryPort(final int discoveryPort) { |
|
|
|
|
if (discoveryPort == 0) { |
|
|
|
|
this.discoveryPort = OptionalInt.empty(); |
|
|
|
|
} else { |
|
|
|
|
this.discoveryPort = OptionalInt.of(discoveryPort); |
|
|
|
|
} |
|
|
|
|
return this; |
|
|
|
|
return discoveryPort(Optional.of(discoveryPort)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|