Issue # 5: initialize node repository if there are no nodes in DB

This commit is contained in:
Christian Basler 2015-06-17 06:00:03 +02:00
parent 49a9e0c5f7
commit 9b0de83706
5 changed files with 64 additions and 11 deletions

View File

@ -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);
}
}

View File

@ -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<Connection> 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<NetworkAddress> addresses = ctx.getNodeRegistry().getKnownAddresses(8 - connections.size(), ctx.getStreams());
if (active < 8) {
List<NetworkAddress> addresses = ctx.getNodeRegistry().getKnownAddresses(8 - active, ctx.getStreams());
for (NetworkAddress address : addresses) {
startConnection(new Connection(ctx, CLIENT, address, listener));
}

View File

@ -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<NetworkAddress> getKnownAddresses(int limit, long... streams) {
List<NetworkAddress> 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<NetworkAddress> doGetKnownNodes(int limit, long... streams) {
List<NetworkAddress> 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;
}

View File

@ -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

View File

@ -45,6 +45,13 @@ public class JdbcNodeRegistryTest {
));
}
@Test
public void testInitNodes() throws Exception {
config.reset();
List<NetworkAddress> knownAddresses = registry.getKnownAddresses(1, 1);
assertEquals(10, knownAddresses.size());
}
@Test
public void testGetKnownAddresses() throws Exception {
List<NetworkAddress> knownAddresses = registry.getKnownAddresses(2, 1);