aboutsummaryrefslogtreecommitdiff
path: root/Hermes/client.rkt
diff options
context:
space:
mode:
Diffstat (limited to 'Hermes/client.rkt')
-rw-r--r--Hermes/client.rkt94
1 files changed, 38 insertions, 56 deletions
diff --git a/Hermes/client.rkt b/Hermes/client.rkt
index 25be149..3b65cfa 100644
--- a/Hermes/client.rkt
+++ b/Hermes/client.rkt
@@ -1,102 +1,84 @@
#lang racket
(require math/base) ;; for random number generation
-
+;; TODO clean up string message output and alignment
;; author: Ibrahim Mkusa
;; about: print and read concurrently
;; notes: output may need to be aligned and formatted nicely
-;; 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
-
-;; 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
-
-;; Notes connect to server on localhost
-;; use client template of tcpvanilla
-;; use event for read-write
+(define host "10.0.0.160") ; internal home
+(define host2 "67.186.191.81")
+(define port-num 4321)
-;; modify read-loop-i
-; read a value and send it to server via output-port
-
-; is there something in the input port. If yes? display it
-; in the hello world
+; custodian for client connections
+(define main-client-cust (make-custodian))
; make connection to 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
+ (define-values (in out) (tcp-connect host2 port-no)) ;; define values
(display in)
(displayln out)
;; 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))
- ; (thread (lambda ()
- ;; make threads 2 lines
(define a (thread
(lambda ()
+ (displayln "Starting receiver thread.")
(let loop []
(receive-messages in)
(sleep 1)
(loop)))))
(define t (thread
(lambda ()
+ (displayln "Starting sender thread.")
(let loop []
(send-messages username out)
(sleep 1)
(loop)))))
+ (displayln "Now waiting for sender thread.")
(thread-wait t) ;; returns prompt back to drracket
+ (displayln "Closing client ports.")
(close-input-port in)
(close-output-port out))
(custodian-shutdown-all main-client-cust))
-;; the send-messages
+;; sends a message to the server
(define (send-messages username out)
+ ; get current time
+ (define date-today (seconds->date (current-seconds) #t))
+ ;TODO pad the second if its only 1 character
+ (define date-print (string-append (number->string (date-hour date-today))
+ ":"
+ (number->string (date-minute date-today))
+ ":"
+ (number->string (date-second date-today))
+ " | "))
;; intelligent read, quits when user types in "quit"
- ;(semaphore-wait fair)
- ; (display usernamei)
(define input (read-line))
- ;; do something over here with input maybe send it out
+ (cond ((string=? input "quit")
+ (displayln (string-append date-print username " signing out. See ya!") out)
+ (flush-output out)
+ (exit)))
- ;; 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))))
- (cond ((string=? input "quit") (exit)))
- ;; modify to send messages to out port
- (displayln (string-append username ": " input) out)
- (flush-output out)
-
- ;(semaphore-post fair)
- ; (read-loop-i out)
-)
+ (displayln (string-append date-print username ": " input) out)
+ (flush-output out))
-
-
-;; 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
+; receives input from server and displays it to stdout
(define (receive-messages in)
- ; (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
- (define evt (sync/timeout 30 (read-line-evt in)))
+ ; retrieve a message from server
+ (define evt (sync/timeout 60 (read-line-evt in)))
+
(cond [(eof-object? evt)
- (displayln "Server connection closed")
- (exit)]
+ (displayln "Server connection closed.")
+ (custodian-shutdown-all main-client-cust)
+ ;(exit)
+ ]
[(string? evt)
(displayln evt)] ; could time stamp here or to send message
[else
- (displayln (string-append "Nothing received from server for 2 minutes."))]
- )
- ;(semaphore-post fair)
-)
+ (displayln (string-append "Nothing received from server for 2 minutes."))]))
+(displayln "Starting client.")
(define stop (client 4321))
-