diff options
-rw-r--r-- | simulation.scm | 67 |
1 files changed, 50 insertions, 17 deletions
diff --git a/simulation.scm b/simulation.scm index 34bf901..01f31a1 100644 --- a/simulation.scm +++ b/simulation.scm @@ -1,6 +1,7 @@ (define-module (simulation) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) + #:use-module (srfi srfi-69) #:use-module (ice-9 atomic) #:use-module (ice-9 match) #:use-module (fibers) @@ -92,7 +93,7 @@ (message-device-id msg)))))) -(define (make-radio in gateways) +(define (make-radio in end-devices gateways) ;(define devices (hash-map ...)) ;; it needs a device-id <-> channel mapping ;; in: listen from devices: check collisions and power transmission @@ -117,25 +118,57 @@ (= (message-device-id msg) (message-device-id x)))) started)) (if (interference? msg started) (ll "Interference!!!!!!") - (for-each (lambda (gateway) - (put-message gateways - (make-message (message-id msg) - (message-device-id msg) - (message-channel-n msg) - 'data))) - gateways)))))))) - + (if (message-uplink? msg) + (hash-for-each (lambda (k gateway) + (put-message (device-channel gateway) + (make-message (message-id msg) + (message-device-id msg) + (message-channel-n msg) + #t + 'data))) + gateways) + (put-message + (device-channel (hash-ref end-devices (message-device-id msg))) + (make-message (message-id msg) + (message-device-id msg) + (message-channel-n msg) + #f + 'ack)))))))))) + + +(define-record-type <device> + (make-device channel thunk) + device? + (channel device-channel) + (thunk device-thunk)) (define (run-simulation) - (let* ((radio-chn (make-channel)) - (gateway-chn (make-channel))) - (spawn-fiber (make-class-a 1 1 radio-chn (make-channel))) - (spawn-fiber (make-class-a 2 1 radio-chn (make-channel))) - (spawn-fiber (make-class-a 3 1 radio-chn (make-channel))) - (spawn-fiber (make-class-a 4 1 radio-chn (make-channel))) - (spawn-fiber (make-gateway 5 radio-chn gateway-chn)) - (spawn-fiber (make-radio radio-chn (list gateway-chn))))) + (let* ((radio-chn (make-channel)) + (end-devices (make-hash-table)) + (gateways (make-hash-table))) + + (for-each + (lambda (id) + (let ((chn (make-channel))) + (hash-table-set! + end-devices id + (make-device chn (make-class-a id 1 radio-chn chn))))) + (iota 6)) + + (for-each + (lambda (id) + (let ((chn (make-channel))) + (hash-table-set! + gateways id + (make-device chn (make-gateway id chn radio-chn))))) + (iota 6 10)) + + (spawn-fiber (make-radio radio-chn end-devices gateways)) + (hash-table-walk end-devices + (lambda (_ device) (spawn-fiber (device-thunk device)))) + (hash-table-walk gateways + (lambda (_ device) (spawn-fiber (device-thunk device)))))) (run-fibers run-simulation |