2017-04-05 19:30:10 +00:00
|
|
|
#lang racket
|
2017-04-05 20:01:17 +00:00
|
|
|
(require math/base) ;; for random number generation
|
|
|
|
|
2017-04-05 20:22:51 +00:00
|
|
|
|
2017-04-05 19:30:10 +00:00
|
|
|
;; author: Ibrahim Mkusa
|
|
|
|
;; about: print and read concurrently
|
2017-04-05 20:22:51 +00:00
|
|
|
;; notes: output may need to be aligned and formatted nicely
|
2017-04-05 20:39:47 +00:00
|
|
|
;; 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
|
2017-04-05 19:30:10 +00:00
|
|
|
|
2017-04-05 19:46:50 +00:00
|
|
|
;; create custodian for managing all resources
|
|
|
|
;; so we can shutdown everything at once
|
2017-04-05 20:39:47 +00:00
|
|
|
;(define guard (make-custodian (current-custodian)))
|
|
|
|
;(current-custodian guard)
|
2017-04-05 19:30:10 +00:00
|
|
|
;; reads values continously from stdin and redisplays them
|
|
|
|
(define (read-loop)
|
|
|
|
(display (read-line))
|
|
|
|
(display "\n")
|
|
|
|
(read-loop)
|
|
|
|
)
|
|
|
|
|
|
|
|
(define input-prompt "input: ")
|
|
|
|
(define output-prompt "output: ")
|
|
|
|
|
|
|
|
;; prompt for username and bind to a variable username
|
|
|
|
(display "What's your name?\n")
|
|
|
|
(define username (read-line))
|
|
|
|
(define usernamei (string-append username ": ")) ;; make username appear nicer in a prompt
|
|
|
|
(define fair (make-semaphore 1))
|
|
|
|
|
|
|
|
;; intelligent read, quits when user types in "quit"
|
|
|
|
(define (read-loop-i)
|
2017-04-05 20:01:17 +00:00
|
|
|
|
2017-04-05 19:30:10 +00:00
|
|
|
|
2017-04-05 20:22:51 +00:00
|
|
|
;(semaphore-wait fair)
|
2017-04-05 20:01:17 +00:00
|
|
|
(display usernamei)
|
2017-04-05 19:30:10 +00:00
|
|
|
(define input (read-line))
|
|
|
|
;; do something over here with input maybe send it out
|
2017-04-05 20:01:17 +00:00
|
|
|
|
|
|
|
;; Tests input if its a quit then kills all threads
|
|
|
|
;; An if would be better here tbh
|
2017-04-05 19:46:50 +00:00
|
|
|
(cond ((string=? input "quit") (begin (kill-thread a)
|
|
|
|
(kill-thread t))))
|
2017-04-05 19:30:10 +00:00
|
|
|
(display (string-append output-prompt input "\n"))
|
2017-04-05 20:22:51 +00:00
|
|
|
;(semaphore-post fair)
|
2017-04-05 19:30:10 +00:00
|
|
|
(read-loop-i)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
;; print hello world continously
|
2017-04-05 20:01:17 +00:00
|
|
|
;; "(hello-world)" can be executed as part of background thread
|
|
|
|
;; that prints in the event there is something in the input port
|
2017-04-05 19:30:10 +00:00
|
|
|
(define (hello-world)
|
2017-04-05 20:22:51 +00:00
|
|
|
(sleep (random-integer 0 15)) ;; sleep between 0 and 15 seconds to simulate coms
|
2017-04-05 20:01:17 +00:00
|
|
|
;; with server
|
2017-04-05 20:22:51 +00:00
|
|
|
;(semaphore-wait fair)
|
|
|
|
;; we will retrieve the line printed below from the server
|
|
|
|
;; at this time we simulate the input from different users
|
|
|
|
(define what-to-print (random-integer 0 2))
|
|
|
|
(if (= what-to-print 0)
|
|
|
|
(display "Doug: What's up, up?\n")
|
|
|
|
(display "Fred: Looking good, good!\n"))
|
|
|
|
;(semaphore-post fair)
|
2017-04-05 19:30:10 +00:00
|
|
|
(hello-world))
|
|
|
|
|
|
|
|
(define t (thread (lambda ()
|
|
|
|
(read-loop-i))))
|
|
|
|
(define a (thread (lambda ()
|
|
|
|
(hello-world))))
|
2017-04-05 19:46:50 +00:00
|
|
|
|
|
|
|
(thread-wait t) ;; returns prompt back to drracket
|
2017-04-05 19:30:10 +00:00
|
|
|
;; below doesn't execute
|
|
|
|
; (sleep 10)
|
|
|
|
; (kill-thread t)
|
|
|
|
; (define a (thread (display "hello world!\n")))
|
|
|
|
; (display "John: hello soso\n")
|
|
|
|
; (display "Emmanuel: cumbaya!!!!\n")
|