;;;; -*- Scheme -*- ;; ;; neighbors example for HLSIM ;; ;; David DeRoure June 12 1997, revised Sep 14 1997 ;; dder@martigny.ai.mit.edu ;; ;; This file should be compiled with (cps "neighbors1") then ;; (cbf "neighbors1"). It uses definitions in club5d.scm. ;; See neighbor1s.scm for the simulation setup. See the HLSIM ;; documentation for further details. ;; ;; Each processor repeatedly broadcasts its id, and keeps ;; count of its neighbors by counting the number of unique ;; ids received; the count is represented in the color. ;; The period between broadcasts is random to reduce repeated ;; collisions. (declare (usual-integrations)) ;; Every processor has an id (not the processor number, as that would ;; be cheating) (define processor-id (random 1000000)) (define neighbors '()) (define collisions 0) (define primitive-message-event (make-event)) (define (receive-loop) (select (global-timeout (set! transmit? #F)) (primitive-message-event => (lambda (message) (event.clear! primitive-message-event) (if (eq? message 'collision) (set! collisions (1+ collisions)) (if (not (member message neighbors)) (begin (set! neighbors (cons message neighbors)) (color-me (length neighbors))))) (receive-loop))) ;; The following clause causes isolated processors to stop after ;; neighbor-timeout - it is optional, because isolated processors ;; can have no effect even if they keep going! When a processor ;; is found to be isolated, it changes from blue to black. (neighbor-timeout (event.clear! neighbor-timeout) (if (and (zero? (length neighbors)) (zero? collisions)) (begin (color-me "black") (set! transmit? #F)))))) (define (transmit-loop) (wait (make-timeout-event (+ 100 (random 1000)))) (broadcast processor-id) (if transmit? (transmit-loop))) ;; Run the loops (define global-timeout (make-timeout-event 10000)) (define neighbor-timeout (make-timeout-event 5000)) (define transmit? #T) (color-me 0) (parallel (transmit-loop) (receive-loop)) ;; end of neighbors1.scm