tidied up client.rkt. More stuff to TODO.txt

This commit is contained in:
Ibrahim Mkusa 2017-04-12 13:49:20 -04:00
parent 2899891a63
commit 4c26f1eaa3
2 changed files with 7 additions and 34 deletions

View File

@ -6,3 +6,5 @@
6. keep count of connected clients using object orientation
7. maybe fiddle around with irc library
8. separate main running code from definitions
9. closure connections, messages, threads. Avoid using set! without an object
like make-account

View File

@ -4,15 +4,12 @@
;; 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
; custodian for client connections
(define main-client-cust (make-custodian))
; make connection to server
(define (client port-no)
(parameterize ([current-custodian main-client-cust])
;; connect to server at port 8080
(define-values (in out) (tcp-connect "localhost" port-no)) ;; define values
@ -22,8 +19,6 @@
(displayln "What's your name?")
(define username (read-line))
; (thread (lambda ()
;; make threads 2 lines
(define a (thread
(lambda ()
(displayln "Starting receiver thread.")
@ -46,37 +41,17 @@
(custodian-shutdown-all main-client-cust))
;; the send-messages
;; sends a message to the server
(define (send-messages username out)
;; 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
;; 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)
(flush-output out))
;(semaphore-post fair)
; (read-loop-i 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
; retrieve a message from server
(define evt (sync/timeout 60 (read-line-evt in)))
(cond [(eof-object? evt)
(displayln "Server connection closed.")
@ -86,11 +61,7 @@
[(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."))]))
(define stop (client 4321))
(displayln "Client started.")