@@ -13,7 +13,6 @@ module Control.Distributed.Process.Internal.Types
13 , LocalProcess(..)
13 , LocalProcess(..)
14 , LocalProcessState(..)
14 , LocalProcessState(..)
15 , Process(..)
15 , Process(..)
17 -- * Typed channels
16 -- * Typed channels
18 , LocalChannelId
17 , LocalChannelId
19 , ChannelId(..)
18 , ChannelId(..)
@@ -25,11 +24,7 @@ module Control.Distributed.Process.Internal.Types
25 , Static(..)
24 , Static(..)
26 , Closure(..)
25 , Closure(..)
27 , CallReply(..)
26 , CallReply(..)
28− , RemoteTable
27+ , RemoteTable(..)
33 -- * Messages
28 -- * Messages
34 , Message(..)
29 , Message(..)
35 -- * Node controller user-visible data types
30 -- * Node controller user-visible data types
@@ -44,10 +39,9 @@ module Control.Distributed.Process.Internal.Types
44 -- * Node controller internal data types
39 -- * Node controller internal data types
45 , NCMsg(..)
40 , NCMsg(..)
46 , ProcessSignal(..)
41 , ProcessSignal(..)
47− -- * Serialization/deserialization
42+ -- * MessageT monad
48− , createMessage
43+ , MessageT(..)
49− , messageToPayload
44+ , MessageState(..)
51 -- * Accessors
45 -- * Accessors
52 , localProcesses
46 , localProcesses
53 , localPidCounter
47 , localPidCounter
@@ -58,27 +52,20 @@ module Control.Distributed.Process.Internal.Types
58 , channelCounter
52 , channelCounter
59 , typedChannels
53 , typedChannels
60 , typedChannelWithId
54 , typedChannelWithId
61− -- * MessageT monad
55+ , remoteTableLabels
62− , MessageT(..)
56+ , remoteTableSenders
63− , MessageState(..)
57+ , remoteTableLabel
64− , runMessageT
58+ , remoteTableSender
67import Data.Map (Map)
61import Data.Map (Map)
68−import qualified Data.Map as Map (empty)
69import Data.Int (Int32)
62import Data.Int (Int32)
70−import Data.Typeable (Typeable, TypeRep, TyCon, typeRepTyCon, typeOf)
63+import Data.Typeable (Typeable, TypeRep)
71−import Data.Binary (Binary, encode, decode, put, get, putWord8, getWord8)
64+import Data.Binary (Binary(put, get), putWord8, getWord8)
72import Data.ByteString.Lazy (ByteString)
65import Data.ByteString.Lazy (ByteString)
73−import Data.Accessor (Accessor, accessor, (^=), (^.))
66+import Data.Accessor (Accessor, accessor)
74import qualified Data.Accessor.Container as DAC (mapMaybe)
67import qualified Data.Accessor.Container as DAC (mapMaybe)
75−import qualified Data.ByteString.Lazy as BSL
68+import qualified Data.ByteString.Lazy as BSL (ByteString)
81−import qualified Data.ByteString as BSS (ByteString, concat)
82import Control.Category ((>>>))
69import Control.Category ((>>>))
83import Control.Exception (Exception)
70import Control.Exception (Exception)
84import Control.Concurrent (ThreadId)
71import Control.Concurrent (ThreadId)
@@ -87,28 +74,12 @@ import Control.Concurrent.Chan (Chan)
87import Control.Concurrent.STM (TChan, TVar)
74import Control.Concurrent.STM (TChan, TVar)
88import qualified Network.Transport as NT (EndPoint, EndPointAddress, Connection)
75import qualified Network.Transport as NT (EndPoint, EndPointAddress, Connection)
89import Control.Applicative (Applicative, (<$>), (<*>))
76import Control.Applicative (Applicative, (<$>), (<*>))
90−import Control.Monad.Reader (MonadReader(..), ReaderT, runReaderT)
77+import Control.Monad.Reader (MonadReader(..), ReaderT)
91import Control.Monad.IO.Class (MonadIO)
78import Control.Monad.IO.Class (MonadIO)
92−import Control.Monad.State (MonadState, StateT, evalStateT)
79+import Control.Monad.State (MonadState, StateT)
93−import Control.Distributed.Process.Serializable
80+import Control.Distributed.Process.Serializable (Fingerprint, Serializable)
101import Control.Distributed.Process.Internal.CQueue (CQueue)
81import Control.Distributed.Process.Internal.CQueue (CQueue)
102−import Control.Distributed.Process.Internal.Dynamic
82+import Control.Distributed.Process.Internal.Dynamic (Dynamic)
111−import Control.Distributed.Process.Internal.TypeRep ()
113--------------------------------------------------------------------------------
84--------------------------------------------------------------------------------
114-- Node and process identifiers --
85-- Node and process identifiers --
@@ -192,10 +163,6 @@ newtype Process a = Process {
193 deriving (Functor, Monad, MonadIO, MonadReader LocalProcess, Typeable, Applicative)
164 deriving (Functor, Monad, MonadIO, MonadReader LocalProcess, Typeable, Applicative)
195−-- | Deconstructor for 'Process' (not exported to the public API)
196−runLocalProcess :: LocalNode -> Process a -> LocalProcess -> IO a
197−runLocalProcess node proc = runMessageT node . runReaderT (unProcess proc)
199--------------------------------------------------------------------------------
166--------------------------------------------------------------------------------
200-- Typed channels --
167-- Typed channels --
201--------------------------------------------------------------------------------
168--------------------------------------------------------------------------------
@@ -264,105 +231,6 @@ data RemoteTable = RemoteTable {
264 , _remoteTableSenders :: Map TypeRep Dynamic
231 , _remoteTableSenders :: Map TypeRep Dynamic
267−-- | Initial (empty) remote-call meta data
268−initRemoteTable :: RemoteTable
269−initRemoteTable = RemoteTable Map.empty Map.empty
271−registerLabel :: String -> Dynamic -> RemoteTable -> RemoteTable
272−registerLabel label dyn = remoteTableLabel label ^= Just dyn
274−registerSender :: TypeRep -> Dynamic -> RemoteTable -> RemoteTable
275−registerSender typ dyn = remoteTableSender typ ^= Just dyn
277−resolveClosure :: RemoteTable -> StaticLabel -> ByteString -> Maybe Dynamic
278−-- Special support for call
279−resolveClosure rtable Call env = do
280− proc <- resolveClosure rtable label env'
281− sender <- rtable ^. remoteTableSender (dynTypeRep proc)
282− proc `bind` (sender `dynApp` toDyn (pid :: ProcessId))
284− (label, env', pid) = decode env
285− bind = dynBind tyConProcess bindProcess
286− bindProcess :: Process a -> (a -> Process b) -> Process b
288−-- Generic closure combinators
289−resolveClosure rtable ClosureApply env = do
290− f <- resolveClosure rtable labelf envf
291− x <- resolveClosure rtable labelx envx
294− (labelf, envf, labelx, envx) = decode env
295−resolveClosure _rtable ClosureConst env =
296− return $ Dynamic (decode env) (unsafeCoerce# const)
297−resolveClosure _rtable ClosureUnit _env =
300−resolveClosure _rtable CpId env =
301− return $ Dynamic (decode env) (unsafeCoerce# cpId)
303− cpId :: forall a. a -> Process a
305−resolveClosure _rtable CpComp env =
306− return $ Dynamic (decode env) (unsafeCoerce# cpComp)
308− cpComp :: forall a b c. (a -> Process b) -> (b -> Process c) -> a -> Process c
309− cpComp p q a = p a >>= q
310−resolveClosure _rtable CpFirst env =
311− return $ Dynamic (decode env) (unsafeCoerce# cpFirst)
313− cpFirst :: forall a b c. (a -> Process b) -> (a, c) -> Process (b, c)
314− cpFirst p (a, c) = do b <- p a ; return (b, c)
315−resolveClosure _rtable CpSwap env =
316− return $ Dynamic (decode env) (unsafeCoerce# cpSwap)
318− cpSwap :: forall a b. (a, b) -> Process (b, a)
319− cpSwap (a, b) = return (b, a)
320−resolveClosure _rtable CpCopy env =
321− return $ Dynamic (decode env) (unsafeCoerce# cpCopy)
323− cpCopy :: forall a. a -> Process (a, a)
324− cpCopy a = return (a, a)
325−resolveClosure _rtable CpLeft env =
326− return $ Dynamic (decode env) (unsafeCoerce# cpLeft)
328− cpLeft :: forall a b c. (a -> Process b) -> Either a c -> Process (Either b c)
329− cpLeft p (Left a) = do b <- p a ; return (Left b)
330− cpLeft _ (Right c) = return (Right c)
331−resolveClosure _rtable CpMirror env =
332− return $ Dynamic (decode env) (unsafeCoerce# cpMirror)
334− cpMirror :: forall a b. Either a b -> Process (Either b a)
335− cpMirror (Left a) = return (Right a)
336− cpMirror (Right b) = return (Left b)
337−resolveClosure _rtable CpUntag env =
338− return $ Dynamic (decode env) (unsafeCoerce# cpUntag)
340− cpUntag :: forall a. Either a a -> Process a
341− cpUntag (Left a) = return a
342− cpUntag (Right a) = return a
343−resolveClosure rtable CpApply env =
344− return $ Dynamic typApply (unsafeCoerce# cpApply)
346− cpApply :: forall a b. (Closure (a -> Process b), a) -> Process b
347− cpApply (Closure (Static flabel) fenv, x) = do
348− let Just f = resolveClosure rtable flabel fenv
349− Dynamic typResult val = f `dynApp` Dynamic typA (unsafeCoerce# x)
350− if typResult == typProcB
351− then unsafeCoerce# val
352− else error $ "Type error in cpApply: "
353− ++ "mismatch between " ++ show typResult
354− ++ " and " ++ show typProcB
356− (typApply, typA, typProcB) = decode env
358−-- User defined closures
359−resolveClosure rtable (UserStatic label) env = do
360− val <- rtable ^. remoteTableLabel label
361− dynApply val (toDyn env)
363−tyConProcess :: TyCon
364−tyConProcess = typeRepTyCon (typeOf (undefined :: Process ()))
366--------------------------------------------------------------------------------
234--------------------------------------------------------------------------------
367-- Messages --
235-- Messages --
368--------------------------------------------------------------------------------
236--------------------------------------------------------------------------------
@@ -448,26 +316,6 @@ data ProcessSignal =
448 | Spawn (Closure (Process ())) SpawnRef
316 | Spawn (Closure (Process ())) SpawnRef
449 deriving Show
317 deriving Show
451−--------------------------------------------------------------------------------
452−-- Serialization/deserialization --
453−--------------------------------------------------------------------------------
455−-- | Turn any serialiable term into a message
456−createMessage :: Serializable a => a -> Message
457−createMessage a = Message (fingerprint a) (encode a)
459−-- | Serialize a message
460−messageToPayload :: Message -> [BSS.ByteString]
461−messageToPayload (Message fp enc) = encodeFingerprint fp : BSL.toChunks enc
463−-- | Deserialize a message
464−payloadToMessage :: [BSS.ByteString] -> Message
465−payloadToMessage payload = Message fp msg
467− (encFp, msg) = BSL.splitAt (fromIntegral sizeOfFingerprint)
468− $ BSL.fromChunks payload
469− fp = decodeFingerprint . BSS.concat . BSL.toChunks $ encFp
471--------------------------------------------------------------------------------
319--------------------------------------------------------------------------------
472-- Binary instances --
320-- Binary instances --
473--------------------------------------------------------------------------------
321--------------------------------------------------------------------------------
@@ -640,14 +488,3 @@ data MessageState = MessageState {
640 messageLocalNode :: LocalNode
488 messageLocalNode :: LocalNode
641 , _messageConnections :: Map Identifier NT.Connection
489 , _messageConnections :: Map Identifier NT.Connection
644−runMessageT :: Monad m => LocalNode -> MessageT m a -> m a
645−runMessageT localNode m =
646− evalStateT (unMessageT m) $ initMessageState localNode
648−initMessageState :: LocalNode -> MessageState
649−initMessageState localNode = MessageState {
650− messageLocalNode = localNode
651− , _messageConnections = Map.empty