module Control.Distributed.Process.Management.Internal.Types
( MxAgentId(..)
, MxTableId(..)
, MxAgentState(..)
, MxAgent(..)
, MxAction(..)
, ChannelSelector(..)
, MxAgentStart(..)
, Fork
, MxSink
, MxEvent(..)
, Addressable(..)
) where
import Control.Applicative (Applicative)
import Control.Concurrent.STM
( TChan
)
import Control.Distributed.Process.Internal.Types
( Process
, ProcessId
, Message
, SendPort
, 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 MxTableId =
MxForAgent !MxAgentId
| MxForPid !ProcessId
deriving (Typeable, Generic)
instance Binary MxTableId where
data MxAgentState s = MxAgentState
{
mxAgentId :: !MxAgentId
, mxBus :: !(TChan Message)
, mxSharedTable :: !ProcessId
, 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 MxAgentStart = MxAgentStart
{
mxAgentTableChan :: SendPort ProcessId
, mxAgentIdStart :: MxAgentId
}
deriving (Typeable, Generic)
instance Binary MxAgentStart where
data ChannelSelector = InputChan | Mailbox
data MxAction =
MxAgentDeactivate !String
| MxAgentPrioritise !ChannelSelector
| MxAgentReady
| MxAgentSkip
type MxSink s = Message -> MxAgent s (Maybe MxAction)