module Control.Concurrent.Network.Slave
(
NCContext(..)
, initSlave
, slaveID
, numSlaves
, printMsg
) where
import Control.Concurrent
import Control.Concurrent.Network.Protocol
import System.Log.Logger
import System.IO
import Network
data NCContext = NCC { hdl :: MVar Handle }
initSlave :: HostName -> PortID -> IO NCContext
initSlave h p = do
debugM rootLoggerName "Initialise slave"
hdl <- connectTo h p
hSetBuffering hdl NoBuffering
hdl' <- newMVar hdl
return NCC { hdl = hdl' }
slaveID :: NCContext -> IO Int
slaveID nc = do
hdl' <- takeMVar (hdl nc)
writeProtoId hdl' SID
res <- readBinary hdl'
putMVar (hdl nc) hdl'
return res
numSlaves :: NCContext -> IO Int
numSlaves nc = do
hdl' <- takeMVar (hdl nc)
writeProtoId hdl' NSL
res <- readBinary hdl'
putMVar (hdl nc) hdl'
return res
printMsg :: NCContext -> String -> IO ()
printMsg nc msg = do
hdl' <- takeMVar (hdl nc)
writeProtoId hdl' PMS
writeBinary hdl' msg
putMVar (hdl nc) hdl'