aboutsummaryrefslogtreecommitdiff
path: root/Hermes/server.rkt
blob: de615a5e369475e53ec737f6c9a76d337fbd1fd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#lang racket

(require "modules/general.rkt") ;; common function(s)
(require math/base) ;; for random number generation


(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 sleep-t 0.1)

; track number of connections with closure
(define (make-count no-count)
  (define (increment)
    (set! no-count (+ no-count 1))
    no-count)
  (define (decrement)
    (set! no-count (- no-count 1))
    no-count)
  (define (current-count)
    no-count)
  (define (dispatch m)
    (cond [(eq? m 'increment) increment]
          [(eq? m 'decrement) decrement]
          [(eq? m 'current-count) current-count]))
  dispatch)
(define c-count (make-count 0))
; a semaphore to control access to c-count
(define c-count-s (make-semaphore 1))


; track list of input output port pairs in a list contained in a closure
(define (make-connections connections)
  (define (null-cons?)
    (null? connections))
   (define (add username in out)
    (set! connections (append connections (list (list username in out))))
    connections)
   (define (cons-list)
     connections)
   (define (remove-ports in out)
     (set! connections
       (filter 
         (lambda (ports)
           (if (and (eq? in (get-input-port ports))
                    (eq? out (get-output-port ports)))
             #f
             #t))
         connections)))
   (define (dispatch m)
     (cond [(eq? m 'null-cons) null-cons?]
           [(eq? m 'cons-list) cons-list]
           [(eq? m 'remove-ports) remove-ports]
           [(eq? m 'add) add]))
   dispatch)
; "instantiate" to track the connections
(define c-connections (make-connections '()))
; a semaphore to control acess to c-connections
(define connections-s (make-semaphore 1)) ;; control access to connections

; Track received messages in a closure. Initialy messages is '()
(define (make-messages messages)
  (define (add message)
    (set! messages (append messages (list message)))
    messages)
  (define (mes-list)
    messages)
  (define (remove-top)
    (set! messages (rest messages))
    messages)
  (define (dispatch m)
    (cond [(eq? m 'add) add]
          [(eq? m 'mes-list) mes-list]
          [(eq? m 'remove-top) remove-top]))
  dispatch)
; "instantiate" a make-message variable to track our messages
(define c-messages (make-messages '()))
; semaphore to control access to c-messages
(define messages-s (make-semaphore 1))  ;; control access to messages

; two files to store error messages, and channel conversations
(define error-out (open-output-file "./error_server.txt" #:exists 'append))
(define convs-out (open-output-file "./conversations_server.txt" #:exists 'append))
(define error-out-s (make-semaphore 1))
(define convs-out-s (make-semaphore 1))

; Main server code wrapped in a function
(define (serve port-no)
  ; custodian manages resources put under its domain
  (define main-cust (make-custodian))
  ; "parameterize" puts resources under the domain of created custodian
  (parameterize ([current-custodian main-cust])
    (define listener (tcp-listen port-no 5 #t))
    (define (loop)
      (receive-clients listener)
      (loop))
    (displayln-safe "Starting up the listener." error-out-s error-out)
    (thread loop)
    (displayln-safe "Listener successfully started." error-out-s error-out)
    ;; Create a thread whose job is to simply call broadcast iteratively
    (thread (lambda ()
              (displayln-safe "Broadcast thread started!\n")
              (let loopb []
                (sleep sleep-t)  ;; wait 0.5 secs before beginning to broadcast
                (broadcast)
                (loopb)))))
  (lambda ()
    (displayln-safe "Goodbye, shutting down all services" error-out-s error-out)
    (semaphore-wait error-out-s)
    (semaphore-wait convs-out-s)
    (close-output-port error-out)
    (close-output-port convs-out)
    (semaphore-post error-out-s)
    (semaphore-post convs-out-s)
    (custodian-shutdown-all main-cust)))

(define (receive-clients listener)
  (define cust (make-custodian))
  (parameterize ([current-custodian cust])
    (define-values (in out) (tcp-accept listener))

    ; TODO do some error checking
    (define username-evt (sync (read-line-evt in 'linefeed)))
    

    
    ; increment number of connections
    (semaphore-wait c-count-s)
    ((c-count 'increment))
    (semaphore-post c-count-s)

    (displayln-safe successful-connection-m)
    (displayln welcome-message out)
    ;; print to server log and client
    (define print-no-users (string-append "Number of users in chat: "
                                          (number->string ((c-count 'current-count)))))
    (displayln print-no-users out)
    (displayln-safe print-no-users convs-out-s convs-out)
    (flush-output out)
    (semaphore-wait connections-s)
    ; TODO add in a username so we have (username input output)
    ((c-connections 'add) username-evt in out)
    (semaphore-post connections-s)

    ; start a thread to deal with specific client and add descriptor value to the list of threads
    (define threadcom (thread (lambda ()
              (chat_with_client in out)))) ; comms between server and particular client

    ;; Watcher thread:
    ;; kills current thread for waiting too long for connection from
    (thread (lambda ()
              (displayln-safe (string-append
                                "Started a thread to kill hanging "
                                "connecting threads"))
              (sleep 1360)
              (custodian-shutdown-all cust)))))

; whisper selector for the username and message
(define (whisper-info exp)
  (cadr exp))

(define (whisper-to exp)
  (caddr exp))

(define (whisper-message exp)
  (cadddr exp))

(define (chat_with_client in out) 
  ; deals with queueing incoming messages for server to broadcast to all clients
  (define (something-to-say in)
    (define evt-t0 (sync  (read-line-evt in 'linefeed)))
    (cond [(eof-object? evt-t0)
           (semaphore-wait connections-s)
           ((c-connections 'remove-ports) in out)
           (semaphore-post connections-s)
           ; TODO some form of identification for this client
           (displayln-safe "Connection closed. EOF received" error-out-s error-out)
           (semaphore-wait c-count-s)
           ((c-count 'decrement))
           (semaphore-post c-count-s)
           ;(exit)
           (kill-thread (current-thread))]
          [(string? evt-t0)
           ; use regexes to evaluate received input from client
           (define whisper (regexp-match #px"(.*)/whisper\\s+(\\w+)\\s+(.*)" evt-t0)) ; is client trying to whisper to someone
           (define list-count  (regexp-match #px"(.*)/list\\s+count\\s*" evt-t0)) ;; is client asking for number of logged in users
           (define list-users (regexp-match #px"(.*)/list\\s+users\\s*" evt-t0)) ;; user names
           ; do something whether it was a message, a whisper, request for number of users and so on
           ; TODO if user doesn't exist handle it********
           (cond [whisper
                  (semaphore-wait connections-s)
                  ; get output port for user
                  (define that-user-ports
                    (first (filter
                     (lambda (ports)
                       (if (string=? (whisper-to whisper) (get-username ports))
                           #t
                           #f))
                     ((c-connections 'cons-list)))))
                  ; try to send that user the whisper
                  (if (port-closed? (get-output-port that-user-ports))
                      (begin
                        (displayln "User is unavailable" out)
                        (flush-output out))
                      (begin
                        (displayln (string-append (whisper-info whisper) (whisper-message whisper))
                                   (get-output-port that-user-ports))
                        (flush-output (get-output-port that-user-ports))))
                  (semaphore-post connections-s)]
                 [list-count
                  ;;should put a semaphore on connections
                  (semaphore-wait c-count-s)
                  (semaphore-wait connections-s)
                  (define no-of-users (string-append "Number of users in chat: "
                                          (number->string ((c-count 'current-count)))))
                  (displayln no-of-users out)
                  (flush-output out)
                  (semaphore-post connections-s)
                  (semaphore-post c-count-s)
                  ]
                 [list-users
                  (semaphore-wait connections-s)
                  ; map over connections sending the username to the client
                  (displayln "Here is a list of users in chat." out)
                  (map
                   (lambda (ports)
                     (displayln (get-username ports) out))
                   ((c-connections 'cons-list)))
                  (flush-output out)
                  (semaphore-post connections-s)]
                 [else
                  ; Its an ordinarly message
                  ; (displayln-safe evt-t0) debug purposes
                  (semaphore-wait messages-s)
                  ; evaluate it .
                  ((c-messages 'add) evt-t0)
                  (semaphore-post messages-s)])]    
          [else
           (displayln-safe "Timeout waiting. Nothing received from client")]))

  ; Executes methods above in another thread
  (thread (lambda ()
            (let loop []
              (something-to-say in)
              ; (sleep 1)
              (loop)))))

; extracts output port from a list pair of username, input and output port
(define (get-output-port ports)
  (caddr ports))

; extracts input port
(define (get-input-port ports)
  (cadr ports))

; extract username
(define (get-username ports)
  (car ports))

; broadcasts received message from clients periodically
; TODO before broadcasting the message make sure the ports is still open
; no EOF if it is remove client from connections
(define broadcast
  (lambda ()
    (semaphore-wait messages-s)
    (cond [(not (null? ((c-messages 'mes-list))))
        (map
            (lambda (ports)
              (if (not (port-closed? (get-output-port ports)))
                (begin 
                    (displayln (first ((c-messages 'mes-list))) (get-output-port ports))
                    (flush-output (get-output-port ports)))
                (displayln-safe "Failed to broadcast. Port not open." error-out-s error-out)))
            ((c-connections 'cons-list)))
        (displayln-safe (first ((c-messages 'mes-list))) convs-out-s convs-out)
        ;; remove top message from "queue" after broadcasting
        ((c-messages 'remove-top))
        ; debugging displayln below
        ; (displayln "Message broadcasted")
        ]) ; end of cond
    (semaphore-post messages-s)))

(define stop-server (serve 4321)) ;; start server then close with stop
(displayln-safe "Server process started\n" error-out-s error-out)