Refactored some TODOs in NodeAddress and NodeAddressLookup

This commit is contained in:
Sergey Rozhnov 2018-02-15 15:49:53 +04:00
parent 9c2f0d5c00
commit 180eb6bf9f
2 changed files with 27 additions and 23 deletions

View File

@ -43,25 +43,33 @@ class NodeAddressLookup {
}
Set<NodeAddress> resolveNodeAddresses() {
NodeAddresses nodeAddresses = NodeAddresses.fromString(seedNodes);
NodeAddresses allNodeAddresses = getAllAddresses();
Set<String> bannedHosts = getBannedHosts();
allNodeAddresses = allNodeAddresses.excludeByHost(bannedHosts);
if (myAddress != null) {
allNodeAddresses = allNodeAddresses.excludeByFullAddress(myAddress);
}
log.debug("We received banned seed nodes={}, seedNodeAddresses={}", bannedHosts, allNodeAddresses);
return allNodeAddresses.toSet();
}
private NodeAddresses getAllAddresses() {
NodeAddresses nodeAddresses = Optional.ofNullable(seedNodes)
.map(nodes -> NodeAddresses.fromString(seedNodes))
.orElse(new NodeAddresses(Collections.emptySet()));
if (nodeAddresses.isEmpty()) {
Set<NodeAddress> delegate = isLocalHostUsed
? DEFAULT_LOCALHOST_SEED_NODE_ADDRESSES
Set<NodeAddress> delegate = isLocalHostUsed ? DEFAULT_LOCALHOST_SEED_NODE_ADDRESSES
: DEFAULT_TOR_SEED_NODE_ADDRESSES;
nodeAddresses = NodeAddresses.fromSet(delegate, networkId);
}
Set<String> bannedHosts = getBannedHosts(environment);
nodeAddresses = nodeAddresses.excludeByHost(bannedHosts);
// TODO refactor when null
nodeAddresses = nodeAddresses.excludeByFullAddress(myAddress);
log.debug("We received banned seed nodes={}, seedNodeAddresses={}", bannedHosts, nodeAddresses);
return nodeAddresses.toSet();
return nodeAddresses;
}
private Set<String> getBannedHosts(BisqEnvironment environment) {
private Set<String> getBannedHosts() {
return Optional.ofNullable(environment.getBannedSeedNodes())
.map(HashSet::new)
.map(hosts -> (Set<String>) hosts)

View File

@ -5,23 +5,19 @@ import com.google.common.collect.ImmutableSet;
import io.bisq.network.p2p.NodeAddress;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
// TODO to many methods
class NodeAddresses {
private final Set<NodeAddress> delegate;
static NodeAddresses fromString(@Nullable String seedNodes) {
Set<NodeAddress> addresses = Optional.ofNullable(seedNodes)
.map(StringUtils::deleteWhitespace)
.map(nodes -> nodes.split(","))
.map(Arrays::stream)
.orElse(Stream.empty())
static NodeAddresses fromString(String seedNodes) {
String trimmed = StringUtils.deleteWhitespace(seedNodes);
String[] nodes = trimmed.split(",");
Set<NodeAddress> addresses = Arrays.stream(nodes)
.map(NodeAddress::new)
.collect(Collectors.toSet());
return new NodeAddresses(addresses);
@ -34,7 +30,7 @@ class NodeAddresses {
return new NodeAddresses(delegate);
}
private NodeAddresses(Set<NodeAddress> delegate) {
NodeAddresses(Set<NodeAddress> delegate) {
this.delegate = delegate;
}