Synchronisation API and related refactorings / improvements
-> lets you synchronize with the Bitmessage network without staying connected
This commit is contained in:
@ -31,6 +31,7 @@ import ch.dissem.bitmessage.utils.Property;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
@ -62,10 +63,14 @@ public class BitmessageContext {
|
||||
|
||||
private final InternalContext ctx;
|
||||
|
||||
private Listener listener;
|
||||
private final Listener listener;
|
||||
private final NetworkHandler.MessageListener networkListener;
|
||||
|
||||
private BitmessageContext(Builder builder) {
|
||||
ctx = new InternalContext(builder);
|
||||
listener = builder.listener;
|
||||
networkListener = new DefaultMessageListener(ctx, listener);
|
||||
|
||||
// As this thread is used for parts that do POW, which itself uses parallel threads, only
|
||||
// one should be executed at any time.
|
||||
pool = Executors.newFixedThreadPool(1);
|
||||
@ -179,15 +184,22 @@ public class BitmessageContext {
|
||||
);
|
||||
}
|
||||
|
||||
public void startup(Listener listener) {
|
||||
this.listener = listener;
|
||||
ctx.getNetworkHandler().start(new DefaultMessageListener(ctx, listener));
|
||||
public void startup() {
|
||||
ctx.getNetworkHandler().start(networkListener);
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
ctx.getNetworkHandler().stop();
|
||||
}
|
||||
|
||||
public void synchronize(InetAddress host, int port, long timeoutInSeconds) {
|
||||
ctx.getNetworkHandler().synchronize(host, port, networkListener, timeoutInSeconds);
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
ctx.getInventory().cleanup();
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return ctx.getNetworkHandler().isRunning();
|
||||
}
|
||||
@ -268,6 +280,7 @@ public class BitmessageContext {
|
||||
ProofOfWorkEngine proofOfWorkEngine;
|
||||
Security security;
|
||||
MessageCallback messageCallback;
|
||||
Listener listener;
|
||||
|
||||
public Builder() {
|
||||
}
|
||||
@ -317,6 +330,11 @@ public class BitmessageContext {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder listener(Listener listener) {
|
||||
this.listener = listener;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BitmessageContext build() {
|
||||
nonNull("inventory", inventory);
|
||||
nonNull("nodeRegistry", nodeRegistry);
|
||||
|
@ -82,7 +82,7 @@ public class InternalContext {
|
||||
streams.add(1L);
|
||||
}
|
||||
|
||||
init(inventory, nodeRegistry, networkHandler, addressRepository, messageRepository, proofOfWorkEngine);
|
||||
init(security, inventory, nodeRegistry, networkHandler, addressRepository, messageRepository, proofOfWorkEngine);
|
||||
for (BitmessageAddress identity : addressRepository.getIdentities()) {
|
||||
streams.add(identity.getStream());
|
||||
}
|
||||
|
@ -36,5 +36,7 @@ public interface Inventory {
|
||||
|
||||
void storeObject(ObjectMessage object);
|
||||
|
||||
boolean contains(ObjectMessage object);
|
||||
|
||||
void cleanup();
|
||||
}
|
||||
|
@ -34,38 +34,43 @@ import static java.util.Collections.newSetFromMap;
|
||||
public class MemoryNodeRegistry implements NodeRegistry {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MemoryNodeRegistry.class);
|
||||
|
||||
private final Map<Long, Set<NetworkAddress>> stableNodes = new HashMap<>();
|
||||
private final Map<Long, Set<NetworkAddress>> stableNodes = new ConcurrentHashMap<>();
|
||||
private final Map<Long, Set<NetworkAddress>> knownNodes = new ConcurrentHashMap<>();
|
||||
|
||||
public MemoryNodeRegistry() {
|
||||
try (InputStream in = getClass().getClassLoader().getResourceAsStream("nodes.txt")) {
|
||||
Scanner scanner = new Scanner(in);
|
||||
long stream = 0;
|
||||
Set<NetworkAddress> streamSet = null;
|
||||
while (scanner.hasNext()) {
|
||||
try {
|
||||
String line = scanner.nextLine().trim();
|
||||
if (line.startsWith("#") || line.isEmpty()) {
|
||||
// Ignore
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("[stream")) {
|
||||
stream = Long.parseLong(line.substring(8, line.lastIndexOf(']')));
|
||||
streamSet = new HashSet<>();
|
||||
stableNodes.put(stream, streamSet);
|
||||
} else if (streamSet != null) {
|
||||
int portIndex = line.lastIndexOf(':');
|
||||
InetAddress inetAddress = InetAddress.getByName(line.substring(0, portIndex));
|
||||
int port = Integer.valueOf(line.substring(portIndex + 1));
|
||||
streamSet.add(new NetworkAddress.Builder().ip(inetAddress).port(port).stream(stream).build());
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try (InputStream in = getClass().getClassLoader().getResourceAsStream("nodes.txt")) {
|
||||
Scanner scanner = new Scanner(in);
|
||||
long stream = 0;
|
||||
Set<NetworkAddress> streamSet = null;
|
||||
while (scanner.hasNext()) {
|
||||
try {
|
||||
String line = scanner.nextLine().trim();
|
||||
if (line.startsWith("#") || line.isEmpty()) {
|
||||
// Ignore
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("[stream")) {
|
||||
stream = Long.parseLong(line.substring(8, line.lastIndexOf(']')));
|
||||
streamSet = new HashSet<>();
|
||||
stableNodes.put(stream, streamSet);
|
||||
} else if (streamSet != null) {
|
||||
int portIndex = line.lastIndexOf(':');
|
||||
InetAddress inetAddress = InetAddress.getByName(line.substring(0, portIndex));
|
||||
int port = Integer.valueOf(line.substring(portIndex + 1));
|
||||
streamSet.add(new NetworkAddress.Builder().ip(inetAddress).port(port).stream(stream).build());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOG.warn(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOG.warn(e.getMessage(), e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}, "node registry initializer").start();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -28,6 +28,8 @@ public interface MessageRepository {
|
||||
|
||||
List<Label> getLabels(Label.Type... types);
|
||||
|
||||
int countUnread(Label label);
|
||||
|
||||
List<Plaintext> findMessages(Label label);
|
||||
|
||||
List<Plaintext> findMessages(Status status);
|
||||
|
@ -1,15 +1,19 @@
|
||||
[stream 1]
|
||||
|
||||
[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
|
||||
|
||||
[2604:2000:1380:9f:82e:148b:2746:d0c7]:8080
|
||||
158.222.211.81:8080
|
||||
24.188.198.204:8111
|
||||
109.147.204.113:1195
|
||||
178.11.46.221:8444
|
||||
|
||||
# Add named nodes at the end, as resolving them might take time
|
||||
dissem.ch:8444
|
||||
|
||||
[stream 2]
|
||||
# none yet
|
Reference in New Issue
Block a user