server code for interacting with client is done, not tested. Working on
server broadcast function
This commit is contained in:
parent
2e0ddaaf3a
commit
f56f1cc46d
142
server.rkt
142
server.rkt
@ -6,11 +6,24 @@
|
|||||||
;; might need to access it
|
;; might need to access it
|
||||||
(define connections '()) ;; maintains a list of open ports
|
(define connections '()) ;; maintains a list of open ports
|
||||||
;; ((in1, out1), (in2, out2), (in3, out3), (in4, out4) ...)
|
;; ((in1, out1), (in2, out2), (in3, out3), (in4, out4) ...)
|
||||||
|
(define connections-s (make-semaphore 1)) ;; control access to connections
|
||||||
|
|
||||||
|
;; every 5 seconds run to broadcast top message in list
|
||||||
|
;; and remove it from list
|
||||||
|
(define messages-s (make-semaphore 1)) ;; control access to messages
|
||||||
|
(define messages '()) ;; stores a list of messages(strings) from currents
|
||||||
|
|
||||||
|
(define threads-s (make-semaphore 1)) ;; control access to threads
|
||||||
;; lets keep thread descriptor values
|
;; lets keep thread descriptor values
|
||||||
;
|
(define threads '()) ;; stores a list of client serving threads as thread descriptor values
|
||||||
|
|
||||||
(define fair (make-semaphore 1)) ;; managing connections above
|
;; define a broadcast function
|
||||||
|
(define broadcast
|
||||||
|
(lambda ()
|
||||||
|
(semaphore-wait messages-s)
|
||||||
|
(semaphore-wait threads-s)
|
||||||
|
(map (lambda (thread-descriptor)
|
||||||
|
()))))
|
||||||
|
|
||||||
(define can-i-broadcast (make-semaphore 1))
|
(define can-i-broadcast (make-semaphore 1))
|
||||||
|
|
||||||
@ -40,116 +53,61 @@
|
|||||||
(define cust (make-custodian))
|
(define cust (make-custodian))
|
||||||
(parameterize ([current-custodian cust])
|
(parameterize ([current-custodian cust])
|
||||||
(define-values (in out) (tcp-accept listener))
|
(define-values (in out) (tcp-accept listener))
|
||||||
(semaphore-wait fair)
|
(semaphore-wait connections-s)
|
||||||
;; keep track of open ports
|
;; keep track of open ports
|
||||||
(append connections (list (list in out)))
|
(append connections (list (list in out)))
|
||||||
(semaphore-wait fiar)
|
(semaphore-wait connections-s)
|
||||||
|
|
||||||
; thread will communicate to all clients at once in a broadcast
|
; start a thread to deal with specific client and add descriptor value to the list of threads
|
||||||
; manner
|
(append threads (list (thread (lambda ()
|
||||||
(thread (lambda ()
|
|
||||||
(handle in out) ;; this handles connection with that specific client
|
(handle in out) ;; this handles connection with that specific client
|
||||||
(close-input-port in)
|
(close-input-port in)
|
||||||
(close-output-port out)))
|
(close-output-port out))))
|
||||||
)
|
)
|
||||||
;; Watcher thread:
|
;; Watcher thread:
|
||||||
;; kills current thread for waiting too long for connection from
|
;; kills current thread for waiting too long for connection from
|
||||||
;; clients
|
;; clients
|
||||||
(thread (lambda ()
|
(thread (lambda ()
|
||||||
(sleep 120)
|
(sleep 120)
|
||||||
(custodian-shutdown-all cust))))
|
(custodian-shutdown-all cust)))))
|
||||||
|
|
||||||
; (define (handle connections)
|
; (define (handle connections)
|
||||||
; ())
|
; ())
|
||||||
;; each thread needs 2 new threads
|
;; each thread needs 2 new threads
|
||||||
(define (handle in out)
|
(define (handle in out)
|
||||||
; define function to deal with in
|
; define function to deal with incoming messages from client
|
||||||
(define (something-to-say in)
|
(define (something-to-say in)
|
||||||
(sync/timeout 4 (read-line-evt in 'linefeed)))
|
(define evt-t0 (sync/timeout 120 (read-line-evt in 'linefeed)))
|
||||||
|
(cond [(not evt-t0)
|
||||||
|
(displayln "Nothing received from " (current-thread) "exiting")]
|
||||||
|
[(string? evt-t0)
|
||||||
|
(semaphore-wait messages-s)
|
||||||
|
; append the message to list of messages
|
||||||
|
(append messages (list evt-t0))
|
||||||
|
(semaphore-post messages-s)]))
|
||||||
|
|
||||||
|
|
||||||
; define function to deal with out
|
; define function to deal with out
|
||||||
; thread them each
|
(define (something-to-send out)
|
||||||
; (server-loop in out)
|
(define evt-t1 (sync/timeout 120 (thread-receive-evt)))
|
||||||
(sleep 5) ;; wait 5 seconds to guarantee client has already send message
|
;; send message to client
|
||||||
(define echo (read-line in)) ;; bind message to echo
|
(fprintf out "~a~n" (thread-receive))
|
||||||
(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)
|
(flush-output out)
|
||||||
)
|
)
|
||||||
;; This is a single server communicating directly to the client
|
; thread them each
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
||||||
|
|
||||||
|
;; i could bind to values, and call wait on them
|
||||||
|
;; thread that deals with incoming messages for that particular thread
|
||||||
|
(thread (lambda ()
|
||||||
|
(let loop []
|
||||||
|
(something-to-say in)
|
||||||
|
(loop))))
|
||||||
|
|
||||||
;; author: Ibrahim Mkusa
|
(thread (lambda ()
|
||||||
;; about: print and read concurrently
|
(let loop []
|
||||||
;; notes: output may need to be aligned and formatted nicely
|
(something-to-say out)
|
||||||
;; look into
|
(loop))))
|
||||||
;; 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
|
; (server-loop in out)
|
||||||
|
; (sleep 5) ;; wait 5 seconds to guarantee client has already send message
|
||||||
;; create custodian for managing all resources
|
'ok
|
||||||
;; 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
|
|
||||||
|
|
||||||
;; 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")
|
|
||||||
|
Loading…
Reference in New Issue
Block a user