public
Created

  • Download Gist
Remoting.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
{-
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"
config
1 2
cfgRole MASTER
cfgHostName localhost

To run the master/slave version:

mkdir slave
cat config | sed s/MASTER/SLAVE/ > slave/config
ghc --make Remoting.hs -o remoting
cd slave
../remoting &
cd ..
./remoting

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.