goblins-echo/goblins-echo.scm
2024-01-29 15:07:18 -05:00

46 lines
1.4 KiB
Scheme

(use-modules (goblins)
(goblins vat)
(goblins ocapn captp)
(goblins ocapn ids)
(goblins ocapn netlayer prelay-utils)
(goblins ocapn netlayer prelay)
(fibers conditions)
(ice-9 match))
(define can-quit?
(make-condition))
(define echo-vat
(spawn-vat #:name "echo-vat"))
(define (^listener bcom)
(lambda (text) (format #t "~a\n" text)))
(define (listen setup-sref)
(with-vat echo-vat
(on (fetch-and-spawn-prelay-netlayer setup-sref)
(lambda (netlayer)
(define mycapn (spawn-mycapn netlayer))
(define listener (spawn ^listener))
(define listener-id ($ mycapn 'register listener 'prelay))
(format #t "Listener registered at ~s\n" (ocapn-id->string listener-id))))))
(define (say setup-sref listener-sref text)
(with-vat echo-vat
(on (fetch-and-spawn-prelay-netlayer setup-sref)
(lambda (netlayer)
(on (<- (<- (spawn-mycapn netlayer) 'enliven listener-sref) text)
(lambda (_)
(format #t "sent message ~s.\n" text)
(signal-condition! can-quit?)))))))
(match (command-line)
[(_cmd "listen" setup-id)
(listen (string->ocapn-id setup-id))]
[(_cmd "say" setup-id listener-id text)
(say (string->ocapn-id setup-id) (string->ocapn-id listener-id) text)]
[unknown-cmd
(format #t "Unknown command ~a\n" unknown-cmd)
(signal-condition! can-quit?)])
(wait can-quit?)