From ea1419eda11148c0d732e1b904bce0ac7f1f924e Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Thu, 8 Oct 2015 13:12:39 +0200 Subject: [PATCH] Synchronisation API - added option to wait for the synchronization to finish --- .../dissem/bitmessage/BitmessageContext.java | 18 ++++++++++++++++-- .../bitmessage/networking/Connection.java | 2 ++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/domain/src/main/java/ch/dissem/bitmessage/BitmessageContext.java b/domain/src/main/java/ch/dissem/bitmessage/BitmessageContext.java index 0b3ad1d..d7d8980 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/BitmessageContext.java +++ b/domain/src/main/java/ch/dissem/bitmessage/BitmessageContext.java @@ -192,8 +192,22 @@ public class BitmessageContext { ctx.getNetworkHandler().stop(); } - public void synchronize(InetAddress host, int port, long timeoutInSeconds) { - ctx.getNetworkHandler().synchronize(host, port, networkListener, timeoutInSeconds); + /** + * @param host a trusted node that must be reliable (it's used for every synchronization) + * @param port of the trusted host, default is 8444 + * @param timeoutInSeconds synchronization should end no later than about 5 seconds after the timeout elapsed, even + * if not all objects were fetched + * @param wait waits for the synchronization thread to finish + */ + public void synchronize(InetAddress host, int port, long timeoutInSeconds, boolean wait) { + Thread t = ctx.getNetworkHandler().synchronize(host, port, networkListener, timeoutInSeconds); + if (wait) { + try { + t.join(); + } catch (InterruptedException e) { + throw new RuntimeException(e.getMessage(), e); + } + } } public void cleanup() { diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java index 5015094..6aa4210 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java @@ -204,6 +204,7 @@ public class Connection implements Runnable { return false; } if (syncTimeout < UnixTime.now()) { + LOG.info("Synchronization timed out"); return true; } if (msg == null) { @@ -213,6 +214,7 @@ public class Connection implements Runnable { readTimeoutCounter = 0; if (!(msg.getPayload() instanceof Addr) && !(msg.getPayload() instanceof GetData) && requestedObjects.isEmpty() && sendingQueue.isEmpty()) { + LOG.info("Synchronisation completed"); return true; } return false;