netserver/Miscutils.hs
author unc0rr
Wed, 30 Apr 2008 19:44:54 +0000
changeset 890 1d8c4a5ec622
parent 889 3bf9dc791f45
child 894 2ca76a7f3121
permissions -rw-r--r--
- Improve server core - Protocol in separate module
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
	{
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 852
diff changeset
    12
		chan :: TChan String,
851
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    13
		handle :: Handle,
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    14
		nick :: String,
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    15
		room :: String,
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    16
		isMaster :: Bool
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
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    19
data RoomInfo =
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    20
	RoomInfo
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    21
	{
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    22
		name :: String,
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    23
		password :: String
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
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    26
849
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    27
sendMsg :: Handle -> String -> IO()
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    28
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
    29
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    30
sendAll :: [Handle] -> String -> IO[()]
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    31
sendAll clientsList str = mapM (\x -> sendMsg x str) clientsList
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    32
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    33
sendOthers :: [Handle] -> Handle -> String -> IO[()]
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    34
sendOthers clientsList clientHandle str = sendAll (filter (/= clientHandle) clientsList) str
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    35
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    36
extractCmd :: String -> (String, [String])
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    37
extractCmd str = if ws == [] then ("", []) else (head ws, tail ws)
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    38
		where ws = words str
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    39
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    40
manipState :: TVar[a] -> ([a] -> [a]) -> IO()
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    41
manipState state op =
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    42
	atomically $ do
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    43
			ls <- readTVar state
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    44
			writeTVar state $ op ls
82ac0596aa3c Start work on standalone server in Haskell
unc0rr
parents:
diff changeset
    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
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    47
manipState2 state1 state2 op =
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    48
	atomically $ do
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    49
			ls1 <- readTVar state1
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    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
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    52
			writeTVar state1 ol1
8ffa4ad0d8ea Introduce function to atomically change both lists
unc0rr
parents: 849
diff changeset
    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
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 852
diff changeset
    55
890
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    56
tselect :: [ClientInfo] -> STM (String, ClientInfo)
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    57
tselect = foldl orElse retry . map (\ci -> (flip (,) ci) `fmap` readTChan (chan ci))
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 852
diff changeset
    58