Cleaned up code and added loggers to pinpoint error
This commit is contained in:
parent
44c715c55c
commit
2f0d04ce7f
@ -23,9 +23,11 @@
|
|||||||
; is there something in the input port. If yes? display it
|
; is there something in the input port. If yes? display it
|
||||||
; in the hello world
|
; in the hello world
|
||||||
|
|
||||||
|
; custodian for client connections
|
||||||
|
(define main-client-cust (make-custodian))
|
||||||
; make connection to server
|
; make connection to server
|
||||||
(define (client port-no)
|
(define (client port-no)
|
||||||
(define main-client-cust (make-custodian))
|
|
||||||
(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
|
||||||
@ -39,17 +41,21 @@
|
|||||||
;; make threads 2 lines
|
;; make threads 2 lines
|
||||||
(define a (thread
|
(define a (thread
|
||||||
(lambda ()
|
(lambda ()
|
||||||
|
(displayln "Startting receiver thread\n")
|
||||||
(let loop []
|
(let loop []
|
||||||
(receive-messages in)
|
(receive-messages in)
|
||||||
(sleep 1)
|
(sleep 1)
|
||||||
(loop)))))
|
(loop)))))
|
||||||
(define t (thread
|
(define t (thread
|
||||||
(lambda ()
|
(lambda ()
|
||||||
|
(displayln "Starting sender thread\n")
|
||||||
(let loop []
|
(let loop []
|
||||||
(send-messages username out)
|
(send-messages username out)
|
||||||
(sleep 1)
|
(sleep 1)
|
||||||
(loop)))))
|
(loop)))))
|
||||||
|
(displayln "Now waiting for sender thread")
|
||||||
(thread-wait t) ;; returns prompt back to drracket
|
(thread-wait t) ;; returns prompt back to drracket
|
||||||
|
(displayln "Closing client ports")
|
||||||
(close-input-port in)
|
(close-input-port in)
|
||||||
(close-output-port out))
|
(close-output-port out))
|
||||||
(custodian-shutdown-all main-client-cust))
|
(custodian-shutdown-all main-client-cust))
|
||||||
@ -69,7 +75,7 @@
|
|||||||
;(kill-thread t))))
|
;(kill-thread t))))
|
||||||
(cond ((string=? input "quit") (exit)))
|
(cond ((string=? input "quit") (exit)))
|
||||||
;; modify to send messages to out port
|
;; modify to send messages to out port
|
||||||
(displayln (string-append username ": " input) out)
|
(displayln (string-append username ": " input "\n") out)
|
||||||
(flush-output out)
|
(flush-output out)
|
||||||
|
|
||||||
;(semaphore-post fair)
|
;(semaphore-post fair)
|
||||||
@ -89,7 +95,9 @@
|
|||||||
(define evt (sync/timeout 30 (read-line-evt in)))
|
(define evt (sync/timeout 30 (read-line-evt in)))
|
||||||
(cond [(eof-object? evt)
|
(cond [(eof-object? evt)
|
||||||
(displayln "Server connection closed")
|
(displayln "Server connection closed")
|
||||||
(exit)]
|
(custodian-shutdown-all main-client-cust)
|
||||||
|
;(exit)
|
||||||
|
]
|
||||||
[(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
|
||||||
@ -99,4 +107,5 @@
|
|||||||
)
|
)
|
||||||
|
|
||||||
(define stop (client 4321))
|
(define stop (client 4321))
|
||||||
|
(display "Client started\n")
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
;; every 5 seconds run to broadcast top message in list
|
;; every 5 seconds run to broadcast top message in list
|
||||||
;; and remove it from list
|
;; and remove it from list
|
||||||
(define messages-s (make-semaphore 1)) ;; control access to messages
|
(define messages-s (make-semaphore 1)) ;; control access to messages
|
||||||
(define messages '("hello, world!")) ;; stores a list of messages(strings) from currents
|
(define messages '()) ;; stores a list of messages(strings) from currents
|
||||||
|
|
||||||
(define threads-s (make-semaphore 1)) ;; control access to threads
|
(define threads-s (make-semaphore 1)) ;; control access to threads
|
||||||
;; lets keep thread descriptor values
|
;; lets keep thread descriptor values
|
||||||
@ -35,13 +35,14 @@
|
|||||||
(define (loop)
|
(define (loop)
|
||||||
(accept-and-handle listener)
|
(accept-and-handle listener)
|
||||||
(loop))
|
(loop))
|
||||||
|
(displayln "threading the listeneter")
|
||||||
(thread loop)
|
(thread loop)
|
||||||
;; Create a thread whose job is to simply call broadcast iteratively
|
;; Create a thread whose job is to simply call broadcast iteratively
|
||||||
(thread (lambda ()
|
(thread (lambda ()
|
||||||
|
(display "Broadcast thread started!\n")
|
||||||
(let loopb []
|
(let loopb []
|
||||||
(sleep 30) ;; wait 30 secs before beginning to broadcast
|
(sleep 10) ;; wait 30 secs before beginning to broadcast
|
||||||
(broadcast)
|
(broadcast)
|
||||||
(sleep 10) ;; sleep for 10 seconds between broadcasts
|
|
||||||
(loopb)))))
|
(loopb)))))
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(displayln "\nGoodbye, shutting down all services\n")
|
(displayln "\nGoodbye, shutting down all services\n")
|
||||||
@ -51,25 +52,42 @@
|
|||||||
(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))
|
||||||
|
(displayln "Sucessfully connected to a client")
|
||||||
|
(display in)
|
||||||
|
(displayln out)
|
||||||
|
(displayln "Sending client Welcome message")
|
||||||
|
(displayln "Welcome to Hermes coms")
|
||||||
|
(flush-output out)
|
||||||
; discard request header
|
; discard request header
|
||||||
; Discard the request header (up to blank line):
|
; Discard the request header (up to blank line):
|
||||||
(regexp-match #rx"(\r\n|^)\r\n" in)
|
; (regexp-match #rx"(\r\n|^)\r\n" in)
|
||||||
(semaphore-wait connections-s)
|
(semaphore-wait connections-s)
|
||||||
|
(displayln "Got the semaphore")
|
||||||
;; keep track of open ports
|
;; keep track of open ports
|
||||||
(set! connections (append connections (list (list in out))))
|
(set! connections (append connections (list (list in out))))
|
||||||
(semaphore-post connections-s)
|
(semaphore-post connections-s)
|
||||||
|
(displayln "Successfully added a pair of ports")
|
||||||
|
connections
|
||||||
|
|
||||||
; start a thread to deal with specific client and add descriptor value to the list of threads
|
; start a thread to deal with specific client and add descriptor value to the list of threads
|
||||||
(set! threads (append threads (list (thread (lambda ()
|
(semaphore-wait threads-s)
|
||||||
|
(define threadcom (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)
|
(display "handle successfully completed\n")
|
||||||
(close-output-port out))))
|
; this might have been the issue
|
||||||
)
|
;(close-input-port in)
|
||||||
)
|
; (close-output-port out)
|
||||||
|
)))
|
||||||
|
(set! threads (append threads (list threadcom)))
|
||||||
|
(semaphore-post threads-s)
|
||||||
|
(displayln "Successfully created a thread")
|
||||||
|
(displayln threadcom)
|
||||||
|
threads
|
||||||
;; 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 ()
|
||||||
|
(display "Started a thread to kill hanging connecting thread\n")
|
||||||
(sleep 360)
|
(sleep 360)
|
||||||
(custodian-shutdown-all cust)))))
|
(custodian-shutdown-all cust)))))
|
||||||
|
|
||||||
@ -79,9 +97,9 @@
|
|||||||
(define (handle in out)
|
(define (handle in out)
|
||||||
; define function to deal with incoming messages from client
|
; define function to deal with incoming messages from client
|
||||||
(define (something-to-say in)
|
(define (something-to-say in)
|
||||||
(define evt-t0 (sync/timeout 30 (read-line-evt in 'linefeed)))
|
(define evt-t0 (sync/timeout 60 (read-line-evt in 'linefeed)))
|
||||||
(cond [(eof-object? evt-t0)
|
(cond [(eof-object? evt-t0)
|
||||||
(displayln (string-append "Connection closed " (current-thread) "exiting"))
|
(displayln "Connection closed. EOF received")
|
||||||
(exit)
|
(exit)
|
||||||
]
|
]
|
||||||
[(string? evt-t0)
|
[(string? evt-t0)
|
||||||
@ -91,11 +109,11 @@
|
|||||||
(set! messages (append messages (list evt-t0)))
|
(set! messages (append messages (list evt-t0)))
|
||||||
(semaphore-post messages-s)]
|
(semaphore-post messages-s)]
|
||||||
[else
|
[else
|
||||||
(displayln (string-append "Nothing received from " (current-thread)))]))
|
(displayln "Timeout waiting. Nothing received from client")]))
|
||||||
|
|
||||||
|
|
||||||
; define function to deal with out
|
; define function to deal with out
|
||||||
(define (something-to-send out)
|
(define (something-to-broadcast out)
|
||||||
(define evt-t1 (sync/timeout 120 (thread-receive-evt)))
|
(define evt-t1 (sync/timeout 120 (thread-receive-evt)))
|
||||||
;; send message to client
|
;; send message to client
|
||||||
(fprintf out "~a~n" (thread-receive))
|
(fprintf out "~a~n" (thread-receive))
|
||||||
@ -113,7 +131,7 @@
|
|||||||
|
|
||||||
(thread (lambda ()
|
(thread (lambda ()
|
||||||
(let loop []
|
(let loop []
|
||||||
(something-to-send out)
|
(something-to-broadcast out)
|
||||||
(sleep 1)
|
(sleep 1)
|
||||||
(loop))))
|
(loop))))
|
||||||
; (server-loop in out)
|
; (server-loop in out)
|
||||||
@ -131,10 +149,14 @@
|
|||||||
(thread-send thread-descriptor (first messages)))
|
(thread-send thread-descriptor (first messages)))
|
||||||
threads)
|
threads)
|
||||||
(set! messages (rest messages))
|
(set! messages (rest messages))
|
||||||
|
(displayln "Broadcasted a message\n")
|
||||||
)
|
)
|
||||||
(display "No message to display\n") ; for later create file port for errors and save error messages to that file
|
(display "No message to display\n") ; for later create file port for errors and save error messages to that file
|
||||||
)
|
)
|
||||||
|
messages ; whats the current state of messages
|
||||||
(semaphore-post threads-s)
|
(semaphore-post threads-s)
|
||||||
(semaphore-post messages-s)))
|
(semaphore-post messages-s)))
|
||||||
|
|
||||||
(define stop (serve 4321)) ;; start server then close with stop
|
(define stop (serve 4321)) ;; start server then close with stop
|
||||||
|
(display "Server process started\n")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user