refactored code to use thread-safe displayln-safe
This commit is contained in:
parent
8984b45ac1
commit
3a5ce0d2aa
@ -4,4 +4,5 @@
|
|||||||
4. message parsable?
|
4. message parsable?
|
||||||
5. command parsable?
|
5. command parsable?
|
||||||
6. keep count of connected clients using object orientation
|
6. keep count of connected clients using object orientation
|
||||||
7.
|
7. maybe fiddle around with irc library
|
||||||
|
8. separate main running code from definitions
|
||||||
|
@ -7,12 +7,6 @@
|
|||||||
;; look into
|
;; look into
|
||||||
;; https://docs.racket-lang.org/gui/text-field_.html#%28meth._%28%28%28lib._mred%2Fmain..rkt%29._text-field~25%29._get-editor%29%29
|
;; https://docs.racket-lang.org/gui/text-field_.html#%28meth._%28%28%28lib._mred%2Fmain..rkt%29._text-field~25%29._get-editor%29%29
|
||||||
|
|
||||||
; Takes a string and a semaphore to print safely to stdout
|
|
||||||
(define displayln-safe
|
|
||||||
(lambda (a-string a-semaphore)
|
|
||||||
(semaphore-wait a-semaphore)
|
|
||||||
(displayln a-string)
|
|
||||||
(semaphore-post a-semaphore)))
|
|
||||||
|
|
||||||
; custodian for client connections
|
; custodian for client connections
|
||||||
(define main-client-cust (make-custodian))
|
(define main-client-cust (make-custodian))
|
||||||
|
@ -23,7 +23,12 @@
|
|||||||
;; lets keep things civil
|
;; lets keep things civil
|
||||||
(define stdout (make-semaphore 1))
|
(define stdout (make-semaphore 1))
|
||||||
|
|
||||||
;;
|
; Takes a string and a semaphore to print safely to stdout
|
||||||
|
(define displayln-safe
|
||||||
|
(lambda (a-string a-semaphore)
|
||||||
|
(semaphore-wait a-semaphore)
|
||||||
|
(displayln a-string)
|
||||||
|
(semaphore-post a-semaphore)))
|
||||||
|
|
||||||
;; This is a relay server making two clients communicate
|
;; This is a relay server making two clients communicate
|
||||||
;; Both `server' and `accept-and-handle' change
|
;; Both `server' and `accept-and-handle' change
|
||||||
@ -43,9 +48,7 @@
|
|||||||
(thread loop)
|
(thread loop)
|
||||||
;; Create a thread whose job is to simply call broadcast iteratively
|
;; Create a thread whose job is to simply call broadcast iteratively
|
||||||
(thread (lambda ()
|
(thread (lambda ()
|
||||||
(semaphore-wait stdout)
|
(displayln-safe "Broadcast thread started!\n" stdout)
|
||||||
(display "Broadcast thread started!\n")
|
|
||||||
(semaphore-post stdout)
|
|
||||||
(let loopb []
|
(let loopb []
|
||||||
(sleep 0.5) ;; wait 0.5 secs before beginning to broadcast
|
(sleep 0.5) ;; wait 0.5 secs before beginning to broadcast
|
||||||
(broadcast)
|
(broadcast)
|
||||||
@ -58,12 +61,11 @@
|
|||||||
(define cust (make-custodian))
|
(define cust (make-custodian))
|
||||||
(parameterize ([current-custodian cust])
|
(parameterize ([current-custodian cust])
|
||||||
(define-values (in out) (tcp-accept listener))
|
(define-values (in out) (tcp-accept listener))
|
||||||
(semaphore-wait stdout)
|
;; TODO
|
||||||
(displayln "Sucessfully connected to a client")
|
(displayln-safe (string-append
|
||||||
;(display in)
|
"Successfully connected to a client.\n"
|
||||||
;(displayln out)
|
"Sending client a welcome message.")
|
||||||
(displayln "Sending client Welcome message")
|
stdout)
|
||||||
(semaphore-post stdout)
|
|
||||||
(displayln "Welcome to Hermes coms\nType your message below" out)
|
(displayln "Welcome to Hermes coms\nType your message below" out)
|
||||||
(flush-output out)
|
(flush-output out)
|
||||||
; discard request header
|
; discard request header
|
||||||
@ -95,9 +97,9 @@
|
|||||||
;; kills current thread for waiting too long for connection from
|
;; kills current thread for waiting too long for connection from
|
||||||
;; clients
|
;; clients
|
||||||
(thread (lambda ()
|
(thread (lambda ()
|
||||||
(semaphore-wait stdout)
|
(displayln-safe (string-append
|
||||||
(display "Started a thread to kill hanging connecting thread\n")
|
"Started a thread to kill hanging "
|
||||||
(semaphore-post stdout)
|
"connecting threads") stdout)
|
||||||
(sleep 1360)
|
(sleep 1360)
|
||||||
(custodian-shutdown-all cust)))))
|
(custodian-shutdown-all cust)))))
|
||||||
|
|
||||||
@ -109,9 +111,8 @@
|
|||||||
(define (something-to-say in)
|
(define (something-to-say in)
|
||||||
(define evt-t0 (sync/timeout 60 (read-line-evt in 'linefeed)))
|
(define evt-t0 (sync/timeout 60 (read-line-evt in 'linefeed)))
|
||||||
(cond [(eof-object? evt-t0)
|
(cond [(eof-object? evt-t0)
|
||||||
(semaphore-wait stdout)
|
(displayln-safe "Connection closed. EOF received"
|
||||||
(displayln "Connection closed. EOF received")
|
stdout)
|
||||||
(semaphore-post stdout)
|
|
||||||
(exit)
|
(exit)
|
||||||
]
|
]
|
||||||
[(string? evt-t0)
|
[(string? evt-t0)
|
||||||
@ -121,9 +122,7 @@
|
|||||||
(set! messages (append messages (list evt-t0)))
|
(set! messages (append messages (list evt-t0)))
|
||||||
(semaphore-post messages-s)]
|
(semaphore-post messages-s)]
|
||||||
[else
|
[else
|
||||||
(semaphore-wait stdout)
|
(displayln-safe "Timeout waiting. Nothing received from client" stdout)]))
|
||||||
(displayln "Timeout waiting. Nothing received from client")
|
|
||||||
(semaphore-post stdout)]))
|
|
||||||
|
|
||||||
; -----NO LONGER NECESSARY not using thread mailboxes ----
|
; -----NO LONGER NECESSARY not using thread mailboxes ----
|
||||||
; define function to deal with out
|
; define function to deal with out
|
||||||
|
Loading…
x
Reference in New Issue
Block a user