From c22902cc915c855374433cdbfd55e03c4106938b Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 2 Aug 2022 09:37:36 +0300 Subject: [PATCH] Updata to DataForge 0.6 --- build.gradle.kts | 2 +- .../space/kscience/controls/manager/DeviceManager.kt | 2 +- .../kotlin/space/kscience/controls/ports/phrases.kt | 12 ++++++------ .../kotlin/space/kscience/controls/ports/TcpPort.kt | 2 +- .../space/kscience/controls/serial/SerialPort.kt | 2 +- .../controls/xodus/XodusDeviceMessageStorage.kt | 3 +-- .../space/kscience/controls/storage/storageCommon.kt | 4 ++-- .../space/kscience/controls/storage/workDirectory.kt | 2 +- .../space/kscience/controls/ports/KtorTcpPort.kt | 2 +- .../space/kscience/controls/demo/DemoDevice.kt | 2 +- .../kscience/controls/demo/car/MagixVirtualCar.kt | 2 +- .../space/kscience/controls/demo/car/VirtualCar.kt | 2 +- .../devices/pimotionmaster/PiMotionMasterDevice.kt | 2 +- 13 files changed, 19 insertions(+), 20 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 48be680..75b36df 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,7 +2,7 @@ plugins { id("ru.mipt.npm.gradle.project") } -val dataforgeVersion: String by extra("0.5.2") +val dataforgeVersion: String by extra("0.6.0-dev-12") val ktorVersion: String by extra(ru.mipt.npm.gradle.KScienceVersions.ktorVersion) val rsocketVersion by extra("0.15.4") diff --git a/controls-core/src/commonMain/kotlin/space/kscience/controls/manager/DeviceManager.kt b/controls-core/src/commonMain/kotlin/space/kscience/controls/manager/DeviceManager.kt index 2e7d186..a3a5bfd 100644 --- a/controls-core/src/commonMain/kotlin/space/kscience/controls/manager/DeviceManager.kt +++ b/controls-core/src/commonMain/kotlin/space/kscience/controls/manager/DeviceManager.kt @@ -31,7 +31,7 @@ public class DeviceManager : AbstractPlugin(), DeviceHub { override val tag: PluginTag = PluginTag("devices", group = PluginTag.DATAFORGE_GROUP) override val type: KClass = DeviceManager::class - override fun invoke(meta: Meta, context: Context): DeviceManager = DeviceManager() + override fun build(context: Context, meta: Meta): DeviceManager = DeviceManager() } } diff --git a/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/phrases.kt b/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/phrases.kt index 896d87a..8543825 100644 --- a/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/phrases.kt +++ b/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/phrases.kt @@ -10,10 +10,10 @@ import kotlinx.coroutines.flow.transform /** * Transform byte fragments into complete phrases using given delimiter. Not thread safe. */ -public fun Flow.withDelimiter(delimiter: ByteArray, expectedMessageSize: Int = 32): Flow { +public fun Flow.withDelimiter(delimiter: ByteArray): Flow { require(delimiter.isNotEmpty()) { "Delimiter must not be empty" } - val output = BytePacketBuilder(expectedMessageSize) + val output = BytePacketBuilder() var matcherPosition = 0 return transform { chunk -> @@ -40,12 +40,12 @@ public fun Flow.withDelimiter(delimiter: ByteArray, expectedMessageSi /** * Transform byte fragments into utf-8 phrases using utf-8 delimiter */ -public fun Flow.withDelimiter(delimiter: String, expectedMessageSize: Int = 32): Flow { - return withDelimiter(delimiter.encodeToByteArray(), expectedMessageSize).map { it.decodeToString() } +public fun Flow.withDelimiter(delimiter: String): Flow { + return withDelimiter(delimiter.encodeToByteArray()).map { it.decodeToString() } } /** * A flow of delimited phrases */ -public suspend fun Port.delimitedIncoming(delimiter: ByteArray, expectedMessageSize: Int = 32): Flow = - receiving().withDelimiter(delimiter, expectedMessageSize) +public suspend fun Port.delimitedIncoming(delimiter: ByteArray): Flow = + receiving().withDelimiter(delimiter) diff --git a/controls-core/src/jvmMain/kotlin/space/kscience/controls/ports/TcpPort.kt b/controls-core/src/jvmMain/kotlin/space/kscience/controls/ports/TcpPort.kt index 2ef0d89..d458c4d 100644 --- a/controls-core/src/jvmMain/kotlin/space/kscience/controls/ports/TcpPort.kt +++ b/controls-core/src/jvmMain/kotlin/space/kscience/controls/ports/TcpPort.kt @@ -83,7 +83,7 @@ public class TcpPort private constructor( return TcpPort(context, host, port, coroutineContext) } - override fun invoke(meta: Meta, context: Context): Port { + override fun build(context: Context, meta: Meta): Port { val host = meta["host"].string ?: "localhost" val port = meta["port"].int ?: error("Port value for TCP port is not defined in $meta") return open(context, host, port) diff --git a/controls-serial/src/main/kotlin/space/kscience/controls/serial/SerialPort.kt b/controls-serial/src/main/kotlin/space/kscience/controls/serial/SerialPort.kt index eab88b6..737e90d 100644 --- a/controls-serial/src/main/kotlin/space/kscience/controls/serial/SerialPort.kt +++ b/controls-serial/src/main/kotlin/space/kscience/controls/serial/SerialPort.kt @@ -77,7 +77,7 @@ public class SerialPort private constructor( return SerialPort(context, jssc, coroutineContext) } - override fun invoke(meta: Meta, context: Context): Port { + override fun build(context: Context, meta: Meta): Port { val name by meta.string { error("Serial port name not defined") } val baudRate by meta.int(BAUDRATE_9600) val dataBits by meta.int(DATABITS_8) diff --git a/controls-storage/controls-xodus/src/main/kotlin/space/kscience/controls/xodus/XodusDeviceMessageStorage.kt b/controls-storage/controls-xodus/src/main/kotlin/space/kscience/controls/xodus/XodusDeviceMessageStorage.kt index 2255d89..72cdd1f 100644 --- a/controls-storage/controls-xodus/src/main/kotlin/space/kscience/controls/xodus/XodusDeviceMessageStorage.kt +++ b/controls-storage/controls-xodus/src/main/kotlin/space/kscience/controls/xodus/XodusDeviceMessageStorage.kt @@ -111,8 +111,7 @@ public class XodusDeviceMessageStorage( internal const val DEVICE_MESSAGE_ENTITY_TYPE = "controls-kt.message" public val XODUS_STORE_PROPERTY: Name = Name.of("xodus", "storagePath") - - override fun invoke(meta: Meta, context: Context): XodusDeviceMessageStorage { + override fun build(context: Context, meta: Meta): XodusDeviceMessageStorage { val io = context.fetch(IOPlugin) val storePath = io.workDirectory.resolve( meta[XODUS_STORE_PROPERTY]?.string diff --git a/controls-storage/src/commonMain/kotlin/space/kscience/controls/storage/storageCommon.kt b/controls-storage/src/commonMain/kotlin/space/kscience/controls/storage/storageCommon.kt index 672e2cc..2a453cf 100644 --- a/controls-storage/src/commonMain/kotlin/space/kscience/controls/storage/storageCommon.kt +++ b/controls-storage/src/commonMain/kotlin/space/kscience/controls/storage/storageCommon.kt @@ -15,7 +15,7 @@ import space.kscience.dataforge.context.logger //TODO replace by plugin? public fun DeviceManager.storage( factory: Factory, -): DeviceMessageStorage = factory(meta, context) +): DeviceMessageStorage = factory.build(context, meta) /** * Begin to store DeviceMessages from this DeviceManager @@ -28,7 +28,7 @@ public fun DeviceManager.storeMessages( factory: Factory, filterCondition: suspend (DeviceMessage) -> Boolean = { true }, ): Job { - val storage = factory(meta, context) + val storage = factory.build(context, meta) logger.debug { "Message storage with meta = $meta created" } return hubMessageFlow(context).filter(filterCondition).onEach { message -> diff --git a/controls-storage/src/jvmMain/kotlin/space/kscience/controls/storage/workDirectory.kt b/controls-storage/src/jvmMain/kotlin/space/kscience/controls/storage/workDirectory.kt index 7e4086f..62ae391 100644 --- a/controls-storage/src/jvmMain/kotlin/space/kscience/controls/storage/workDirectory.kt +++ b/controls-storage/src/jvmMain/kotlin/space/kscience/controls/storage/workDirectory.kt @@ -3,8 +3,8 @@ package space.kscience.controls.storage import space.kscience.dataforge.context.ContextBuilder import space.kscience.dataforge.io.IOPlugin import space.kscience.dataforge.meta.get -import space.kscience.dataforge.meta.set import space.kscience.dataforge.meta.string +import space.kscience.dataforge.values.set import java.nio.file.Path import kotlin.io.path.Path diff --git a/controls-tcp/src/main/kotlin/space/kscience/controls/ports/KtorTcpPort.kt b/controls-tcp/src/main/kotlin/space/kscience/controls/ports/KtorTcpPort.kt index 2c77aa5..9f652b2 100644 --- a/controls-tcp/src/main/kotlin/space/kscience/controls/ports/KtorTcpPort.kt +++ b/controls-tcp/src/main/kotlin/space/kscience/controls/ports/KtorTcpPort.kt @@ -65,7 +65,7 @@ public class KtorTcpPort internal constructor( return KtorTcpPort(context, host, port, coroutineContext) } - override fun invoke(meta: Meta, context: Context): Port { + override fun build(context: Context, meta: Meta): Port { val host = meta["host"].string ?: "localhost" val port = meta["port"].int ?: error("Port value for TCP port is not defined in $meta") return open(context, host, port) diff --git a/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoDevice.kt b/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoDevice.kt index 26ac20b..4abad84 100644 --- a/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoDevice.kt +++ b/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoDevice.kt @@ -35,7 +35,7 @@ class DemoDevice(context: Context, meta: Meta) : DeviceBySpec(DemoDe companion object : DeviceSpec(), Factory { - override fun invoke(meta: Meta, context: Context): DemoDevice = DemoDevice(context, meta) + override fun build(context: Context, meta: Meta): DemoDevice = DemoDevice(context, meta) // register virtual properties based on actual object state val timeScale by mutableProperty(MetaConverter.double, DemoDevice::timeScaleState) { diff --git a/demo/car/src/main/kotlin/space/kscience/controls/demo/car/MagixVirtualCar.kt b/demo/car/src/main/kotlin/space/kscience/controls/demo/car/MagixVirtualCar.kt index 48eaa15..b1b57e5 100644 --- a/demo/car/src/main/kotlin/space/kscience/controls/demo/car/MagixVirtualCar.kt +++ b/demo/car/src/main/kotlin/space/kscience/controls/demo/car/MagixVirtualCar.kt @@ -43,6 +43,6 @@ class MagixVirtualCar(context: Context, meta: Meta) : VirtualCar(context, meta) } companion object : Factory { - override fun invoke(meta: Meta, context: Context): MagixVirtualCar = MagixVirtualCar(context, meta) + override fun build(context: Context, meta: Meta): MagixVirtualCar = MagixVirtualCar(context, meta) } } diff --git a/demo/car/src/main/kotlin/space/kscience/controls/demo/car/VirtualCar.kt b/demo/car/src/main/kotlin/space/kscience/controls/demo/car/VirtualCar.kt index a1240a4..db0638c 100644 --- a/demo/car/src/main/kotlin/space/kscience/controls/demo/car/VirtualCar.kt +++ b/demo/car/src/main/kotlin/space/kscience/controls/demo/car/VirtualCar.kt @@ -110,6 +110,6 @@ open class VirtualCar(context: Context, meta: Meta) : DeviceBySpec(I } companion object : Factory { - override fun invoke(meta: Meta, context: Context): VirtualCar = VirtualCar(context, meta) + override fun build(context: Context, meta: Meta): VirtualCar = VirtualCar(context, meta) } } diff --git a/demo/motors/src/main/kotlin/ru/mipt/npm/devices/pimotionmaster/PiMotionMasterDevice.kt b/demo/motors/src/main/kotlin/ru/mipt/npm/devices/pimotionmaster/PiMotionMasterDevice.kt index ff35345..9928e45 100644 --- a/demo/motors/src/main/kotlin/ru/mipt/npm/devices/pimotionmaster/PiMotionMasterDevice.kt +++ b/demo/motors/src/main/kotlin/ru/mipt/npm/devices/pimotionmaster/PiMotionMasterDevice.kt @@ -135,7 +135,7 @@ class PiMotionMasterDevice( companion object : DeviceSpec(), Factory { - override fun invoke(meta: Meta, context: Context): PiMotionMasterDevice = PiMotionMasterDevice(context) + override fun build(context: Context, meta: Meta): PiMotionMasterDevice = PiMotionMasterDevice(context) val connected by booleanProperty(descriptorBuilder = { info = "True if the connection address is defined and the device is initialized"