Erlang-style Distributed Haskell: Counter Example

Multiple clients can connect to the server and send requests for incrementing, decrementing or displaying the value of the global counter.

The server:

> import Distributed > > data Msg = Inc | Dec | GetValue Pid | Value Int > deriving (Show, Read) > instance Serialize Msg > > counter' n = do > receive (\v -> case v of > Inc -> counter' (n+1) > Dec -> counter' (n-1) > GetValue pid -> do > pid <!> (Value n) > counter' n > )

Main part of the server using the preprocessor:

> receive > Inc -> counter' (n+1) > Dec -> counter' (n-1) > GetValue pid -> do > pid <!> (Value n) > counter' n

The client:

> import Distributed > > data Msg = Value Int | GetValue Pid | Inc | Dec > deriving (Show, Read) > instance Serialize Msg > > destHost = "localhost" > destNode = "Counter-Node" > > client :: Pid -> DIO Msg () > client pid = do > proc $ putStrLn "i,d,v?" > str <- proc $ getLine > case str of > "i" -> do remoteSend destHost destNode "Counter" Inc > client pid > "d" -> do remoteSend destHost destNode "Counter" Dec > client pid > "v" -> do me <- self > remoteSend destHost destNode "Counter" (GetValue me) > receive (\v -> case v of > Value v -> do > proc (putStrLn ("\n"++(show v))) > client pid > ) > _ -> client pid
Volker Stolz
Last modified: Thu Jan 25 11:06:15 CET 2001