diff --git a/README.md b/README.md index 5d824b92..0c4dfd06 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ dependencies { ### Android - Open `lib/android` in Android Studio - To enable documentation for the LDK code, follow this guide: [How to attach JavaDoc to the library in Android Studio](https://medium.com/@mydogtom/tip-how-to-attach-javadoc-to-the-library-in-android-studio-5ff43c4303b3), ie.: - 1. Switch to `Project` view in the Project browser tool windo + 1. Switch to `Project` view in the Project browser tool window 2. Expand `External Libraries` 3. Right click on `Gradle: ./libs/LDK-release.aar` then `Library Properties…` 4. Tap the ➕ button and select the `./lib/android/libs/ldk-java-javadoc.jar` file diff --git a/lib/android/src/main/java/com/reactnativeldk/LdkModule.kt b/lib/android/src/main/java/com/reactnativeldk/LdkModule.kt index 1f1a8b79..ad83c861 100644 --- a/lib/android/src/main/java/com/reactnativeldk/LdkModule.kt +++ b/lib/android/src/main/java/com/reactnativeldk/LdkModule.kt @@ -10,7 +10,6 @@ import org.ldk.enums.Currency import org.ldk.enums.Network import org.ldk.enums.Recipient import org.ldk.enums.RetryableSendFailure -import org.ldk.impl.bindings.LDKPaymentSendFailure.DuplicatePayment import org.ldk.impl.bindings.get_ldk_c_bindings_version import org.ldk.impl.bindings.get_ldk_version import org.ldk.structs.* @@ -29,6 +28,10 @@ import java.nio.file.Files import java.nio.file.Paths import java.text.SimpleDateFormat import java.util.* +import java.util.concurrent.ConcurrentLinkedQueue +import java.util.concurrent.ScheduledFuture +import java.util.concurrent.ScheduledThreadPoolExecutor +import java.util.concurrent.TimeUnit //MARK: ************Replicate in typescript and swift************ @@ -178,9 +181,9 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod private var currentBlockchainHeight: Double? = null //List of peers that "should" remain connected. Stores address: String, port: Double, pubKey: String - private var addedPeers: MutableList> = mutableListOf() - private var currentlyConnectingPeers: MutableList = mutableListOf() - private var timerTaskScheduled: Boolean = false + private var addedPeers = ConcurrentLinkedQueue>() + private var currentlyConnectingPeers = ConcurrentLinkedQueue() + private var periodicDroppedPeersHandler: ScheduledFuture<*>? = null //Static to be accessed from other classes companion object { @@ -508,14 +511,10 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod peerHandler = channelManagerConstructor!!.nio_peer_handler - //Start watching for dropped peers every 1 second - if (!timerTaskScheduled) { - Timer().schedule(object : TimerTask() { - override fun run() { - handleDroppedPeers() - } - }, 1000, 3000) - timerTaskScheduled = true + //after 1s, Start watching for dropped peers every 3 seconds + if (periodicDroppedPeersHandler == null) { + periodicDroppedPeersHandler = ScheduledThreadPoolExecutor(1) + .scheduleWithFixedDelay(::handleDroppedPeers,1, 3, TimeUnit.SECONDS) } //Cached for restarts @@ -617,7 +616,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod handleResolve(promise, LdkCallbackResponses.chain_sync_success) } - fun handleDroppedPeers() { + private fun handleDroppedPeers() { peerHandler ?: return LdkEventEmitter.send(EventTypes.native_log, "Handling dropped peers error. Peer handler not initialized.") LdkEventEmitter.send(EventTypes.native_log, "Checking for dropped peers") @@ -675,7 +674,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod currentlyConnectingPeers.remove(pubKey) //Should retry if success or fail - if (!addedPeers.map { it["pubKey"] as String }.contains(pubKey)) { + if (addedPeers.none { it["pubKey"] as String == pubKey }) { addedPeers.add(hashMapOf( "address" to address, "port" to port,