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.Internal.StrictMVar (withMVar, modifyMVar_)
17import Control.Distributed.Process.Serializable ()
19import Control.Concurrent.Chan (writeChan)
20import Control.Monad (unless)
21import Control.Monad.IO.Class (MonadIO, liftIO)
22import Control.Monad.Reader (ask)
23import qualified Network.Transport as NT
28 , Reliability(ReliableOrdered)
31import Control.Distributed.Process.Internal.Types
32 ( LocalNode(localState, localEndPoint, localCtrlChan)
35 , localConnectionBetween
42 , DiedReason(DiedDisconnect)
43 , ImplicitReconnect(WithImplicitReconnect)
49 , SendPortId(sendPortProcessId)
50 , Identifier(NodeIdentifier, ProcessIdentifier, SendPortIdentifier)
52import Control.Distributed.Process.Serializable (Serializable)
54--------------------------------------------------------------------------------
56--------------------------------------------------------------------------------
58sendPayload :: LocalNode
64sendPayload node from to implicitReconnect payload = do
65 mConn <- connBetween node from to implicitReconnect
66 didSend <- case mConn of
68 didSend <- NT.send conn payload
70 Left _err -> return False
71 Right () -> return True
72 Nothing -> return False
74 writeChan (localCtrlChan node) NCMsg
76 , ctrlMsgSignal = Died to DiedDisconnect
86sendBinary node from to implicitReconnect
87 = sendPayload node from to implicitReconnect . BSL.toChunks . encode
89sendMessage :: Serializable a
96sendMessage node from to implicitReconnect =
97 sendPayload node from to implicitReconnect . messageToPayload . createMessage
99setupConnBetween :: LocalNode
103 -> IO (Maybe NT.Connection)
104setupConnBetween node from to implicitReconnect = do
105 mConn <- NT.connect endPoint
106 (nodeAddress . nodeOf $ to)
108 NT.defaultConnectHints
111 didSend <- NT.send conn (BSL.toChunks . encode $ to)
116 modifyMVar_ nodeState $ return .
117 (localConnectionBetween from to ^= Just (conn, implicitReconnect))
122 endPoint = localEndPoint node
123 nodeState = localState node
125connBetween :: LocalNode
129 -> IO (Maybe NT.Connection)
130connBetween node from to implicitReconnect = do
131 mConn <- withMVar nodeState $ return . (^. localConnectionBetween from to)
136 setupConnBetween node from to implicitReconnect
138 nodeState = localState node
140disconnect :: LocalNode -> Identifier -> Identifier -> IO ()
141disconnect node from to =
142 modifyMVar_ (localState node) $ \st ->
143 case st ^. localConnectionBetween from to of
148 return (localConnectionBetween from to ^= Nothing $ st)
150closeImplicitReconnections :: LocalNode -> Identifier -> IO ()
151closeImplicitReconnections node to =
152 modifyMVar_ (localState node) $ \st -> do
153 let shouldClose (_, to') (_, WithImplicitReconnect) = to `impliesDeathOf` to'
154 shouldClose _ _ = False
155 let (affected, unaffected) = Map.partitionWithKey shouldClose (st ^. localConnections)
156 mapM_ (NT.close . fst) (Map.elems affected)
157 return (localConnections ^= unaffected $ st)
159-- | @a `impliesDeathOf` b@ is true if the death of @a@ (for instance, a node)
160-- implies the death of @b@ (for instance, a process on that node)
161impliesDeathOf :: Identifier
164NodeIdentifier nid `impliesDeathOf` NodeIdentifier nid' =
166NodeIdentifier nid `impliesDeathOf` ProcessIdentifier pid =
167 processNodeId pid == nid
168NodeIdentifier nid `impliesDeathOf` SendPortIdentifier cid =
169 processNodeId (sendPortProcessId cid) == nid
170ProcessIdentifier pid `impliesDeathOf` ProcessIdentifier pid' =
172ProcessIdentifier pid `impliesDeathOf` SendPortIdentifier cid =
173 sendPortProcessId cid == pid
174SendPortIdentifier cid `impliesDeathOf` SendPortIdentifier cid' =
176_ `impliesDeathOf` _ =
180-- Send a control message
181sendCtrlMsg :: Maybe NodeId -- ^ Nothing for the local node
182 -> ProcessSignal -- ^ Message to send
184sendCtrlMsg mNid signal = do
186 let msg = NCMsg { ctrlMsgSender = ProcessIdentifier (processId proc)
187 , ctrlMsgSignal = signal
191 liftIO $ writeChan (localCtrlChan (processNode proc)) msg
193 liftIO $ sendBinary (processNode proc)
194 (ProcessIdentifier (processId proc))
196 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