aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIbrahim Mkusa <ibrahimmkusa@gmail.com>2017-04-14 00:44:42 -0400
committerIbrahim Mkusa <ibrahimmkusa@gmail.com>2017-04-14 00:45:33 -0400
commitf8fef5c5ae63c0933f92a25d82da447ff44c2444 (patch)
tree514ad4ef920614139f824ea6f58821ca84afde0b
parent8d9765ca8c154f1a30695f2de3c26166d0451ae4 (diff)
now handles disconnected clients
-rw-r--r--Hermes/TODO2
-rw-r--r--Hermes/server.rkt15
2 files changed, 16 insertions, 1 deletions
diff --git a/Hermes/TODO b/Hermes/TODO
index bff56e6..c8dbc75 100644
--- a/Hermes/TODO
+++ b/Hermes/TODO
@@ -9,6 +9,6 @@
11. user can ask for no of logged in users. Server has to pars
e
12. Hide user's own input in command line
-** 13. Need to gracefully handle disconnected clients by removing from list
+** 13. Need to gracefully handle disconnected clients by removing from list user filter
of connections
14. bye message prompt for clients
diff --git a/Hermes/server.rkt b/Hermes/server.rkt
index 5b599a7..5673eca 100644
--- a/Hermes/server.rkt
+++ b/Hermes/server.rkt
@@ -32,9 +32,19 @@
connections)
(define (cons-list)
connections)
+ (define (remove-ports in out)
+ (set! connections
+ (filter
+ (lambda (ports)
+ (if (and (eq? in (get-input-port ports))
+ (eq? out (get-output-port ports)))
+ #f
+ #t))
+ connections)))
(define (dispatch m)
(cond [(eq? m 'null-cons) null-cons?]
[(eq? m 'cons-list) cons-list]
+ [(eq? m 'remove-ports) remove-ports]
[(eq? m 'add) add]))
dispatch)
(define c-connections (make-connections '()))
@@ -133,6 +143,11 @@
(define (something-to-say in)
(define evt-t0 (sync/timeout 60 (read-line-evt in 'linefeed)))
(cond [(eof-object? evt-t0)
+ ; TODO remove pair of ports associated with client
+ (semaphore-wait connections-s)
+ ((c-connections 'remove-ports) in out)
+ (semaphore-post connections-s)
+
(displayln-safe "Connection closed. EOF received"
stdout)
(semaphore-wait c-count-s)