From e5b67030914f01b64479f946494f5126a5ee3c76 Mon Sep 17 00:00:00 2001 From: woodser <woodser@protonmail.com> Date: Sun, 25 Aug 2024 12:12:26 -0400 Subject: [PATCH] synchronize connection check --- .../monero/common/MoneroRpcConnection.java | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/main/java/monero/common/MoneroRpcConnection.java b/src/main/java/monero/common/MoneroRpcConnection.java index 91539272c..2dafe2450 100644 --- a/src/main/java/monero/common/MoneroRpcConnection.java +++ b/src/main/java/monero/common/MoneroRpcConnection.java @@ -237,37 +237,39 @@ public Object getAttribute(String key) { * @return true if there is a change in status, false otherwise */ public boolean checkConnection(long timeoutMs) { - Boolean isOnlineBefore = isOnline; - Boolean isAuthenticatedBefore = isAuthenticated; - long startTime = System.currentTimeMillis(); - try { - if (MoneroUtils.isNativeLibraryLoaded()) { - List<Long> heights = new ArrayList<Long>(); - for (long i = 0; i < 100; i++) heights.add(i); - Map<String, Object> params = new HashMap<String, Object>(); - params.put("heights", heights); - sendBinaryRequest("get_blocks_by_height.bin", params, timeoutMs); // assume daemon connection - } else { - sendJsonRequest("get_version", null, timeoutMs); - } - isOnline = true; - isAuthenticated = true; - } catch (Exception e) { - isOnline = false; - isAuthenticated = null; - responseTime = null; - if (e instanceof MoneroRpcError) { - if (((MoneroRpcError) e).getCode() == 401) { - isOnline = true; - isAuthenticated = false; - } else if (((MoneroRpcError) e).getCode() == 404) { // fallback to latency check - isOnline = true; - isAuthenticated = true; + synchronized (this) { + Boolean isOnlineBefore = isOnline; + Boolean isAuthenticatedBefore = isAuthenticated; + long startTime = System.currentTimeMillis(); + try { + if (MoneroUtils.isNativeLibraryLoaded()) { + List<Long> heights = new ArrayList<Long>(); + for (long i = 0; i < 100; i++) heights.add(i); + Map<String, Object> params = new HashMap<String, Object>(); + params.put("heights", heights); + sendBinaryRequest("get_blocks_by_height.bin", params, timeoutMs); // assume daemon connection + } else { + sendJsonRequest("get_version", null, timeoutMs); + } + isOnline = true; + isAuthenticated = true; + } catch (Exception e) { + isOnline = false; + isAuthenticated = null; + responseTime = null; + if (e instanceof MoneroRpcError) { + if (((MoneroRpcError) e).getCode() == 401) { + isOnline = true; + isAuthenticated = false; + } else if (((MoneroRpcError) e).getCode() == 404) { // fallback to latency check + isOnline = true; + isAuthenticated = true; + } } } + if (isOnline) responseTime = System.currentTimeMillis() - startTime; + return isOnlineBefore != isOnline || isAuthenticatedBefore != isAuthenticated; } - if (isOnline) responseTime = System.currentTimeMillis() - startTime; - return isOnlineBefore != isOnline || isAuthenticatedBefore != isAuthenticated; } /**