From fffe082249d0b363651e5c828d63dfacef676222 Mon Sep 17 00:00:00 2001 From: woodser Date: Sat, 6 Aug 2022 15:38:44 -0400 Subject: [PATCH] update wallet rpc poll period on startSyncing() --- src/main/java/monero/common/TaskLooper.java | 69 ++++++++++++------- .../java/monero/wallet/MoneroWalletRpc.java | 5 ++ 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/main/java/monero/common/TaskLooper.java b/src/main/java/monero/common/TaskLooper.java index 8edd14fa..c218f701 100644 --- a/src/main/java/monero/common/TaskLooper.java +++ b/src/main/java/monero/common/TaskLooper.java @@ -8,6 +8,7 @@ public class TaskLooper { private Runnable task; + private long periodInMs; private boolean isStarted; private boolean isLooping; @@ -26,39 +27,57 @@ public TaskLooper(Runnable task) { * @param periodInMs the loop period in milliseconds */ public synchronized void start(long periodInMs) { - isStarted = true; - if (isLooping) return; - - // start looping - isLooping = true; - Thread loop = new Thread(new Runnable() { - @Override - public void run() { - while (isStarted && !Thread.currentThread().isInterrupted()) { - - // run the task - long startTime = System.currentTimeMillis(); - task.run(); - - // wait remaining period - if (isStarted) { - try { TimeUnit.MILLISECONDS.sleep(periodInMs - (System.currentTimeMillis() - startTime)); } // target fixed period by accounting for run time - catch (Exception e) { - isLooping = false; - if (isStarted) throw new RuntimeException(e); + synchronized (this) { + this.periodInMs = periodInMs; + if (isStarted) return; + isStarted = true; + + // start looping + if (isLooping) return; + isLooping = true; + TaskLooper that = this; + Thread loop = new Thread(new Runnable() { + @Override + public void run() { + while (isStarted && !Thread.currentThread().isInterrupted()) { + + // run the task + long startTime = System.currentTimeMillis(); + task.run(); + + // wait remaining period + if (isStarted) { + try { TimeUnit.MILLISECONDS.sleep(that.periodInMs - (System.currentTimeMillis() - startTime)); } // target fixed period by accounting for run time + catch (Exception e) { + isLooping = false; + if (isStarted) throw new RuntimeException(e); + } } } + isLooping = false; } - isLooping = false; - } - }); - loop.start(); + }); + loop.start(); + } } /** * Stop the task loop. */ public void stop() { - isStarted = false; + synchronized (this) { + isStarted = false; + } + } + + /** + * Set the loop period in milliseconds. + * + * @param periodInMs the loop period in milliseconds + */ + public void setPeriodInMs(long periodInMs) { + synchronized (this) { + this.periodInMs = periodInMs; + } } } diff --git a/src/main/java/monero/wallet/MoneroWalletRpc.java b/src/main/java/monero/wallet/MoneroWalletRpc.java index d02891e5..692c9799 100644 --- a/src/main/java/monero/wallet/MoneroWalletRpc.java +++ b/src/main/java/monero/wallet/MoneroWalletRpc.java @@ -721,6 +721,7 @@ public void startSyncing(Long syncPeriodInMs) { // update sync period for poller this.syncPeriodInMs = syncPeriodInSeconds * 1000; + if (walletPoller != null) walletPoller.setPeriodInMs(syncPeriodInMs); // poll if listening poll(); @@ -2300,6 +2301,10 @@ public void setIsPolling(boolean isPolling) { else looper.stop(); } + public void setPeriodInMs(long periodInMs) { + looper.setPeriodInMs(periodInMs); + } + public void poll() { try {