git.iko.soy/distributed-process
on master help sign in
43 lines 1.4 kB history raw
1-- | Like Latency, but creating lots of channels
2import System.Environment
3import Control.Monad
4import Control.Applicative
5import Control.Distributed.Process
6import Control.Distributed.Process.Node
7import Network.Transport.TCP (createTransport, defaultTCPAddr, defaultTCPParameters)
8import Data.Binary (encode, decode)
9import qualified Data.ByteString.Lazy as BSL
11pingServer :: Process ()
12pingServer = forever $ do
13 them <- expect
14 sendChan them ()
15 -- TODO: should this be automatic?
16 reconnectPort them
18pingClient :: Int -> ProcessId -> Process ()
19pingClient n them = do
20 replicateM_ n $ do
21 (sc, rc) <- newChan :: Process (SendPort (), ReceivePort ())
22 send them sc
23 receiveChan rc
24 liftIO . putStrLn $ "Did " ++ show n ++ " pings"
26initialProcess :: String -> Process ()
27initialProcess "SERVER" = do
28 us <- getSelfPid
29 liftIO $ BSL.writeFile "pingServer.pid" (encode us)
30 pingServer
31initialProcess "CLIENT" = do
32 n <- liftIO $ getLine
33 them <- liftIO $ decode <$> BSL.readFile "pingServer.pid"
34 pingClient (read n) them
36main :: IO ()
37main = do
38 [role, host, port] <- getArgs
39 trans <- createTransport (defaultTCPAddr host port) defaultTCPParameters
40 case trans of
41 Right transport -> do node <- newLocalNode transport initRemoteTable
42 runProcess node $ initialProcess role
43 Left other -> error $ show other