From c5dd948c80a427467fcd41f66b8fb5649024fc3d Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Sat, 29 Apr 2023 20:29:01 -0400 Subject: [PATCH] added timeout to checkConnection --- README.md | 9 ++- .../main/java/land/fx/fula/FulaModule.java | 68 ++++++++++++------- package.json | 2 +- src/interfaces/fulaNativeModule.ts | 4 +- src/protocols/fula.ts | 9 +-- 5 files changed, 58 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 60a3b40..cb109e2 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ const peerId //returns peerId as string exchange: 'noop'|'', //add noop for testing without a backend autoFlush: boolean, //Default to false. Always set to false unless you know what you are doing. explicitly write data to disk after each operation if set to true useRelay: boolean, //default to true. If true it forces the connection through relay - refresh: boolean //forces the fula object to be recreated. default is false + refresh: boolean? //forces the fula object to be recreated. default is false ) ``` @@ -43,7 +43,7 @@ await fula.init( exchange: 'noop'|'', //add noop for testing without a backend autoFlush: boolean, //Default to false. Always set to false unless you know what you are doing. explicitly write data to disk after each operation if set to true useRelay: boolean, //default to true. If true it forces the connection through relay - refresh: boolean //forces the fula object to be recreated. default is false + refresh: boolean? //forces the fula object to be recreated. default is false ); ``` @@ -133,7 +133,9 @@ await fula.isReady( //checks if client can reach server const result //returns true if it can, and false if it cannot = -await fula.checkConnection(); +await fula.checkConnection( + timeout: number? //default to 20. Maximum time in seconds that checkConnection waits before throwing an error +); ``` @@ -143,6 +145,7 @@ const result //returns true if there are, and false if everything is synced with = await fula.checkFailedActions( retry: boolean //if true, it tries to sync device with server, if not, it only checks + timeout: number? //default to 20. Maximum time in seconds that checkConnection waits before throwing an error ); ``` diff --git a/android/src/main/java/land/fx/fula/FulaModule.java b/android/src/main/java/land/fx/fula/FulaModule.java index 6ae82af..937335f 100755 --- a/android/src/main/java/land/fx/fula/FulaModule.java +++ b/android/src/main/java/land/fx/fula/FulaModule.java @@ -21,6 +21,11 @@ import javax.crypto.SecretKey; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + import org.json.JSONObject; import org.json.JSONArray; @@ -153,12 +158,12 @@ private byte[] convertStringToByte(@NonNull String data) { } @ReactMethod - public void checkConnection(Promise promise) { + public void checkConnection(int timeout, Promise promise) { Log.d("ReactNative", "checkConnection started"); ThreadUtils.runOnExecutor(() -> { if (this.fula != null) { try { - boolean connectionStatus = this.checkConnectionInternal(); + boolean connectionStatus = this.checkConnectionInternal(timeout); promise.resolve(connectionStatus); } catch (Exception e) { @@ -249,31 +254,46 @@ public void logout(String identityString, String storePath, Promise promise) { }); } - private boolean checkConnectionInternal() throws Exception { + private boolean checkConnectionInternal(int timeout) throws Exception { try { - Log.d("ReactNative", "checkConnectionInternal started"); - if (this.fula != null) { - try { - Log.d("ReactNative", "connectToBlox started"); - this.fula.connectToBlox(); - return true; - } - catch (Exception e) { - Log.d("ReactNative", "checkConnectionInternal failed with Error: " + e.getMessage()); - return false; + Log.d("ReactNative", "checkConnectionInternal started"); + if (this.fula != null) { + try { + Log.d("ReactNative", "connectToBlox started"); + + AtomicBoolean connectionStatus = new AtomicBoolean(false); + ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); + executor.schedule(() -> { + try { + this.fula.connectToBlox(); + connectionStatus.set(true); + } catch (Exception e) { + Log.d("ReactNative", "checkConnectionInternal failed with Error: " + e.getMessage()); + } + }, 0, TimeUnit.SECONDS); + + executor.schedule(() -> { + executor.shutdown(); + }, timeout, TimeUnit.SECONDS); + + executor.awaitTermination(timeout, TimeUnit.SECONDS); + return connectionStatus.get(); + } catch (Exception e) { + Log.d("ReactNative", "checkConnectionInternal failed with Error: " + e.getMessage()); + return false; + } + } else { + Log.d("ReactNative", "checkConnectionInternal failed because fula is not initialized "); + return false; } - } else { - Log.d("ReactNative", "checkConnectionInternal failed because fula is not initialized "); - return false; - } } catch (Exception e) { - Log.d("ReactNative", "checkConnectionInternal failed with Error: " + e.getMessage()); - throw (e); + Log.d("ReactNative", "checkConnectionInternal failed with Error: " + e.getMessage()); + throw (e); } - } +} @ReactMethod - private void checkFailedActions(boolean retry, Promise promise) throws Exception { + private void checkFailedActions(boolean retry, int timeout, Promise promise) throws Exception { try { if (this.fula != null) { if (!retry) { @@ -287,7 +307,7 @@ private void checkFailedActions(boolean retry, Promise promise) throws Exception } } else { Log.d("ReactNative", "checkFailedActions with retry"); - boolean retryResults = this.retryFailedActionsInternal(); + boolean retryResults = this.retryFailedActionsInternal(timeout); promise.resolve(!retryResults); } } else { @@ -299,13 +319,13 @@ private void checkFailedActions(boolean retry, Promise promise) throws Exception } } - private boolean retryFailedActionsInternal() throws Exception { + private boolean retryFailedActionsInternal(int timeout) throws Exception { try { Log.d("ReactNative", "retryFailedActionsInternal started"); if (this.fula != null) { //Fula is initialized try { - boolean connectionCheck = this.checkConnectionInternal(); + boolean connectionCheck = this.checkConnectionInternal(timeout); if(connectionCheck) { try { Log.d("ReactNative", "retryFailedPushes started"); diff --git a/package.json b/package.json index 638ffed..cd9111b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@functionland/react-native-fula", - "version": "1.1.5", + "version": "1.1.6", "description": "This package is a bridge to use the Fula libp2p protocols in the react-native which is using wnfs", "main": "lib/commonjs/index", "module": "lib/module/index", diff --git a/src/interfaces/fulaNativeModule.ts b/src/interfaces/fulaNativeModule.ts index 8b880cb..7350202 100644 --- a/src/interfaces/fulaNativeModule.ts +++ b/src/interfaces/fulaNativeModule.ts @@ -22,8 +22,8 @@ interface FulaNativeModule { ) => Promise; isReady: (filesystemCheck: boolean) => Promise; logout: (identity: string, storePath: string) => Promise; - checkFailedActions: (retry: boolean) => Promise; - checkConnection: () => Promise; + checkFailedActions: (retry: boolean, timeout: number) => Promise; + checkConnection: (timeout: number) => Promise; get: (key: string) => Promise; has: (key: Uint8Array) => Promise; push: () => Promise; diff --git a/src/protocols/fula.ts b/src/protocols/fula.ts index 1fde45d..3e742ed 100644 --- a/src/protocols/fula.ts +++ b/src/protocols/fula.ts @@ -74,16 +74,17 @@ export const logout = ( * Checks if there are any un-synced changes on the device */ export const checkFailedActions = ( - retry: boolean = false + retry: boolean = false, + timeout: number = 20 ): Promise => { - return Fula.checkFailedActions(retry); + return Fula.checkFailedActions(retry, timeout); }; /** * Checks if there are any un-synced changes on the device */ -export const checkConnection = (): Promise => { - return Fula.checkConnection(); +export const checkConnection = (timeout: number = 20): Promise => { + return Fula.checkConnection(timeout); }; /**