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 6. keep count of connected clients using object orientation
7. maybe fiddle around with irc library 7. maybe fiddle around with irc library
8. separate main running code from definitions 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 ;; author: Ibrahim Mkusa
;; about: print and read concurrently ;; about: print and read concurrently
;; notes: output may need to be aligned and formatted nicely ;; 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 ; custodian for client connections
(define main-client-cust (make-custodian)) (define main-client-cust (make-custodian))
; make connection to server ; make connection to server
(define (client port-no) (define (client port-no)
(parameterize ([current-custodian main-client-cust]) (parameterize ([current-custodian main-client-cust])
;; connect to server at port 8080 ;; connect to server at port 8080
(define-values (in out) (tcp-connect "localhost" port-no)) ;; define values (define-values (in out) (tcp-connect "localhost" port-no)) ;; define values
@ -22,8 +19,6 @@
(displayln "What's your name?") (displayln "What's your name?")
(define username (read-line)) (define username (read-line))
; (thread (lambda ()
;; make threads 2 lines
(define a (thread (define a (thread
(lambda () (lambda ()
(displayln "Starting receiver thread.") (displayln "Starting receiver thread.")
@ -46,37 +41,17 @@
(custodian-shutdown-all main-client-cust)) (custodian-shutdown-all main-client-cust))
;; the send-messages ;; sends a message to the server
(define (send-messages username out) (define (send-messages username out)
;; intelligent read, quits when user types in "quit" ;; intelligent read, quits when user types in "quit"
;(semaphore-wait fair)
; (display usernamei)
(define input (read-line)) (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))) (cond ((string=? input "quit") (exit)))
;; modify to send messages to out port
(displayln (string-append username ": " input) out) (displayln (string-append username ": " input) out)
(flush-output out) (flush-output out))
;(semaphore-post fair) ; receives input from server and displays it to stdout
; (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
(define (receive-messages in) (define (receive-messages in)
; (sleep (random-integer 0 15)) ;; sleep between 0 and 15 seconds to simulate coms ; retrieve a message from server
;; with server
;(semaphore-wait fair)
;; we will retrieve the line printed below from the server
(define evt (sync/timeout 60 (read-line-evt in))) (define evt (sync/timeout 60 (read-line-evt in)))
(cond [(eof-object? evt) (cond [(eof-object? evt)
(displayln "Server connection closed.") (displayln "Server connection closed.")
@ -86,11 +61,7 @@
[(string? evt) [(string? evt)
(displayln evt)] ; could time stamp here or to send message (displayln evt)] ; could time stamp here or to send message
[else [else
(displayln (string-append "Nothing received from server for 2 minutes."))] (displayln (string-append "Nothing received from server for 2 minutes."))]))
)
;(semaphore-post fair)
)
(define stop (client 4321)) (define stop (client 4321))
(displayln "Client started.") (displayln "Client started.")