package refactor

This commit is contained in:
Alexander Nozik 2021-06-21 14:09:59 +03:00
parent d79d345a44
commit 5e9cbab94d
15 changed files with 56 additions and 35 deletions

View File

@ -13,7 +13,7 @@ kotlin {
sourceSets { sourceSets {
commonMain { commonMain {
dependencies { dependencies {
implementation(project(":magix:magix-service")) implementation(project(":magix:magix-rsocket"))
implementation(project(":controls-core")) implementation(project(":controls-core"))
} }
} }

View File

@ -1,4 +1,3 @@
@file:OptIn(ExperimentalCoroutinesApi::class, KtorExperimentalAPI::class, FlowPreview::class)
package ru.mipt.npm.controls.server package ru.mipt.npm.controls.server
@ -18,12 +17,9 @@ import io.ktor.routing.routing
import io.ktor.server.cio.CIO import io.ktor.server.cio.CIO
import io.ktor.server.engine.ApplicationEngine import io.ktor.server.engine.ApplicationEngine
import io.ktor.server.engine.embeddedServer import io.ktor.server.engine.embeddedServer
import io.ktor.util.KtorExperimentalAPI
import io.ktor.util.getValue import io.ktor.util.getValue
import io.ktor.websocket.WebSockets import io.ktor.websocket.WebSockets
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.html.* import kotlinx.html.*
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
@ -74,7 +70,6 @@ public fun ApplicationEngine.whenStarted(callback: Application.() -> Unit) {
public const val WEB_SERVER_TARGET: String = "@webServer" public const val WEB_SERVER_TARGET: String = "@webServer"
@OptIn(KtorExperimentalAPI::class)
public fun Application.deviceModule( public fun Application.deviceModule(
manager: DeviceManager, manager: DeviceManager,
deviceNames: Collection<String> = manager.devices.keys.map { it.toString() }, deviceNames: Collection<String> = manager.devices.keys.map { it.toString() },
@ -114,7 +109,8 @@ public fun Application.deviceModule(
} }
deviceNames.forEach { deviceName -> deviceNames.forEach { deviceName ->
val device = val device =
manager.getOrNull(deviceName) ?: error("The device with name $deviceName not found in $manager") manager.getOrNull(deviceName)
?: error("The device with name $deviceName not found in $manager")
div { div {
id = deviceName id = deviceName
h2 { +deviceName } h2 { +deviceName }

View File

@ -4,7 +4,6 @@ import io.ktor.network.selector.ActorSelectorManager
import io.ktor.network.sockets.aSocket import io.ktor.network.sockets.aSocket
import io.ktor.network.sockets.openReadChannel import io.ktor.network.sockets.openReadChannel
import io.ktor.network.sockets.openWriteChannel import io.ktor.network.sockets.openWriteChannel
import io.ktor.util.KtorExperimentalAPI
import io.ktor.utils.io.consumeEachBufferRange import io.ktor.utils.io.consumeEachBufferRange
import io.ktor.utils.io.core.Closeable import io.ktor.utils.io.core.Closeable
import io.ktor.utils.io.writeAvailable import io.ktor.utils.io.writeAvailable
@ -29,7 +28,6 @@ public class KtorTcpPort internal constructor(
override fun toString(): String = "port[tcp:$host:$port]" override fun toString(): String = "port[tcp:$host:$port]"
@OptIn(KtorExperimentalAPI::class)
private val futureSocket = scope.async { private val futureSocket = scope.async {
aSocket(ActorSelectorManager(Dispatchers.IO)).tcp().connect(InetSocketAddress(host, port)) aSocket(ActorSelectorManager(Dispatchers.IO)).tcp().connect(InetSocketAddress(host, port))
} }

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

View File

@ -5,6 +5,6 @@ plugins {
} }
dependencies { dependencies {
implementation(project(":magix:magix-service")) implementation(project(":magix:magix-rsocket"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk9:${ru.mipt.npm.gradle.KScienceVersions.coroutinesVersion}") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk9:${ru.mipt.npm.gradle.KScienceVersions.coroutinesVersion}")
} }

View File

@ -6,8 +6,8 @@ import kotlinx.serialization.KSerializer
import ru.mipt.npm.magix.api.MagixEndpoint import ru.mipt.npm.magix.api.MagixEndpoint
import ru.mipt.npm.magix.api.MagixMessage import ru.mipt.npm.magix.api.MagixMessage
import ru.mipt.npm.magix.api.MagixMessageFilter import ru.mipt.npm.magix.api.MagixMessageFilter
import ru.mipt.npm.magix.service.RSocketMagixEndpoint import ru.mipt.npm.magix.rsocket.RSocketMagixEndpoint
import ru.mipt.npm.magix.service.withTcp import ru.mipt.npm.magix.rsocket.withTcp
import java.util.concurrent.Flow import java.util.concurrent.Flow
public class ControlsMagixClient<T>( public class ControlsMagixClient<T>(

View File

@ -3,25 +3,24 @@ plugins {
`maven-publish` `maven-publish`
} }
description = """
Magix endpoint (client) based on RSocket
""".trimIndent()
kscience { kscience {
useSerialization { useSerialization {
json() json()
} }
} }
val dataforgeVersion: String by rootProject.extra
val ktorVersion: String by rootProject.extra val ktorVersion: String by rootProject.extra
val rsocketVersion: String by rootProject.extra val rsocketVersion: String by rootProject.extra
repositories{
maven("https://maven.pkg.github.com/altavir/ktor-client-sse")
}
kotlin { kotlin {
sourceSets { sourceSets {
commonMain { commonMain {
dependencies { dependencies {
api(project(":magix:magix-api")) api(projects.magix.magixApi)
implementation("io.ktor:ktor-client-core:$ktorVersion") implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.rsocket.kotlin:rsocket-transport-ktor-client:$rsocketVersion") implementation("io.rsocket.kotlin:rsocket-transport-ktor-client:$rsocketVersion")
} }

View File

@ -1,4 +1,4 @@
package ru.mipt.npm.magix.service package ru.mipt.npm.magix.rsocket
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.features.websocket.WebSockets import io.ktor.client.features.websocket.WebSockets
@ -51,6 +51,9 @@ public class RSocketMagixEndpoint<T>(
connectionConfig(rSocketConfig) connectionConfig(rSocketConfig)
} }
/**
* Build a websocket based endpoint connected to [host], [port] and given routing [path]
*/
public suspend fun <T> withWebSockets( public suspend fun <T> withWebSockets(
host: String, host: String,
port: Int, port: Int,

View File

@ -1,4 +1,4 @@
package ru.mipt.npm.magix.service package ru.mipt.npm.magix.rsocket
import io.ktor.network.selector.ActorSelectorManager import io.ktor.network.selector.ActorSelectorManager
import io.ktor.network.sockets.SocketOptions import io.ktor.network.sockets.SocketOptions
@ -11,7 +11,7 @@ import kotlin.coroutines.coroutineContext
/** /**
* Create a plain TCP based [RSocketMagixEndpoint] * Create a plain TCP based [RSocketMagixEndpoint] connected to [host] and [port]
*/ */
public suspend fun <T> RSocketMagixEndpoint.Companion.withTcp( public suspend fun <T> RSocketMagixEndpoint.Companion.withTcp(
host: String, host: String,

View File

@ -5,7 +5,6 @@ import io.ktor.network.sockets.aSocket
import io.ktor.server.cio.CIO import io.ktor.server.cio.CIO
import io.ktor.server.engine.ApplicationEngine import io.ktor.server.engine.ApplicationEngine
import io.ktor.server.engine.embeddedServer import io.ktor.server.engine.embeddedServer
import io.ktor.util.KtorExperimentalAPI
import io.rsocket.kotlin.core.RSocketServer import io.rsocket.kotlin.core.RSocketServer
import io.rsocket.kotlin.transport.ktor.serverTransport import io.rsocket.kotlin.transport.ktor.serverTransport
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@ -14,7 +13,6 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import ru.mipt.npm.magix.api.MagixEndpoint.Companion.DEFAULT_MAGIX_RAW_PORT import ru.mipt.npm.magix.api.MagixEndpoint.Companion.DEFAULT_MAGIX_RAW_PORT
import ru.mipt.npm.magix.api.MagixEndpoint.Companion.DEFAULT_MAGIX_WS_PORT import ru.mipt.npm.magix.api.MagixEndpoint.Companion.DEFAULT_MAGIX_WS_PORT
@OptIn(KtorExperimentalAPI::class)
public fun CoroutineScope.startMagixServer( public fun CoroutineScope.startMagixServer(
port: Int = DEFAULT_MAGIX_WS_PORT, port: Int = DEFAULT_MAGIX_WS_PORT,
rawSocketPort: Int = DEFAULT_MAGIX_RAW_PORT, rawSocketPort: Int = DEFAULT_MAGIX_RAW_PORT,

View File

@ -10,7 +10,6 @@ import io.ktor.routing.post
import io.ktor.routing.route import io.ktor.routing.route
import io.ktor.routing.routing import io.ktor.routing.routing
import io.ktor.serialization.json import io.ktor.serialization.json
import io.ktor.util.KtorExperimentalAPI
import io.ktor.util.getValue import io.ktor.util.getValue
import io.ktor.websocket.WebSockets import io.ktor.websocket.WebSockets
import io.rsocket.kotlin.ConnectionAcceptor import io.rsocket.kotlin.ConnectionAcceptor
@ -70,7 +69,6 @@ internal fun CoroutineScope.magixAcceptor(magixFlow: MutableSharedFlow<GenericMa
/** /**
* Create a message filter from call parameters * Create a message filter from call parameters
*/ */
@OptIn(KtorExperimentalAPI::class)
private fun ApplicationCall.buildFilter(): MagixMessageFilter { private fun ApplicationCall.buildFilter(): MagixMessageFilter {
val query = request.queryParameters val query = request.queryParameters

View File

@ -5,7 +5,6 @@ import io.ktor.http.CacheControl
import io.ktor.http.ContentType import io.ktor.http.ContentType
import io.ktor.response.cacheControl import io.ktor.response.cacheControl
import io.ktor.response.respondBytesWriter import io.ktor.response.respondBytesWriter
import io.ktor.util.KtorExperimentalAPI
import io.ktor.utils.io.ByteWriteChannel import io.ktor.utils.io.ByteWriteChannel
import io.ktor.utils.io.writeStringUtf8 import io.ktor.utils.io.writeStringUtf8
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@ -30,7 +29,6 @@ public suspend fun ByteWriteChannel.writeSseFlow(events: Flow<SseEvent>): Unit =
flush() flush()
} }
@OptIn(KtorExperimentalAPI::class)
public suspend fun ApplicationCall.respondSse(events: Flow<SseEvent>) { public suspend fun ApplicationCall.respondSse(events: Flow<SseEvent>) {
response.cacheControl(CacheControl.NoCache(null)) response.cacheControl(CacheControl.NoCache(null))
respondBytesWriter(contentType = ContentType.Text.EventStream) { respondBytesWriter(contentType = ContentType.Text.EventStream) {

View File

@ -0,0 +1,10 @@
plugins {
id("ru.mipt.npm.gradle.jvm")
`maven-publish`
}
dependencies {
api(projects.magix.magixApi)
implementation("org.zeromq:jeromq:0.5.2")
}

View File

@ -0,0 +1,22 @@
package ru.mipt.npm.magix.zmq
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.KSerializer
import ru.mipt.npm.magix.api.MagixEndpoint
import ru.mipt.npm.magix.api.MagixMessage
import ru.mipt.npm.magix.api.MagixMessageFilter
import kotlin.coroutines.CoroutineContext
class ZmqMagixEndpoint<T>(
private val coroutineContext: CoroutineContext,
private val payloadSerializer: KSerializer<T>,
) : MagixEndpoint<T> {
override fun subscribe(filter: MagixMessageFilter): Flow<MagixMessage<T>> {
TODO("Not yet implemented")
}
override suspend fun broadcast(message: MagixMessage<T>) {
TODO("Not yet implemented")
}
}

View File

@ -1,4 +1,6 @@
rootProject.name = "controls.kt" rootProject.name = "controls"
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
pluginManagement { pluginManagement {
val toolsVersion = "0.10.0" val toolsVersion = "0.10.0"
@ -26,12 +28,9 @@ include(
":magix", ":magix",
":magix:magix-api", ":magix:magix-api",
":magix:magix-server", ":magix:magix-server",
":magix:magix-service", ":magix:magix-rsocket",
":magix:magix-java-client", ":magix:magix-java-client",
":magix:magix-zmq",
":controls-magix-client", ":controls-magix-client",
":motors" ":motors"
) )
//includeBuild("../dataforge-core")
//includeBuild("../plotly.kt")
include("magix-java-client")