socketson-0.1.0.0: src/Network/Socketson/ServerState.hs
module Network.Socketson.ServerState where
-- mvar:
-- import Control.Concurrent.MVar
-- random numbers:
import Crypto.Random.DRBG
-- | The inner state of the `socketson` service.
data ServerState =
ServerState { storePath :: FilePath
-- ^ path to the session data storage
, maxClients :: Int
-- ^ number of maximal allowed clients
, connectedClients :: Int
-- ^ number of connected clients
, randomGen :: CtrDRBG
-- ^ the random generator, used for generating session keys.
}
{-| Opens a new server state with given parameters. Initializes a fresh random number generator to generate session keys.
newState pathToSessionStore maxClients
-}
newState :: FilePath -> Int -> IO ServerState
newState fp maxcl =
do --plock <- newMVar fp
g <- newGenIO :: IO CtrDRBG
return (ServerState fp maxcl 0 g)
------
-- utility
-- | Increments the number of connected clients.
incClients :: ServerState -> ServerState
incClients st = st { connectedClients = connectedClients st + 1 }
-- | Decrements the number of connected clients.
decClients :: ServerState -> ServerState
decClients st = st { connectedClients = connectedClients st - 1 }
-- | Checks if there is still capacity for clients to connect.
existsCapacity :: ServerState -> Bool
existsCapacity st = (maxClients st - connectedClients st) > 0