From 5b9535e9964816eb228d3797f4c8a3e7676d1f53 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 25 Apr 2018 08:12:25 +0200 Subject: Multiplatform kvision-common and kvision-server modules. Support for automatic remote bindings (work in progress). --- .../kotlin/pl/treksoft/kvision/remote/Jooby.kt | 34 ++ .../pl/treksoft/kvision/remote/RemoteAgent.kt | 417 +++++++++++++++++++++ .../pl/treksoft/kvision/remote/ServiceManager.kt | 117 ++++++ 3 files changed, 568 insertions(+) create mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/RemoteAgent.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt (limited to 'src/main/kotlin/pl/treksoft/kvision/remote') diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt b/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt new file mode 100644 index 00000000..a5c580d6 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package pl.treksoft.kvision.remote + +/** + * A Jooby based server. + * Not used on the js platform. + */ +actual open class JoobyServer + +/** + * A server request. + * Not used on the js platform. + */ +actual interface Request diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/RemoteAgent.kt b/src/main/kotlin/pl/treksoft/kvision/remote/RemoteAgent.kt new file mode 100644 index 00000000..3f7276f0 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/remote/RemoteAgent.kt @@ -0,0 +1,417 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package pl.treksoft.kvision.remote + +import kotlinx.coroutines.experimental.Deferred +import kotlinx.serialization.KSerializer +import kotlinx.serialization.internal.ArrayListSerializer +import kotlinx.serialization.internal.BooleanSerializer +import kotlinx.serialization.internal.CharSerializer +import kotlinx.serialization.internal.DoubleSerializer +import kotlinx.serialization.internal.LongSerializer +import kotlinx.serialization.internal.StringSerializer +import kotlinx.serialization.json.JSON +import kotlinx.serialization.list +import kotlinx.serialization.serializer +import pl.treksoft.jquery.JQueryXHR +import pl.treksoft.jquery.jQuery +import pl.treksoft.kvision.utils.obj +import kotlin.js.Promise +import kotlin.js.js +import kotlin.js.undefined +import kotlin.reflect.KClass +import kotlin.js.JSON as NativeJSON + +internal class NonStandardTypeException(type: String) : Exception("Non standard type: $type!") + +/** + * Client side agent for remote calls. + */ +@Suppress("EXPERIMENTAL_FEATURE_WARNING", "LargeClass", "TooManyFunctions") +open class RemoteAgent(val serviceManager: ServiceManager) { + + /** + * Executes defined call to a remote web service. + */ + inline fun call(noinline function: T.(Request?) -> Deferred): Promise { + val url = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return ajaxCall(url, null).then { + try { + deserialize(it, RET::class.js.name) + } catch (t: NonStandardTypeException) { + JSON.parse(RET::class.serializer(), it) + } + } + } + + /** + * Executes defined call to a remote web service. + */ + inline fun call(noinline function: T.(Request?) -> Deferred>): Promise> { + val url = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return ajaxCall(url, null).then { + try { + deserializeLists(it, RET::class.js.name) + } catch (t: NonStandardTypeException) { + JSON.parse(RET::class.serializer().list, it) + } + } + } + + /** + * Executes defined call to a remote web service. + */ + inline fun call( + noinline function: T.(PAR, Request?) -> Deferred, + p: PAR, serializer: KSerializer? = null + ): Promise { + val data = serialize(p, serializer) + val url = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return ajaxCall(url, data).then { + try { + @Suppress("UNCHECKED_CAST") + deserialize(it, (RET::class as KClass).js.name) + } catch (t: NonStandardTypeException) { + JSON.parse(RET::class.serializer(), it) + } + } + } + + /** + * Executes defined call to a remote web service. + */ + inline fun call( + noinline function: T.(PAR, Request?) -> Deferred>, + p: PAR, serializer: KSerializer? = null + ): Promise> { + val data = serialize(p, serializer) + val url = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return ajaxCall(url, data).then { + try { + deserializeLists(it, RET::class.js.name) + } catch (t: NonStandardTypeException) { + JSON.parse(RET::class.serializer().list, it) + } + } + } + + /** + * Executes defined call to a remote web service. + */ + inline fun call( + noinline function: T.(PAR1, PAR2, Request?) -> Deferred, + p1: PAR1, p2: PAR2, serializer1: KSerializer? = null, serializer2: KSerializer? = null + ): Promise { + val data1 = serialize(p1, serializer1) + val data2 = serialize(p2, serializer2) + val data = "[ $data1 , $data2 ]" + val url = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return ajaxCall(url, data).then { + try { + deserialize(it, RET::class.js.name) + } catch (t: NonStandardTypeException) { + JSON.parse(RET::class.serializer(), it) + } + } + } + + /** + * Executes defined call to a remote web service. + */ + inline fun call( + noinline function: T.(PAR1, PAR2, Request?) -> Deferred>, + p1: PAR1, p2: PAR2, serializer1: KSerializer? = null, serializer2: KSerializer? = null + ): Promise> { + val data1 = serialize(p1, serializer1) + val data2 = serialize(p2, serializer2) + val data = "[ $data1 , $data2 ]" + val url = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return ajaxCall(url, data).then { + try { + deserializeLists(it, RET::class.js.name) + } catch (t: NonStandardTypeException) { + JSON.parse(RET::class.serializer().list, it) + } + } + } + + /** + * Executes defined call to a remote web service. + */ + inline fun call( + noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred, + p1: PAR1, p2: PAR2, p3: PAR3, serializer1: KSerializer? = null, serializer2: KSerializer? = null, + serializer3: KSerializer? = null + ): Promise { + val data1 = serialize(p1, serializer1) + val data2 = serialize(p2, serializer2) + val data3 = serialize(p3, serializer3) + val data = "[ $data1 , $data2 , $data3 ]" + val url = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return ajaxCall(url, data).then { + try { + deserialize(it, RET::class.js.name) + } catch (t: NonStandardTypeException) { + JSON.parse(RET::class.serializer(), it) + } + } + } + + /** + * Executes defined call to a remote web service. + */ + inline fun call( + noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred>, + p1: PAR1, p2: PAR2, p3: PAR3, serializer1: KSerializer? = null, serializer2: KSerializer? = null, + serializer3: KSerializer? = null + ): Promise> { + val data1 = serialize(p1, serializer1) + val data2 = serialize(p2, serializer2) + val data3 = serialize(p3, serializer3) + val data = "[ $data1 , $data2 , $data3 ]" + val url = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return ajaxCall(url, data).then { + try { + deserializeLists(it, RET::class.js.name) + } catch (t: NonStandardTypeException) { + JSON.parse(RET::class.serializer().list, it) + } + } + } + + /** + * Executes defined call to a remote web service. + */ + inline fun call( + noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred, + p1: PAR1, + p2: PAR2, + p3: PAR3, + p4: PAR4, + serializer1: KSerializer? = null, + serializer2: KSerializer? = null, + serializer3: KSerializer? = null, + serializer4: KSerializer? = null + ): Promise { + val data1 = serialize(p1, serializer1) + val data2 = serialize(p2, serializer2) + val data3 = serialize(p3, serializer3) + val data4 = serialize(p4, serializer4) + val data = "[ $data1 , $data2 , $data3 , $data4 ]" + val url = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return ajaxCall(url, data).then { + try { + deserialize(it, RET::class.js.name) + } catch (t: NonStandardTypeException) { + JSON.parse(RET::class.serializer(), it) + } + } + } + + /** + * Executes defined call to a remote web service. + */ + inline fun call( + noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred>, + p1: PAR1, + p2: PAR2, + p3: PAR3, + p4: PAR4, + serializer1: KSerializer? = null, + serializer2: KSerializer? = null, + serializer3: KSerializer? = null, + serializer4: KSerializer? = null + ): Promise> { + val data1 = serialize(p1, serializer1) + val data2 = serialize(p2, serializer2) + val data3 = serialize(p3, serializer3) + val data4 = serialize(p4, serializer4) + val data = "[ $data1 , $data2 , $data3 , $data4 ]" + val url = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return ajaxCall(url, data).then { + try { + deserializeLists(it, RET::class.js.name) + } catch (t: NonStandardTypeException) { + JSON.parse(RET::class.serializer().list, it) + } + } + } + + /** + * Executes defined call to a remote web service. + */ + @Suppress("LongParameterList") + inline fun call( + noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred, + p1: PAR1, + p2: PAR2, + p3: PAR3, + p4: PAR4, + p5: PAR5, + serializer1: KSerializer? = null, + serializer2: KSerializer? = null, + serializer3: KSerializer? = null, + serializer4: KSerializer? = null, + serializer5: KSerializer? = null + ): Promise { + val data1 = serialize(p1, serializer1) + val data2 = serialize(p2, serializer2) + val data3 = serialize(p3, serializer3) + val data4 = serialize(p4, serializer4) + val data5 = serialize(p5, serializer5) + val data = "[ $data1 , $data2 , $data3 , $data4 , $data5 ]" + val url = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return ajaxCall(url, data).then { + try { + deserialize(it, RET::class.js.name) + } catch (t: NonStandardTypeException) { + JSON.parse(RET::class.serializer(), it) + } + } + } + + /** + * Executes defined call to a remote web service. + */ + @Suppress("LongParameterList") + inline fun call( + noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred>, + p1: PAR1, + p2: PAR2, + p3: PAR3, + p4: PAR4, + p5: PAR5, + serializer1: KSerializer? = null, + serializer2: KSerializer? = null, + serializer3: KSerializer? = null, + serializer4: KSerializer? = null, + serializer5: KSerializer? = null + ): Promise> { + val data1 = serialize(p1, serializer1) + val data2 = serialize(p2, serializer2) + val data3 = serialize(p3, serializer3) + val data4 = serialize(p4, serializer4) + val data5 = serialize(p5, serializer5) + val data = "[ $data1 , $data2 , $data3 , $data4 , $data5 ]" + val url = + serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + return ajaxCall(url, data).then { + try { + deserializeLists(it, RET::class.js.name) + } catch (t: NonStandardTypeException) { + JSON.parse(RET::class.serializer().list, it) + } + } + } + + /** + * @suppress + * Internal function + */ + @Suppress("UnsafeCastFromDynamic") + fun ajaxCall(url: String, data: Any?): Promise = + Promise({ resolve, reject -> + jQuery.ajax(url, obj { + this.contentType = "application/json" + this.data = data + this.method = "POST" + this.success = + { data: Any, _: Any, _: Any -> + resolve(NativeJSON.stringify(data)) + } + this.error = + { xhr: JQueryXHR, _: String, errorText: String -> + val message = if (xhr.responseJSON != null && xhr.responseJSON != undefined) { + xhr.responseJSON.toString() + } else { + errorText + } + reject(Exception(message)) + } + }) + }) + + /** + * @suppress + * Internal function + */ + @Suppress("TooGenericExceptionCaught") + inline fun serialize(value: PAR, serializer: KSerializer?): String? { + return value?.let { + if (serializer != null) { + JSON.stringify(serializer, it) + } else { + try { + @Suppress("UNCHECKED_CAST") + JSON.stringify((PAR::class as KClass).serializer(), it as Any) + } catch (e: Throwable) { + it.toString() + } + } + } + } + + /** + * @suppress + * Internal function + */ + @Suppress("UNCHECKED_CAST") + fun deserialize(value: String, type: String): RET { + return when (type) { + "String" -> JSON.parse(StringSerializer, value) as RET + "Number" -> JSON.parse(DoubleSerializer, value) as RET + "Long" -> JSON.parse(LongSerializer, value) as RET + "Boolean" -> JSON.parse(BooleanSerializer, value) as RET + "Char" -> JSON.parse(CharSerializer, value) as RET + else -> throw NonStandardTypeException(type) + } + } + + /** + * @suppress + * Internal function + */ + @Suppress("UNCHECKED_CAST") + fun deserializeLists(value: String, type: String): List { + return when (type) { + "String" -> JSON.parse(ArrayListSerializer(StringSerializer), value) as List + "Number" -> JSON.parse(ArrayListSerializer(DoubleSerializer), value) as List + "Long" -> JSON.parse(ArrayListSerializer(LongSerializer), value) as List + "Boolean" -> JSON.parse(ArrayListSerializer(BooleanSerializer), value) as List + "Char" -> JSON.parse(ArrayListSerializer(CharSerializer), value) as List + else -> throw NonStandardTypeException(type) + } + } +} diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt b/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt new file mode 100644 index 00000000..cd42f038 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package pl.treksoft.kvision.remote + +import kotlinx.coroutines.experimental.Deferred + +/** + * Multiplatform service manager. + */ +actual open class ServiceManager actual constructor(service: T?) { + protected val calls: MutableMap = mutableMapOf() + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected actual inline fun bind( + route: String, + noinline function: T.(Request?) -> Deferred + ) { + calls[function.toString()] = "$SERVICE_PREFIX/$route" + } + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected actual inline fun bind( + route: String, + noinline function: T.(PAR, Request?) -> Deferred + ) { + calls[function.toString()] = "$SERVICE_PREFIX/$route" + } + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected actual inline fun bind( + route: String, + noinline function: T.(PAR1, PAR2, Request?) -> Deferred + ) { + calls[function.toString()] = "$SERVICE_PREFIX/$route" + } + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected actual inline fun bind( + route: String, + noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred + ) { + calls[function.toString()] = "$SERVICE_PREFIX/$route" + } + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected actual inline fun bind( + route: String, + noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred + ) { + calls[function.toString()] = "$SERVICE_PREFIX/$route" + } + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected actual inline fun bind( + route: String, + noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred + ) { + calls[function.toString()] = "$SERVICE_PREFIX/$route" + } + + /** + * Applies all defined routes to the given server. + * Not used on the js platform. + */ + actual fun applyRoutes(k: JoobyServer) { + } + + /** + * Returns the list of defined bindings. + * Not used on the jvm platform. + */ + actual fun getCalls(): Map = calls +} -- cgit