syndesizer: absorb json_socket_translator

This commit is contained in:
Emery Hemingway 2024-01-08 21:14:33 +02:00
parent ad3b160e56
commit 3c050e242e
6 changed files with 65 additions and 13 deletions

View File

@ -5,6 +5,60 @@
A Syndicate multitool. Includes a number of different actors that become active via configuration.
Whether you use a single instance for many protocols or many specialized instances is up to you.
### JSON Socket Translator
Communicate with sockets that send and receive lines of JSON using `<send …>` and `<recv …>` messages.
Do not send messages into the dataspace configure with `<json-socket-translator …>` until `<connected @socketPath string>` is asserted.
```
# MPV configuration example
<require-service <daemon mpv-server>>
<daemon mpv-server {
argv: [
"/run/current-system/sw/bin/mpv"
"--really-quiet"
"--idle=yes"
"--no-audio-display"
"--input-ipc-server=/run/user/1000/mpv.sock"
"--volume=75"
]
protocol: none
}>
let ?mpvSpace = dataspace
? <service-state <daemon mpv-server> ready> [
<require-service <daemon syndesizer>>
? <service-object <daemon syndesizer> ?cap> [
$cap <json-socket-translator {
dataspace: $mpvSpace
socket: "/run/user/1000/mpv.sock"
}>
]
]
$mpvSpace [
# announce the dataspace when the translator is connected
? <connected $socketPath> [
$config <mpv $mpvSpace>
$config <bind <ref { oid: "mpv" key: #x"" }> $mpvSpace #f>
]
# translate <play-file > to an MPV command
?? <play-file ?file> [
! <send { "command": ["loadfile" $file "append-play"] }>
]
# clear the playlist on idle so it doesn't grow indefinitely
?? <recv {"event": "idle"}> [
! <send { "command": ["playlist-clear"] }>
]
]
```
### Webooks
Listens for webhook requests and sends request data to a dataspace as messages.
@ -53,12 +107,6 @@ Example configuration:
Wrapper that executes a command, parses its JSON output, converts to Preserves record `<recv @jsonData any>`, and publishes and messages to its initial dataspace.
## json_socket_translator
Utility to communicate with sockets that send and receive lines of JSON using `<send …>` and `<recv …>` messages. Compatible with [mpv](https://mpv.io/), see [mpv.config-example.pr](./mpv.config-example.pr).
Do not send messages immediately to the dataspace passed `json_socket_translator`, wait until it asserts `<connected @socketPath string>`.
## mintsturdyref
A utility for minting [Sturdyrefs](https://synit.org/book/operation/builtin/gatekeeper.html#sturdyrefs).

View File

@ -3,10 +3,10 @@ embeddedType EntityRef.Cap .
JsonTranslatorConnected = <connected @path string>.
JsonSocketTranslatorArguments = {
JsonSocketTranslatorArguments = <json-socket-translator {
dataspace: #!any
socket: string
}.
}>.
WebhooksArguments = <webhooks {
endpoints: {[string ...]: #!any ...:...}

View File

@ -6,10 +6,13 @@ type
JsonTranslatorConnected* {.preservesRecord: "connected".} = object
`path`*: string
JsonSocketTranslatorArguments* {.preservesDictionary.} = object
JsonSocketTranslatorArgumentsField0* {.preservesDictionary.} = object
`dataspace`* {.preservesEmbedded.}: EmbeddedRef
`socket`*: string
JsonSocketTranslatorArguments* {.preservesRecord: "json-socket-translator".} = object
`field0`*: JsonSocketTranslatorArgumentsField0
WebhooksArgumentsField0* {.preservesDictionary.} = object
`endpoints`*: Table[seq[string], EmbeddedRef]
`listen`*: Tcp

View File

@ -5,9 +5,10 @@
import syndicate, syndicate/relays, syndicate/actors/timers
import ./syndesizer/webhooks
import ./syndesizer/[json_socket_translator, webhooks]
runActor("syndesizer") do (turn: var Turn; root: Cap):
connectStdio(turn, root)
discard spawnTimers(turn, root)
discard spawnJsonSocketTranslator(turn, root)
discard spawnWebhookActor(turn, root)

View File

@ -5,10 +5,10 @@ import std/[asyncdispatch, asyncnet, json]
from std/nativesockets import AF_UNIX, SOCK_STREAM, Protocol
import preserves, preserves/jsonhooks, syndicate, syndicate/relays
import ./schema/config, ./json_messages
import ../schema/config, ../json_messages
proc spawnJsonSocketTranslator*(turn: var Turn; root: Cap): Actor =
spawn("json_socket_translator", turn) do (turn: var Turn):
spawn("json-socket-translator", turn) do (turn: var Turn):
during(turn, root, ?:JsonSocketTranslatorArguments) do (ds: Cap, socketPath: string):
let socket = newAsyncSocket(
domain = AF_UNIX,

View File

@ -5,7 +5,7 @@ author = "Emery Hemingway"
description = "Utilites for Syndicated Actors and Synit"
license = "unlicense"
srcDir = "src"
bin = @["cache_actor", "json_socket_translator", "json_translator", "mintsturdyref", "mount_actor", "msg", "net_mapper", "preserve_process_environment", "syndex_card", "syndump"]
bin = @["cache_actor", "json_translator", "mintsturdyref", "mount_actor", "msg", "net_mapper", "preserve_process_environment", "syndex_card", "syndump"]
# Dependencies