1module Control.Distributed.Process.Internal.Messaging
6 , closeImplicitReconnections
11import Data.Accessor ((^.), (^=))
12import Data.Binary (Binary, encode)
13import qualified Data.Map as Map (partitionWithKey, elems)
14import qualified Data.ByteString.Lazy as BSL (toChunks)
15import qualified Data.ByteString as BSS (ByteString)
16import Control.Distributed.Process.Serializable ()
18import Control.Concurrent.Chan (writeChan)
19import Control.Monad (unless)
20import Control.Monad.IO.Class (liftIO)
21import Control.Monad.Reader (ask)
22import qualified Network.Transport as NT
27 , Reliability(ReliableOrdered)
30import Control.Distributed.Process.Internal.Types
31 ( LocalNode(localState, localEndPoint, localCtrlChan)
33 , modifyValidLocalState_
36 , localConnectionBetween
43 , DiedReason(DiedDisconnect)
44 , ImplicitReconnect(WithImplicitReconnect)
50 , SendPortId(sendPortProcessId)
51 , Identifier(NodeIdentifier, ProcessIdentifier, SendPortIdentifier)
53import Control.Distributed.Process.Serializable (Serializable)
55--------------------------------------------------------------------------------
57--------------------------------------------------------------------------------
59sendPayload :: LocalNode
65sendPayload node from to implicitReconnect payload = do
66 mConn <- connBetween node from to implicitReconnect
67 didSend <- case mConn of
69 didSend <- NT.send conn payload
71 Left _err -> return False
72 Right () -> return True
73 Nothing -> return False
75 writeChan (localCtrlChan node) NCMsg
77 , ctrlMsgSignal = Died to DiedDisconnect
87sendBinary node from to implicitReconnect
88 = sendPayload node from to implicitReconnect . BSL.toChunks . encode
90sendMessage :: Serializable a
97sendMessage node from to implicitReconnect =
98 sendPayload node from to implicitReconnect . messageToPayload . createMessage
100setupConnBetween :: LocalNode
104 -> IO (Maybe NT.Connection)
105setupConnBetween node from to implicitReconnect = do
106 mConn <- NT.connect (localEndPoint node)
107 (nodeAddress . nodeOf $ to)
109 NT.defaultConnectHints
112 didSend <- NT.send conn (BSL.toChunks . encode $ to)
117 modifyValidLocalState_ node $
118 return . (localConnectionBetween from to ^= Just (conn, implicitReconnect))
123connBetween :: LocalNode
127 -> IO (Maybe NT.Connection)
128connBetween node from to implicitReconnect = do
129 mConn <- withValidLocalState node $
130 return . (^. localConnectionBetween from to)
135 setupConnBetween node from to implicitReconnect
137disconnect :: LocalNode -> Identifier -> Identifier -> IO ()
138disconnect node from to =
139 modifyValidLocalState_ node $ \vst ->
140 case vst ^. localConnectionBetween from to of
145 return $ localConnectionBetween from to ^= Nothing $ vst
147closeImplicitReconnections :: LocalNode -> Identifier -> IO ()
148closeImplicitReconnections node to =
149 modifyValidLocalState_ node $ \vst -> do
150 let shouldClose (_, to') (_, WithImplicitReconnect) = to `impliesDeathOf` to'
151 shouldClose _ _ = False
152 let (affected, unaffected) =
153 Map.partitionWithKey shouldClose (vst ^. localConnections)
154 mapM_ (NT.close . fst) (Map.elems affected)
155 return $ localConnections ^= unaffected $ vst
157-- | @a `impliesDeathOf` b@ is true if the death of @a@ (for instance, a node)
158-- implies the death of @b@ (for instance, a process on that node)
159impliesDeathOf :: Identifier
162NodeIdentifier nid `impliesDeathOf` NodeIdentifier nid' =
164NodeIdentifier nid `impliesDeathOf` ProcessIdentifier pid =
165 processNodeId pid == nid
166NodeIdentifier nid `impliesDeathOf` SendPortIdentifier cid =
167 processNodeId (sendPortProcessId cid) == nid
168ProcessIdentifier pid `impliesDeathOf` ProcessIdentifier pid' =
170ProcessIdentifier pid `impliesDeathOf` SendPortIdentifier cid =
171 sendPortProcessId cid == pid
172SendPortIdentifier cid `impliesDeathOf` SendPortIdentifier cid' =
174_ `impliesDeathOf` _ =
178-- Send a control message. Evaluates the message to HNF before sending it.
180-- The message shouldn't produce more errors when further evaluated. If
181-- evaluation threw errors the node controller or the receiver would crash when
183sendCtrlMsg :: Maybe NodeId -- ^ Nothing for the local node
184 -> ProcessSignal -- ^ Message to send
186sendCtrlMsg mNid signal = do
188 let msg = NCMsg { ctrlMsgSender = ProcessIdentifier (processId proc)
189 , ctrlMsgSignal = signal
193 liftIO $ writeChan (localCtrlChan (processNode proc)) $! msg
195 liftIO $ sendBinary (processNode proc)
196 (ProcessIdentifier (processId proc))
198 WithImplicitReconnect
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list