From f8fef5c5ae63c0933f92a25d82da447ff44c2444 Mon Sep 17 00:00:00 2001 From: Ibrahim Mkusa Date: Fri, 14 Apr 2017 00:44:42 -0400 Subject: [PATCH] now handles disconnected clients --- Hermes/TODO | 2 +- Hermes/server.rkt | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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)