@@ -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
58 , getSelfPid
58 , getSelfPid
63+ , ProcessMonitorException(..)
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
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
@@ -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)
170--------------------------------------------------------------------------------
176--------------------------------------------------------------------------------
171−-- Cloud Haskell API --
177+-- Basic Cloud Haskell API --
172--------------------------------------------------------------------------------
178--------------------------------------------------------------------------------
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
201+monitorProcess :: ProcessId -> ProcessId -> MonitorAction -> Process ()
202+monitorProcess = undefined
204+linkProcess :: ProcessId -> Process ()
205+linkProcess = undefined
207+--------------------------------------------------------------------------------
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+--------------------------------------------------------------------------------
214+-- | The different kinds of monitoring available between processes.
216+ -- ^ MaMonitor means that the monitor process will be sent a
217+ -- ProcessMonitorException message when the monitee terminates for any
220+ -- ^ MaLink means that the monitor process will receive an asynchronous
221+ -- exception of type ProcessMonitorException when the monitee terminates
224+ -- ^ MaLinkError means that the monitor process will receive an
225+ -- asynchronous exception of type ProcessMonitorException when the monitee
226+ -- terminates abnormally
228+ deriving (Typeable, Show, Ord, Eq)
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
239+-- | Part of the notification system of process monitoring, indicating why the
240+-- monitor is being notified.
242+ -- ^ the monitee terminated normally
244+ -- ^ the monitee terminated with an uncaught exception, which is given as a
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.
251+ -- ^ SrInvalid: the monitee was not running at the time of the attempt to
252+ -- establish monitoring
254+ deriving (Typeable,Show)
256+instance Binary MonitorAction where
257+ put MaMonitor = putWord8 0
258+ put MaLink = putWord8 1
259+ put MaLinkError = putWord8 2
261+ get = do x <- getWord8
263+ 0 -> return MaMonitor
265+ 2 -> return MaLinkError
266+ _ -> fail "Invalid MonitorAction"
268+instance Binary ProcessMonitorException where
269+ put (ProcessMonitorException pid sr) = put pid >> put sr
270+ get = liftM2 ProcessMonitorException get get
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
278+ get = do a <- getWord8
280+ 0 -> return SrNormal
281+ 1 -> liftM SrException get
282+ 2 -> return SrNoPing
283+ 3 -> return SrInvalid
284+ _ -> fail "Invalid SignalReason"
286+instance Exception ProcessMonitorException
288+instance Show ProcessMonitorException where
289+ show (ProcessMonitorException pid why) =
290+ "ProcessMonitorException: "
292+ ++ " has terminated because "
195--------------------------------------------------------------------------------
295--------------------------------------------------------------------------------
196-- Initialization --
296-- Initialization --
197--------------------------------------------------------------------------------
297--------------------------------------------------------------------------------