From 1265e94d9275357c77276b1ddba6c07a3e2e529e Mon Sep 17 00:00:00 2001 From: David Florness Date: Tue, 10 Mar 2020 16:00:41 -0600 Subject: [PATCH] When a client quits, let the bulletin know --- bulletin.rkt | 12 +++++++++++- client.rkt | 27 +++++++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/bulletin.rkt b/bulletin.rkt index 1b65e45..7bc42b0 100644 --- a/bulletin.rkt +++ b/bulletin.rkt @@ -185,6 +185,15 @@ "Too early to request sums" #:code 403)))) + (define (quit request) + (let* ([token (string->symbol + (hash-ref + (bytes->jsexpr (request-post-data/raw request)) + 'token))] + [username (hash-ref (hash-ref _peers token) 'username)]) + (displayln (format "~a quit!" username)) + (hash-remove! _peers token))) + (define-values (dispatch-route url-generator) (dispatch-rules [("register") #:method "post" register ] @@ -195,7 +204,8 @@ [("outputs") #:method "post" outputs ] [("values") #:method "post" _values ] [("sum") #:method "post" sum ] - [("sums") #:method "get" sums ])) + [("sums") #:method "get" sums ] + [("quit") #:method "post" quit ])) (define/contract (internal-server-error url ex) (url? any/c . -> . can-be-response?) diff --git a/client.rkt b/client.rkt index 391bd00..a4f1652 100644 --- a/client.rkt +++ b/client.rkt @@ -72,13 +72,6 @@ [point : Integer] [bulletin : requester] [token : JSExpr]) - (display "retrieving candidates...") - (define candidates (cast (json-response-body - (retry-request (get bulletin "/candidates"))) - (Listof String))) - (define cand-count (cast (length candidates) Positive-Integer)) - (newline) - (display "waiting for voting to commence...") (define peer-count (cast (json-response-body @@ -86,6 +79,15 @@ Positive-Integer)) (newline) + (break-enabled #f) ; voting has begun! + + (display "retrieving candidates...") + (define candidates (cast (json-response-body + (retry-request (get bulletin "/candidates"))) + (Listof String))) + (define cand-count (cast (length candidates) Positive-Integer)) + (newline) + (define vote (let* ([vote (get-vote candidates)] [m cand-count] @@ -170,6 +172,8 @@ (Listof String)))))))) (newline) + (break-enabled #t) ; it's ok if the client quits at this point + (display "finding constant...") (define constant (let ([k (length peer-inputs)] @@ -331,7 +335,14 @@ ; the bulletin can have multiple elections, so loop (let loop () (with-handlers - ([exn:fail:network:http:error? + ([exn:break? + (λ ([ex : exn:break]) + (post bulletin "/quit" + #:data + (jsexpr->string + (hasheq + 'token token))))] + [exn:fail:network:http:error? (λ ([ex : exn:fail:network:http:error]) (break-enabled #t) (if (eqv? 410 (exn:fail:network:http:error-code ex)) -- 2.38.4