diff --git a/build.gradle.kts b/build.gradle.kts index 6d7df71..8a4a56f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,9 +2,9 @@ plugins { id("ru.mipt.npm.gradle.project") } -val dataforgeVersion: String by extra("0.5.1") +val dataforgeVersion: String by extra("0.5.2") val ktorVersion: String by extra(ru.mipt.npm.gradle.KScienceVersions.ktorVersion) -val rsocketVersion by extra("0.13.1") +val rsocketVersion by extra("0.15.4") allprojects { group = "ru.mipt.npm" diff --git a/controls-opcua/src/main/kotlin/ru/mipt/npm/controls/opcua/client/MiloDevice.kt b/controls-opcua/src/main/kotlin/ru/mipt/npm/controls/opcua/client/MiloDevice.kt index de56325..04393fc 100644 --- a/controls-opcua/src/main/kotlin/ru/mipt/npm/controls/opcua/client/MiloDevice.kt +++ b/controls-opcua/src/main/kotlin/ru/mipt/npm/controls/opcua/client/MiloDevice.kt @@ -39,8 +39,8 @@ public suspend inline fun MiloDevice.readOpcWithTime( val time = data.serverTime ?: error("No server time provided") val meta: Meta = when (val content = data.value.value) { is T -> return content to time - content is Meta -> content as Meta - content is ExtensionObject -> (content as ExtensionObject).decode(client.dynamicSerializationContext) as Meta + is Meta -> content + is ExtensionObject -> content.decode(client.dynamicSerializationContext) as Meta else -> error("Incompatible OPC property value $content") } diff --git a/controls-server/build.gradle.kts b/controls-server/build.gradle.kts index 7eba824..710550c 100644 --- a/controls-server/build.gradle.kts +++ b/controls-server/build.gradle.kts @@ -15,7 +15,9 @@ dependencies { implementation(project(":controls-tcp")) implementation(projects.magix.magixServer) implementation("io.ktor:ktor-server-cio:$ktorVersion") - implementation("io.ktor:ktor-websockets:$ktorVersion") - implementation("io.ktor:ktor-serialization:$ktorVersion") - implementation("io.ktor:ktor-html-builder:$ktorVersion") + implementation("io.ktor:ktor-server-websockets:$ktorVersion") + implementation("io.ktor:ktor-server-content-negotiation:$ktorVersion") + implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion") + implementation("io.ktor:ktor-server-html-builder:$ktorVersion") + implementation("io.ktor:ktor-server-status-pages:$ktorVersion") } \ No newline at end of file diff --git a/controls-server/src/main/kotlin/ru/mipt/npm/controls/server/deviceWebServer.kt b/controls-server/src/main/kotlin/ru/mipt/npm/controls/server/deviceWebServer.kt index f896a24..c04c378 100644 --- a/controls-server/src/main/kotlin/ru/mipt/npm/controls/server/deviceWebServer.kt +++ b/controls-server/src/main/kotlin/ru/mipt/npm/controls/server/deviceWebServer.kt @@ -1,24 +1,23 @@ package ru.mipt.npm.controls.server -import io.ktor.application.* -import io.ktor.features.CORS -import io.ktor.features.StatusPages -import io.ktor.html.respondHtml import io.ktor.http.HttpStatusCode -import io.ktor.request.receiveText -import io.ktor.response.respond -import io.ktor.response.respondRedirect -import io.ktor.response.respondText -import io.ktor.routing.get -import io.ktor.routing.post -import io.ktor.routing.route -import io.ktor.routing.routing +import io.ktor.server.application.* import io.ktor.server.cio.CIO import io.ktor.server.engine.ApplicationEngine import io.ktor.server.engine.embeddedServer -import io.ktor.util.getValue -import io.ktor.websocket.WebSockets +import io.ktor.server.html.respondHtml +import io.ktor.server.plugins.statuspages.StatusPages +import io.ktor.server.request.receiveText +import io.ktor.server.response.respond +import io.ktor.server.response.respondRedirect +import io.ktor.server.response.respondText +import io.ktor.server.routing.get +import io.ktor.server.routing.post +import io.ktor.server.routing.route +import io.ktor.server.routing.routing +import io.ktor.server.util.getValue +import io.ktor.server.websocket.WebSockets import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.html.* @@ -52,11 +51,11 @@ public fun CoroutineScope.startDeviceServer( return this.embeddedServer(CIO, port, host) { install(WebSockets) - install(CORS) { - anyHost() - } +// install(CORS) { +// anyHost() +// } install(StatusPages) { - exception { cause -> + exception { call, cause -> call.respond(HttpStatusCode.BadRequest, cause.message ?: "") } } @@ -83,15 +82,15 @@ public fun Application.deviceManagerModule( rawSocketPort: Int = MagixEndpoint.DEFAULT_MAGIX_RAW_PORT, buffer: Int = 100, ) { - if (featureOrNull(WebSockets) == null) { + if (pluginOrNull(WebSockets) == null) { install(WebSockets) } - if (featureOrNull(CORS) == null) { - install(CORS) { - anyHost() - } - } +// if (pluginOrNull(CORS) == null) { +// install(CORS) { +// anyHost() +// } +// } routing { route(route) { diff --git a/controls-server/src/main/kotlin/ru/mipt/npm/controls/server/responses.kt b/controls-server/src/main/kotlin/ru/mipt/npm/controls/server/responses.kt index 8041acb..fe56b96 100644 --- a/controls-server/src/main/kotlin/ru/mipt/npm/controls/server/responses.kt +++ b/controls-server/src/main/kotlin/ru/mipt/npm/controls/server/responses.kt @@ -1,8 +1,8 @@ package ru.mipt.npm.controls.server -import io.ktor.application.ApplicationCall import io.ktor.http.ContentType -import io.ktor.response.respondText +import io.ktor.server.application.ApplicationCall +import io.ktor.server.response.respondText import kotlinx.serialization.json.JsonObjectBuilder import kotlinx.serialization.json.buildJsonObject import ru.mipt.npm.controls.api.DeviceMessage diff --git a/controls-storage/src/jvmMain/kotlin/ru/mipt/npm/controls/storage/storageJvm.kt b/controls-storage/src/jvmMain/kotlin/ru/mipt/npm/controls/storage/storageJvm.kt index cf09ceb..28135dc 100644 --- a/controls-storage/src/jvmMain/kotlin/ru/mipt/npm/controls/storage/storageJvm.kt +++ b/controls-storage/src/jvmMain/kotlin/ru/mipt/npm/controls/storage/storageJvm.kt @@ -1,6 +1,6 @@ package ru.mipt.npm.controls.storage -import io.ktor.application.Application +import io.ktor.server.application.Application import kotlinx.coroutines.InternalCoroutinesApi import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow diff --git a/controls-tcp/build.gradle.kts b/controls-tcp/build.gradle.kts index 88d2928..4ed2905 100644 --- a/controls-tcp/build.gradle.kts +++ b/controls-tcp/build.gradle.kts @@ -1,17 +1,10 @@ plugins { - id("ru.mipt.npm.gradle.mpp") + id("ru.mipt.npm.gradle.jvm") } val ktorVersion: String by rootProject.extra -kotlin { - sourceSets { - commonMain { - dependencies { - api(project(":controls-core")) - api("io.ktor:ktor-network:$ktorVersion") - } - } - - } -} \ No newline at end of file +dependencies { + api(project(":controls-core")) + api("io.ktor:ktor-network:$ktorVersion") +} diff --git a/controls-tcp/src/jvmMain/kotlin/ru/mipt/npm/controls/ports/KtorTcpPort.kt b/controls-tcp/src/main/kotlin/ru/mipt/npm/controls/ports/KtorTcpPort.kt similarity index 97% rename from controls-tcp/src/jvmMain/kotlin/ru/mipt/npm/controls/ports/KtorTcpPort.kt rename to controls-tcp/src/main/kotlin/ru/mipt/npm/controls/ports/KtorTcpPort.kt index d3f48d3..c9227d5 100644 --- a/controls-tcp/src/jvmMain/kotlin/ru/mipt/npm/controls/ports/KtorTcpPort.kt +++ b/controls-tcp/src/main/kotlin/ru/mipt/npm/controls/ports/KtorTcpPort.kt @@ -16,7 +16,6 @@ import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.get import space.kscience.dataforge.meta.int import space.kscience.dataforge.meta.string -import java.net.InetSocketAddress import kotlin.coroutines.CoroutineContext public class KtorTcpPort internal constructor( @@ -29,7 +28,7 @@ public class KtorTcpPort internal constructor( override fun toString(): String = "port[tcp:$host:$port]" private val futureSocket = scope.async { - aSocket(ActorSelectorManager(Dispatchers.IO)).tcp().connect(InetSocketAddress(host, port)) + aSocket(ActorSelectorManager(Dispatchers.IO)).tcp().connect(host, port) } private val writeChannel = scope.async { diff --git a/demo/build.gradle.kts b/demo/build.gradle.kts index 522357e..f4c06ef 100644 --- a/demo/build.gradle.kts +++ b/demo/build.gradle.kts @@ -1,15 +1,13 @@ plugins { kotlin("jvm") - id("org.openjfx.javafxplugin") version "0.0.9" + id("org.openjfx.javafxplugin") version "0.0.10" application } repositories { mavenCentral() - jcenter() maven("https://repo.kotlin.link") - maven("https://kotlin.bintray.com/kotlinx") } val ktorVersion: String by rootProject.extra @@ -26,9 +24,9 @@ dependencies { implementation("io.ktor:ktor-client-cio:$ktorVersion") implementation("no.tornado:tornadofx:1.7.20") - implementation("space.kscience:plotlykt-server:0.5.0-dev-1") - implementation("com.github.Ricky12Awesome:json-schema-serialization:0.6.6") - implementation("ch.qos.logback:logback-classic:1.2.3") + implementation("space.kscience:plotlykt-server:0.5.2-dev-2") +// implementation("com.github.Ricky12Awesome:json-schema-serialization:0.6.6") + implementation("ch.qos.logback:logback-classic:1.2.11") } tasks.withType().configureEach { diff --git a/demo/car/build.gradle.kts b/demo/car/build.gradle.kts index 93968e7..5bce76e 100644 --- a/demo/car/build.gradle.kts +++ b/demo/car/build.gradle.kts @@ -26,8 +26,8 @@ dependencies { implementation("io.ktor:ktor-client-cio:$ktorVersion") implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.3.1") implementation("no.tornado:tornadofx:1.7.20") - implementation("space.kscience:plotlykt-server:0.5.0-dev-1") - implementation("ch.qos.logback:logback-classic:1.2.3") + implementation("space.kscience:plotlykt-server:0.5.0") + implementation("ch.qos.logback:logback-classic:1.2.11") implementation("org.jetbrains.xodus:xodus-entity-store:1.3.232") implementation("org.jetbrains.xodus:xodus-environment:1.3.232") implementation("org.jetbrains.xodus:xodus-vfs:1.3.232") @@ -46,6 +46,6 @@ javafx { modules("javafx.controls") } -//application { -// mainClass.set("ru.mipt.npm.controls.demo.DemoControllerViewKt") -//} \ No newline at end of file +application { + mainClass.set("ru.mipt.npm.controls.demo.car.VirtualCarControllerKt") +} \ No newline at end of file diff --git a/demo/src/main/kotlin/ru/mipt/npm/controls/demo/demoDeviceServer.kt b/demo/src/main/kotlin/ru/mipt/npm/controls/demo/demoDeviceServer.kt index 8d6f95b..7f65add 100644 --- a/demo/src/main/kotlin/ru/mipt/npm/controls/demo/demoDeviceServer.kt +++ b/demo/src/main/kotlin/ru/mipt/npm/controls/demo/demoDeviceServer.kt @@ -1,12 +1,12 @@ package ru.mipt.npm.controls.demo -import io.ktor.application.install -import io.ktor.features.CORS +import io.ktor.server.application.install import io.ktor.server.cio.CIO import io.ktor.server.engine.ApplicationEngine import io.ktor.server.engine.embeddedServer -import io.ktor.websocket.WebSockets -import io.rsocket.kotlin.transport.ktor.server.RSocketSupport +import io.ktor.server.plugins.cors.routing.CORS +import io.ktor.server.websocket.WebSockets +import io.rsocket.kotlin.ktor.server.RSocketSupport import kotlinx.coroutines.flow.* import kotlinx.coroutines.launch import kotlinx.html.div @@ -54,33 +54,32 @@ suspend fun Trace.updateXYFrom(flow: Flow>>) { } -suspend fun MagixEndpoint.startDemoDeviceServer(): ApplicationEngine = - embeddedServer(CIO, 9090) { - install(WebSockets) - install(RSocketSupport) +suspend fun MagixEndpoint.startDemoDeviceServer(): ApplicationEngine = embeddedServer(CIO, 9090) { + install(WebSockets) + install(RSocketSupport) - install(CORS) { - anyHost() - } + install(CORS) { + anyHost() + } - val sinFlow = MutableSharedFlow()// = device.sin.flow() - val cosFlow = MutableSharedFlow()// = device.cos.flow() + val sinFlow = MutableSharedFlow()// = device.sin.flow() + val cosFlow = MutableSharedFlow()// = device.cos.flow() - launch { - subscribe().collect { magix -> - (magix.payload as? PropertyChangedMessage)?.let { message -> - when (message.property) { - "sin" -> sinFlow.emit(message.value) - "cos" -> cosFlow.emit(message.value) - } + launch { + subscribe().collect { magix -> + (magix.payload as? PropertyChangedMessage)?.let { message -> + when (message.property) { + "sin" -> sinFlow.emit(message.value) + "cos" -> cosFlow.emit(message.value) } } } + } - plotlyModule().apply { - updateMode = PlotlyUpdateMode.PUSH - updateInterval = 50 - }.page { container -> + plotlyModule{ + updateMode = PlotlyUpdateMode.PUSH + updateInterval = 50 + page { container -> val sinCosFlow = sinFlow.zip(cosFlow) { sin, cos -> sin.double!! to cos.double!! } @@ -140,6 +139,8 @@ suspend fun MagixEndpoint.startDemoDeviceServer(): ApplicationEng } } } - } - }.apply { start() } + + } + } +}.apply { start() } diff --git a/demo/src/main/kotlin/ru/mipt/npm/controls/demo/generateMessageSchema.kt b/demo/src/main/kotlin/ru/mipt/npm/controls/demo/generateMessageSchema.kt index 420bca3..85522a5 100644 --- a/demo/src/main/kotlin/ru/mipt/npm/controls/demo/generateMessageSchema.kt +++ b/demo/src/main/kotlin/ru/mipt/npm/controls/demo/generateMessageSchema.kt @@ -1,10 +1,10 @@ package ru.mipt.npm.controls.demo -import com.github.ricky12awesome.jss.encodeToSchema -import com.github.ricky12awesome.jss.globalJson -import ru.mipt.npm.controls.api.DeviceMessage +//import com.github.ricky12awesome.jss.encodeToSchema +//import com.github.ricky12awesome.jss.globalJson +//import ru.mipt.npm.controls.api.DeviceMessage -fun main() { - val schema = globalJson.encodeToSchema(DeviceMessage.serializer(), generateDefinitions = false) - println(schema) -} \ No newline at end of file +//fun main() { +// val schema = globalJson.encodeToSchema(DeviceMessage.serializer(), generateDefinitions = false) +// println(schema) +//} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index fbaacb0..7077cd1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,10 +3,9 @@ kotlin.mpp.stability.nowarn=true kotlin.jupyter.add.scanner=false -org.gradle.jvmargs=-XX:MaxMetaspaceSize=1G org.gradle.parallel=true publishing.github=false publishing.sonatype=false -toolsVersion=0.10.9-kotlin-1.6.10 \ No newline at end of file +toolsVersion=0.11.5-kotlin-1.6.21 \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index ffed3a2..aa991fc 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/magix/magix-java-client/build.gradle.kts b/magix/magix-java-client/build.gradle.kts index 5e34637..d250b3f 100644 --- a/magix/magix-java-client/build.gradle.kts +++ b/magix/magix-java-client/build.gradle.kts @@ -8,3 +8,8 @@ dependencies { implementation(project(":magix:magix-rsocket")) implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk9:${ru.mipt.npm.gradle.KScienceVersions.coroutinesVersion}") } + +java { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 +} diff --git a/magix/magix-rsocket/build.gradle.kts b/magix/magix-rsocket/build.gradle.kts index 1e0647c..3ded3e8 100644 --- a/magix/magix-rsocket/build.gradle.kts +++ b/magix/magix-rsocket/build.gradle.kts @@ -22,7 +22,13 @@ kotlin { dependencies { api(projects.magix.magixApi) implementation("io.ktor:ktor-client-core:$ktorVersion") - implementation("io.rsocket.kotlin:rsocket-transport-ktor-client:$rsocketVersion") + implementation("io.rsocket.kotlin:rsocket-ktor-client:$rsocketVersion") + } + } + jvmMain{ + dependencies{ + implementation("io.ktor:ktor-network:$ktorVersion") + implementation("io.rsocket.kotlin:rsocket-transport-ktor-tcp:$rsocketVersion") } } } diff --git a/magix/magix-rsocket/src/commonMain/kotlin/ru/mipt/npm/magix/rsocket/RSocketMagixEndpoint.kt b/magix/magix-rsocket/src/commonMain/kotlin/ru/mipt/npm/magix/rsocket/RSocketMagixEndpoint.kt index 490ddaf..7fdcee4 100644 --- a/magix/magix-rsocket/src/commonMain/kotlin/ru/mipt/npm/magix/rsocket/RSocketMagixEndpoint.kt +++ b/magix/magix-rsocket/src/commonMain/kotlin/ru/mipt/npm/magix/rsocket/RSocketMagixEndpoint.kt @@ -1,16 +1,17 @@ package ru.mipt.npm.magix.rsocket import io.ktor.client.HttpClient -import io.ktor.client.features.websocket.WebSockets +import io.ktor.client.plugins.websocket.WebSockets import io.rsocket.kotlin.RSocket import io.rsocket.kotlin.core.RSocketConnector import io.rsocket.kotlin.core.RSocketConnectorBuilder +import io.rsocket.kotlin.ktor.client.RSocketSupport +import io.rsocket.kotlin.ktor.client.rSocket import io.rsocket.kotlin.payload.buildPayload import io.rsocket.kotlin.payload.data -import io.rsocket.kotlin.transport.ktor.client.RSocketSupport -import io.rsocket.kotlin.transport.ktor.client.rSocket import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map @@ -79,7 +80,7 @@ public suspend fun MagixEndpoint.Companion.rSocketWithWebSockets( val rSocket = client.rSocket(host, port, path) //Ensure client is closed after rSocket if finished - rSocket.job.invokeOnCompletion { + rSocket.coroutineContext[Job]?.invokeOnCompletion { client.close() } diff --git a/magix/magix-rsocket/src/jvmMain/kotlin/ru/mipt/npm/magix/rsocket/withTcp.kt b/magix/magix-rsocket/src/jvmMain/kotlin/ru/mipt/npm/magix/rsocket/withTcp.kt index 90c2ffa..d3c0719 100644 --- a/magix/magix-rsocket/src/jvmMain/kotlin/ru/mipt/npm/magix/rsocket/withTcp.kt +++ b/magix/magix-rsocket/src/jvmMain/kotlin/ru/mipt/npm/magix/rsocket/withTcp.kt @@ -1,11 +1,9 @@ package ru.mipt.npm.magix.rsocket -import io.ktor.network.selector.ActorSelectorManager import io.ktor.network.sockets.SocketOptions import io.ktor.util.InternalAPI import io.rsocket.kotlin.core.RSocketConnectorBuilder -import io.rsocket.kotlin.transport.ktor.TcpClientTransport -import kotlinx.coroutines.Dispatchers +import io.rsocket.kotlin.transport.ktor.tcp.TcpClientTransport import kotlinx.serialization.KSerializer import ru.mipt.npm.magix.api.MagixEndpoint import kotlin.coroutines.coroutineContext @@ -23,7 +21,6 @@ public suspend fun MagixEndpoint.Companion.rSocketWithTcp( rSocketConfig: RSocketConnectorBuilder.ConnectionConfigBuilder.() -> Unit = {}, ): RSocketMagixEndpoint { val transport = TcpClientTransport( - ActorSelectorManager(Dispatchers.IO), hostname = host, port = port, configure = tcpConfig diff --git a/magix/magix-server/build.gradle.kts b/magix/magix-server/build.gradle.kts index 7f01ea8..5215f19 100644 --- a/magix/magix-server/build.gradle.kts +++ b/magix/magix-server/build.gradle.kts @@ -21,12 +21,13 @@ val ktorVersion: String = ru.mipt.npm.gradle.KScienceVersions.ktorVersion dependencies{ api(project(":magix:magix-api")) api("io.ktor:ktor-server-cio:$ktorVersion") - api("io.ktor:ktor-websockets:$ktorVersion") - api("io.ktor:ktor-serialization:$ktorVersion") - api("io.ktor:ktor-html-builder:$ktorVersion") + api("io.ktor:ktor-server-websockets:$ktorVersion") + api("io.ktor:ktor-server-content-negotiation:$ktorVersion") + api("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion") + api("io.ktor:ktor-server-html-builder:$ktorVersion") - api("io.rsocket.kotlin:rsocket-core:$rsocketVersion") - api("io.rsocket.kotlin:rsocket-transport-ktor-server:$rsocketVersion") + api("io.rsocket.kotlin:rsocket-ktor-server:$rsocketVersion") + api("io.rsocket.kotlin:rsocket-transport-ktor-tcp:$rsocketVersion") api("org.zeromq:jeromq:0.5.2") } \ No newline at end of file diff --git a/magix/magix-server/src/main/kotlin/ru/mipt/npm/magix/server/magixModule.kt b/magix/magix-server/src/main/kotlin/ru/mipt/npm/magix/server/magixModule.kt index 3211243..a19c8a4 100644 --- a/magix/magix-server/src/main/kotlin/ru/mipt/npm/magix/server/magixModule.kt +++ b/magix/magix-server/src/main/kotlin/ru/mipt/npm/magix/server/magixModule.kt @@ -1,24 +1,23 @@ package ru.mipt.npm.magix.server -import io.ktor.application.* -import io.ktor.features.CORS -import io.ktor.features.ContentNegotiation -import io.ktor.html.respondHtml -import io.ktor.request.receive -import io.ktor.routing.get -import io.ktor.routing.post -import io.ktor.routing.route -import io.ktor.routing.routing -import io.ktor.serialization.json -import io.ktor.util.getValue -import io.ktor.websocket.WebSockets +import io.ktor.serialization.kotlinx.json.json +import io.ktor.server.application.* +import io.ktor.server.html.respondHtml +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation +import io.ktor.server.request.receive +import io.ktor.server.routing.get +import io.ktor.server.routing.post +import io.ktor.server.routing.route +import io.ktor.server.routing.routing +import io.ktor.server.util.getValue +import io.ktor.server.websocket.WebSockets import io.rsocket.kotlin.ConnectionAcceptor import io.rsocket.kotlin.RSocketRequestHandler +import io.rsocket.kotlin.ktor.server.RSocketSupport +import io.rsocket.kotlin.ktor.server.rSocket import io.rsocket.kotlin.payload.Payload import io.rsocket.kotlin.payload.buildPayload import io.rsocket.kotlin.payload.data -import io.rsocket.kotlin.transport.ktor.server.RSocketSupport -import io.rsocket.kotlin.transport.ktor.server.rSocket import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.* import kotlinx.html.* @@ -88,23 +87,23 @@ private fun ApplicationCall.buildFilter(): MagixMessageFilter { * Attache magix http/sse and websocket-based rsocket event loop + statistics page to existing [MutableSharedFlow] */ public fun Application.magixModule(magixFlow: MutableSharedFlow, route: String = "/") { - if (featureOrNull(WebSockets) == null) { + if (pluginOrNull(WebSockets) == null) { install(WebSockets) } - if (featureOrNull(CORS) == null) { - install(CORS) { - //TODO consider more safe policy - anyHost() - } - } - if (featureOrNull(ContentNegotiation) == null) { +// if (pluginOrNull(CORS) == null) { +// install(CORS) { +// //TODO consider more safe policy +// anyHost() +// } +// } + if (pluginOrNull(ContentNegotiation) == null) { install(ContentNegotiation) { json() } } - if (featureOrNull(RSocketSupport) == null) { + if (pluginOrNull(RSocketSupport) == null) { install(RSocketSupport) } @@ -150,7 +149,7 @@ public fun Application.magixModule(magixFlow: MutableSharedFlow, - rawSocketPort: Int = DEFAULT_MAGIX_RAW_PORT -): Job { - val tcpTransport = TcpServerTransport(ActorSelectorManager(Dispatchers.IO), port = rawSocketPort) - val rSocketJob = RSocketServer().bind(tcpTransport, magixAcceptor(magixFlow)) + rawSocketPort: Int = DEFAULT_MAGIX_RAW_PORT, +): TcpServer { + val tcpTransport = TcpServerTransport(port = rawSocketPort) + val rSocketJob: TcpServer = RSocketServer().bindIn(this, tcpTransport, magixAcceptor(magixFlow)) + coroutineContext[Job]?.invokeOnCompletion { - rSocketJob.cancel() + rSocketJob.handlerJob.cancel() } + return rSocketJob; } @@ -42,7 +41,7 @@ public fun CoroutineScope.startMagixServer( buffer: Int = 100, enableRawRSocket: Boolean = true, enableZmq: Boolean = true, - applicationConfiguration: Application.(MutableSharedFlow) -> Unit = {} + applicationConfiguration: Application.(MutableSharedFlow) -> Unit = {}, ): ApplicationEngine { val logger = LoggerFactory.getLogger("magix-server") val magixFlow = MutableSharedFlow( diff --git a/magix/magix-server/src/main/kotlin/ru/mipt/npm/magix/server/sse.kt b/magix/magix-server/src/main/kotlin/ru/mipt/npm/magix/server/sse.kt index a1c057f..c771ab0 100644 --- a/magix/magix-server/src/main/kotlin/ru/mipt/npm/magix/server/sse.kt +++ b/magix/magix-server/src/main/kotlin/ru/mipt/npm/magix/server/sse.kt @@ -1,14 +1,13 @@ package ru.mipt.npm.magix.server -import io.ktor.application.ApplicationCall import io.ktor.http.CacheControl import io.ktor.http.ContentType -import io.ktor.response.cacheControl -import io.ktor.response.respondBytesWriter +import io.ktor.server.application.ApplicationCall +import io.ktor.server.response.cacheControl +import io.ktor.server.response.respondBytesWriter import io.ktor.utils.io.ByteWriteChannel import io.ktor.utils.io.writeStringUtf8 import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.collect /** * The data class representing a SSE Event that will be sent to the client. diff --git a/motors/src/main/kotlin/ru/mipt/npm/devices/pimotionmaster/piDebugServer.kt b/motors/src/main/kotlin/ru/mipt/npm/devices/pimotionmaster/piDebugServer.kt index f5a4c81..021dff5 100644 --- a/motors/src/main/kotlin/ru/mipt/npm/devices/pimotionmaster/piDebugServer.kt +++ b/motors/src/main/kotlin/ru/mipt/npm/devices/pimotionmaster/piDebugServer.kt @@ -8,10 +8,8 @@ import io.ktor.util.InternalAPI import io.ktor.util.moveToByteArray import io.ktor.utils.io.writeAvailable import kotlinx.coroutines.* -import kotlinx.coroutines.flow.collect import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.Global -import java.net.InetSocketAddress val exceptionHandler = CoroutineExceptionHandler { _, throwable -> throwable.printStackTrace() @@ -20,7 +18,7 @@ val exceptionHandler = CoroutineExceptionHandler { _, throwable -> @OptIn(InternalAPI::class) fun Context.launchPiDebugServer(port: Int, axes: List): Job = launch(exceptionHandler) { val virtualDevice = PiMotionMasterVirtualDevice(this@launchPiDebugServer, axes) - val server = aSocket(ActorSelectorManager(Dispatchers.IO)).tcp().bind(InetSocketAddress("localhost", port)) + val server = aSocket(ActorSelectorManager(Dispatchers.IO)).tcp().bind("localhost", port) println("Started virtual port server at ${server.localAddress}") while (isActive) { diff --git a/settings.gradle.kts b/settings.gradle.kts index 6a4062d..7a8cfae 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -8,9 +8,9 @@ pluginManagement { repositories { mavenLocal() - maven("https://repo.kotlin.link") - mavenCentral() gradlePluginPortal() + mavenCentral() + maven("https://repo.kotlin.link") } plugins { @@ -27,8 +27,8 @@ dependencyResolutionManagement { repositories { mavenLocal() - maven("https://repo.kotlin.link") mavenCentral() + maven("https://repo.kotlin.link") } versionCatalogs { @@ -45,7 +45,7 @@ include( ":controls-server", ":controls-opcua", ":demo", - ":demo:car", +// ":demo:car", ":magix", ":magix:magix-api", ":magix:magix-server",