Added counter to keep track number of connected clients
This commit is contained in:
parent
4c26f1eaa3
commit
cffd7a4299
@ -6,5 +6,8 @@
|
|||||||
6. keep count of connected clients using object orientation
|
6. keep count of connected clients using object orientation
|
||||||
7. maybe fiddle around with irc library
|
7. maybe fiddle around with irc library
|
||||||
8. separate main running code from definitions
|
8. separate main running code from definitions
|
||||||
9. closure connections, messages, threads. Avoid using set! without an object
|
**9. closure connections, messages, threads. Avoid using set! without an object
|
||||||
like make-account
|
like make-account
|
||||||
|
make own count to deal with closures
|
||||||
|
10. authentication for databases
|
||||||
|
11. user can ask for no of logged in users. Server has to parse
|
||||||
|
@ -4,10 +4,35 @@
|
|||||||
;; TODO wrap "safer send in a function that takes care of semaphores"
|
;; TODO wrap "safer send in a function that takes care of semaphores"
|
||||||
|
|
||||||
;; globals
|
;; globals
|
||||||
;; must control access via semaphore as listener thread or broadcast thread
|
; track number of connections with closure
|
||||||
;; might need to access it
|
(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))
|
||||||
|
(define c-count-s (make-semaphore 1))
|
||||||
|
|
||||||
(define connections '()) ;; maintains a list of open ports
|
(define connections '()) ;; maintains a list of open ports
|
||||||
;; ((in1, out1), (in2, out2), (in3, out3), (in4, out4) ...)
|
|
||||||
|
(define (make-connections connections)
|
||||||
|
(define (null-cons?)
|
||||||
|
(null? connections))
|
||||||
|
(define (add in out)
|
||||||
|
(set! connections (append connections (list (list in out))))
|
||||||
|
connections)
|
||||||
|
(define (cons-list)
|
||||||
|
connections))
|
||||||
|
|
||||||
(define connections-s (make-semaphore 1)) ;; control access to connections
|
(define connections-s (make-semaphore 1)) ;; control access to connections
|
||||||
|
|
||||||
;; every 5 seconds run to broadcast top message in list
|
;; every 5 seconds run to broadcast top message in list
|
||||||
@ -54,7 +79,11 @@
|
|||||||
(define cust (make-custodian))
|
(define cust (make-custodian))
|
||||||
(parameterize ([current-custodian cust])
|
(parameterize ([current-custodian cust])
|
||||||
(define-values (in out) (tcp-accept listener))
|
(define-values (in out) (tcp-accept listener))
|
||||||
;; TODO
|
; increment number of connections
|
||||||
|
(semaphore-wait c-count-s)
|
||||||
|
((c-count 'increment))
|
||||||
|
(semaphore-post c-count-s)
|
||||||
|
|
||||||
(displayln-safe (string-append
|
(displayln-safe (string-append
|
||||||
"Successfully connected to a client.\n"
|
"Successfully connected to a client.\n"
|
||||||
"Sending client a welcome message.")
|
"Sending client a welcome message.")
|
||||||
@ -88,8 +117,11 @@
|
|||||||
(cond [(eof-object? evt-t0)
|
(cond [(eof-object? evt-t0)
|
||||||
(displayln-safe "Connection closed. EOF received"
|
(displayln-safe "Connection closed. EOF received"
|
||||||
stdout)
|
stdout)
|
||||||
(exit)
|
(semaphore-wait c-count-s)
|
||||||
]
|
((c-count 'decrement))
|
||||||
|
(semaphore-post c-count-s)
|
||||||
|
;(exit)
|
||||||
|
(kill-thread (current-thread))]
|
||||||
[(string? evt-t0)
|
[(string? evt-t0)
|
||||||
(semaphore-wait messages-s)
|
(semaphore-wait messages-s)
|
||||||
; append the message to list of messages
|
; append the message to list of messages
|
||||||
|
Loading…
x
Reference in New Issue
Block a user