2021-07-04 14:44:53 +03:00
|
|
|
import kotlinx.coroutines.cancel
|
2021-07-03 14:16:31 +03:00
|
|
|
import kotlinx.coroutines.coroutineScope
|
|
|
|
import kotlinx.coroutines.delay
|
|
|
|
import kotlinx.coroutines.flow.catch
|
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
import kotlinx.coroutines.flow.onEach
|
|
|
|
import kotlinx.coroutines.isActive
|
2021-07-04 14:44:53 +03:00
|
|
|
import kotlinx.serialization.json.*
|
2021-07-03 14:16:31 +03:00
|
|
|
import org.slf4j.LoggerFactory
|
|
|
|
import ru.mipt.npm.magix.api.MagixEndpoint
|
|
|
|
import ru.mipt.npm.magix.api.MagixMessage
|
|
|
|
import ru.mipt.npm.magix.server.startMagixServer
|
|
|
|
import ru.mipt.npm.magix.zmq.ZmqMagixEndpoint
|
2021-07-04 14:44:53 +03:00
|
|
|
import java.awt.Desktop
|
|
|
|
import java.net.URI
|
2021-07-03 14:16:31 +03:00
|
|
|
|
|
|
|
|
2022-06-04 15:46:13 +03:00
|
|
|
suspend fun MagixEndpoint.sendJson(
|
2021-07-03 14:16:31 +03:00
|
|
|
origin: String,
|
|
|
|
format: String = "json",
|
|
|
|
target: String? = null,
|
|
|
|
id: String? = null,
|
|
|
|
parentId: String? = null,
|
|
|
|
user: JsonElement? = null,
|
|
|
|
builder: JsonObjectBuilder.() -> Unit
|
2022-06-01 10:11:12 +03:00
|
|
|
): Unit = broadcast(MagixMessage(origin, buildJsonObject(builder), format, target, id, parentId, user))
|
2021-07-03 14:16:31 +03:00
|
|
|
|
2021-07-04 14:44:53 +03:00
|
|
|
internal const val numberOfMessages = 100
|
|
|
|
|
2021-07-03 14:16:31 +03:00
|
|
|
suspend fun main(): Unit = coroutineScope {
|
|
|
|
val logger = LoggerFactory.getLogger("magix-demo")
|
|
|
|
logger.info("Starting magix server")
|
2021-07-04 14:44:53 +03:00
|
|
|
val server = startMagixServer(
|
|
|
|
buffer = 10,
|
|
|
|
enableRawRSocket = false //Disable rsocket to avoid kotlin 1.5 compatibility issue
|
|
|
|
)
|
|
|
|
|
|
|
|
server.apply {
|
|
|
|
val host = "localhost"//environment.connectors.first().host
|
|
|
|
val port = environment.connectors.first().port
|
|
|
|
val uri = URI("http", null, host, port, "/state", null, null)
|
|
|
|
Desktop.getDesktop().browse(uri)
|
|
|
|
}
|
2021-07-03 14:16:31 +03:00
|
|
|
|
|
|
|
logger.info("Starting client")
|
2021-07-04 14:44:53 +03:00
|
|
|
//Create zmq magix endpoint and wait for to finish
|
2022-06-04 15:46:13 +03:00
|
|
|
ZmqMagixEndpoint("tcp://localhost").use { client ->
|
2021-07-03 14:16:31 +03:00
|
|
|
logger.info("Starting subscription")
|
2021-07-04 14:44:53 +03:00
|
|
|
client.subscribe().onEach {
|
|
|
|
println(it.payload)
|
2022-06-04 15:46:13 +03:00
|
|
|
if (it.payload.jsonObject["index"]?.jsonPrimitive?.int == numberOfMessages) {
|
2021-07-04 14:44:53 +03:00
|
|
|
logger.info("Index $numberOfMessages reached. Terminating")
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}.catch { it.printStackTrace() }.launchIn(this)
|
2021-07-03 14:16:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
var counter = 0
|
|
|
|
while (isActive) {
|
|
|
|
delay(500)
|
2021-07-04 14:44:53 +03:00
|
|
|
val index = (counter++).toString()
|
|
|
|
logger.info("Sending message number $index")
|
|
|
|
client.sendJson("magix-demo", id = index) {
|
2021-07-03 14:16:31 +03:00
|
|
|
put("message", "Hello world!")
|
2021-07-04 14:44:53 +03:00
|
|
|
put("index", index)
|
2021-07-03 14:16:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|