aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIbrahim Mkusa <ibrahimmkusa@gmail.com>2017-04-15 20:56:47 -0400
committerIbrahim Mkusa <ibrahimmkusa@gmail.com>2017-04-15 20:56:47 -0400
commit413d13d7a65fdb6797903a0c56808730fd265b37 (patch)
treed132f51e29173d464ae13ce5f02ffa57897bc775
parent11bcec105200eef5da68bce4f1952338d204d8c1 (diff)
created modules/general.rkt to house common functions
-rw-r--r--.gitignore8
-rw-r--r--Hermes/client.rkt6
-rw-r--r--Hermes/modules/general.rkt24
-rw-r--r--Hermes/server.rkt24
4 files changed, 38 insertions, 24 deletions
diff --git a/.gitignore b/.gitignore
index 49a9d25..7c07843 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,10 @@
# ignore temporary files
*~
+
+# ignore logs and configuration files
+*.out
+*.conf
+
+# ignore racket compile files
+*.dep
+*.zo
diff --git a/Hermes/client.rkt b/Hermes/client.rkt
index 5e99a96..9f20dc4 100644
--- a/Hermes/client.rkt
+++ b/Hermes/client.rkt
@@ -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
diff --git a/Hermes/modules/general.rkt b/Hermes/modules/general.rkt
new file mode 100644
index 0000000..b33eb8a
--- /dev/null
+++ b/Hermes/modules/general.rkt
@@ -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)])))
+
diff --git a/Hermes/server.rkt b/Hermes/server.rkt
index 54b1384..a309ddc 100644
--- a/Hermes/server.rkt
+++ b/Hermes/server.rkt
@@ -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))