Skip to content

Instantly share code, notes, and snippets.

@jeremyjh
Created December 9, 2016 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyjh/416f50f33a6c65511672b53013392239 to your computer and use it in GitHub Desktop.
Save jeremyjh/416f50f33a6c65511672b53013392239 to your computer and use it in GitHub Desktop.
module Main where
import Control.Distributed.Process hiding (call)
import Control.Distributed.Process.Node as Node (initRemoteTable, runProcess, newLocalNode)
import Control.Distributed.Process.Extras (resolve, __remoteTable)
import Network.Transport.TCP
import Control.Concurrent (threadDelay)
import Control.Monad
import Network.Transport hiding (send)
import System.Environment
import Data.ByteString.Char8 as BS
main :: IO ()
main = do
args <- getArgs
case args of
["server", host, port] ->
startServer host port
["client", host, port, serv] ->
startClient host port serv
where
startServer h p
= do startCommon h p $ do
register "foo" =<< getSelfPid
forever $ liftIO $ threadDelay 10000
startClient h p srv
= do startCommon h p $ do
let n = makeNodeId srv
liftIO $ print n
Just s <- resolve (n, "foo")
liftIO $ print s
send s ()
startCommon h p k
= do Right tcp <- createTransport h p defaultTCPParameters
n <- newLocalNode tcp (__remoteTable initRemoteTable )
Node.runProcess n k
makeNodeId :: String -> NodeId
makeNodeId addr = NodeId . EndPointAddress . BS.concat $ [BS.pack addr, BS.pack ":0"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment