@@ -13,7 +13,6 @@ module Control.Distributed.Process.Internal.Types
1313 , LocalProcess(..)
1414 , LocalProcessState(..)
1515 , Process(..)
16− , runLocalProcess
1716 -- * Typed channels
1817 , LocalChannelId
1918 , ChannelId(..)
@@ -25,11 +24,7 @@ module Control.Distributed.Process.Internal.Types
2524 , Static(..)
2625 , Closure(..)
2726 , CallReply(..)
28− , RemoteTable
29− , initRemoteTable
30− , registerLabel
31− , registerSender
32− , resolveClosure
27+ , RemoteTable(..)
3328 -- * Messages
3429 , Message(..)
3530 -- * Node controller user-visible data types
@@ -44,10 +39,9 @@ module Control.Distributed.Process.Internal.Types
4439 -- * Node controller internal data types
4540 , NCMsg(..)
4641 , ProcessSignal(..)
47− -- * Serialization/deserialization
48− , createMessage
49− , messageToPayload
50− , payloadToMessage
42+ -- * MessageT monad
43+ , MessageT(..)
44+ , MessageState(..)
5145 -- * Accessors
5246 , localProcesses
5347 , localPidCounter
@@ -58,27 +52,20 @@ module Control.Distributed.Process.Internal.Types
5852 , channelCounter
5953 , typedChannels
6054 , typedChannelWithId
61− -- * MessageT monad
62− , MessageT(..)
63− , MessageState(..)
64− , runMessageT
55+ , remoteTableLabels
56+ , remoteTableSenders
57+ , remoteTableLabel
58+ , remoteTableSender
6559 ) where
6660
6761import Data.Map (Map)
68−import qualified Data.Map as Map (empty)
6962import Data.Int (Int32)
70−import Data.Typeable (Typeable, TypeRep, TyCon, typeRepTyCon, typeOf)
71−import Data.Binary (Binary, encode, decode, put, get, putWord8, getWord8)
63+import Data.Typeable (Typeable, TypeRep)
64+import Data.Binary (Binary(put, get), putWord8, getWord8)
7265import Data.ByteString.Lazy (ByteString)
73−import Data.Accessor (Accessor, accessor, (^=), (^.))
66+import Data.Accessor (Accessor, accessor)
7467import qualified Data.Accessor.Container as DAC (mapMaybe)
75−import qualified Data.ByteString.Lazy as BSL
76− ( ByteString
77− , toChunks
78− , fromChunks
79− , splitAt
80− )
81−import qualified Data.ByteString as BSS (ByteString, concat)
68+import qualified Data.ByteString.Lazy as BSL (ByteString)
8269import Control.Category ((>>>))
8370import Control.Exception (Exception)
8471import Control.Concurrent (ThreadId)
@@ -87,28 +74,12 @@ import Control.Concurrent.Chan (Chan)
8774import Control.Concurrent.STM (TChan, TVar)
8875import qualified Network.Transport as NT (EndPoint, EndPointAddress, Connection)
8976import Control.Applicative (Applicative, (<$>), (<*>))
90−import Control.Monad.Reader (MonadReader(..), ReaderT, runReaderT)
77+import Control.Monad.Reader (MonadReader(..), ReaderT)
9178import Control.Monad.IO.Class (MonadIO)
92−import Control.Monad.State (MonadState, StateT, evalStateT)
93−import Control.Distributed.Process.Serializable
94− ( Fingerprint
95− , Serializable
96− , encodeFingerprint
97− , decodeFingerprint
98− , fingerprint
99− , sizeOfFingerprint
100− )
79+import Control.Monad.State (MonadState, StateT)
80+import Control.Distributed.Process.Serializable (Fingerprint, Serializable)
10181import Control.Distributed.Process.Internal.CQueue (CQueue)
102−import Control.Distributed.Process.Internal.Dynamic
103− ( Dynamic(..)
104− , toDyn
105− , dynTypeRep
106− , dynApp
107− , dynBind
108− , dynApply
109− , unsafeCoerce#
110− )
111−import Control.Distributed.Process.Internal.TypeRep ()
82+import Control.Distributed.Process.Internal.Dynamic (Dynamic)
11283
11384--------------------------------------------------------------------------------
11485-- Node and process identifiers --
@@ -192,10 +163,6 @@ newtype Process a = Process {
192163 }
193164 deriving (Functor, Monad, MonadIO, MonadReader LocalProcess, Typeable, Applicative)
194165
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)
198−
199166--------------------------------------------------------------------------------
200167-- Typed channels --
201168--------------------------------------------------------------------------------
@@ -264,105 +231,6 @@ data RemoteTable = RemoteTable {
264231 , _remoteTableSenders :: Map TypeRep Dynamic
265232 }
266233
267−-- | Initial (empty) remote-call meta data
268−initRemoteTable :: RemoteTable
269−initRemoteTable = RemoteTable Map.empty Map.empty
270−
271−registerLabel :: String -> Dynamic -> RemoteTable -> RemoteTable
272−registerLabel label dyn = remoteTableLabel label ^= Just dyn
273−
274−registerSender :: TypeRep -> Dynamic -> RemoteTable -> RemoteTable
275−registerSender typ dyn = remoteTableSender typ ^= Just dyn
276−
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))
283− where
284− (label, env', pid) = decode env
285− bind = dynBind tyConProcess bindProcess
286− bindProcess :: Process a -> (a -> Process b) -> Process b
287− bindProcess = (>>=)
288−-- Generic closure combinators
289−resolveClosure rtable ClosureApply env = do
290− f <- resolveClosure rtable labelf envf
291− x <- resolveClosure rtable labelx envx
292− f `dynApply` x
293− where
294− (labelf, envf, labelx, envx) = decode env
295−resolveClosure _rtable ClosureConst env =
296− return $ Dynamic (decode env) (unsafeCoerce# const)
297−resolveClosure _rtable ClosureUnit _env =
298− return $ toDyn ()
299−-- Arrow combinators
300−resolveClosure _rtable CpId env =
301− return $ Dynamic (decode env) (unsafeCoerce# cpId)
302− where
303− cpId :: forall a. a -> Process a
304− cpId = return
305−resolveClosure _rtable CpComp env =
306− return $ Dynamic (decode env) (unsafeCoerce# cpComp)
307− where
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)
312− where
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)
317− where
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)
322− where
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)
327− where
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)
333− where
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)
339− where
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)
345− where
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
355−
356− (typApply, typA, typProcB) = decode env
357−
358−-- User defined closures
359−resolveClosure rtable (UserStatic label) env = do
360− val <- rtable ^. remoteTableLabel label
361− dynApply val (toDyn env)
362−
363−tyConProcess :: TyCon
364−tyConProcess = typeRepTyCon (typeOf (undefined :: Process ()))
365−
366234--------------------------------------------------------------------------------
367235-- Messages --
368236--------------------------------------------------------------------------------
@@ -448,26 +316,6 @@ data ProcessSignal =
448316 | Spawn (Closure (Process ())) SpawnRef
449317 deriving Show
450318
451−--------------------------------------------------------------------------------
452−-- Serialization/deserialization --
453−--------------------------------------------------------------------------------
454−
455−-- | Turn any serialiable term into a message
456−createMessage :: Serializable a => a -> Message
457−createMessage a = Message (fingerprint a) (encode a)
458−
459−-- | Serialize a message
460−messageToPayload :: Message -> [BSS.ByteString]
461−messageToPayload (Message fp enc) = encodeFingerprint fp : BSL.toChunks enc
462−
463−-- | Deserialize a message
464−payloadToMessage :: [BSS.ByteString] -> Message
465−payloadToMessage payload = Message fp msg
466− where
467− (encFp, msg) = BSL.splitAt (fromIntegral sizeOfFingerprint)
468− $ BSL.fromChunks payload
469− fp = decodeFingerprint . BSS.concat . BSL.toChunks $ encFp
470−
471319--------------------------------------------------------------------------------
472320-- Binary instances --
473321--------------------------------------------------------------------------------
@@ -640,14 +488,3 @@ data MessageState = MessageState {
640488 messageLocalNode :: LocalNode
641489 , _messageConnections :: Map Identifier NT.Connection
642490 }
643−
644−runMessageT :: Monad m => LocalNode -> MessageT m a -> m a
645−runMessageT localNode m =
646− evalStateT (unMessageT m) $ initMessageState localNode
647−
648−initMessageState :: LocalNode -> MessageState
649−initMessageState localNode = MessageState {
650− messageLocalNode = localNode
651− , _messageConnections = Map.empty
652− }
653−