author | unc0rr |
Wed, 30 Apr 2008 20:18:30 +0000 | |
changeset 892 | dfe97199f17e |
parent 890 | 1d8c4a5ec622 |
child 894 | 2ca76a7f3121 |
permissions | -rw-r--r-- |
849 | 1 |
module Miscutils where |
2 |
||
3 |
import IO |
|
4 |
import System.IO |
|
5 |
import Control.Concurrent |
|
6 |
import Control.Concurrent.STM |
|
7 |
import Control.Exception (finally) |
|
8 |
||
851 | 9 |
data ClientInfo = |
10 |
ClientInfo |
|
11 |
{ |
|
889 | 12 |
chan :: TChan String, |
851 | 13 |
handle :: Handle, |
14 |
nick :: String, |
|
15 |
room :: String, |
|
16 |
isMaster :: Bool |
|
17 |
} |
|
18 |
||
19 |
data RoomInfo = |
|
20 |
RoomInfo |
|
21 |
{ |
|
22 |
name :: String, |
|
23 |
password :: String |
|
24 |
} |
|
25 |
||
26 |
||
849 | 27 |
sendMsg :: Handle -> String -> IO() |
28 |
sendMsg clientHandle str = finally (return ()) (hPutStrLn clientHandle str >> hFlush clientHandle) -- catch exception when client tries to send to other |
|
29 |
||
30 |
sendAll :: [Handle] -> String -> IO[()] |
|
31 |
sendAll clientsList str = mapM (\x -> sendMsg x str) clientsList |
|
32 |
||
33 |
sendOthers :: [Handle] -> Handle -> String -> IO[()] |
|
34 |
sendOthers clientsList clientHandle str = sendAll (filter (/= clientHandle) clientsList) str |
|
35 |
||
36 |
extractCmd :: String -> (String, [String]) |
|
37 |
extractCmd str = if ws == [] then ("", []) else (head ws, tail ws) |
|
38 |
where ws = words str |
|
39 |
||
40 |
manipState :: TVar[a] -> ([a] -> [a]) -> IO() |
|
41 |
manipState state op = |
|
42 |
atomically $ do |
|
43 |
ls <- readTVar state |
|
44 |
writeTVar state $ op ls |
|
45 |
||
852
f756a1d3324c
Handle the case when the room is already created by someone else
unc0rr
parents:
851
diff
changeset
|
46 |
manipState2 :: TVar[ClientInfo] -> TVar[RoomInfo] -> ([ClientInfo] -> [RoomInfo] -> ([ClientInfo], [RoomInfo], Bool)) -> IO Bool |
851 | 47 |
manipState2 state1 state2 op = |
48 |
atomically $ do |
|
49 |
ls1 <- readTVar state1 |
|
50 |
ls2 <- readTVar state2 |
|
852
f756a1d3324c
Handle the case when the room is already created by someone else
unc0rr
parents:
851
diff
changeset
|
51 |
let (ol1, ol2, res) = op ls1 ls2 |
851 | 52 |
writeTVar state1 ol1 |
53 |
writeTVar state2 ol2 |
|
852
f756a1d3324c
Handle the case when the room is already created by someone else
unc0rr
parents:
851
diff
changeset
|
54 |
return res |
889 | 55 |
|
890 | 56 |
tselect :: [ClientInfo] -> STM (String, ClientInfo) |
57 |
tselect = foldl orElse retry . map (\ci -> (flip (,) ci) `fmap` readTChan (chan ci)) |
|
889 | 58 |