Formatting edits to code to enhance readibility for coming merge

This commit is contained in:
Ibrahim Mkusa 2017-04-20 15:29:06 -04:00
parent 95f7e74433
commit 42eceeb36f
2 changed files with 27 additions and 19 deletions

View File

@ -25,7 +25,8 @@
(define error-out (open-output-file "./error_client.out" #:exists 'append)) (define error-out (open-output-file "./error_client.out" #:exists 'append))
(define error-out-s (make-semaphore 1)) (define error-out-s (make-semaphore 1))
; custodian for client connections ; custodian for client connections. Define at top level since a function needs
; to see it
(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)

View File

@ -1,6 +1,6 @@
#lang racket #lang racket
(require "modules/general.rkt") (require "modules/general.rkt") ;; common function(s)
(require math/base) ;; for random number generation (require math/base) ;; for random number generation
@ -53,11 +53,12 @@
[(eq? m 'remove-ports) remove-ports] [(eq? m 'remove-ports) remove-ports]
[(eq? m 'add) add])) [(eq? m 'add) add]))
dispatch) dispatch)
; "instantiate" to track the connections
(define c-connections (make-connections '())) (define c-connections (make-connections '()))
; a semaphore to control acess to c-connections ; a semaphore to control acess to c-connections
(define connections-s (make-semaphore 1)) ;; control access to connections (define connections-s (make-semaphore 1)) ;; control access to connections
; Track received messages in a closure ; Track received messages in a closure. Initialy messages is '()
(define (make-messages messages) (define (make-messages messages)
(define (add message) (define (add message)
(set! messages (append messages (list message))) (set! messages (append messages (list message)))
@ -72,6 +73,7 @@
[(eq? m 'mes-list) mes-list] [(eq? m 'mes-list) mes-list]
[(eq? m 'remove-top) remove-top])) [(eq? m 'remove-top) remove-top]))
dispatch) dispatch)
; "instantiate" a make-message variable to track our messages
(define c-messages (make-messages '())) (define c-messages (make-messages '()))
; semaphore to control access to c-messages ; semaphore to control access to c-messages
(define messages-s (make-semaphore 1)) ;; control access to messages (define messages-s (make-semaphore 1)) ;; control access to messages
@ -81,9 +83,12 @@
(define convs-out (open-output-file "./conversations_server.txt" #:exists 'append)) (define convs-out (open-output-file "./conversations_server.txt" #:exists 'append))
(define error-out-s (make-semaphore 1)) (define error-out-s (make-semaphore 1))
(define convs-out-s (make-semaphore 1)) (define convs-out-s (make-semaphore 1))
; TODO finish logging all error related messages to
; Main server code wrapped in a function
(define (serve port-no) (define (serve port-no)
; custodian manages resources put under its domain
(define main-cust (make-custodian)) (define main-cust (make-custodian))
; "parameterize" puts resources under the domain of created custodian
(parameterize ([current-custodian main-cust]) (parameterize ([current-custodian main-cust])
(define listener (tcp-listen port-no 5 #t)) (define listener (tcp-listen port-no 5 #t))
(define (loop) (define (loop)
@ -114,8 +119,7 @@
(parameterize ([current-custodian cust]) (parameterize ([current-custodian cust])
(define-values (in out) (tcp-accept listener)) (define-values (in out) (tcp-accept listener))
;TODO retrive user name for client here ; TODO do some error checking
; do some error checking
(define username-evt (sync (read-line-evt in 'linefeed))) (define username-evt (sync (read-line-evt in 'linefeed)))
@ -224,6 +228,7 @@
(flush-output out) (flush-output out)
(semaphore-post connections-s)] (semaphore-post connections-s)]
[else [else
; Its an ordinarly message
; (displayln-safe evt-t0) debug purposes ; (displayln-safe evt-t0) debug purposes
(semaphore-wait messages-s) (semaphore-wait messages-s)
; evaluate it . ; evaluate it .
@ -239,7 +244,7 @@
; (sleep 1) ; (sleep 1)
(loop))))) (loop)))))
; extracts output port from a list pair of input and output port ; extracts output port from a list pair of username, input and output port
(define (get-output-port ports) (define (get-output-port ports)
(caddr ports)) (caddr ports))
@ -258,7 +263,7 @@
(lambda () (lambda ()
(semaphore-wait messages-s) (semaphore-wait messages-s)
(cond [(not (null? ((c-messages 'mes-list)))) (cond [(not (null? ((c-messages 'mes-list))))
(begin (map (map
(lambda (ports) (lambda (ports)
(if (not (port-closed? (get-output-port ports))) (if (not (port-closed? (get-output-port ports)))
(begin (begin
@ -267,9 +272,11 @@
(displayln-safe "Failed to broadcast. Port not open." error-out-s error-out))) (displayln-safe "Failed to broadcast. Port not open." error-out-s error-out)))
((c-connections 'cons-list))) ((c-connections 'cons-list)))
(displayln-safe (first ((c-messages 'mes-list))) convs-out-s convs-out) (displayln-safe (first ((c-messages 'mes-list))) convs-out-s convs-out)
;; remove top message ;; remove top message from "queue" after broadcasting
((c-messages 'remove-top)) ((c-messages 'remove-top))
(displayln "Message broadcasted"))]) ; debugging displayln below
; (displayln "Message broadcasted")
]) ; end of cond
(semaphore-post messages-s))) (semaphore-post messages-s)))
(define stop-server (serve 4321)) ;; start server then close with stop (define stop-server (serve 4321)) ;; start server then close with stop