1{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE RecordWildCards #-}
4module Control.Distributed.Process.Management.Table
8 , startTableCoordinator
17import Control.Distributed.Process.Internal.Primitives
34import Control.Distributed.Process.Internal.Types
37 , ProcessMonitorNotification(..)
41 , unsafeCreateUnencodedMessage
43import Control.Distributed.Process.Management.Types
48import Control.Distributed.Process.Serializable (Serializable)
49import Control.Monad.IO.Class (liftIO)
50import Data.Accessor (Accessor, accessor, (^=), (^:))
51import Data.Binary (Binary)
52import Data.Map.Strict (Map)
53import qualified Data.Map.Strict as Map
54import Data.Typeable (Typeable)
58-- An extremely lightweight shared Map implementation, for use
59-- by /management agents/ and their cohorts. Each agent is assigned
60-- a table, into which any serializable @Message@ can be inserted.
61-- Data are inserted, removed and searched for via their key, which
62-- is a string. Tables can be purged, values can be set, fetched or
70 | Set !String !Message
71 | Get !String !(SendPort (Maybe Message)) -- see [note: un-typed send port]
72 deriving (Typeable, Generic)
73instance Binary MxTableRequest where
76 MxTableState { _name :: !String, _entries :: !(Map String Message) }
78type MxTables = Map MxAgentId ProcessId
80mxTableCoordinator :: String
81mxTableCoordinator = "mx.table.coordinator"
83delete :: MxTableId -> Process ()
84delete = sendReq Delete
86purge :: MxTableId -> Process ()
89clear :: String -> MxTableId -> Process ()
90clear k = sendReq (Clear k)
92set :: String -> Message -> MxTableId -> Process ()
93set k v = sendReq (Set k v)
95fetch :: forall a. (Serializable a)
99fetch (MxForPid pid) key = get pid key
100fetch mxId@(MxForAgent _) key = do
101 (sp, rp) <- newChan :: Process (SendPort (Maybe Message),
102 ReceivePort (Maybe Message))
103 sendReq (Get key sp) mxId
104 receiveChan rp >>= maybe (return Nothing)
105 (unwrapMessage :: Message -> Process (Maybe a))
107-- [note: un-typed send port]
108-- Here, fetch uses a typed channel over a raw Message to obtain
109-- its result, so type checking is deferred until receipt and will
110-- be handled in the caller's thread. This is necessary because
111-- the server portion of the code knows nothing about the types
112-- involved, nor should it, since these tables can be used to
113-- store arbitrary serializable data.
115get :: forall a. (Serializable a)
120 safeFetch pid key >>= maybe (return Nothing)
121 (unwrapMessage :: Message -> Process (Maybe a))
123safeFetch :: ProcessId -> String -> Process (Maybe Message)
124safeFetch pid key = do
126 send pid $ Get key sp
130 , matchIf (\(ProcessMonitorNotification _ pid' _) -> pid' == pid)
131 (\_ -> return $ Just (unsafeCreateUnencodedMessage ()))
134sendReq :: MxTableRequest -> MxTableId -> Process ()
135sendReq req tid = (resolve tid) req
137resolve :: Serializable a => MxTableId -> (a -> Process ())
138resolve (MxForAgent agent) = \msg -> nsend mxTableCoordinator (agent, msg)
139resolve (MxForPid pid) = \msg -> send pid msg
141startTableCoordinator :: Fork -> Process ()
142startTableCoordinator fork = run Map.empty
144 run :: MxTables -> Process ()
147 -- note that this state change can race with MxAgentStart requests
148 match (\(ProcessMonitorNotification _ pid _) -> do
149 return $ Map.filter (/= pid) tables)
150 , match (\(MxAgentStart ch agent) -> do
151 lookupAgent tables agent >>= \(p, t) -> do
152 sendChan ch p >> return t)
153 , match (\req@(agent, tReq :: MxTableRequest) -> do
156 lookupAgent tables agent >>= \(p, t) -> do
157 safeFetch p k >>= sendChan sp >> return t
159 handleRequest tables req)
160 , matchAny (\_ -> return tables) -- unrecognised messages are dropped
163 handleRequest :: MxTables
164 -> (MxAgentId, MxTableRequest)
166 handleRequest tables' (agent, req) = do
167 lookupAgent tables' agent >>= \(p, t) -> send p req >> return t
169 lookupAgent :: MxTables -> MxAgentId -> Process (ProcessId, MxTables)
170 lookupAgent tables' agentId' = do
171 case Map.lookup agentId' tables' of
172 Nothing -> launchNew agentId' tables'
173 Just p -> return (p, tables')
175 launchNew :: MxAgentId
177 -> Process (ProcessId, MxTables)
178 launchNew mxId tblMap = do
179 let initState = MxTableState { _name = (agentId mxId)
180 , _entries = Map.empty
182 (pid, _) <- spawnSup $ tableHandler initState
183 return $ (pid, Map.insert mxId pid tblMap)
187 -- we need to use that passed in "fork", in order to
188 -- break an import cycle with Node.hs via the management
189 -- agent, management API and the tracing modules
190 them <- liftIO $ fork $ link us >> proc
194tableHandler :: MxTableState -> Process ()
195tableHandler state = do
197 match (handleTableRequest state)
198 , matchAny (\_ -> return (Just state))
202 Just s' -> tableHandler s'
204 handleTableRequest _ Delete = return Nothing
205 handleTableRequest st Purge = return $ Just $ (entries ^= Map.empty) $ st
206 handleTableRequest st (Clear k) = return $ Just $ (entries ^: Map.delete k) $ st
207 handleTableRequest st (Set k v) = return $ Just $ (entries ^: Map.insert k v) st
208 handleTableRequest st (Get k c) = getEntry k c st >> return (Just st)
211 -> SendPort (Maybe Message)
214getEntry k m MxTableState{..} = do
215 sendChan m =<< return (Map.lookup k _entries)
217entries :: Accessor MxTableState (Map String Message)
218entries = accessor _entries (\ls st -> st { _entries = ls })
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