~edwargix/tallyard

1265e94d9275357c77276b1ddba6c07a3e2e529e — David Florness 5 years ago 581af4a
When a client quits, let the bulletin know
2 files changed, 30 insertions(+), 9 deletions(-)

M bulletin.rkt
M client.rkt
M bulletin.rkt => bulletin.rkt +11 -1
@@ 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?)

M client.rkt => client.rkt +19 -8
@@ 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))