Make one's own arrival non-special
This commit is contained in:
parent
daa5336bb2
commit
7303df46ed
|
@ -38,7 +38,7 @@
|
||||||
(define (arrive! connection-id)
|
(define (arrive! connection-id)
|
||||||
(call-with-semaphore active-connection-mutex
|
(call-with-semaphore active-connection-mutex
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(thread-send (current-thread) `(you-are ,connection-id))
|
(thread-send (current-thread) `(arrived ,connection-id))
|
||||||
(for ([c active-connections])
|
(for ([c active-connections])
|
||||||
(thread-send (current-thread) `(arrived ,(cdr c)))
|
(thread-send (current-thread) `(arrived ,(cdr c)))
|
||||||
(thread-send (car c) `(arrived ,connection-id)))
|
(thread-send (car c) `(arrived ,connection-id)))
|
||||||
|
|
|
@ -26,7 +26,7 @@ class ConnectionHandler(StreamRequestHandler):
|
||||||
self.depart()
|
self.depart()
|
||||||
|
|
||||||
def arrive(self):
|
def arrive(self):
|
||||||
self.announce('you are %s' % (self.connection_id,))
|
self.announce('%s arrived' % (self.connection_id,))
|
||||||
with active_connection_mutex:
|
with active_connection_mutex:
|
||||||
for c in active_connections:
|
for c in active_connections:
|
||||||
self.announce('%s arrived' % (c.connection_id,))
|
self.announce('%s arrived' % (c.connection_id,))
|
||||||
|
|
|
@ -50,7 +50,7 @@
|
||||||
(channel-put ch active-connections)
|
(channel-put ch active-connections)
|
||||||
(loop active-connections)]
|
(loop active-connections)]
|
||||||
[`(arrive ,caller ,connection-id)
|
[`(arrive ,caller ,connection-id)
|
||||||
(thread-send caller `(you-are ,connection-id))
|
(thread-send caller `(arrived ,connection-id))
|
||||||
(for ([c active-connections])
|
(for ([c active-connections])
|
||||||
(thread-send caller `(arrived ,(cdr c)))
|
(thread-send caller `(arrived ,(cdr c)))
|
||||||
(thread-send (car c) `(arrived ,connection-id)))
|
(thread-send (car c) `(arrived ,connection-id)))
|
||||||
|
|
|
@ -43,7 +43,7 @@
|
||||||
(let loop ()
|
(let loop ()
|
||||||
(match (thread-receive)
|
(match (thread-receive)
|
||||||
[`(arrive ,caller ,connection-id)
|
[`(arrive ,caller ,connection-id)
|
||||||
(thread-send caller `(you-are ,connection-id))
|
(thread-send caller `(arrived ,connection-id))
|
||||||
(for ([c active-connections])
|
(for ([c active-connections])
|
||||||
(thread-send caller `(arrived ,(cdr c)))
|
(thread-send caller `(arrived ,(cdr c)))
|
||||||
(thread-send (car c) `(arrived ,connection-id)))
|
(thread-send (car c) `(arrived ,connection-id)))
|
||||||
|
|
2
chat.erl
2
chat.erl
|
@ -17,6 +17,7 @@ accept_loop(LSock, IndexPid) ->
|
||||||
index(Connected) ->
|
index(Connected) ->
|
||||||
receive
|
receive
|
||||||
{arrive, Pid} ->
|
{arrive, Pid} ->
|
||||||
|
Pid ! {utterance, {arrive, Pid}},
|
||||||
[begin
|
[begin
|
||||||
P ! {utterance, {arrive, Pid}},
|
P ! {utterance, {arrive, Pid}},
|
||||||
Pid ! {utterance, {arrive, P}}
|
Pid ! {utterance, {arrive, P}}
|
||||||
|
@ -39,7 +40,6 @@ say(Sock, V) ->
|
||||||
|
|
||||||
connection(Sock, IndexPid) ->
|
connection(Sock, IndexPid) ->
|
||||||
IndexPid ! {arrive, self()},
|
IndexPid ! {arrive, self()},
|
||||||
say(Sock, {you_are, self()}),
|
|
||||||
connection_mainloop(Sock, IndexPid).
|
connection_mainloop(Sock, IndexPid).
|
||||||
|
|
||||||
connection_mainloop(Sock, IndexPid) ->
|
connection_mainloop(Sock, IndexPid) ->
|
||||||
|
|
3
chat.hs
3
chat.hs
|
@ -33,11 +33,10 @@ broadcast str conns = sequence_ $ map sendStr conns
|
||||||
where sendStr (_, hdl) = hPutStrLn hdl str
|
where sendStr (_, hdl) = hPutStrLn hdl str
|
||||||
|
|
||||||
arrive index name hdl = do
|
arrive index name hdl = do
|
||||||
hPutStrLn hdl ("you are " ++ name)
|
|
||||||
old <- atomically $ do old <- readTVar index
|
old <- atomically $ do old <- readTVar index
|
||||||
writeTVar index ((name, hdl) : old)
|
writeTVar index ((name, hdl) : old)
|
||||||
return old
|
return old
|
||||||
broadcast (name ++ " arrived") old
|
broadcast (name ++ " arrived") ((name, hdl) : old)
|
||||||
sequence_ $ map (\ (otherName, _) -> hPutStrLn hdl (otherName ++ " arrived")) old
|
sequence_ $ map (\ (otherName, _) -> hPutStrLn hdl (otherName ++ " arrived")) old
|
||||||
|
|
||||||
depart index name hdl = do
|
depart index name hdl = do
|
||||||
|
|
Loading…
Reference in New Issue