aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIbrahim Mkusa <ibrahimmkusa@gmail.com>2017-04-12 12:57:55 -0400
committerIbrahim Mkusa <ibrahimmkusa@gmail.com>2017-04-12 12:57:55 -0400
commit3a5ce0d2aa1a6b4ad129ff4654baed9022edce42 (patch)
tree3503fcf03f47977593a08ecf4a843f922241255b
parent8984b45ac129ac35c4b05e00d7fbbd89cb086e58 (diff)
refactored code to use thread-safe displayln-safe
-rw-r--r--Hermes/TODO.txt3
-rw-r--r--Hermes/client.rkt6
-rw-r--r--Hermes/server.rkt37
3 files changed, 20 insertions, 26 deletions
diff --git a/Hermes/TODO.txt b/Hermes/TODO.txt
index 9f9ae33..d66b14a 100644
--- a/Hermes/TODO.txt
+++ b/Hermes/TODO.txt
@@ -4,4 +4,5 @@
4. message parsable?
5. command parsable?
6. keep count of connected clients using object orientation
-7.
+7. maybe fiddle around with irc library
+8. separate main running code from definitions
diff --git a/Hermes/client.rkt b/Hermes/client.rkt
index 894c178..064db9e 100644
--- a/Hermes/client.rkt
+++ b/Hermes/client.rkt
@@ -7,12 +7,6 @@
;; 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
-; 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
(define main-client-cust (make-custodian))
diff --git a/Hermes/server.rkt b/Hermes/server.rkt
index a178222..ad84acc 100644
--- a/Hermes/server.rkt
+++ b/Hermes/server.rkt
@@ -23,7 +23,12 @@
;; lets keep things civil
(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
;; Both `server' and `accept-and-handle' change
@@ -43,9 +48,7 @@
(thread loop)
;; Create a thread whose job is to simply call broadcast iteratively
(thread (lambda ()
- (semaphore-wait stdout)
- (display "Broadcast thread started!\n")
- (semaphore-post stdout)
+ (displayln-safe "Broadcast thread started!\n" stdout)
(let loopb []
(sleep 0.5) ;; wait 0.5 secs before beginning to broadcast
(broadcast)
@@ -58,12 +61,11 @@
(define cust (make-custodian))
(parameterize ([current-custodian cust])
(define-values (in out) (tcp-accept listener))
- (semaphore-wait stdout)
- (displayln "Sucessfully connected to a client")
- ;(display in)
- ;(displayln out)
- (displayln "Sending client Welcome message")
- (semaphore-post stdout)
+ ;; TODO
+ (displayln-safe (string-append
+ "Successfully connected to a client.\n"
+ "Sending client a welcome message.")
+ stdout)
(displayln "Welcome to Hermes coms\nType your message below" out)
(flush-output out)
; discard request header
@@ -95,9 +97,9 @@
;; kills current thread for waiting too long for connection from
;; clients
(thread (lambda ()
- (semaphore-wait stdout)
- (display "Started a thread to kill hanging connecting thread\n")
- (semaphore-post stdout)
+ (displayln-safe (string-append
+ "Started a thread to kill hanging "
+ "connecting threads") stdout)
(sleep 1360)
(custodian-shutdown-all cust)))))
@@ -109,9 +111,8 @@
(define (something-to-say in)
(define evt-t0 (sync/timeout 60 (read-line-evt in 'linefeed)))
(cond [(eof-object? evt-t0)
- (semaphore-wait stdout)
- (displayln "Connection closed. EOF received")
- (semaphore-post stdout)
+ (displayln-safe "Connection closed. EOF received"
+ stdout)
(exit)
]
[(string? evt-t0)
@@ -121,9 +122,7 @@
(set! messages (append messages (list evt-t0)))
(semaphore-post messages-s)]
[else
- (semaphore-wait stdout)
- (displayln "Timeout waiting. Nothing received from client")
- (semaphore-post stdout)]))
+ (displayln-safe "Timeout waiting. Nothing received from client" stdout)]))
; -----NO LONGER NECESSARY not using thread mailboxes ----
; define function to deal with out