From e09975a02c24c53a6b8c4a704b74236945c3fdcc Mon Sep 17 00:00:00 2001 From: Ibrahim Mkusa Date: Fri, 14 Apr 2017 09:57:40 -0400 Subject: tidied up --- Hermes/concurrentreadandprint.rkt | 75 ----------------------------------- Hermes/tcpcommunication.rkt | 60 ---------------------------- tests/gui/concurrentreadandprint.rkt | 75 +++++++++++++++++++++++++++++++++++ tests/tcpvanilla/tcpcommunication.rkt | 60 ++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 135 deletions(-) delete mode 100644 Hermes/concurrentreadandprint.rkt delete mode 100644 Hermes/tcpcommunication.rkt create mode 100644 tests/gui/concurrentreadandprint.rkt create mode 100644 tests/tcpvanilla/tcpcommunication.rkt diff --git a/Hermes/concurrentreadandprint.rkt b/Hermes/concurrentreadandprint.rkt deleted file mode 100644 index 95d02c1..0000000 --- a/Hermes/concurrentreadandprint.rkt +++ /dev/null @@ -1,75 +0,0 @@ -#lang racket -(require math/base) ;; for random number generation - -;; a proof of concept -;; one thread waits for input -;; another displays messages in the background - - -;; create custodian for managing all resources -;; so we can shutdown everything at once -;(define guard (make-custodian (current-custodian))) -;(current-custodian guard) -;; 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) - - - ;(semaphore-wait fair) - (display usernamei) - (define input (read-line)) - ;; do something over here with input maybe send it out - - ;; Tests input if its a quit then kills all threads - ;; An if would be better here tbh - (cond ((string=? input "quit") (begin (kill-thread a) - (kill-thread t)))) - (display (string-append output-prompt input "\n")) - ;(semaphore-post fair) - (read-loop-i) - ) - - -;; print hello world continously -;; "(hello-world)" can be executed as part of background thread -;; that prints in the event there is something in the input port -(define (hello-world) - (sleep (random-integer 0 15)) ;; sleep between 0 and 15 seconds to simulate coms - ;; with server - ;(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) - (hello-world)) - -(define t (thread (lambda () - (read-loop-i)))) -(define a (thread (lambda () - (hello-world)))) - -(thread-wait t) ;; returns prompt back to drracket -;; 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") diff --git a/Hermes/tcpcommunication.rkt b/Hermes/tcpcommunication.rkt deleted file mode 100644 index 134e697..0000000 --- a/Hermes/tcpcommunication.rkt +++ /dev/null @@ -1,60 +0,0 @@ -#lang racket -;; Reads input iteratively then sends it to local server -;; client reads back the message and displays it - -(require math/base) ;; for random number generation - -(define listener (tcp-listen 4326 5 #t)) -(define a (thread (lambda () - (define-values (s-in s-out) (tcp-accept listener)) - ; Discard the request header (up to blank line): - ;(regexp-match #rx"(\r\n|^)\r\n" s-in) - (sleep 10) - (define (echo) - (define input (read-line s-in)) - (displayln input s-out) - (flush-output s-out) - (if (eof-object? input) - (displayln "Done talking\n") - (echo))) - (echo) - (close-input-port s-in) - (close-output-port s-out) - (tcp-close listener) - 'ok))) - -(define t (thread (lambda () - (define-values (c-in c-out) (tcp-connect "localhost" 4326)) - (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) - ;(semaphore-wait fair) - ; (display usernamei) - (define input (read-line)) - ;; do something over here with input maybe send it out - - ;; Tests input if its a quit then kills all threads - ;; An if would be better here tbh - (cond ((string=? input "quit") (exit))) - (display (string-append output-prompt input "\n") c-out) - (flush-output c-out) - (displayln (read-line c-in)) ;; server echoes back sent input - ;(semaphore-post fair) - (read-loop-i) - ) - (read-loop-i) - 'ok))) - -;(kill-thread a) -;(kill-thread t) -(thread-wait t) -(display "DONE!!\n") - diff --git a/tests/gui/concurrentreadandprint.rkt b/tests/gui/concurrentreadandprint.rkt new file mode 100644 index 0000000..95d02c1 --- /dev/null +++ b/tests/gui/concurrentreadandprint.rkt @@ -0,0 +1,75 @@ +#lang racket +(require math/base) ;; for random number generation + +;; a proof of concept +;; one thread waits for input +;; another displays messages in the background + + +;; create custodian for managing all resources +;; so we can shutdown everything at once +;(define guard (make-custodian (current-custodian))) +;(current-custodian guard) +;; 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) + + + ;(semaphore-wait fair) + (display usernamei) + (define input (read-line)) + ;; do something over here with input maybe send it out + + ;; Tests input if its a quit then kills all threads + ;; An if would be better here tbh + (cond ((string=? input "quit") (begin (kill-thread a) + (kill-thread t)))) + (display (string-append output-prompt input "\n")) + ;(semaphore-post fair) + (read-loop-i) + ) + + +;; print hello world continously +;; "(hello-world)" can be executed as part of background thread +;; that prints in the event there is something in the input port +(define (hello-world) + (sleep (random-integer 0 15)) ;; sleep between 0 and 15 seconds to simulate coms + ;; with server + ;(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) + (hello-world)) + +(define t (thread (lambda () + (read-loop-i)))) +(define a (thread (lambda () + (hello-world)))) + +(thread-wait t) ;; returns prompt back to drracket +;; 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") diff --git a/tests/tcpvanilla/tcpcommunication.rkt b/tests/tcpvanilla/tcpcommunication.rkt new file mode 100644 index 0000000..134e697 --- /dev/null +++ b/tests/tcpvanilla/tcpcommunication.rkt @@ -0,0 +1,60 @@ +#lang racket +;; Reads input iteratively then sends it to local server +;; client reads back the message and displays it + +(require math/base) ;; for random number generation + +(define listener (tcp-listen 4326 5 #t)) +(define a (thread (lambda () + (define-values (s-in s-out) (tcp-accept listener)) + ; Discard the request header (up to blank line): + ;(regexp-match #rx"(\r\n|^)\r\n" s-in) + (sleep 10) + (define (echo) + (define input (read-line s-in)) + (displayln input s-out) + (flush-output s-out) + (if (eof-object? input) + (displayln "Done talking\n") + (echo))) + (echo) + (close-input-port s-in) + (close-output-port s-out) + (tcp-close listener) + 'ok))) + +(define t (thread (lambda () + (define-values (c-in c-out) (tcp-connect "localhost" 4326)) + (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) + ;(semaphore-wait fair) + ; (display usernamei) + (define input (read-line)) + ;; do something over here with input maybe send it out + + ;; Tests input if its a quit then kills all threads + ;; An if would be better here tbh + (cond ((string=? input "quit") (exit))) + (display (string-append output-prompt input "\n") c-out) + (flush-output c-out) + (displayln (read-line c-in)) ;; server echoes back sent input + ;(semaphore-post fair) + (read-loop-i) + ) + (read-loop-i) + 'ok))) + +;(kill-thread a) +;(kill-thread t) +(thread-wait t) +(display "DONE!!\n") + -- cgit v1.2.3