Clients can now whisper to each other /whisper

This commit is contained in:
Ibrahim Mkusa 2017-04-16 16:24:16 -04:00
parent 5d36ba1d5d
commit 61e6c97317
2 changed files with 45 additions and 6 deletions

View File

@ -1,5 +1,5 @@
FEATURES FEATURES
5. command(whisper, color, quit, count, users), message parsable? 5. command(whisper, count, users), message parsable? parse in the client side should do something similar for settings (color, quit)
16. plain tcp -> ssl based 16. plain tcp -> ssl based
17. fix breaks for improper disconnects from clients 17. fix breaks for improper disconnects from clients
18. Add topics after project completion 18. Add topics after project completion

View File

@ -150,6 +150,16 @@
(sleep 1360) (sleep 1360)
(custodian-shutdown-all cust))))) (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) (define (chat_with_client in out)
; deals with queueing incoming messages for server to broadcast to all clients ; deals with queueing incoming messages for server to broadcast to all clients
(define (something-to-say in) (define (something-to-say in)
@ -166,11 +176,36 @@
;(exit) ;(exit)
(kill-thread (current-thread))] (kill-thread (current-thread))]
[(string? evt-t0) [(string? evt-t0)
(semaphore-wait messages-s) ; use regexes to evaluate received input from client
; append the message to list of messages NO NEED done during broadcast (define whisper (regexp-match #px"(.*)/whisper\\s+(\\w+)\\s+(.*)" evt-t0)) ; is client trying to whisper to someone
; (displayln-safe evt-t0 convs-out-s convs-out) (define list-count (regexp-match #px"(^/list)\\s+(count).*" evt-t0)) ;; is client asking for number of logged in users
((c-messages 'add) evt-t0) (define list-users (regexp-match #px"(^/list)\\s+(users).*" evt-t0)) ;; user names
(semaphore-post messages-s)] (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)]
[else
(displayln-safe evt-t0)
(semaphore-wait messages-s)
; evaluate it .
((c-messages 'add) evt-t0)
(semaphore-post messages-s)])]
[else [else
(displayln-safe "Timeout waiting. Nothing received from client")])) (displayln-safe "Timeout waiting. Nothing received from client")]))
@ -189,6 +224,10 @@
(define (get-input-port ports) (define (get-input-port ports)
(cadr ports)) (cadr ports))
; extract username
(define (get-username ports)
(car ports))
; broadcasts received message from clients periodically ; broadcasts received message from clients periodically
; TODO before broadcasting the message make sure the ports is still open ; TODO before broadcasting the message make sure the ports is still open
; no EOF if it is remove client from connections ; no EOF if it is remove client from connections