created modules/general.rkt to house common functions
This commit is contained in:
parent
11bcec1052
commit
413d13d7a6
8
.gitignore
vendored
8
.gitignore
vendored
@ -1,2 +1,10 @@
|
||||
# ignore temporary files
|
||||
*~
|
||||
|
||||
# ignore logs and configuration files
|
||||
*.out
|
||||
*.conf
|
||||
|
||||
# ignore racket compile files
|
||||
*.dep
|
||||
*.zo
|
||||
|
@ -13,13 +13,13 @@
|
||||
(define host3 "localhost")
|
||||
(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 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 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))
|
||||
|
||||
; custodian for client connections
|
||||
|
24
Hermes/modules/general.rkt
Normal file
24
Hermes/modules/general.rkt
Normal 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)])))
|
||||
|
@ -1,6 +1,9 @@
|
||||
#lang racket
|
||||
|
||||
(require "modules/general.rkt")
|
||||
(require math/base) ;; for random number generation
|
||||
|
||||
|
||||
;; globals
|
||||
(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.")
|
||||
@ -72,27 +75,6 @@
|
||||
; semaphore to control access to c-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
|
||||
(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))
|
||||
|
Loading…
Reference in New Issue
Block a user