From fa56c151c2551ad22577766c67ed52832066bf33 Mon Sep 17 00:00:00 2001 From: Tim Vaughan Date: Tue, 27 Apr 2021 12:45:47 +0200 Subject: [PATCH] Receiving messages over UDP works. --- sam.scm | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/sam.scm b/sam.scm index c819ca5..8e8a730 100644 --- a/sam.scm +++ b/sam.scm @@ -5,6 +5,7 @@ (import (chicken io) (chicken string) + (chicken port) matchable srfi-18 ; threads srfi-69 ; hash-table @@ -16,7 +17,7 @@ ;; Actors (define this-host "localhost") -(define this-port 1234) +(define this-port 8000) (define (make-address host port id) (make-uri #:scheme "actor" @@ -24,15 +25,13 @@ #:port port #:path (list '/ id))) -(define next-local-address - (let ((mutex (make-mutex "actor address mutex"))) - (lambda () - (let ((res #f)) - (mutex-lock! mutex) - (set! res (make-address this-host this-port (uuid))) - (mutex-unlock! mutex) - res)))) - +(define (make-local-address . args) + (make-address this-host + this-port + (if (null? args) + (uuid) + (car args)))) + (define (address-id address) (cadr (uri-path address))) (define (address-local? address) @@ -42,7 +41,7 @@ (define actor-table (make-hash-table)) (define (make-actor beh) - (let* ((address (next-local-address)) + (let* ((address (make-local-address)) (id (address-id address))) (hash-table-set! actor-table id beh) address)) @@ -137,6 +136,23 @@ (loop))) +;; Network + +(define (start-network-listener) + (thread-start! + (lambda () + (let ((s (udp-open-socket*))) + (udp-bind! s #f this-port) + (let loop () + (let-values (((n str) (udp-recv s 1024))) + (print "Received " n " bytes over network: " str) + (match (with-input-from-string str read) + ((uri-str message ...) + (apply send-message (cons (uri-reference uri-str) message))) + (else + (print "Warning: received badly formatted message string '" str "'")))) + (loop)))))) + ;; System interface (define system @@ -163,10 +179,13 @@ (thread-start! (lambda () - (thread-sleep! 10) + (thread-sleep! 120) (send-message system 'shutdown))) +(print (uri->string system)) + (start-scheduler) +(start-network-listener) (start-console) ;; (thread-join! scheduler-thread) -- 2.20.1