netserver/Miscutils.hs
author unc0rr
Sun, 27 Apr 2008 10:14:00 +0000
changeset 878 45bff6dadfce
parent 852 f756a1d3324c
child 889 3bf9dc791f45
permissions -rw-r--r--
- Fix baseball bat - Small formatting changes in newhwserv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
849
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
     1
module Miscutils where
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
     2
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
     3
import IO
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
     4
import System.IO
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
     5
import Control.Concurrent
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
     6
import Control.Concurrent.STM
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
     7
import Control.Exception (finally)
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
     8
851
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
     9
data ClientInfo =
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    10
	ClientInfo
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    11
	{
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    12
		handle :: Handle,
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    13
		nick :: String,
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    14
		room :: String,
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    15
		isMaster :: Bool
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    16
	}
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    17
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    18
data RoomInfo =
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    19
	RoomInfo
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    20
	{
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    21
		name :: String,
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    22
		password :: String
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    23
	}
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    24
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    25
849
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    26
sendMsg :: Handle -> String -> IO()
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    27
sendMsg clientHandle str = finally (return ()) (hPutStrLn clientHandle str >> hFlush clientHandle) -- catch exception when client tries to send to other
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    28
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    29
sendAll :: [Handle] -> String -> IO[()]
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    30
sendAll clientsList str = mapM (\x -> sendMsg x str) clientsList
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    31
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    32
sendOthers :: [Handle] -> Handle -> String -> IO[()]
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    33
sendOthers clientsList clientHandle str = sendAll (filter (/= clientHandle) clientsList) str
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    34
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    35
extractCmd :: String -> (String, [String])
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    36
extractCmd str = if ws == [] then ("", []) else (head ws, tail ws)
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    37
		where ws = words str
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    38
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    39
manipState :: TVar[a] -> ([a] -> [a]) -> IO()
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    40
manipState state op =
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    41
	atomically $ do
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    42
			ls <- readTVar state
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    43
			writeTVar state $ op ls
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    44
852
f756a1d3324c Handle the case when the room is already created by someone else
unc0rr
parents: 851
diff changeset
    45
manipState2 :: TVar[ClientInfo] -> TVar[RoomInfo] -> ([ClientInfo] -> [RoomInfo] -> ([ClientInfo], [RoomInfo], Bool)) -> IO Bool
851
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    46
manipState2 state1 state2 op =
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    47
	atomically $ do
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    48
			ls1 <- readTVar state1
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    49
			ls2 <- readTVar state2
852
f756a1d3324c Handle the case when the room is already created by someone else
unc0rr
parents: 851
diff changeset
    50
			let (ol1, ol2, res) = op ls1 ls2
851
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    51
			writeTVar state1 ol1
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    52
			writeTVar state2 ol2
852
f756a1d3324c Handle the case when the room is already created by someone else
unc0rr
parents: 851
diff changeset
    53
			return res
851
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    54