From 02ce13b784cb420507af014e2069401f1525096e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 19 Jul 2020 21:10:10 +0300 Subject: [PATCH] fix serialization --- .../control/controllers/DeviceMessage.kt | 18 +++++++++++++++--- .../control/server/deviceWebServer.kt | 12 +++++------- .../dataforge/control/demo/demoDeviceServer.kt | 1 + 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/dataforge-device-core/src/commonMain/kotlin/hep/dataforge/control/controllers/DeviceMessage.kt b/dataforge-device-core/src/commonMain/kotlin/hep/dataforge/control/controllers/DeviceMessage.kt index 78cccba..e666f9b 100644 --- a/dataforge-device-core/src/commonMain/kotlin/hep/dataforge/control/controllers/DeviceMessage.kt +++ b/dataforge-device-core/src/commonMain/kotlin/hep/dataforge/control/controllers/DeviceMessage.kt @@ -3,10 +3,11 @@ package hep.dataforge.control.controllers import hep.dataforge.control.controllers.DeviceMessage.Companion.PAYLOAD_VALUE_KEY import hep.dataforge.meta.* import hep.dataforge.names.asName +import kotlinx.serialization.* - +@Serializable class DeviceMessage : Scheme() { - var id by string { error("The message id must not be empty") } + var id by string() var parent by string() var origin by string() var target by string() @@ -25,7 +26,7 @@ class DeviceMessage : Scheme() { fun append(spec: Specification, block: T.() -> Unit): T = spec.invoke(block).also { config.append(MESSAGE_PAYLOAD_KEY, it) } - companion object : SchemeSpec(::DeviceMessage) { + companion object : SchemeSpec(::DeviceMessage), KSerializer { val MESSAGE_ACTION_KEY = "action".asName() val MESSAGE_PAYLOAD_KEY = "payload".asName() val PAYLOAD_VALUE_KEY = "value".asName() @@ -47,6 +48,17 @@ class DeviceMessage : Scheme() { parent = request?.id status = RESPONSE_FAIL_STATUS }.apply(block) + + override val descriptor: SerialDescriptor = MetaSerializer.descriptor + + override fun deserialize(decoder: Decoder): DeviceMessage { + val meta = MetaSerializer.deserialize(decoder) + return wrap(meta) + } + + override fun serialize(encoder: Encoder, value: DeviceMessage) { + MetaSerializer.serialize(encoder, value.toMeta()) + } } } diff --git a/dataforge-device-server/src/main/kotlin/hep/dataforge/control/server/deviceWebServer.kt b/dataforge-device-server/src/main/kotlin/hep/dataforge/control/server/deviceWebServer.kt index 9f31b24..0ccc854 100644 --- a/dataforge-device-server/src/main/kotlin/hep/dataforge/control/server/deviceWebServer.kt +++ b/dataforge-device-server/src/main/kotlin/hep/dataforge/control/server/deviceWebServer.kt @@ -14,7 +14,6 @@ import hep.dataforge.meta.toMetaItem import hep.dataforge.meta.wrap import io.ktor.application.* import io.ktor.features.CORS -import io.ktor.features.ContentNegotiation import io.ktor.features.StatusPages import io.ktor.html.respondHtml import io.ktor.http.HttpStatusCode @@ -22,7 +21,6 @@ import io.ktor.request.receiveText import io.ktor.response.respond import io.ktor.response.respondRedirect import io.ktor.routing.* -import io.ktor.serialization.json import io.ktor.server.cio.CIO import io.ktor.server.engine.ApplicationEngine import io.ktor.server.engine.embeddedServer @@ -63,9 +61,9 @@ fun CoroutineScope.startDeviceServer( install(CORS) { anyHost() } - install(ContentNegotiation) { - json() - } +// install(ContentNegotiation) { +// json() +// } install(StatusPages) { exception { cause -> call.respond(HttpStatusCode.BadRequest, cause.message ?: "") @@ -96,7 +94,7 @@ private suspend fun ApplicationCall.message(target: MessageController) { val request = DeviceMessage.wrap(meta) val response = target.respondMessage(request) - respond(response.toMeta()) + respondMessage(response) } private suspend fun ApplicationCall.getProperty(target: MessageController) { @@ -111,7 +109,7 @@ private suspend fun ApplicationCall.getProperty(target: MessageController) { } val response = target.respondMessage(request) - respond(response.toMeta()) + respondMessage(response) } private suspend fun ApplicationCall.setProperty(target: MessageController) { diff --git a/demo/src/main/kotlin/hep/dataforge/control/demo/demoDeviceServer.kt b/demo/src/main/kotlin/hep/dataforge/control/demo/demoDeviceServer.kt index 9233644..907f6c3 100644 --- a/demo/src/main/kotlin/hep/dataforge/control/demo/demoDeviceServer.kt +++ b/demo/src/main/kotlin/hep/dataforge/control/demo/demoDeviceServer.kt @@ -50,6 +50,7 @@ suspend fun Trace.updateXYFrom(flow: Flow>>) { } + fun CoroutineScope.startDemoDeviceServer(device: DemoDevice): ApplicationEngine { val server = startDeviceServer(mapOf("demo" to device)) server.whenStarted {