Add magix message registry service

This commit is contained in:
Alexander Nozik 2023-08-16 18:26:21 +03:00
parent 583fe5b54c
commit ce03540b37
23 changed files with 290 additions and 93 deletions

View File

@ -53,11 +53,6 @@ Example view of a demo:
> - [deviceHub](controls-core/src/commonMain/kotlin/space/kscience/controls/api/DeviceHub.kt) : Grouping of devices into local tree-like hubs.
### [controls-ktor-tcp](controls-ktor-tcp)
>
>
> **Maturity**: EXPERIMENTAL
### [controls-magix-client](controls-magix-client)
>
>
@ -73,6 +68,16 @@ Example view of a demo:
>
> **Maturity**: EXPERIMENTAL
### [controls-pi](controls-pi)
>
>
> **Maturity**: EXPERIMENTAL
### [controls-ports-ktor](controls-ports-ktor)
>
>
> **Maturity**: EXPERIMENTAL
### [controls-serial](controls-serial)
>
>

View File

@ -6,7 +6,7 @@ Magix service for binding controls devices (both as RPC client and server
## Artifact:
The Maven coordinates of this project are `space.kscience:controls-magix-client:0.1.1-SNAPSHOT`.
The Maven coordinates of this project are `space.kscience:controls-magix-client:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
@ -16,7 +16,7 @@ repositories {
}
dependencies {
implementation 'space.kscience:controls-magix-client:0.1.1-SNAPSHOT'
implementation 'space.kscience:controls-magix-client:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
@ -27,6 +27,6 @@ repositories {
}
dependencies {
implementation("space.kscience:controls-magix-client:0.1.1-SNAPSHOT")
implementation("space.kscience:controls-magix-client:0.2.0-dev-2")
}
```

View File

@ -2,3 +2,31 @@
A plugin for Controls-kt device server on top of modbus-rtu/modbus-tcp protocols
## Usage
## Artifact:
The Maven coordinates of this project are `space.kscience:controls-modbus:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
repositories {
maven { url 'https://repo.kotlin.link' }
mavenCentral()
}
dependencies {
implementation 'space.kscience:controls-modbus:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
```kotlin
repositories {
maven("https://repo.kotlin.link")
mavenCentral()
}
dependencies {
implementation("space.kscience:controls-modbus:0.2.0-dev-2")
}
```

View File

@ -2,3 +2,31 @@
## Usage
## Artifact:
The Maven coordinates of this project are `space.kscience:controls-opcua:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
repositories {
maven { url 'https://repo.kotlin.link' }
mavenCentral()
}
dependencies {
implementation 'space.kscience:controls-opcua:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
```kotlin
repositories {
maven("https://repo.kotlin.link")
mavenCentral()
}
dependencies {
implementation("space.kscience:controls-opcua:0.2.0-dev-2")
}
```

View File

@ -1,4 +1,32 @@
# Module controls-ktor-tcp
# Module controls-ports-ktor
## Usage
## Artifact:
The Maven coordinates of this project are `space.kscience:controls-ports-ktor:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
repositories {
maven { url 'https://repo.kotlin.link' }
mavenCentral()
}
dependencies {
implementation 'space.kscience:controls-ports-ktor:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
```kotlin
repositories {
maven("https://repo.kotlin.link")
mavenCentral()
}
dependencies {
implementation("space.kscience:controls-ports-ktor:0.2.0-dev-2")
}
```

View File

@ -6,7 +6,7 @@
## Artifact:
The Maven coordinates of this project are `space.kscience:controls-serial:0.1.1-SNAPSHOT`.
The Maven coordinates of this project are `space.kscience:controls-serial:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
@ -16,7 +16,7 @@ repositories {
}
dependencies {
implementation 'space.kscience:controls-serial:0.1.1-SNAPSHOT'
implementation 'space.kscience:controls-serial:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
@ -27,6 +27,6 @@ repositories {
}
dependencies {
implementation("space.kscience:controls-serial:0.1.1-SNAPSHOT")
implementation("space.kscience:controls-serial:0.2.0-dev-2")
}
```

View File

@ -6,7 +6,7 @@ A magix event loop server with web server for visualization.
## Artifact:
The Maven coordinates of this project are `space.kscience:controls-server:0.1.1-SNAPSHOT`.
The Maven coordinates of this project are `space.kscience:controls-server:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
@ -16,7 +16,7 @@ repositories {
}
dependencies {
implementation 'space.kscience:controls-server:0.1.1-SNAPSHOT'
implementation 'space.kscience:controls-server:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
@ -27,6 +27,6 @@ repositories {
}
dependencies {
implementation("space.kscience:controls-server:0.1.1-SNAPSHOT")
implementation("space.kscience:controls-server:0.2.0-dev-2")
}
```

View File

@ -6,7 +6,7 @@
## Artifact:
The Maven coordinates of this project are `space.kscience:controls-storage:0.1.1-SNAPSHOT`.
The Maven coordinates of this project are `space.kscience:controls-storage:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
@ -16,7 +16,7 @@ repositories {
}
dependencies {
implementation 'space.kscience:controls-storage:0.1.1-SNAPSHOT'
implementation 'space.kscience:controls-storage:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
@ -27,6 +27,6 @@ repositories {
}
dependencies {
implementation("space.kscience:controls-storage:0.1.1-SNAPSHOT")
implementation("space.kscience:controls-storage:0.2.0-dev-2")
}
```

View File

@ -6,7 +6,7 @@
## Artifact:
The Maven coordinates of this project are `space.kscience:controls-xodus:0.1.1-SNAPSHOT`.
The Maven coordinates of this project are `space.kscience:controls-xodus:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
@ -16,7 +16,7 @@ repositories {
}
dependencies {
implementation 'space.kscience:controls-xodus:0.1.1-SNAPSHOT'
implementation 'space.kscience:controls-xodus:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
@ -27,6 +27,6 @@ repositories {
}
dependencies {
implementation("space.kscience:controls-xodus:0.1.1-SNAPSHOT")
implementation("space.kscience:controls-xodus:0.2.0-dev-2")
}
```

View File

@ -6,7 +6,7 @@
## Artifact:
The Maven coordinates of this project are `space.kscience:magix-api:0.1.1-SNAPSHOT`.
The Maven coordinates of this project are `space.kscience:magix-api:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
@ -16,7 +16,7 @@ repositories {
}
dependencies {
implementation 'space.kscience:magix-api:0.1.1-SNAPSHOT'
implementation 'space.kscience:magix-api:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
@ -27,6 +27,6 @@ repositories {
}
dependencies {
implementation("space.kscience:magix-api:0.1.1-SNAPSHOT")
implementation("space.kscience:magix-api:0.2.0-dev-2")
}
```

View File

@ -54,3 +54,8 @@ public interface MagixEndpoint {
}
}
}
/**
* An alias for [MagixEndpoint.broadcast]
*/
public suspend fun MagixEndpoint.send(message: MagixMessage): Unit = broadcast(message)

View File

@ -1,17 +0,0 @@
package space.kscience.magix.api
import kotlinx.serialization.json.JsonElement
/**
* An interface to access distributed Magix property registry
*/
public interface MagixRegistry {
/**
* Request a property with name [propertyName] and user authentication data [user].
*
* Return a property value in its generic form or null if it is not present.
*
* Throw an exception if property is present but access is denied.
*/
public suspend fun request(propertyName: String, user: JsonElement? = null): JsonElement?
}

View File

@ -1,30 +0,0 @@
package space.kscience.magix.api
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.serialization.json.JsonElement
/**
* Launch magix message converter service
*/
public fun <T, R> CoroutineScope.launchMagixConverter(
endpoint: MagixEndpoint,
filter: MagixMessageFilter,
outputFormat: String,
newOrigin: String? = null,
transformer: suspend (JsonElement) -> JsonElement,
): Job = endpoint.subscribe(filter).onEach { message->
val newPayload = transformer(message.payload)
val transformed: MagixMessage = MagixMessage(
outputFormat,
newPayload,
newOrigin ?: message.sourceEndpoint,
message.targetEndpoint,
message.id,
message.parentId,
message.user
)
endpoint.broadcast(transformed)
}.launchIn(this)

View File

@ -0,0 +1,81 @@
package space.kscience.magix.services
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import space.kscience.magix.api.MagixEndpoint
import space.kscience.magix.api.MagixFormat
import space.kscience.magix.api.broadcast
import space.kscience.magix.api.subscribe
/**
* An interface to access distributed Magix property registry
*/
public interface MagixRegistry {
/**
* Request a property with name [propertyName] and user authentication data [user].
*
* Return a property value in its generic form or null if it is not present.
*
* Throw exception access is denied, or request failed.
*/
public suspend fun request(propertyName: String, user: JsonElement? = null): JsonElement?
}
@Serializable
public sealed class MagixRegistryMessage {
public abstract val propertyName: String
public companion object {
public val format: MagixFormat<MagixRegistryMessage> = MagixFormat(serializer(), setOf("magix.registry"))
}
}
@Serializable
@SerialName("registry.request")
public class MagixRegistryRequestMessage(
override val propertyName: String,
) : MagixRegistryMessage()
@Serializable
@SerialName("registry.value")
public class MagixRegistryValueMessage(
override val propertyName: String,
public val value: JsonElement,
) : MagixRegistryMessage()
@Serializable
@SerialName("registry.error")
public class MagixRegistryErrorMessage(
override val propertyName: String,
public val errorType: String?,
public val errorMessage: String? = null,
) : MagixRegistryMessage()
public fun CoroutineScope.launchMagixRegistry(
endpoint: MagixEndpoint,
registry: MagixRegistry,
originFilter: Collection<String>? = null,
targetFilter: Collection<String>? = null,
): Job = endpoint.subscribe(MagixRegistryMessage.format, originFilter, targetFilter)
.onEach { (magixMessage, payload) ->
if (payload is MagixRegistryRequestMessage) {
try {
val value = registry.request(payload.propertyName, magixMessage.user)
endpoint.broadcast(
MagixRegistryMessage.format,
MagixRegistryValueMessage(payload.propertyName, value ?: JsonNull)
)
} catch (ex: Exception){
endpoint.broadcast(
MagixRegistryMessage.format,
MagixRegistryErrorMessage(payload.propertyName, ex::class.simpleName, ex.message)
)
}
}
}.launchIn(this)

View File

@ -0,0 +1,41 @@
package space.kscience.magix.services
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.serialization.json.JsonElement
import space.kscience.magix.api.MagixEndpoint
import space.kscience.magix.api.MagixMessage
import space.kscience.magix.api.MagixMessageFilter
/**
* Launch magix message converter service.
*
* The service converts a payload from one format into another.
*
* @param endpoint The endpoint this converter is attached to
* @param filter a filter for messages to be converted.
* @param outputFormat a new value for [MagixMessage.format] field
* @param newSource a new value of [MagixMessage.sourceEndpoint]. By default uses the original message value
* @param transformer a function to transform the payload.
*/
public fun CoroutineScope.launchMagixConverter(
endpoint: MagixEndpoint,
filter: MagixMessageFilter,
outputFormat: String,
newSource: String? = null,
transformer: suspend (JsonElement) -> JsonElement,
): Job = endpoint.subscribe(filter).onEach { message->
val newPayload = transformer(message.payload)
val transformed: MagixMessage = MagixMessage(
outputFormat,
newPayload,
newSource ?: message.sourceEndpoint,
message.targetEndpoint,
message.id,
message.parentId,
message.user
)
endpoint.broadcast(transformed)
}.launchIn(this)

View File

@ -6,7 +6,7 @@
## Artifact:
The Maven coordinates of this project are `space.kscience:magix-java-client:0.1.1-SNAPSHOT`.
The Maven coordinates of this project are `space.kscience:magix-java-client:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
@ -16,7 +16,7 @@ repositories {
}
dependencies {
implementation 'space.kscience:magix-java-client:0.1.1-SNAPSHOT'
implementation 'space.kscience:magix-java-client:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
@ -27,6 +27,6 @@ repositories {
}
dependencies {
implementation("space.kscience:magix-java-client:0.1.1-SNAPSHOT")
implementation("space.kscience:magix-java-client:0.2.0-dev-2")
}
```

View File

@ -6,7 +6,7 @@ MQTT client magix endpoint
## Artifact:
The Maven coordinates of this project are `space.kscience:magix-mqtt:0.1.1-SNAPSHOT`.
The Maven coordinates of this project are `space.kscience:magix-mqtt:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
@ -16,7 +16,7 @@ repositories {
}
dependencies {
implementation 'space.kscience:magix-mqtt:0.1.1-SNAPSHOT'
implementation 'space.kscience:magix-mqtt:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
@ -27,6 +27,6 @@ repositories {
}
dependencies {
implementation("space.kscience:magix-mqtt:0.1.1-SNAPSHOT")
implementation("space.kscience:magix-mqtt:0.2.0-dev-2")
}
```

View File

@ -6,7 +6,7 @@ RabbitMQ client magix endpoint
## Artifact:
The Maven coordinates of this project are `space.kscience:magix-rabbit:0.1.1-SNAPSHOT`.
The Maven coordinates of this project are `space.kscience:magix-rabbit:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
@ -16,7 +16,7 @@ repositories {
}
dependencies {
implementation 'space.kscience:magix-rabbit:0.1.1-SNAPSHOT'
implementation 'space.kscience:magix-rabbit:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
@ -27,6 +27,6 @@ repositories {
}
dependencies {
implementation("space.kscience:magix-rabbit:0.1.1-SNAPSHOT")
implementation("space.kscience:magix-rabbit:0.2.0-dev-2")
}
```

View File

@ -6,7 +6,7 @@ Magix endpoint (client) based on RSocket
## Artifact:
The Maven coordinates of this project are `space.kscience:magix-rsocket:0.1.1-SNAPSHOT`.
The Maven coordinates of this project are `space.kscience:magix-rsocket:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
@ -16,7 +16,7 @@ repositories {
}
dependencies {
implementation 'space.kscience:magix-rsocket:0.1.1-SNAPSHOT'
implementation 'space.kscience:magix-rsocket:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
@ -27,6 +27,6 @@ repositories {
}
dependencies {
implementation("space.kscience:magix-rsocket:0.1.1-SNAPSHOT")
implementation("space.kscience:magix-rsocket:0.2.0-dev-2")
}
```

View File

@ -6,7 +6,7 @@ A magix event loop implementation in Kotlin. Includes HTTP/SSE and RSocket route
## Artifact:
The Maven coordinates of this project are `space.kscience:magix-server:0.1.1-SNAPSHOT`.
The Maven coordinates of this project are `space.kscience:magix-server:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
@ -16,7 +16,7 @@ repositories {
}
dependencies {
implementation 'space.kscience:magix-server:0.1.1-SNAPSHOT'
implementation 'space.kscience:magix-server:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
@ -27,6 +27,6 @@ repositories {
}
dependencies {
implementation("space.kscience:magix-server:0.1.1-SNAPSHOT")
implementation("space.kscience:magix-server:0.2.0-dev-2")
}
```

View File

@ -1,4 +1,32 @@
# Module magix-storage
Magix history database API
## Usage
## Artifact:
The Maven coordinates of this project are `space.kscience:magix-storage:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
repositories {
maven { url 'https://repo.kotlin.link' }
mavenCentral()
}
dependencies {
implementation 'space.kscience:magix-storage:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
```kotlin
repositories {
maven("https://repo.kotlin.link")
mavenCentral()
}
dependencies {
implementation("space.kscience:magix-storage:0.2.0-dev-2")
}
```

View File

@ -6,7 +6,7 @@
## Artifact:
The Maven coordinates of this project are `space.kscience:magix-storage-xodus:0.1.1-SNAPSHOT`.
The Maven coordinates of this project are `space.kscience:magix-storage-xodus:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
@ -16,7 +16,7 @@ repositories {
}
dependencies {
implementation 'space.kscience:magix-storage-xodus:0.1.1-SNAPSHOT'
implementation 'space.kscience:magix-storage-xodus:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
@ -27,6 +27,6 @@ repositories {
}
dependencies {
implementation("space.kscience:magix-storage-xodus:0.1.1-SNAPSHOT")
implementation("space.kscience:magix-storage-xodus:0.2.0-dev-2")
}
```

View File

@ -6,7 +6,7 @@ ZMQ client endpoint for Magix
## Artifact:
The Maven coordinates of this project are `space.kscience:magix-zmq:0.1.1-SNAPSHOT`.
The Maven coordinates of this project are `space.kscience:magix-zmq:0.2.0-dev-2`.
**Gradle Groovy:**
```groovy
@ -16,7 +16,7 @@ repositories {
}
dependencies {
implementation 'space.kscience:magix-zmq:0.1.1-SNAPSHOT'
implementation 'space.kscience:magix-zmq:0.2.0-dev-2'
}
```
**Gradle Kotlin DSL:**
@ -27,6 +27,6 @@ repositories {
}
dependencies {
implementation("space.kscience:magix-zmq:0.1.1-SNAPSHOT")
implementation("space.kscience:magix-zmq:0.2.0-dev-2")
}
```