@@ -15,7 +15,7 @@ counter = go 0
1515 b <- expect
1616 case b of
1717 Nothing -> go (n + 1)
18− Just them -> send them n
18+ Just them -> send them n >> go 0
1919
2020count :: Int -> ProcessId -> Process ()
2121count n them = do
@@ -25,22 +25,19 @@ count n them = do
2525 n' <- expect
2626 liftIO $ print (n == n')
2727
28+initialProcess :: String -> Process ()
29+initialProcess "SERVER" = do
30+ us <- getSelfPid
31+ liftIO $ BSL.writeFile "counter.pid" (encode us)
32+ counter
33+initialProcess "CLIENT" = do
34+ n <- liftIO $ getLine
35+ them <- liftIO $ decode <$> BSL.readFile "counter.pid"
36+ count (read n) them
37+
2838main :: IO ()
2939main = 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−
40+ [role, host, port] <- getArgs
41+ Right transport <- createTransport host port defaultTCPParameters
42+ node <- newLocalNode transport initRemoteTable
43+ runProcess node $ initialProcess role