git.iko.soy/distributed-process
on master help sign in
Import some code from the remote package
059daf4Edsko de Vries
distributed-process/src/Control/Distributed/Process.hs+105−5
@@ -50,12 +50,18 @@
50-- [4] "Delivery of Messages", post on erlang-questions
50-- [4] "Delivery of Messages", post on erlang-questions
51-- http://erlang.org/pipermail/erlang-questions/2012-February/064767.html
51-- http://erlang.org/pipermail/erlang-questions/2012-February/064767.html
52module Control.Distributed.Process
52module Control.Distributed.Process
53− ( -- * Cloud Haskell API
53+ ( -- * Basic cloud Haskell API
54 ProcessId
54 ProcessId
55 , Process
55 , Process
56 , expect
56 , expect
57 , send
57 , send
58 , getSelfPid
58 , getSelfPid
59+ , monitorProcess
60+ , linkProcess
61+ -- * Monitoring
62+ , MonitorAction(..)
63+ , ProcessMonitorException(..)
64+ , SignalReason(..)
59 -- * Initialization
65 -- * Initialization
60 , newLocalNode
66 , newLocalNode
61 , forkProcess
67 , forkProcess
@@ -68,18 +74,18 @@ import qualified Data.ByteString.Lazy as BSL ( ByteString
68 , fromChunks
74 , fromChunks
69 , splitAt
75 , splitAt
70 )
76 )
71−import Data.Binary (Binary, decode, encode, put, get)
77+import Data.Binary (Binary, decode, encode, put, get, getWord8, putWord8)
72import Data.Map (Map)
78import Data.Map (Map)
73import qualified Data.Map as Map (empty, lookup, insert, delete)
79import qualified Data.Map as Map (empty, lookup, insert, delete)
74import qualified Data.List as List (delete)
80import qualified Data.List as List (delete)
75import Data.Int (Int32)
81import Data.Int (Int32)
76import Data.Typeable (Typeable)
82import Data.Typeable (Typeable)
77−import Control.Monad (void, liftM2)
83+import Control.Monad (void, liftM, liftM2)
78import Control.Monad.Reader (MonadReader(..), ReaderT, runReaderT)
84import Control.Monad.Reader (MonadReader(..), ReaderT, runReaderT)
79import Control.Monad.IO.Class (MonadIO, liftIO)
85import Control.Monad.IO.Class (MonadIO, liftIO)
80import Control.Applicative ((<$>))
86import Control.Applicative ((<$>))
81import Control.Category ((>>>))
87import Control.Category ((>>>))
82−import Control.Exception (throwIO)
88+import Control.Exception (Exception, throwIO)
83import Control.Concurrent (forkIO)
89import Control.Concurrent (forkIO)
84import Control.Concurrent.MVar ( MVar
90import Control.Concurrent.MVar ( MVar
85 , newMVar
91 , newMVar
@@ -168,7 +174,7 @@ newtype Process a = Process { unProcess :: ReaderT ProcessState IO a }
168 deriving (Functor, Monad, MonadIO, MonadReader ProcessState)
174 deriving (Functor, Monad, MonadIO, MonadReader ProcessState)
169
175
170--------------------------------------------------------------------------------
176--------------------------------------------------------------------------------
171−-- Cloud Haskell API --
177+-- Basic Cloud Haskell API --
172--------------------------------------------------------------------------------
178--------------------------------------------------------------------------------
173
179
174expect :: forall a. Serializable a => Process a
180expect :: forall a. Serializable a => Process a
@@ -192,6 +198,100 @@ send pid msg = do
192getSelfPid :: Process ProcessId
198getSelfPid :: Process ProcessId
193getSelfPid = processId <$> ask
199getSelfPid = processId <$> ask
194
200
201+monitorProcess :: ProcessId -> ProcessId -> MonitorAction -> Process ()
202+monitorProcess = undefined
203+
204+linkProcess :: ProcessId -> Process ()
205+linkProcess = undefined
206+
207+--------------------------------------------------------------------------------
208+-- Monitoring --
209+-- --
210+-- TODO: Many of these definitions are not available in the paper, and are --
211+-- taken from 'remote'. Do we want to stick to them precisely? --
212+--------------------------------------------------------------------------------
213+
214+-- | The different kinds of monitoring available between processes.
215+data MonitorAction =
216+ -- ^ MaMonitor means that the monitor process will be sent a
217+ -- ProcessMonitorException message when the monitee terminates for any
218+ -- reason.
219+ MaMonitor
220+ -- ^ MaLink means that the monitor process will receive an asynchronous
221+ -- exception of type ProcessMonitorException when the monitee terminates
222+ -- for any reason
223+ | MaLink
224+ -- ^ MaLinkError means that the monitor process will receive an
225+ -- asynchronous exception of type ProcessMonitorException when the monitee
226+ -- terminates abnormally
227+ | MaLinkError
228+ deriving (Typeable, Show, Ord, Eq)
229+
230+-- | The main form of notification to a monitoring process that a monitored
231+-- process has terminated. This data structure can be delivered to the monitor
232+-- either as a message (if the monitor is of type 'MaMonitor') or as an
233+-- asynchronous exception (if the monitor is of type 'MaLink' or
234+-- 'MaLinkError'). It contains the PID of the monitored process and the reason
235+-- for its nofication.
236+data ProcessMonitorException = ProcessMonitorException ProcessId SignalReason
237+ deriving (Typeable)
238+
239+-- | Part of the notification system of process monitoring, indicating why the
240+-- monitor is being notified.
241+data SignalReason =
242+ -- ^ the monitee terminated normally
243+ SrNormal
244+ -- ^ the monitee terminated with an uncaught exception, which is given as a
245+ -- string
246+ | SrException String
247+ -- ^ the monitee is believed to have ended or be inaccessible, as the node
248+ -- on which its running is not responding to pings. This may indicate a
249+ -- network bisection or that the remote node has crashed.
250+ | SrNoPing
251+ -- ^ SrInvalid: the monitee was not running at the time of the attempt to
252+ -- establish monitoring
253+ | SrInvalid
254+ deriving (Typeable,Show)
255+
256+instance Binary MonitorAction where
257+ put MaMonitor = putWord8 0
258+ put MaLink = putWord8 1
259+ put MaLinkError = putWord8 2
260+
261+ get = do x <- getWord8
262+ case x of
263+ 0 -> return MaMonitor
264+ 1 -> return MaLink
265+ 2 -> return MaLinkError
266+ _ -> fail "Invalid MonitorAction"
267+
268+instance Binary ProcessMonitorException where
269+ put (ProcessMonitorException pid sr) = put pid >> put sr
270+ get = liftM2 ProcessMonitorException get get
271+
272+instance Binary SignalReason where
273+ put SrNormal = putWord8 0
274+ put (SrException s) = putWord8 1 >> put s
275+ put SrNoPing = putWord8 2
276+ put SrInvalid = putWord8 3
277+
278+ get = do a <- getWord8
279+ case a of
280+ 0 -> return SrNormal
281+ 1 -> liftM SrException get
282+ 2 -> return SrNoPing
283+ 3 -> return SrInvalid
284+ _ -> fail "Invalid SignalReason"
285+
286+instance Exception ProcessMonitorException
287+
288+instance Show ProcessMonitorException where
289+ show (ProcessMonitorException pid why) =
290+ "ProcessMonitorException: "
291+ ++ show pid
292+ ++ " has terminated because "
293+ ++ show why
294+
195--------------------------------------------------------------------------------
295--------------------------------------------------------------------------------
196-- Initialization --
296-- Initialization --
197--------------------------------------------------------------------------------
297--------------------------------------------------------------------------------