From 8fc175e6c34c66d7c0cdadc60ed71de6166dd59d Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Mon, 16 Dec 2024 19:53:41 +0100 Subject: simulation: rename message to packet --- simulation.scm | 88 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 44 insertions(+), 44 deletions(-) (limited to 'simulation.scm') diff --git a/simulation.scm b/simulation.scm index 6661b32..09cc16d 100644 --- a/simulation.scm +++ b/simulation.scm @@ -42,14 +42,14 @@ ;; 'unconfirmed-data (uplink) ;; 'confirmed-data (uplink) ;; 'ack (downlink) -(define-record-type - (make-message id device-id channel-n uplink? body) - message? - (id message-id) - (device-id message-device-id) - (channel-n message-channel-n) - (uplink? message-uplink?) ;; TODO: we could check this - (body message-body)) +(define-record-type + (make-packet id device-id channel-n uplink? body) + packet? + (id packet-id) + (device-id packet-device-id) + (channel-n packet-channel-n) + (uplink? packet-uplink?) ;; TODO: we could check this + (body packet-body)) (define-record-type (make-device channel thunk) @@ -79,30 +79,30 @@ (define (im-listening?) (atomic-box-ref listening?)) - (define (confirm message-id) - (when (eq? message-id (atomic-box-compare-and-swap! to-confirm message-id #f)) + (define (confirm packet-id) + (when (eq? packet-id (atomic-box-compare-and-swap! to-confirm packet-id #f)) (spawn-fiber - (lambda () "confirm confirmation message")))) + (lambda () "confirm confirmation packet")))) (define (upstream) - (define current-message 0) + (define current-packet 0) (forever (when (eq? #f (atomic-box-ref to-confirm)) (ll "Device ~a waiting for data" id) (sleep (rand-time))) ;; wait for more data (let* ((confirmed? #t) ;; random? - (message (make-message - current-message + (packet (make-packet + current-packet id channel #t (if confirmed? 'confirmed-data 'unconfirmed-data)))) (when confirmed? - (atomic-box-compare-and-swap! to-confirm #f current-message)) - (put-message upstream-chn (make-event 'start message)) + (atomic-box-compare-and-swap! to-confirm #f current-packet)) + (put-message upstream-chn (make-event 'start packet)) (sleep time-on-air) - (put-message upstream-chn (make-event 'end message))) - (set! current-message (1+ current-message)) + (put-message upstream-chn (make-event 'end packet))) + (set! current-packet (1+ current-packet)) (sleep RECEIVE_DELAY1) (start-listening!) (sleep RX1) @@ -117,8 +117,8 @@ (forever (let ((msg (get-message downstream-chn))) (when (im-listening?) - (match (message-body msg) - ('ack (confirm (message-id msg)))))))) + (match (packet-body msg) + ('ack (confirm (packet-id msg)))))))) (lambda () (spawn-fiber upstream) @@ -132,10 +132,10 @@ ;; TODO: answer in the second window?? (lambda () (sleep RECEIVE_DELAY1) - (let ((message (make-message seq-number to channel #f 'ack))) - (put-message downstream (make-event 'start message)) + (let ((packet (make-packet seq-number to channel #f 'ack))) + (put-message downstream (make-event 'start packet)) (sleep time-on-air) ;; TODO: size / data-rate - (put-message downstream (make-event 'end message)))))) + (put-message downstream (make-event 'end packet)))))) ;; Upstream: listen, and answer in new fibers (lambda () @@ -143,13 +143,13 @@ (let ((msg (get-message upstream))) (ll "Gateway ~a: Data #~a got from ~a" id - (message-id msg) - (message-device-id msg)) - (match (message-body msg) + (packet-id msg) + (packet-device-id msg)) + (match (packet-body msg) ('confirmed-data - (ack-confirmed-data (message-device-id msg) - (message-channel-n msg) ;; TODO: not right - (message-id msg))) + (ack-confirmed-data (packet-device-id msg) + (packet-channel-n msg) ;; TODO: not right + (packet-id msg))) ('unconfirmed-data #f)))))) @@ -160,37 +160,37 @@ ;; TODO: this is broken, only accounts for the interference of the packet ;; that was already being sent, and not from the new one that produced the ;; interference => both should be affected. - (define (interference? msg started-messages) - (any (lambda (x) (= (message-channel-n msg) (message-channel-n x))) - started-messages)) + (define (interference? msg started-packets) + (any (lambda (x) (= (packet-channel-n msg) (packet-channel-n x))) + started-packets)) (lambda () (forever (let ((ev (get-message in))) (match ev (($ 'start msg) - (ll "Device ~a started ~a of message ~a" - (message-device-id msg) - (message-body msg) - (message-id msg)) + (ll "Device ~a started ~a of packet ~a" + (packet-device-id msg) + (packet-body msg) + (packet-id msg)) (set! started (cons msg started))) (($ 'end msg) - (ll "Device ~a finished ~a of message ~a" - (message-device-id msg) - (message-body msg) - (message-id msg)) + (ll "Device ~a finished ~a of packet ~a" + (packet-device-id msg) + (packet-body msg) + (packet-id msg)) (set! started (remove! (lambda (x) - (and (= (message-id msg) (message-id x)) - (= (message-device-id msg) (message-device-id x)))) started)) + (and (= (packet-id msg) (packet-id x)) + (= (packet-device-id msg) (packet-device-id x)))) started)) (if (interference? msg started) ;; TODO: interferences are broken (ll "Interference!!!!!!") - (if (message-uplink? msg) + (if (packet-uplink? msg) (hash-table-walk gateways (lambda (k gateway) (put-message (device-channel gateway) msg))) (put-message - (device-channel (hash-table-ref end-devices (message-device-id msg))) + (device-channel (hash-table-ref end-devices (packet-device-id msg))) msg))))))))) -- cgit v1.2.3