git.iko.soy/distributed-process
on 7930062 help sign in
39 lines 977 B history raw
1{-# LANGUAGE BangPatterns, TemplateHaskell #-}
3import Control.Monad
4import Control.Applicative
5import Control.Monad.IO.Class (liftIO)
6import qualified Data.ByteString.Lazy as BSL
7import Data.Binary (encode, decode)
8import Remote
10counter :: ProcessM ()
11counter = go 0
12 where
13 go :: Int -> ProcessM ()
14 go !n = do
15 b <- expect
16 case b of
17 Nothing -> go (n + 1)
18 Just them -> send them n >> go 0
20count :: Int -> ProcessId -> ProcessM ()
21count n them = do
22 us <- getSelfPid
23 replicateM_ n $ send them (Nothing :: Maybe ProcessId)
24 send them (Just us)
25 n' <- expect
26 liftIO $ print (n == n')
28initialProcess :: String -> ProcessM ()
29initialProcess "SERVER" = do
30 us <- getSelfPid
31 liftIO $ BSL.writeFile "counter.pid" (encode us)
32 counter
33initialProcess "CLIENT" = do
34 n <- liftIO $ getLine
35 them <- liftIO $ decode <$> BSL.readFile "counter.pid"
36 count (read n) them
38main :: IO ()
39main = remoteInit (Just "config") [] initialProcess