cleaned up displaying format, added to delay to looped functions to not

burn cpu cycles
This commit is contained in:
Ibrahim Mkusa 2017-04-12 12:23:26 -04:00
parent 11f4ae1946
commit 282197b144
2 changed files with 10 additions and 9 deletions

View File

@ -1,5 +1,6 @@
#lang racket #lang racket
(require math/base) ;; for random number generation (require math/base) ;; for random number generation
;; TODO clean up string message output and alignment
;; author: Ibrahim Mkusa ;; author: Ibrahim Mkusa
;; about: print and read concurrently ;; about: print and read concurrently
@ -34,28 +35,28 @@
(display in) (display in)
(displayln out) (displayln out)
;; binds to multiple values akin to unpacking tuples in python ;; binds to multiple values akin to unpacking tuples in python
(display "What's your name?\n") (displayln "What's your name?")
(define username (read-line)) (define username (read-line))
; (thread (lambda () ; (thread (lambda ()
;; make threads 2 lines ;; make threads 2 lines
(define a (thread (define a (thread
(lambda () (lambda ()
(displayln "Starting receiver thread\n") (displayln "Starting receiver thread.")
(let loop [] (let loop []
(receive-messages in) (receive-messages in)
(sleep 1) (sleep 1)
(loop))))) (loop)))))
(define t (thread (define t (thread
(lambda () (lambda ()
(displayln "Starting sender thread\n") (displayln "Starting sender thread.")
(let loop [] (let loop []
(send-messages username out) (send-messages username out)
(sleep 1) (sleep 1)
(loop))))) (loop)))))
(displayln "Now waiting for sender thread") (displayln "Now waiting for sender thread.")
(thread-wait t) ;; returns prompt back to drracket (thread-wait t) ;; returns prompt back to drracket
(displayln "Closing client ports") (displayln "Closing client ports.")
(close-input-port in) (close-input-port in)
(close-output-port out)) (close-output-port out))
(custodian-shutdown-all main-client-cust)) (custodian-shutdown-all main-client-cust))
@ -75,7 +76,7 @@
;(kill-thread t)))) ;(kill-thread t))))
(cond ((string=? input "quit") (exit))) (cond ((string=? input "quit") (exit)))
;; modify to send messages to out port ;; modify to send messages to out port
(displayln (string-append username ": " input "\n") out) (displayln (string-append username ": " input) out)
(flush-output out) (flush-output out)
;(semaphore-post fair) ;(semaphore-post fair)
@ -94,7 +95,7 @@
;; we will retrieve the line printed below from the server ;; we will retrieve the line printed below from the server
(define evt (sync/timeout 60 (read-line-evt in))) (define evt (sync/timeout 60 (read-line-evt in)))
(cond [(eof-object? evt) (cond [(eof-object? evt)
(displayln "Server connection closed") (displayln "Server connection closed.")
(custodian-shutdown-all main-client-cust) (custodian-shutdown-all main-client-cust)
;(exit) ;(exit)
] ]
@ -107,5 +108,5 @@
) )
(define stop (client 4321)) (define stop (client 4321))
(display "Client started\n") (displayln "Client started.")

View File

@ -47,7 +47,7 @@
(display "Broadcast thread started!\n") (display "Broadcast thread started!\n")
(semaphore-post stdout) (semaphore-post stdout)
(let loopb [] (let loopb []
; (sleep 0.5) ;; wait 30 secs before beginning to broadcast (sleep 0.5) ;; wait 0.5 secs before beginning to broadcast
(broadcast) (broadcast)
(loopb))))) (loopb)))))
(lambda () (lambda ()