1
0
Fork 0

Lots of fixes. Have a proper message pump going on

This commit is contained in:
Vivianne 2024-02-02 00:01:48 -05:00
parent 314a29eb88
commit 3db784c18c
1 changed files with 44 additions and 27 deletions

View File

@ -21,7 +21,6 @@
;; (fcntl input F_SETFL (logior O_NONBLOCK flags)))
;; (install-suspendable-ports!)
(define can-quit? (make-condition))
(define ocapn-registry #f)
(define listener-actor #f)
@ -41,11 +40,13 @@
(make-console-command
"/quit"
"- Exits the chat"
(λ (args) (signal-condition! can-quit?)))
(λ (args) (loop! #f)))
(make-console-command
"/help"
"- Prints this help"
(λ (args) (print-help)))
(λ (args)
(print-help)
(loop! #t)))
(make-console-command
"/join"
"<listener-id> - Switch chats to another listener"
@ -60,10 +61,16 @@
(unless ocapn-registry
(error "Relay not yet connected."))
(format #t "Connecting...\n")
(on (<- ocapn-registry 'enliven listener-sref)
(λ (l)
(set! listener-actor l)
(format #t "Connected to actor.\n"))))))))
(format #t "Joined chat.\n")
(loop! #t))
#:catch
(λ (e)
(format #t "Failed: ~a\n" e)
(loop! #t))))))))
(define (print-help)
(format #t "Command reference:\n")
@ -78,40 +85,50 @@
((console-command-thunk matching-command) args)
(begin
(format #t "Don't know how to handle ~a.\n\n" cmd)
(print-help))))
(print-help)
(loop! #t))))
(define (read-line-vow)
(spawn-fibrous-vow
(λ ()
(read-line (readline-port)))))
(define loop-channel (make-channel))
(define (loop! val) (put-message loop-channel val) val)
(define (%loop)
(with-exception-handler (λ (e) (format #t "Command failed: ~s\n" e))
(on (read-line-vow)
(λ (line)
(cond
((eq? 0 (string-length line)) #t)
((is-command? line)
(%eval-command line))
(else
(unless listener-actor
(format #t "Not connected to anyone yet. Use /join <sturdyref>!\n"))
(<- listener-actor line)))
(%loop)))
(define (%loop vat)
(with-exception-handler
(λ (e)
(format #t "Command failed: ~s\n" e)
(loop! #t))
(λ ()
(let ((line (readline)))
(with-vat vat
(cond
((eq? 0 (string-length line)) (loop! #t))
((is-command? line)
(%eval-command line))
(else (if listener-actor
(on (<- listener-actor line) (λ (val) (loop! val)))
(begin
(format #t "Not connected to anyone yet. Use /join <sturdyref>!\n")
(loop! #t))))))))
#:unwind? #t))
(define (say setup-sref)
(define vat (spawn-vat #:name "Speaker Vat"))
(activate-readline)
(set-readline-prompt! " 🐞 > ")
(with-vat vat
(format #t "Connecting to relay...\n")
(on (prelay-sref->mycapn-registry setup-sref)
(λ (r)
(set! ocapn-registry r)
(format #t "Connected to relay.\n")))
(format #t "Connected.\n")
(loop! #t))
#:catch
(λ (e)
(format #t "Failed: ~a\n" e)
(loop! #t))))
(%loop))
(wait can-quit?))
(while (get-message loop-channel)
(with-vat vat
(syscaller-free-fiber
(λ ()
(%loop vat))))))