netserver/Miscutils.hs
changeset 849 82ac0596aa3c
child 851 8ffa4ad0d8ea
equal deleted inserted replaced
848:b31b72756927 849:82ac0596aa3c
       
     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 
       
     9 sendMsg :: Handle -> String -> IO()
       
    10 sendMsg clientHandle str = finally (return ()) (hPutStrLn clientHandle str >> hFlush clientHandle) -- catch exception when client tries to send to other
       
    11 
       
    12 sendAll :: [Handle] -> String -> IO[()]
       
    13 sendAll clientsList str = mapM (\x -> sendMsg x str) clientsList
       
    14 
       
    15 sendOthers :: [Handle] -> Handle -> String -> IO[()]
       
    16 sendOthers clientsList clientHandle str = sendAll (filter (/= clientHandle) clientsList) str
       
    17 
       
    18 extractCmd :: String -> (String, [String])
       
    19 extractCmd str = if ws == [] then ("", []) else (head ws, tail ws)
       
    20 		where ws = words str
       
    21 
       
    22 manipState :: TVar[a] -> ([a] -> [a]) -> IO()
       
    23 manipState state op =
       
    24 	atomically $ do
       
    25 			ls <- readTVar state
       
    26 			writeTVar state $ op ls
       
    27