git.iko.soy/distributed-process
on remove-table-api help sign in
41 lines 1.2 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, 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 Right transport <- createTransport host port defaultTCPParameters
40 node <- newLocalNode transport initRemoteTable
41 runProcess node $ initialProcess role