2017-04-09 17:02:30 +00:00
|
|
|
#lang racket
|
|
|
|
(require math/base) ;; for random number generation
|
|
|
|
|
|
|
|
;; globals
|
|
|
|
;; must control access via semaphore as listener thread or broadcast thread
|
|
|
|
;; might need to access it
|
|
|
|
(define connections '()) ;; maintains a list of open ports
|
|
|
|
;; ((in1, out1), (in2, out2), (in3, out3), (in4, out4) ...)
|
2017-04-09 18:56:46 +00:00
|
|
|
(define connections-s (make-semaphore 1)) ;; control access to connections
|
2017-04-09 17:02:30 +00:00
|
|
|
|
2017-04-09 18:56:46 +00:00
|
|
|
;; 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
|
2017-04-09 17:02:30 +00:00
|
|
|
;; lets keep thread descriptor values
|
2017-04-09 18:56:46 +00:00
|
|
|
(define threads '()) ;; stores a list of client serving threads as thread descriptor values
|
2017-04-09 17:02:30 +00:00
|
|
|
|
2017-04-09 18:56:46 +00:00
|
|
|
;; define a broadcast function
|
|
|
|
(define broadcast
|
|
|
|
(lambda ()
|
|
|
|
(semaphore-wait messages-s)
|
|
|
|
(semaphore-wait threads-s)
|
2017-04-09 19:19:57 +00:00
|
|
|
(if (not (null? messages))
|
|
|
|
(begin (map (lambda (thread-descriptor)
|
|
|
|
(thread-send thread-descriptor (first messages))))
|
|
|
|
(set! messages (rest messages))
|
|
|
|
)
|
|
|
|
(display "No message to display\n") ; for later create file port for errors and save error messages to that file
|
|
|
|
)
|
|
|
|
(semaphore-post threads-s)
|
|
|
|
(semaphore-post messages-s)))
|
2017-04-09 17:02:30 +00:00
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
;; This is a relay server making two clients communicate
|
|
|
|
;; Both `server' and `accept-and-handle' change
|
|
|
|
;; to use a custodian.
|
|
|
|
;; To start server
|
|
|
|
;; (define stop (serve 8080))
|
|
|
|
;; (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))
|
2017-04-09 19:19:57 +00:00
|
|
|
(thread loop)
|
|
|
|
;; Create a thread whose job is to simply call broadcast iteratively
|
|
|
|
(thread (lambda ()
|
|
|
|
(let loopb []
|
|
|
|
broadcast
|
|
|
|
(sleep 10) ;; sleep for 10 seconds between broadcasts
|
|
|
|
(loopb)))))
|
2017-04-09 17:02:30 +00:00
|
|
|
(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))
|
2017-04-09 18:56:46 +00:00
|
|
|
(semaphore-wait connections-s)
|
2017-04-09 17:02:30 +00:00
|
|
|
;; keep track of open ports
|
|
|
|
(append connections (list (list in out)))
|
2017-04-09 18:56:46 +00:00
|
|
|
(semaphore-wait connections-s)
|
2017-04-09 17:02:30 +00:00
|
|
|
|
2017-04-09 18:56:46 +00:00
|
|
|
; start a thread to deal with specific client and add descriptor value to the list of threads
|
|
|
|
(append threads (list (thread (lambda ()
|
2017-04-09 17:02:30 +00:00
|
|
|
(handle in out) ;; this handles connection with that specific client
|
|
|
|
(close-input-port in)
|
2017-04-09 18:56:46 +00:00
|
|
|
(close-output-port out))))
|
|
|
|
)
|
2017-04-09 17:02:30 +00:00
|
|
|
;; Watcher thread:
|
|
|
|
;; kills current thread for waiting too long for connection from
|
|
|
|
;; clients
|
|
|
|
(thread (lambda ()
|
|
|
|
(sleep 120)
|
2017-04-09 18:56:46 +00:00
|
|
|
(custodian-shutdown-all cust)))))
|
2017-04-09 17:02:30 +00:00
|
|
|
|
|
|
|
; (define (handle connections)
|
|
|
|
; ())
|
|
|
|
;; each thread needs 2 new threads
|
|
|
|
(define (handle in out)
|
2017-04-09 18:56:46 +00:00
|
|
|
; define function to deal with incoming messages from client
|
2017-04-09 17:02:30 +00:00
|
|
|
(define (something-to-say in)
|
2017-04-09 18:56:46 +00:00
|
|
|
(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)]))
|
|
|
|
|
|
|
|
|
2017-04-09 17:02:30 +00:00
|
|
|
; define function to deal with out
|
2017-04-09 18:56:46 +00:00
|
|
|
(define (something-to-send out)
|
|
|
|
(define evt-t1 (sync/timeout 120 (thread-receive-evt)))
|
|
|
|
;; send message to client
|
|
|
|
(fprintf out "~a~n" (thread-receive))
|
|
|
|
(flush-output out)
|
|
|
|
)
|
2017-04-09 17:02:30 +00:00
|
|
|
; thread them each
|
2017-04-09 18:56:46 +00:00
|
|
|
|
|
|
|
;; 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))))
|
|
|
|
|
|
|
|
(thread (lambda ()
|
|
|
|
(let loop []
|
|
|
|
(something-to-say out)
|
|
|
|
(loop))))
|
2017-04-09 17:02:30 +00:00
|
|
|
; (server-loop in out)
|
2017-04-09 18:56:46 +00:00
|
|
|
; (sleep 5) ;; wait 5 seconds to guarantee client has already send message
|
|
|
|
'ok
|
|
|
|
)
|