From 49a079922ebe30ea76ca00fc874cfa4017b1a510 Mon Sep 17 00:00:00 2001 From: Ibrahim Mkusa Date: Thu, 30 Mar 2017 00:44:40 -0400 Subject: reorganized some stuff, started experimenting with racket/gui --- feasibility_analysis/client.rkt | 72 ---------------------------- feasibility_analysis/client2.rkt | 43 ----------------- feasibility_analysis/gui/windows.rkt | 9 ++++ feasibility_analysis/server.rkt | 58 ---------------------- feasibility_analysis/tcpevents/README.md | 1 + feasibility_analysis/tcpevents/server.rkt | 39 +++++++++++++++ feasibility_analysis/tcptalk.rkt | 30 ------------ feasibility_analysis/tcpvanilla/client.rkt | 72 ++++++++++++++++++++++++++++ feasibility_analysis/tcpvanilla/client2.rkt | 43 +++++++++++++++++ feasibility_analysis/tcpvanilla/server.rkt | 58 ++++++++++++++++++++++ feasibility_analysis/tcpvanilla/tcptalk.rkt | 30 ++++++++++++ feasibility_analysis/tcpvanilla/testdrive.md | 20 ++++++++ feasibility_analysis/testdrive.md | 20 -------- 13 files changed, 272 insertions(+), 223 deletions(-) delete mode 100644 feasibility_analysis/client.rkt delete mode 100644 feasibility_analysis/client2.rkt create mode 100644 feasibility_analysis/gui/windows.rkt delete mode 100644 feasibility_analysis/server.rkt create mode 100644 feasibility_analysis/tcpevents/README.md create mode 100644 feasibility_analysis/tcpevents/server.rkt delete mode 100644 feasibility_analysis/tcptalk.rkt create mode 100644 feasibility_analysis/tcpvanilla/client.rkt create mode 100644 feasibility_analysis/tcpvanilla/client2.rkt create mode 100644 feasibility_analysis/tcpvanilla/server.rkt create mode 100644 feasibility_analysis/tcpvanilla/tcptalk.rkt create mode 100644 feasibility_analysis/tcpvanilla/testdrive.md delete mode 100644 feasibility_analysis/testdrive.md (limited to 'feasibility_analysis') diff --git a/feasibility_analysis/client.rkt b/feasibility_analysis/client.rkt deleted file mode 100644 index 967d2b9..0000000 --- a/feasibility_analysis/client.rkt +++ /dev/null @@ -1,72 +0,0 @@ -#lang racket - -;; Both `server' and `accept-and-handle' change -;; to use a custodian. -;; To start server -;; (define stop (client 8080)) -;; use your web browser to connect localhost:8080 greeted with "hello world" -;; (stop) to close the server - -(define (client port-no) - (define main-client-cust (make-custodian)) - (parameterize ([current-custodian main-client-cust]) - ;; connect to server at port 8080 - (define-values (in out) (tcp-connect "localhost" port-no)) ;; define values - ;; binds to multiple values akin to unpacking tuples in python - ; (thread (lambda () - (chat in out) - (close-input-port in) - (close-output-port out)) - (custodian-shutdown-all main-client-cust)) - - ; (sleep 60) ;; run for 3 minutes then close - ; (define (loop) - ; (write (read-line (current-input-port)) out) - ; (flush-output out) - ; (write (read-line in) (current-output-port)) - ; (define listener (tcp-listen port-no 5 #t)) - ; (define (loop) - ; (accept-and-handle listener) - ; (loop)) - ; (thread loop))) - ; (custodian-shutdown-all main-client-cust) - #| (lambda () |# - ; (displayln "Goodbye, shutting down client\n") - #| (custodian-shutdown-all main-client-cust)) |# - -(define (chat in out) - ; (driver-loop in out) - (writeln "Ibrahim: Hello, anyone in chat?" out) - (flush-output out) ;; ports are buffered in racket must flush or you - ;; will read #eof - (sleep 10) ;; wait 10 seconds - (define serv-message (read-line in)) - (displayln serv-message) ;; read the servers replay message which is original - ;; with echo appended to it - ) - -; (define input-prompt "Hermes: ") - -(define (driver-loop in out) - ; (prompt-for-input input-prompt) - (display ">>> ") - (define input (read)) - (writeln (string-append "Ibrahim: " input) out) - (flush-output out) - ; (sleep 10) - (define output (read-line in)) - (displayln output) - (driver-loop in out)) - - -#| (let ((input (read))) |# -; ) -; (let ((input (read))) -; (let ((output (mc-eval input the-global-environment))) -; (announce-output output-prompt) -; (user-print output))) -; (driver-loop)) -; -; (define (announce-output string) -; (display string)) -#| |# diff --git a/feasibility_analysis/client2.rkt b/feasibility_analysis/client2.rkt deleted file mode 100644 index 47e3052..0000000 --- a/feasibility_analysis/client2.rkt +++ /dev/null @@ -1,43 +0,0 @@ -#lang racket - -;; Both `server' and `accept-and-handle' change -;; to use a custodian. -;; To start server -;; (define stop (client 8080)) -;; use your web browser to connect localhost:8080 greeted with "hello world" -;; (stop) to close the server - -(define (client port-no) - (define main-client-cust (make-custodian)) - (parameterize ([current-custodian main-client-cust]) - ;; connect to server at port 8080 - (define-values (in out) (tcp-connect "localhost" port-no)) ;; define values - ;; binds to multiple values akin to unpacking tuples in python - (thread (lambda () - (send-message in out) - (close-input-port in) - (close-output-port out)))) - (sleep 20) - ; (define (loop) - ; (write (read-line (current-input-port)) out) - ; (flush-output out) - ; (write (read-line in) (current-output-port)) - ; (define listener (tcp-listen port-no 5 #t)) - ; (define (loop) - ; (accept-and-handle listener) - ; (loop)) - ; (thread loop))) - (custodian-shutdown-all main-client-cust) - #| (lambda () |# - ; (displayln "Goodbye, shutting down client\n") - #| (custodian-shutdown-all main-client-cust)) |#) - -(define (send-message input-port output-port) - (writeln "Doug: Hello, how's it going?" output-port) - (flush-output output-port) ;; ports are buffered in racket must flush or you - ;; will read #eof - (sleep 10) ;; wait 10 seconds - (define serv-message (read-line input-port)) - (displayln serv-message) ;; read the servers replay message which is original - ;; with echo appended to it - ) diff --git a/feasibility_analysis/gui/windows.rkt b/feasibility_analysis/gui/windows.rkt new file mode 100644 index 0000000..4524673 --- /dev/null +++ b/feasibility_analysis/gui/windows.rkt @@ -0,0 +1,9 @@ +#lang racket + +(require racket/gui/base) + +;; Create a new window via the frame class +(define frame (new frame% [label "Example"])) + +;; Show frame(window) by calling it show method +(send frame show #t) ;; you call object methods via send diff --git a/feasibility_analysis/server.rkt b/feasibility_analysis/server.rkt deleted file mode 100644 index bf72aff..0000000 --- a/feasibility_analysis/server.rkt +++ /dev/null @@ -1,58 +0,0 @@ -#lang racket - -;; Both `server' and `accept-and-handle' change -;; to use a custodian. -;; To start server -;; (define stop (serve 8080)) -;; use your web browser to connect localhost:8080 greeted with "hello world" -;; (stop) to close the server - -(define (serve port-no) - (define main-cust (make-custodian)) - (parameterize ([current-custodian main-cust]) - (define listener (tcp-listen port-no 5 #t)) - (define (loop) - (accept-and-handle listener) - (loop)) - (thread loop)) - (lambda () - (displayln "\nGoodbye, shutting down all services\n") - (custodian-shutdown-all main-cust))) - -(define (accept-and-handle listener) - (define cust (make-custodian)) - (parameterize ([current-custodian cust]) - (define-values (in out) (tcp-accept listener)) - (thread (lambda () - (handle in out) ;; this handles connection with that specific client - (close-input-port in) - (close-output-port out)))) - ;; Watcher thread: - (thread (lambda () - (sleep 120) - (custodian-shutdown-all cust)))) - -(define (handle in out) - ; (server-loop in out) - (sleep 5) ;; wait 5 seconds to guarantee client has already send message - (define echo (read-line in)) ;; bind message to echo - (displayln (string-append echo "\n")) - ; echo back the message, appending echo - ; could regex match the input to extract the name - (writeln "Admin: Hello there" out) ;; append "echo " to echo and send back - (flush-output out) -) - -(define input-prompt "Hermes: ") - -(define (server-loop in out) - (define echo (read-line in)) - (displayln echo) - (display ">>> ") - - (define input (read)) - (writeln (string-append "Admin: " input) out) - (flush-output out) - ; (sleep 10) - (server-loop in out)) - diff --git a/feasibility_analysis/tcpevents/README.md b/feasibility_analysis/tcpevents/README.md new file mode 100644 index 0000000..7c3ec21 --- /dev/null +++ b/feasibility_analysis/tcpevents/README.md @@ -0,0 +1 @@ +TCP communication racket concepts of events. For more see the racket guide. diff --git a/feasibility_analysis/tcpevents/server.rkt b/feasibility_analysis/tcpevents/server.rkt new file mode 100644 index 0000000..4313bda --- /dev/null +++ b/feasibility_analysis/tcpevents/server.rkt @@ -0,0 +1,39 @@ +#lang racket + +(define (serve in-port out-port) + (let loop [] + (define evt (sync/timeout 2 + (read-line-evt in-port 'any) + (thread-receive-evt))) + (cond + [(not evt) + (displayln "Timed out, exiting") + (tcp-abandon-port in-port) + (tcp-abandon-port out-port)] + [(string? evt) + (fprintf out-port "~a~n" evt) + (flush-output out-port) + (loop)] + [else + (printf "Received a message in mailbox: ~a~n" + (thread-receive)) + (loop)]))) + +(define port-num 4321) +(define (start-server) + (define listener (tcp-listen port-num)) + (thread + (lambda () + (define-values [in-port out-port] (tcp-accept listener)) + (serve in-port out-port)))) + +(start-server) + +(define client-thread + (thread + (lambda () + (define-values [in-port out-port] (tcp-connect "localhost" port-num)) + (display "first\nsecond\nthird\n" out-port) + (flush-output out-port) + ; copy-port will block until EOF is read from in-port + (copy-port in-port (current-output-port))))) diff --git a/feasibility_analysis/tcptalk.rkt b/feasibility_analysis/tcptalk.rkt deleted file mode 100644 index d069851..0000000 --- a/feasibility_analysis/tcptalk.rkt +++ /dev/null @@ -1,30 +0,0 @@ -#lang racket - -(define listener (tcp-listen 8083 5 #t)) ;; listener to service connection requests -;; client attempts to connect. Receives an input and output port -(define-values (client-in client-out) (tcp-connect "localhost" 8083)) -;; server accepts the connection request. Also gets a pair of ports -(define-values (server-in server-out) (tcp-accept listener)) - -;; client sends identifying message -(display (string-append "Client:My name is " "Ibrahim" "\n") - client-out) -(flush-output client-out) ;; must flush as ports are buffered in racket - -;; server receives and reads it -;; cooler if on separate racket instances -(read-line server-in) ;; --> "Client:My name is #hostname. -;; server replies -(display (string-append "Server:Hi " "Ibrahim" "\n") server-out) -(flush-output server-out) ;; flush flush - -;; client displays server message -(read-line client-in) -(close-output-port server-out) -(close-output-port client-out) -(read-line client-in) ;; --> eof object #eof -(read-line server-in) ;; --> eof object #eof -(tcp-close listener) -; (custodian-shutdown-all (current-custodian)) ;; release all resources including - ;; tcp, file, custom ports - ;; application exits diff --git a/feasibility_analysis/tcpvanilla/client.rkt b/feasibility_analysis/tcpvanilla/client.rkt new file mode 100644 index 0000000..967d2b9 --- /dev/null +++ b/feasibility_analysis/tcpvanilla/client.rkt @@ -0,0 +1,72 @@ +#lang racket + +;; Both `server' and `accept-and-handle' change +;; to use a custodian. +;; To start server +;; (define stop (client 8080)) +;; use your web browser to connect localhost:8080 greeted with "hello world" +;; (stop) to close the server + +(define (client port-no) + (define main-client-cust (make-custodian)) + (parameterize ([current-custodian main-client-cust]) + ;; connect to server at port 8080 + (define-values (in out) (tcp-connect "localhost" port-no)) ;; define values + ;; binds to multiple values akin to unpacking tuples in python + ; (thread (lambda () + (chat in out) + (close-input-port in) + (close-output-port out)) + (custodian-shutdown-all main-client-cust)) + + ; (sleep 60) ;; run for 3 minutes then close + ; (define (loop) + ; (write (read-line (current-input-port)) out) + ; (flush-output out) + ; (write (read-line in) (current-output-port)) + ; (define listener (tcp-listen port-no 5 #t)) + ; (define (loop) + ; (accept-and-handle listener) + ; (loop)) + ; (thread loop))) + ; (custodian-shutdown-all main-client-cust) + #| (lambda () |# + ; (displayln "Goodbye, shutting down client\n") + #| (custodian-shutdown-all main-client-cust)) |# + +(define (chat in out) + ; (driver-loop in out) + (writeln "Ibrahim: Hello, anyone in chat?" out) + (flush-output out) ;; ports are buffered in racket must flush or you + ;; will read #eof + (sleep 10) ;; wait 10 seconds + (define serv-message (read-line in)) + (displayln serv-message) ;; read the servers replay message which is original + ;; with echo appended to it + ) + +; (define input-prompt "Hermes: ") + +(define (driver-loop in out) + ; (prompt-for-input input-prompt) + (display ">>> ") + (define input (read)) + (writeln (string-append "Ibrahim: " input) out) + (flush-output out) + ; (sleep 10) + (define output (read-line in)) + (displayln output) + (driver-loop in out)) + + +#| (let ((input (read))) |# +; ) +; (let ((input (read))) +; (let ((output (mc-eval input the-global-environment))) +; (announce-output output-prompt) +; (user-print output))) +; (driver-loop)) +; +; (define (announce-output string) +; (display string)) +#| |# diff --git a/feasibility_analysis/tcpvanilla/client2.rkt b/feasibility_analysis/tcpvanilla/client2.rkt new file mode 100644 index 0000000..47e3052 --- /dev/null +++ b/feasibility_analysis/tcpvanilla/client2.rkt @@ -0,0 +1,43 @@ +#lang racket + +;; Both `server' and `accept-and-handle' change +;; to use a custodian. +;; To start server +;; (define stop (client 8080)) +;; use your web browser to connect localhost:8080 greeted with "hello world" +;; (stop) to close the server + +(define (client port-no) + (define main-client-cust (make-custodian)) + (parameterize ([current-custodian main-client-cust]) + ;; connect to server at port 8080 + (define-values (in out) (tcp-connect "localhost" port-no)) ;; define values + ;; binds to multiple values akin to unpacking tuples in python + (thread (lambda () + (send-message in out) + (close-input-port in) + (close-output-port out)))) + (sleep 20) + ; (define (loop) + ; (write (read-line (current-input-port)) out) + ; (flush-output out) + ; (write (read-line in) (current-output-port)) + ; (define listener (tcp-listen port-no 5 #t)) + ; (define (loop) + ; (accept-and-handle listener) + ; (loop)) + ; (thread loop))) + (custodian-shutdown-all main-client-cust) + #| (lambda () |# + ; (displayln "Goodbye, shutting down client\n") + #| (custodian-shutdown-all main-client-cust)) |#) + +(define (send-message input-port output-port) + (writeln "Doug: Hello, how's it going?" output-port) + (flush-output output-port) ;; ports are buffered in racket must flush or you + ;; will read #eof + (sleep 10) ;; wait 10 seconds + (define serv-message (read-line input-port)) + (displayln serv-message) ;; read the servers replay message which is original + ;; with echo appended to it + ) diff --git a/feasibility_analysis/tcpvanilla/server.rkt b/feasibility_analysis/tcpvanilla/server.rkt new file mode 100644 index 0000000..bf72aff --- /dev/null +++ b/feasibility_analysis/tcpvanilla/server.rkt @@ -0,0 +1,58 @@ +#lang racket + +;; Both `server' and `accept-and-handle' change +;; to use a custodian. +;; To start server +;; (define stop (serve 8080)) +;; use your web browser to connect localhost:8080 greeted with "hello world" +;; (stop) to close the server + +(define (serve port-no) + (define main-cust (make-custodian)) + (parameterize ([current-custodian main-cust]) + (define listener (tcp-listen port-no 5 #t)) + (define (loop) + (accept-and-handle listener) + (loop)) + (thread loop)) + (lambda () + (displayln "\nGoodbye, shutting down all services\n") + (custodian-shutdown-all main-cust))) + +(define (accept-and-handle listener) + (define cust (make-custodian)) + (parameterize ([current-custodian cust]) + (define-values (in out) (tcp-accept listener)) + (thread (lambda () + (handle in out) ;; this handles connection with that specific client + (close-input-port in) + (close-output-port out)))) + ;; Watcher thread: + (thread (lambda () + (sleep 120) + (custodian-shutdown-all cust)))) + +(define (handle in out) + ; (server-loop in out) + (sleep 5) ;; wait 5 seconds to guarantee client has already send message + (define echo (read-line in)) ;; bind message to echo + (displayln (string-append echo "\n")) + ; echo back the message, appending echo + ; could regex match the input to extract the name + (writeln "Admin: Hello there" out) ;; append "echo " to echo and send back + (flush-output out) +) + +(define input-prompt "Hermes: ") + +(define (server-loop in out) + (define echo (read-line in)) + (displayln echo) + (display ">>> ") + + (define input (read)) + (writeln (string-append "Admin: " input) out) + (flush-output out) + ; (sleep 10) + (server-loop in out)) + diff --git a/feasibility_analysis/tcpvanilla/tcptalk.rkt b/feasibility_analysis/tcpvanilla/tcptalk.rkt new file mode 100644 index 0000000..d069851 --- /dev/null +++ b/feasibility_analysis/tcpvanilla/tcptalk.rkt @@ -0,0 +1,30 @@ +#lang racket + +(define listener (tcp-listen 8083 5 #t)) ;; listener to service connection requests +;; client attempts to connect. Receives an input and output port +(define-values (client-in client-out) (tcp-connect "localhost" 8083)) +;; server accepts the connection request. Also gets a pair of ports +(define-values (server-in server-out) (tcp-accept listener)) + +;; client sends identifying message +(display (string-append "Client:My name is " "Ibrahim" "\n") + client-out) +(flush-output client-out) ;; must flush as ports are buffered in racket + +;; server receives and reads it +;; cooler if on separate racket instances +(read-line server-in) ;; --> "Client:My name is #hostname. +;; server replies +(display (string-append "Server:Hi " "Ibrahim" "\n") server-out) +(flush-output server-out) ;; flush flush + +;; client displays server message +(read-line client-in) +(close-output-port server-out) +(close-output-port client-out) +(read-line client-in) ;; --> eof object #eof +(read-line server-in) ;; --> eof object #eof +(tcp-close listener) +; (custodian-shutdown-all (current-custodian)) ;; release all resources including + ;; tcp, file, custom ports + ;; application exits diff --git a/feasibility_analysis/tcpvanilla/testdrive.md b/feasibility_analysis/tcpvanilla/testdrive.md new file mode 100644 index 0000000..b8e2883 --- /dev/null +++ b/feasibility_analysis/tcpvanilla/testdrive.md @@ -0,0 +1,20 @@ +a simple experiment on communication via tcp ports + +a server runs continously servicing clients. Clients connect, send a message, and +server replies with a message. Message should appear in each separate REPL area +prompt + +run server.rkt in a REPL +``` +,en server.rkt +(define stop (serve 8080)) ;; starts serve listening at port 8080 +(stop) ;; to stop server and free the ports + +``` + +run client.rkt in a separate REPL +``` +,en client.rkt +(define stop (client 8080)) ;; starts client talking to server at port 8080 + +``` diff --git a/feasibility_analysis/testdrive.md b/feasibility_analysis/testdrive.md deleted file mode 100644 index b8e2883..0000000 --- a/feasibility_analysis/testdrive.md +++ /dev/null @@ -1,20 +0,0 @@ -a simple experiment on communication via tcp ports - -a server runs continously servicing clients. Clients connect, send a message, and -server replies with a message. Message should appear in each separate REPL area -prompt - -run server.rkt in a REPL -``` -,en server.rkt -(define stop (serve 8080)) ;; starts serve listening at port 8080 -(stop) ;; to stop server and free the ports - -``` - -run client.rkt in a separate REPL -``` -,en client.rkt -(define stop (client 8080)) ;; starts client talking to server at port 8080 - -``` -- cgit v1.2.3