Issue # 5: initialize node repository if there are no nodes in DB
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
|
10
repositories/src/main/resources/nodes.txt
Normal file
10
repositories/src/main/resources/nodes.txt
Normal 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
|
@ -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);
|
||||
|
Reference in New Issue
Block a user