@@ -0,0 +1,46 @@
1+import System.Environment
2+import Control.Monad
3+import Control.Applicative
4+import Control.Distributed.Process
5+import Control.Distributed.Process.Node
6+import Network.Transport.TCP (createTransport, defaultTCPParameters)
7+import Data.Binary (encode, decode)
8+import qualified Data.ByteString.Lazy as BSL
9+
10+counter :: Process ()
11+counter = go 0
12+ where
13+ go :: Int -> Process ()
14+ go !n = do
15+ b <- expect
16+ case b of
17+ Nothing -> go (n + 1)
18+ Just them -> send them n
19+
20+count :: Int -> ProcessId -> Process ()
21+count 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')
27+
28+main :: IO ()
29+main = do
30+ args <- getArgs
31+ case args of
32+ ["server", host, port] -> do
33+ Right transport <- createTransport host port defaultTCPParameters
34+ node <- newLocalNode transport initRemoteTable
35+ runProcess node $ do
36+ us <- getSelfPid
37+ liftIO $ BSL.writeFile "counter.pid" (encode us)
38+ counter
39+ ["client", host, port, n] -> do
40+ Right transport <- createTransport host port defaultTCPParameters
41+ node <- newLocalNode transport initRemoteTable
42+ runProcess node $ do
43+ them <- liftIO $ decode <$> BSL.readFile "counter.pid"
44+ count (read n) them
45+ _ -> error "Invalid arguments"
46+