Added synchronization code and unit test.

Synchronisation fails if the trusted host has no new messages - this needs to be fixed (but shouldn't be an issue for real world applications)
This commit is contained in:
2015-09-24 08:09:20 +02:00
parent f89d1a342e
commit d67c932fb2
15 changed files with 457 additions and 117 deletions

View File

@ -66,6 +66,17 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
this.ctx = context;
}
@Override
public Thread synchronize(InetAddress trustedHost, int port, MessageListener listener, long timeoutInSeconds) {
try {
Thread t = new Thread(Connection.sync(ctx, trustedHost, port, listener, timeoutInSeconds));
t.start();
return t;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void start(final MessageListener listener) {
if (listener == null) {
@ -150,11 +161,6 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
}
}
@Override
public void synchronize(InetAddress trustedHost, int port, MessageListener listener) throws IOException {
startConnection(new Connection(ctx, CLIENT, new Socket(trustedHost, port), listener, requestedObjects));
}
private void startConnection(Connection c) {
synchronized (connections) {
// prevent connecting twice to the same node