aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIbrahim Mkusa <ibrahimmkusa@gmail.com>2017-04-13 14:01:32 -0400
committerIbrahim Mkusa <ibrahimmkusa@gmail.com>2017-04-13 14:01:32 -0400
commita0fbba4a81b0ebc4819c3413a8bf06ae0d3aeb5c (patch)
treef8fb2990de47b0714d2e6cccb1fceecd28011706
parentcffd7a429993da67bf6d64e713c4a147fe287b9c (diff)
tracking a list of input and output ports via closures and sets
-rw-r--r--Hermes/server.rkt15
1 files changed, 11 insertions, 4 deletions
diff --git a/Hermes/server.rkt b/Hermes/server.rkt
index b0f2dff..216c6e2 100644
--- a/Hermes/server.rkt
+++ b/Hermes/server.rkt
@@ -22,8 +22,8 @@
(define c-count (make-count 0))
(define c-count-s (make-semaphore 1))
-(define connections '()) ;; maintains a list of open ports
+; track list of input output port pairs in a list contained in a closure
(define (make-connections connections)
(define (null-cons?)
(null? connections))
@@ -31,7 +31,13 @@
(set! connections (append connections (list (list in out))))
connections)
(define (cons-list)
- connections))
+ connections)
+ (define (dispatch m)
+ (cond [(eq? m 'null-cons) null-cons?]
+ [(eq? m 'cons-list) cons-list]
+ [(eq? m 'add) add]))
+ dispatch)
+(define c-connections (make-connections '()))
(define connections-s (make-semaphore 1)) ;; control access to connections
@@ -91,7 +97,8 @@
(displayln "Welcome to Hermes coms\nType your message below" out)
(flush-output out)
(semaphore-wait connections-s)
- (set! connections (append connections (list (list in out))))
+ ; (set! connections (append connections (list (list in out))))
+ ((c-connections 'add) in out)
(semaphore-post connections-s)
; start a thread to deal with specific client and add descriptor value to the list of threads
@@ -155,7 +162,7 @@
(lambda (ports)
(displayln (first messages) (get-output-port ports))
(flush-output (get-output-port ports)))
- connections)
+ ((c-connections 'cons-list)))
;; remove top message
(set! messages (rest messages))
(displayln "Message broadcasted"))])