git.iko.soy/distributed-process
on 3606f3d help sign in
45 lines 1.3 kB history raw
1-- | Like Throughput, but send every ping from a different process
2-- (i.e., require a lightweight connection per ping)
3import System.Environment
4import Control.Monad
5import Control.Applicative
6import Control.Distributed.Process
7import Control.Distributed.Process.Node
8import Network.Transport.TCP (createTransport, defaultTCPParameters)
9import Data.Binary (encode, decode)
10import qualified Data.ByteString.Lazy as BSL
12counter :: Process ()
13counter = go 0
14 where
15 go :: Int -> Process ()
16 go !n = do
17 b <- expect
18 case b of
19 Nothing -> go (n + 1)
20 Just them -> send them n >> go 0
22count :: Int -> ProcessId -> Process ()
23count n them = do
24 us <- getSelfPid
25 replicateM_ n . spawnLocal $ send them (Nothing :: Maybe ProcessId)
26 send them (Just us)
27 n' <- expect
28 liftIO $ print (n == n')
30initialProcess :: String -> Process ()
31initialProcess "SERVER" = do
32 us <- getSelfPid
33 liftIO $ BSL.writeFile "counter.pid" (encode us)
34 counter
35initialProcess "CLIENT" = do
36 n <- liftIO $ getLine
37 them <- liftIO $ decode <$> BSL.readFile "counter.pid"
38 count (read n) them
40main :: IO ()
41main = do
42 [role, host, port] <- getArgs
43 Right transport <- createTransport host port defaultTCPParameters
44 node <- newLocalNode transport initRemoteTable
45 runProcess node $ initialProcess role