diff --git a/domain/src/main/java/ch/dissem/bitmessage/entity/valueobject/NetworkAddress.java b/domain/src/main/java/ch/dissem/bitmessage/entity/valueobject/NetworkAddress.java index 8412c84..09cca10 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/entity/valueobject/NetworkAddress.java +++ b/domain/src/main/java/ch/dissem/bitmessage/entity/valueobject/NetworkAddress.java @@ -18,6 +18,7 @@ package ch.dissem.bitmessage.entity.valueobject; import ch.dissem.bitmessage.entity.Streamable; import ch.dissem.bitmessage.utils.Encode; +import ch.dissem.bitmessage.utils.UnixTime; import java.io.IOException; import java.io.OutputStream; @@ -76,6 +77,10 @@ public class NetworkAddress implements Streamable { return time; } + public void setTime(long time) { + this.time = time; + } + public InetAddress toInetAddress() { try { return InetAddress.getByAddress(ipv6); @@ -121,10 +126,6 @@ public class NetworkAddress implements Streamable { Encode.int16(port, stream); } - public void setTime(long time) { - this.time = time; - } - public static final class Builder { private long time; private long stream; @@ -197,6 +198,9 @@ public class NetworkAddress implements Streamable { } public NetworkAddress build() { + if (time == 0) { + time = UnixTime.now(); + } return new NetworkAddress(this); } } diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/NetworkNode.java b/networking/src/main/java/ch/dissem/bitmessage/networking/NetworkNode.java index 84b010e..17d335c 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/NetworkNode.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/NetworkNode.java @@ -22,7 +22,6 @@ import ch.dissem.bitmessage.entity.valueobject.InventoryVector; import ch.dissem.bitmessage.entity.valueobject.NetworkAddress; import ch.dissem.bitmessage.ports.NetworkHandler; import ch.dissem.bitmessage.utils.Collections; -import ch.dissem.bitmessage.utils.DebugUtils; import ch.dissem.bitmessage.utils.Property; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -83,6 +82,7 @@ public class NetworkNode implements NetworkHandler, ContextHolder { @Override public void run() { while (!Thread.interrupted()) { + int active = 0; synchronized (connections) { for (Iterator iterator = connections.iterator(); iterator.hasNext(); ) { Connection c = iterator.next(); @@ -90,10 +90,13 @@ public class NetworkNode implements NetworkHandler, ContextHolder { // Remove the current element from the iterator and the list. iterator.remove(); } + if (c.getState() == ACTIVE) { + active++; + } } } - if (connections.size() < 8) { - List addresses = ctx.getNodeRegistry().getKnownAddresses(8 - connections.size(), ctx.getStreams()); + if (active < 8) { + List addresses = ctx.getNodeRegistry().getKnownAddresses(8 - active, ctx.getStreams()); for (NetworkAddress address : addresses) { startConnection(new Connection(ctx, CLIENT, address, listener)); } diff --git a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcNodeRegistry.java b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcNodeRegistry.java index 9ad8329..7deaa3d 100644 --- a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcNodeRegistry.java +++ b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcNodeRegistry.java @@ -22,9 +22,13 @@ import ch.dissem.bitmessage.utils.UnixTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.io.InputStream; +import java.net.InetAddress; import java.sql.*; import java.util.LinkedList; import java.util.List; +import java.util.Scanner; import static ch.dissem.bitmessage.utils.UnixTime.HOUR; @@ -37,6 +41,35 @@ public class JdbcNodeRegistry extends JdbcHelper implements NodeRegistry { @Override public List getKnownAddresses(int limit, long... streams) { + List result = doGetKnownNodes(limit, streams); + if (result.isEmpty()) { + try (InputStream in = getClass().getClassLoader().getResourceAsStream("nodes.txt")) { + Scanner scanner = new Scanner(in); + while (scanner.hasNext()) { + try { + String line = scanner.nextLine().trim(); + if (line.startsWith("#") || line.isEmpty()) { + // Ignore + continue; + } + int portIndex = line.lastIndexOf(':'); + InetAddress inetAddress = InetAddress.getByName(line.substring(0, portIndex)); + int port = Integer.valueOf(line.substring(portIndex + 1)); + result.add(new NetworkAddress.Builder().ip(inetAddress).port(port).build()); + } catch (IOException e) { + LOG.warn(e.getMessage(), e); + } + } + offerAddresses(result); + return doGetKnownNodes(limit, streams); + } catch (IOException e) { + LOG.error(e.getMessage(), e); + } + } + return result; + } + + private List doGetKnownNodes(int limit, long... streams) { List result = new LinkedList<>(); try (Connection connection = config.getConnection()) { Statement stmt = connection.createStatement(); @@ -53,10 +86,6 @@ public class JdbcNodeRegistry extends JdbcHelper implements NodeRegistry { } catch (SQLException e) { LOG.error(e.getMessage(), e); } - if (result.isEmpty()) { - // FIXME: this is for testing purposes, remove it! - result.add(new NetworkAddress.Builder().ipv4(127, 0, 0, 1).port(8444).build()); - } return result; } diff --git a/repositories/src/main/resources/nodes.txt b/repositories/src/main/resources/nodes.txt new file mode 100644 index 0000000..ad76447 --- /dev/null +++ b/repositories/src/main/resources/nodes.txt @@ -0,0 +1,10 @@ +[2604:2000:1380:9f:82e:148b:2746:d0c7]:8080 +5.45.99.75:8444 +75.167.159.54:8444 +95.165.168.168:8444 +85.180.139.241:8444 +158.222.211.81:8080 +178.62.12.187:8448 +24.188.198.204:8111 +109.147.204.113:1195 +178.11.46.221:8444 \ No newline at end of file diff --git a/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcNodeRegistryTest.java b/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcNodeRegistryTest.java index 8bfda1e..b4f60c0 100644 --- a/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcNodeRegistryTest.java +++ b/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcNodeRegistryTest.java @@ -45,6 +45,13 @@ public class JdbcNodeRegistryTest { )); } + @Test + public void testInitNodes() throws Exception { + config.reset(); + List knownAddresses = registry.getKnownAddresses(1, 1); + assertEquals(10, knownAddresses.size()); + } + @Test public void testGetKnownAddresses() throws Exception { List knownAddresses = registry.getKnownAddresses(2, 1);