From 59ca3d24435fd4bd39dad8e39060a430abf76da5 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Mon, 3 Jun 2024 16:00:30 +0300 Subject: [PATCH] esc-printer-actor: use gatekeeper protocol --- config.prs | 2 ++ src/esc_printer_actor.nim | 21 +++++++++++---------- src/schema/config.nim | 7 +++++++ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/config.prs b/config.prs index 0ccd83f..c683a91 100644 --- a/config.prs +++ b/config.prs @@ -45,6 +45,8 @@ PostgreArguments = . PostgreConnectionParameter = [@key string @val string]. +PrinterStep = . + PulseArguments = . diff --git a/src/esc_printer_actor.nim b/src/esc_printer_actor.nim index 5d00623..a9c38dc 100644 --- a/src/esc_printer_actor.nim +++ b/src/esc_printer_actor.nim @@ -7,6 +7,7 @@ import std/[cmdline, oserrors, posix, sets], preserves, preserves/sugar, syndicate, syndicate/relays, + syndicate/protocols/[gatekeeper, sturdy], ./private/esc_p proc echo(args: varargs[string, `$`]) {.used.} = @@ -67,18 +68,18 @@ method retract(printer: Printer; t: Turn; h: Handle) = elif printer.subscriptHandles.contains h: printer.retract(printer.subscriptHandles, CancelAltScript, h) -proc openPrinter(turn: Turn; devicePath: string): Printer = +let devicePath = paramStr(1) + +proc openPrinter(turn: Turn): Printer = new result result.facet = turn.facet result.device = posix.open(devicePath, O_WRONLY, 0) result.write(InitializePrinter) -proc main = - let devicePath = paramStr(1) - runActor(devicePath) do (turn: Turn): - let printer = turn.newCap openPrinter(turn, devicePath) - resolveEnvironment(turn) do (turn: Turn; ds: Cap): - publish(turn, ds, initRecord( - toSymbol"printer", printer.embed, %devicePath)) - -main() +runActor(devicePath) do (turn: Turn): + let printer = turn.newCap openPrinter(turn) + resolveEnvironment(turn) do (turn: Turn; relay: Cap): + during(turn, relay, grabWithinType(Resolve)) do (step: Value; cont: Cap): + if step.isRecord("printer"): + # Publish for any step. + discard publish(turn, cont, ResolvedAccepted(responderSession: printer)) diff --git a/src/schema/config.nim b/src/schema/config.nim index efbd828..05fe11b 100644 --- a/src/schema/config.nim +++ b/src/schema/config.nim @@ -119,6 +119,11 @@ type UnixAddress* {.preservesRecord: "unix".} = object `path`*: string + PrinterStepField0* {.preservesDictionary.} = object + + PrinterStep* {.preservesRecord: "printer".} = object + `field0`*: PrinterStepField0 + Tcp* {.preservesRecord: "tcp".} = object `host`*: string `port`*: BiggestInt @@ -140,6 +145,7 @@ proc `$`*(x: WebsocketArguments | HttpClientArguments | JsonTranslatorArguments PostgreArguments | PulseArguments | UnixAddress | + PrinterStep | Tcp): string = `$`(toPreserves(x)) @@ -161,5 +167,6 @@ proc encode*(x: WebsocketArguments | HttpClientArguments | PostgreArguments | PulseArguments | UnixAddress | + PrinterStep | Tcp): seq[byte] = encode(toPreserves(x))