diff --git a/.ci/doc/test-snippet.sh b/.ci/doc/test-snippet.sh index d259fceb..a619beeb 100755 --- a/.ci/doc/test-snippet.sh +++ b/.ci/doc/test-snippet.sh @@ -4,7 +4,7 @@ set -e if [ ${1: -3} == ".kt" ] then - cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/kotlin-project/libs/ && cp $1 /mnt/.ci/doc/kotlin-project/src/main/java/SnippetTest.kt && cd /mnt/.ci/doc/kotlin-project/ && ./gradlew build && java -classpath 'libs/sdk-jvm-1.2.1-without-dependencies.jar:' -jar build/libs/project-1.jar + cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/kotlin-project/libs/ && cp $1 /mnt/.ci/doc/kotlin-project/src/main/java/SnippetTest.kt && cd /mnt/.ci/doc/kotlin-project/ && ./gradlew build && java -classpath 'libs/sdk-jvm-1.2.2-without-dependencies.jar:' -jar build/libs/project-1.jar else - cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/java-project/libs/ && cp $1 /mnt/.ci/doc/java-project/src/main/java/SnippetTest.java && cd /mnt/.ci/doc/java-project/ && ./gradlew build && java -classpath 'libs/jvm-1.2.1-without-dependencies.jar:' -jar build/libs/project-1.jar + cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/java-project/libs/ && cp $1 /mnt/.ci/doc/java-project/src/main/java/SnippetTest.java && cd /mnt/.ci/doc/java-project/ && ./gradlew build && java -classpath 'libs/jvm-1.2.2-without-dependencies.jar:' -jar build/libs/project-1.jar fi diff --git a/README.md b/README.md index 2d69f948..13447183 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ https://bintray.com/kuzzle/maven io.kuzzle sdk-jvm - 1.2.1 + 1.2.2 pom ``` @@ -54,19 +54,19 @@ https://bintray.com/kuzzle/maven ### Gradle ```groovy -compile 'io.kuzzle:sdk-jvm:1.2.1' +compile 'io.kuzzle:sdk-jvm:1.2.2' ``` For amd64: ```groovy -compile 'io.kuzzle:sdk-jvm:1.2.1' +compile 'io.kuzzle:sdk-jvm:1.2.2' ``` ### Ivy ```html - + ``` diff --git a/build.gradle.kts b/build.gradle.kts index 682dcb81..bbf27993 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -17,7 +17,7 @@ plugins { val artifactName = "sdk-jvm" val artifactGroup = "io.kuzzle" -val artifactVersion = "1.2.1" +val artifactVersion = "1.2.2" val pomUrl = "https://github.com/kuzzleio/sdk-jvm" val pomScmUrl = "https://github.com/kuzzleio/sdk-jvm" @@ -35,7 +35,7 @@ val pomDeveloperId = "kuzzleio" val pomDeveloperName = "kuzzle" group = "io.kuzzle.sdk" -version = "1.2.1" +version = "1.2.2" val ktorVersion = "1.5.2" repositories { @@ -90,12 +90,12 @@ application { } tasks.withType { - archiveFileName.set("sdk-jvm-1.2.1-without-dependencies.jar") + archiveFileName.set("sdk-jvm-1.2.2-without-dependencies.jar") } tasks { register("fatJar", Jar::class.java) { - archiveFileName.set("sdk-jvm-1.2.1.jar") + archiveFileName.set("sdk-jvm-1.2.2.jar") duplicatesStrategy = DuplicatesStrategy.EXCLUDE manifest { attributes("Main-Class" to application.mainClassName) diff --git a/doc/1/getting-started/java/index.md b/doc/1/getting-started/java/index.md index 975fd5db..c1b29eca 100644 --- a/doc/1/getting-started/java/index.md +++ b/doc/1/getting-started/java/index.md @@ -61,7 +61,7 @@ To build the project, add the following lines: io.kuzzle sdk-jvm - 1.2.1 + 1.2.2 pom ``` @@ -75,14 +75,14 @@ repositories { } } dependencies { - compile 'io.kuzzle:sdk-jvm:1.2.1' + compile 'io.kuzzle:sdk-jvm:1.2.2' } ``` ### Ivy ```html - + ``` diff --git a/doc/1/getting-started/kotlin/index.md b/doc/1/getting-started/kotlin/index.md index 398123bd..e1c52f59 100644 --- a/doc/1/getting-started/kotlin/index.md +++ b/doc/1/getting-started/kotlin/index.md @@ -73,7 +73,7 @@ To build the project, add the following lines: io.kuzzle sdk-jvm - 1.2.1 + 1.2.2 pom ``` @@ -87,14 +87,14 @@ repositories { } } dependencies { - compile 'io.kuzzle:sdk-jvm:1.2.1' + compile 'io.kuzzle:sdk-jvm:1.2.2' } ``` ### Ivy ```html - + ``` diff --git a/src/main/kotlin/io/kuzzle/sdk/controllers/CollectionController.kt b/src/main/kotlin/io/kuzzle/sdk/controllers/CollectionController.kt index da0523c8..e47b325e 100644 --- a/src/main/kotlin/io/kuzzle/sdk/controllers/CollectionController.kt +++ b/src/main/kotlin/io/kuzzle/sdk/controllers/CollectionController.kt @@ -20,7 +20,7 @@ class CollectionController(kuzzle: Kuzzle) : BaseController(kuzzle) { put("action", "create") put("index", index) put("collection", collection) - put("definition", definition) + put("body", definition) } ) .thenApplyAsync { null } diff --git a/src/main/kotlin/io/kuzzle/sdk/protocol/WebSocket.kt b/src/main/kotlin/io/kuzzle/sdk/protocol/WebSocket.kt index b3ebedae..e0018091 100644 --- a/src/main/kotlin/io/kuzzle/sdk/protocol/WebSocket.kt +++ b/src/main/kotlin/io/kuzzle/sdk/protocol/WebSocket.kt @@ -21,6 +21,7 @@ import java.net.ConnectException import java.net.SocketException import java.util.concurrent.CompletableFuture import java.util.concurrent.ConcurrentLinkedQueue +import java.util.concurrent.atomic.AtomicBoolean import kotlin.concurrent.thread open class WebSocket : AbstractProtocol { @@ -33,7 +34,7 @@ open class WebSocket : AbstractProtocol { private val autoReconnect: Boolean private val reconnectionDelay: Long private val reconnectionRetries: Long - private var retryCount: Long = 0 + private val stopRetryingToConnect: AtomicBoolean = AtomicBoolean(false) @KtorExperimentalAPI protected open var client = HttpClient { @@ -61,35 +62,47 @@ open class WebSocket : AbstractProtocol { this.reconnectionRetries = reconnectionRetries } - private fun tryToReconnect(): Boolean { - if (!autoReconnect) - return false + @KtorExperimentalAPI + private fun tryToReconnect(): CompletableFuture { + if (!autoReconnect || this.stopRetryingToConnect.get()) + return CompletableFuture.completedFuture(false) state = ProtocolState.RECONNECTING trigger("networkStateChange", state.toString()) + return CompletableFuture.supplyAsync( + fun(): Boolean { + var retryCount: Long = 0 + while ((reconnectionRetries == -1L || retryCount < reconnectionRetries) && !this.stopRetryingToConnect.get()) { + // If not infinite, increment retryCount + if (reconnectionRetries != -1L) + retryCount++ - while (retryCount < reconnectionRetries) { - retryCount++ - Thread.sleep(reconnectionDelay) - try { - connect() - return true - } catch (e: Exception) { - // Nothing to do, just retry + Thread.sleep(reconnectionDelay) + try { + connect() + return true + } catch (e: Exception) { + // Nothing to do, just retry + } + } + this.stopRetryingToConnect.set(false) + return false } - } - return false + ) } @KtorExperimentalAPI override fun connect() { + if (this.stopRetryingToConnect.get()) + throw Exception("Connection Aborted") + val wait = CompletableFuture() val block: suspend DefaultClientWebSocketSession.() -> Unit = { ws = this // @TODO Create enums for events state = ProtocolState.OPEN trigger("networkStateChange", ProtocolState.OPEN.toString()) - retryCount = 0 + thread(start = true) { while (ws != null) { val payload = queue.poll() @@ -101,7 +114,7 @@ open class WebSocket : AbstractProtocol { } } wait.complete(null) - var reconnected = false + var skip = false try { for (frame in incoming) { when (frame) { @@ -112,9 +125,20 @@ open class WebSocket : AbstractProtocol { } } } catch (e: Exception) { - reconnected = tryToReconnect() + skip = true + tryToReconnect().thenApply( + fun (success: Boolean) { + if (!success) { + state = ProtocolState.CLOSE + trigger("networkStateChange", ProtocolState.CLOSE.toString()) + ws = null + } + // reset stopRetryingToConnect + stopRetryingToConnect.set(false) + } + ) } - if (!reconnected) { + if (!skip) { state = ProtocolState.CLOSE trigger("networkStateChange", ProtocolState.CLOSE.toString()) ws = null @@ -135,7 +159,10 @@ open class WebSocket : AbstractProtocol { block = block ) } - retryCount = 0 + + // On connection success + stopRetryingToConnect.set(false) + // This thread is here to let JAVA run until the socket is closed // In Kotlin this is handled by the block function above but for some reason in JAVA it is // non blocking. @@ -148,11 +175,16 @@ open class WebSocket : AbstractProtocol { is SocketException, is IOException -> { if (state != ProtocolState.RECONNECTING) { - if (!tryToReconnect()) { - wait.completeExceptionally(e) - } else { - wait.complete(null) - } + tryToReconnect().thenAcceptAsync( + fun (success: Boolean) { + if (success) { + wait.complete(null) + } else { + wait.completeExceptionally(e) + } + stopRetryingToConnect.set(false) + } + ) } else { wait.completeExceptionally(e) } @@ -164,11 +196,14 @@ open class WebSocket : AbstractProtocol { } override fun disconnect() { - state = ProtocolState.CLOSE - trigger("networkStateChange", ProtocolState.CLOSE.toString()) - GlobalScope.launch { - ws?.close() - ws = null + if (state != ProtocolState.CLOSE) { + state = ProtocolState.CLOSE + trigger("networkStateChange", ProtocolState.CLOSE.toString()) + stopRetryingToConnect.set(true) + GlobalScope.launch { + ws?.close() + ws = null + } } }