From 69523cc2ed211285468148d835c320447f21fc04 Mon Sep 17 00:00:00 2001 From: Ibrahim Mkusa Date: Wed, 12 Apr 2017 00:01:52 -0400 Subject: [PATCH] Clients can now talk with each other --- Hermes/Makefile | 3 +++ Hermes/server.rkt | 46 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 Hermes/Makefile diff --git a/Hermes/Makefile b/Hermes/Makefile new file mode 100644 index 0000000..eda5bbb --- /dev/null +++ b/Hermes/Makefile @@ -0,0 +1,3 @@ +# Remove idiotic save files +clean: + rm -rf *~ diff --git a/Hermes/server.rkt b/Hermes/server.rkt index 541024e..c57fbe7 100644 --- a/Hermes/server.rkt +++ b/Hermes/server.rkt @@ -139,22 +139,48 @@ 'ok ) +;; a bunch of selectors, predicates for connections +(define (get-output-port ports) + (cadr ports) + ) + +(define (get-input-port ports) + (car ports) +) ;; define a broadcast function (define broadcast (lambda () (semaphore-wait messages-s) - (semaphore-wait threads-s) + (if (not (null? messages)) - (begin (map (lambda (thread-descriptor) - (thread-send thread-descriptor (first messages))) - threads) + (begin (map + (lambda (ports) + (displayln (first messages) (get-output-port ports)) + (flush-output (get-output-port ports)) + ;; log message to server + (displayln "Message sent") + ) + connections) + ;; remove top message (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 - ) - messages ; whats the current state of messages - (semaphore-post threads-s) + ;; current state of messages and connections + messages + connections) + (display "No message to display\n")) + ; Approach one was to broadcast via thread mailboxes + ;(semaphore-wait threads-s) + ;(if (not (null? messages)) + ; (begin (map (lambda (thread-descriptor) + ; (thread-send thread-descriptor (first messages))) + ; threads) + ; (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 + ;) + ; messages ; whats the current state of messages + ;(semaphore-post threads-s) + (semaphore-post messages-s))) (define stop (serve 4321)) ;; start server then close with stop