Initial json_socket_translator utility
This commit is contained in:
parent
9b4710ea66
commit
cf0ac36d4b
|
@ -1,3 +1,4 @@
|
|||
http_translator
|
||||
json_translator
|
||||
msg
|
||||
json_socket_translator
|
||||
|
|
|
@ -10,6 +10,9 @@ See [http_translator.config-example.pr](./http_translator.config-example.pr) for
|
|||
|
||||
Wrapper that executes a command, parses its JSON output, and asserts a Preserves conversion in an `<output …>` record.
|
||||
|
||||
## json_socket_translator
|
||||
|
||||
Utility to communicate with sockets that send and receive lines of JSON. Compatible with [mpv](https://mpv.io/), see [mpv.config-example.pr](./mpv.config-example.pr).
|
||||
|
||||
## msg
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
version 1 .
|
||||
|
||||
JsonSocket = <json-socket @label symbol @path string> .
|
|
@ -0,0 +1,28 @@
|
|||
let ?beepSpace = dataspace
|
||||
<bind "syndicate" #x"" $beepSpace>
|
||||
|
||||
let ?socketPath = "/run/user/1000/mpv.sock"
|
||||
|
||||
<require-service <daemon mpv>>
|
||||
<daemon mpv {
|
||||
argv: ["/run/current-system/sw/bin/mpv"
|
||||
"--input-ipc-server=/run/user/1000/mpv.sock"
|
||||
"--idle=yes"
|
||||
"--really-quiet"
|
||||
]
|
||||
protocol: none
|
||||
}>
|
||||
|
||||
<require-service <daemon mpv-translator>>
|
||||
<daemon mpv-translator {
|
||||
argv: "/home/repo/syndicate/syndicate_utils/src/json_socket_translator"
|
||||
protocol: text/syndicate
|
||||
}>
|
||||
|
||||
? <service-object <daemon mpv-translator> ?mpvSpace> [
|
||||
$mpvSpace <json-socket mpv $socketPath>
|
||||
$beepSpace ?? <beep ?code> [
|
||||
$mpvSpace ! <mpv 1 { "command": ["loadfile" "/tmp/beep.ogg"] }>
|
||||
]
|
||||
|
||||
]
|
|
@ -0,0 +1,14 @@
|
|||
|
||||
import
|
||||
std/typetraits, preserves
|
||||
|
||||
type
|
||||
JsonSocket* {.preservesRecord: "json-socket".} = object
|
||||
`label`*: Symbol
|
||||
`path`*: string
|
||||
|
||||
proc `$`*(x: JsonSocket): string =
|
||||
`$`(toPreserve(x))
|
||||
|
||||
proc encode*(x: JsonSocket): seq[byte] =
|
||||
encode(toPreserve(x))
|
|
@ -0,0 +1,33 @@
|
|||
# SPDX-FileCopyrightText: ☭ 2022 Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[asyncdispatch, asyncnet, json]
|
||||
from std/nativesockets import AF_UNIX, SOCK_STREAM, Protocol
|
||||
import preserves, preserves/jsonhooks, syndicate, syndicate/patterns
|
||||
|
||||
import ./config
|
||||
|
||||
bootDataspace("main") do (ds: Ref; turn: var Turn):
|
||||
connectStdio(ds, turn)
|
||||
during(turn, ds, ?config.JsonSocket) do (label: Assertion, socketPath: string):
|
||||
let socket = newAsyncSocket(
|
||||
domain = AF_UNIX,
|
||||
sockType = SOCK_STREAM,
|
||||
protocol = cast[Protocol](0),
|
||||
buffered = false)
|
||||
waitFor connectUnix(socket, socketPath)
|
||||
|
||||
onMessage(turn, ds, recordPattern(label, ?1, grab())) do (data: Assertion):
|
||||
var js: JsonNode
|
||||
if fromPreserve(js, data):
|
||||
asyncCheck socket.send($js & "\n")
|
||||
|
||||
let f = turn.facet
|
||||
proc processIncoming(fut: Future[string]) {.gcsafe.} =
|
||||
var data = fut.read.parseJson.toPreserve(Ref)
|
||||
run(f) do (turn: var Turn):
|
||||
message(turn, ds, initRecord(label, 0.toPreserve(Ref), data))
|
||||
socket.recvLine.addCallback(processIncoming)
|
||||
socket.recvLine.addCallback(processIncoming)
|
||||
|
||||
runForever()
|
|
@ -5,7 +5,7 @@ author = "Emery Hemingway"
|
|||
description = "Utilites for Syndicated Actors and Synit"
|
||||
license = "unlicense"
|
||||
srcDir = "src"
|
||||
bin = @["http_translator", "json_translator", "msg"]
|
||||
bin = @["http_translator", "json_socket_translator", "json_translator", "msg"]
|
||||
|
||||
|
||||
# Dependencies
|
||||
|
|
Loading…
Reference in New Issue