-
-
Save commandodev/1582108 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cfgRole MASTER | |
cfgHostName localhost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
One process starts the master process | |
Other start do-nothing process | |
master distributes the work among the other processes | |
-} | |
module Main where | |
import Remote | |
import Control.Monad | |
main :: IO () | |
main = remoteInit (Just "config") [] initialProcess | |
{- | |
the argument defines the role. All nodes run from the same executable, the config defines which | |
role the node should assume and then this function branches off depending on the role. | |
-} | |
initialProcess :: String -> ProcessM () | |
initialProcess "MASTER" = masterProcess | |
initialProcess "SLAVE" = slaveProcess | |
initialProcess role = error $ "Unknown role " ++ role | |
masterProcess :: ProcessM () | |
masterProcess = do | |
peers <- getPeers | |
let slaves = findPeerByRole peers "SLAVE" | |
myPid <- getSelfPid | |
forM_ slaves (receiveMessage myPid) | |
receiveMessage :: ProcessId -> NodeId -> ProcessM () | |
receiveMessage myPid node = do | |
Just pid <- nameQuery node "main" | |
send pid myPid | |
msg <- expect | |
say $ "message: " ++ msg | |
slaveProcess :: ProcessM () | |
slaveProcess = do | |
nameSet "main" | |
pid <- expect | |
send pid "PONG" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment