raketka-1.2.0: src/Control/Distributed/Raketka/Master.hs
module Control.Distributed.Raketka.Master where
import Control.Distributed.Process as P
import Control.Monad
import Control.Distributed.Process.Backend.SimpleLocalnet
import Control.Distributed.Raketka.Type.Arg as T
import Control.Distributed.Raketka.Type.Server
import Control.Distributed.Raketka.NodeId as N
{-| * registers own pid
* calls 'whereisRemoteAsync' for each suggested peer
* calls 'startServer'
-}
master :: forall tag ps s c.
Specific tag ps s c =>
Backend
-> Cluster -- ^ server ids from config
-> tag Int -- ^ this server's idx in cluster
-> s -- ^ init custom state
-> Process ()
master backend0 (Cluster ids0) idx0 state0 = do
mynode1 <- getSelfNode
let peers1 = N.nodeId <$> ids0
thisServerId :: ServerId = ids0 !! (untag idx0)
thisServerIdTagged :: tag ServerId = tag $ thisServerId
service1 = T.service thisServerId
peers2 = filter (/= mynode1) peers1
mypid1 <- getSelfPid
register service1 mypid1
forM_ peers2 $ \(peer1::NodeId) -> P.whereisRemoteAsync peer1 service1
startServer thisServerIdTagged state0