Repository navigation
Expand file tree
/
Copy pathSafeCounter.hs
More file actions
103 lines (85 loc) · 3.31 KB
/
Copy pathSafeCounter.hs
File metadata and controls
103 lines (85 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
module SafeCounter
( startCounter,
getCount,
getCountAsync,
incCount,
resetCount,
wait,
waitTimeout,
Fetch(..),
Increment(..),
Reset(..)
) where
import Control.Distributed.Process hiding (call, say)
import Control.Distributed.Process.Extras
import Control.Distributed.Process.Async
import Control.Distributed.Process.ManagedProcess
( ProcessDefinition(..)
, InitHandler
, InitResult(..)
, defaultProcess
, condition
)
import qualified Control.Distributed.Process.ManagedProcess as ManagedProcess (serve)
import Control.Distributed.Process.ManagedProcess.Client
import Control.Distributed.Process.ManagedProcess.Server.Restricted
import Control.Distributed.Process.Extras.Time
import Control.Distributed.Process.Serializable
import Data.Binary
import Data.Typeable (Typeable)
import GHC.Generics
--------------------------------------------------------------------------------
-- Types --
--------------------------------------------------------------------------------
data Increment = Increment
deriving (Show, Typeable, Generic)
instance Binary Increment where
data Fetch = Fetch
deriving (Show, Typeable, Generic)
instance Binary Fetch where
data Reset = Reset deriving (Show, Typeable, Generic)
instance Binary Reset where
--------------------------------------------------------------------------------
-- API --
--------------------------------------------------------------------------------
-- | Increment count
incCount :: ProcessId -> Process Int
incCount sid = call sid Increment
-- | Get the current count
getCount :: ProcessId -> Process Int
getCount sid = call sid Fetch
-- | Get the current count asynchronously
getCountAsync :: ProcessId -> Process (Async Int)
getCountAsync sid = callAsync sid Fetch
-- | Reset the current count
resetCount :: ProcessId -> Process ()
resetCount sid = cast sid Reset
-- | Start a counter server
startCounter :: Int -> Process ProcessId
startCounter startCount =
let server = serverDefinition
in spawnLocal $ ManagedProcess.serve startCount init' server
where init' :: InitHandler Int Int
init' count = return $ InitOk count Infinity
--------------------------------------------------------------------------------
-- Implementation --
--------------------------------------------------------------------------------
serverDefinition :: ProcessDefinition Int
serverDefinition = defaultProcess {
apiHandlers = [
handleCallIf
(condition (\count Increment -> count >= 10)) -- invariant
(\Increment -> halt :: RestrictedProcess Int (Result Int))
, handleCall handleIncrement
, handleCall (\Fetch -> getState >>= reply)
, handleCast (\Reset -> putState (0 :: Int) >> continue)
]
} :: ProcessDefinition Int
halt :: forall s r . Serializable r => RestrictedProcess s (Result r)
halt = haltNoReply (ExitOther "Count > 10")
handleIncrement :: Increment -> RestrictedProcess Int (Result Int)
handleIncrement _ = modifyState (+1) >> getState >>= reply