Files
web-racket/private/webui-wire-ipc.rkt
Hans Dijkema 694d6777e9 Racket integration
Signed-off-by: Hans Dijkema <hans@dijkewijk.nl>
2025-10-14 12:44:45 +02:00

76 lines
3.6 KiB
Racket

(module webui-wire-ipc racket/base
(require "webui-wire-download.rkt")
(provide webui-ipc)
(define re-kind #px"^([^:]+)[:]")
(define (read-eol port)
(read-string 1 port))
(define (process-stderr-reader process-stderr event-queuer log-processor)
(thread (λ ()
(letrec ((reader (λ ()
(let* ((str-length (read-string 8 process-stderr))
(colon (read-string 1 process-stderr)))
(if (eof-object? colon)
(begin
(log-processor 'stderr-reader "webui-wire executable exited")
'process-ended)
(begin
(unless (and
(string? colon)
(string=? colon ":"))
(error "Unexpected input from webui-wire standard error"))
(let* ((length (string->number str-length))
(input (read-string length process-stderr))
(m (regexp-match re-kind input))
)
(read-eol process-stderr)
(if (eq? m #f)
(log-processor 'stderr-reader
(format "Unexpected: no kind: input = ~a" input))
(let ((kind (string->symbol (list-ref m 1)))
(line (substring input (string-length (car m))))
)
(if (eq? kind 'EVENT)
(event-queuer line)
(log-processor kind line))))
)
(reader)
)
))
)
))
(reader)))
)
)
(define (webui-ipc event-queuer log-processor)
(let ((webui-wire-exe (ww-webui-wire)))
(displayln webui-wire-exe)
(call-with-values
(λ () (subprocess #f #f #f webui-wire-exe))
(λ (pid process-stdout process-stdin process-stderr)
;(displayln (format "~a ~a ~a ~a" pid process-stdout process-stdin process-stderr))
(let ((reader-thrd (process-stderr-reader process-stderr event-queuer log-processor)))
(λ (cmd)
(displayln cmd process-stdin)
(flush-output process-stdin)
(let* ((str-length (read-string 8 process-stdout))
(colon (read-string 1 process-stdout)))
;(displayln (format "len: ~a, str-length: ~a, colon: ~a" (string-length str-length) str-length colon))
(unless (and (string? colon)
(string=? colon ":"))
(error "Unexpected input from webui-wire executable"))
(let* ((length (string->number str-length))
(input (read-string length process-stdout))
)
(read-eol process-stdout)
input)))))
))
)
)