{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
module Control.Distributed.Process.Management.Internal.Types
( MxAgentId(..)
, MxAgentState(..)
, MxAgent(..)
, MxAction(..)
, ChannelSelector(..)
, Fork
, MxSink
, MxEvent(..)
, Addressable(..)
) where
import Control.Applicative (Applicative)
import Control.Concurrent.STM
( TChan
)
import Control.Distributed.Process.Internal.Types
( Process
, ProcessId
, Message
, DiedReason
, NodeId
)
import Control.Monad.IO.Class (MonadIO)
import qualified Control.Monad.State as ST
( MonadState
, StateT
)
import Data.Binary
import Data.Typeable (Typeable)
import GHC.Generics
import Network.Transport
( ConnectionId
, EndPointAddress
)
data MxEvent =
MxSpawned ProcessId
| MxRegistered ProcessId String
| MxUnRegistered ProcessId String
| MxProcessDied ProcessId DiedReason
| MxNodeDied NodeId DiedReason
| MxSent ProcessId ProcessId Message
| MxReceived ProcessId Message
| MxConnected ConnectionId EndPointAddress
| MxDisconnected ConnectionId EndPointAddress
| MxUser Message
| MxLog String
| MxTraceTakeover ProcessId
| MxTraceDisable
deriving (Typeable, Generic, Show)
instance Binary MxEvent where
class Addressable a where
resolveToPid :: a -> Maybe ProcessId
instance Addressable MxEvent where
resolveToPid (MxSpawned p) = Just p
resolveToPid (MxProcessDied p _) = Just p
resolveToPid (MxSent _ p _) = Just p
resolveToPid (MxReceived p _) = Just p
resolveToPid _ = Nothing
type Fork = (Process () -> IO ProcessId)
newtype MxAgentId = MxAgentId { agentId :: String }
deriving (Typeable, Binary, Eq, Ord)
data MxAgentState s = MxAgentState
{
mxAgentId :: !MxAgentId
, mxBus :: !(TChan Message)
, mxLocalState :: !s
}
newtype MxAgent s a =
MxAgent
{
unAgent :: ST.StateT (MxAgentState s) Process a
} deriving ( Functor
, Monad
, MonadIO
, ST.MonadState (MxAgentState s)
, Typeable
, Applicative
)
data ChannelSelector = InputChan | Mailbox
data MxAction =
MxAgentDeactivate !String
| MxAgentPrioritise !ChannelSelector
| MxAgentReady
| MxAgentSkip
type MxSink s = Message -> MxAgent s (Maybe MxAction)