created modules/general.rkt to house common functions

This commit is contained in:
Ibrahim Mkusa 2017-04-15 20:56:47 -04:00
parent 11bcec1052
commit 413d13d7a6
4 changed files with 38 additions and 24 deletions

8
.gitignore vendored
View File

@ -1,2 +1,10 @@
# ignore temporary files # ignore temporary files
*~ *~
# ignore logs and configuration files
*.out
*.conf
# ignore racket compile files
*.dep
*.zo

View File

@ -13,13 +13,13 @@
(define host3 "localhost") (define host3 "localhost")
(define port-num 4321) (define port-num 4321)
(define hermes-conf (open-output-file "./hermes.conf" 'append)) (define hermes-conf (open-output-file "./hermes.conf" #:exists'append))
(define hermes-conf-s (make-semaphore 1)) (define hermes-conf-s (make-semaphore 1))
(define convs-out (open-output-file "./convs.out" 'append)) (define convs-out (open-output-file "./convs.out" #:exists 'append))
(define convs-out-s (make-semaphore 1)) (define convs-out-s (make-semaphore 1))
(define error-out (open-output-file "./error.out" 'append)) (define error-out (open-output-file "./error.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

View File

@ -0,0 +1,24 @@
#lang racket
(provide displayln-safe)
;; Several threads may want to print to stdout, so lets make things civil
; constant always available
(define stdout (make-semaphore 1))
; prints to stdout with an optional output port
; requires a specified semaphore for the optional output port
(define displayln-safe
(lambda (a-string [a-semaphore stdout] [a-output-port (current-output-port)])
(cond [(not (and (eq? a-semaphore stdout) (eq? a-output-port (current-output-port))))
(semaphore-wait a-semaphore)
(semaphore-wait stdout)
(displayln a-string a-output-port)
(flush-output a-output-port)
(displayln a-string)
(semaphore-post stdout)
(semaphore-post a-semaphore)]
[else
(semaphore-wait stdout)
(displayln a-string)
(semaphore-post stdout)])))

View File

@ -1,6 +1,9 @@
#lang racket #lang racket
(require "modules/general.rkt")
(require math/base) ;; for random number generation (require math/base) ;; for random number generation
;; globals ;; globals
(define welcome-message "Welcome to Hermes coms. Type your message below") (define welcome-message "Welcome to Hermes coms. Type your message below")
(define successful-connection-m "Successfully connected to a client. Sending client a welcome message.") (define successful-connection-m "Successfully connected to a client. Sending client a welcome message.")
@ -72,27 +75,6 @@
; 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
;; Several threads may want to print to stdout, so lets make things civil
; constant always available
(define stdout (make-semaphore 1))
; TODO refactor to take a port. defaults to current-output port in this context
; Takes a string and a semaphore to print safely to stdout
(define displayln-safe
(lambda (a-string [a-semaphore stdout] [a-output-port (current-output-port)])
(cond [(not (and (eq? a-semaphore stdout) (eq? a-output-port (current-output-port))))
(semaphore-wait a-semaphore)
(semaphore-wait stdout)
(displayln a-string a-output-port)
(flush-output a-output-port)
(displayln a-string)
(semaphore-post stdout)
(semaphore-post a-semaphore)]
[else
(semaphore-wait stdout)
(displayln a-string)
(semaphore-post stdout)])))
; two files to store error messages, and channel conversations ; two files to store error messages, and channel conversations
(define error-out (open-output-file "/home/pcuser/Hermes/Hermes/error.txt" #:exists 'append)) (define error-out (open-output-file "/home/pcuser/Hermes/Hermes/error.txt" #:exists 'append))
(define convs-out (open-output-file "/home/pcuser/Hermes/Hermes/conversations.txt" #:exists 'append)) (define convs-out (open-output-file "/home/pcuser/Hermes/Hermes/conversations.txt" #:exists 'append))