@@ -38,9 +38,11 @@ module Control.Distributed.Process.ManagedProcess.Internal.Types
38
38
, ControlPort (.. )
39
39
, channelControlPort
40
40
, Dispatcher (.. )
41
+ , ExternDispatcher (.. )
41
42
, DeferredDispatcher (.. )
42
43
, ExitSignalDispatcher (.. )
43
44
, MessageMatcher (.. )
45
+ , ExternMatcher (.. )
44
46
, DynMessageHandler (.. )
45
47
, Message (.. )
46
48
, CallResponse (.. )
@@ -166,7 +168,6 @@ data Condition s m =
166
168
| State (s -> Bool ) -- ^ predicated on the process state only
167
169
| Input (m -> Bool ) -- ^ predicated on the input message only
168
170
169
-
170
171
-- | An action (server state transition) in the @Process@ monad
171
172
type Action s = Process (ProcessAction s )
172
173
@@ -254,17 +255,21 @@ data Dispatcher s =
254
255
dispatch :: s -> Message a b -> Process (ProcessAction s )
255
256
, dispatchIf :: s -> Message a b -> Bool
256
257
}
257
- | forall a b . (Serializable a , Serializable b ) =>
258
+
259
+ -- | Provides dispatch for channels and STM actions
260
+ data ExternDispatcher s =
261
+ forall a b . (Serializable a , Serializable b ) =>
258
262
DispatchCC -- control channel dispatch
259
263
{
260
- channel :: ReceivePort (Message a b )
261
- , dispatch :: s -> Message a b -> Process (ProcessAction s )
264
+ channel :: ReceivePort (Message a b )
265
+ , dispatchChan :: s -> Message a b -> Process (ProcessAction s )
262
266
}
263
- | forall a .
267
+ | forall a . ( Serializable a ) =>
264
268
DispatchSTM -- arbitrary STM actions
265
269
{
266
270
stmAction :: STM a
267
- , stmDispatch :: s -> a -> Process (ProcessAction s )
271
+ , dispatchStm :: s -> a -> Process (ProcessAction s )
272
+ , matchStm :: Match P. Message
268
273
}
269
274
270
275
-- | Provides dispatch for any input, returns 'Nothing' for unhandled messages.
@@ -291,10 +296,19 @@ class MessageMatcher d where
291
296
matchDispatch :: UnhandledMessagePolicy -> s -> d s -> Match (ProcessAction s )
292
297
293
298
instance MessageMatcher Dispatcher where
294
- matchDispatch _ s (Dispatch d) = match (d s)
295
- matchDispatch _ s (DispatchIf d cond) = matchIf (cond s) (d s)
296
- matchDispatch _ s (DispatchCC c d) = matchChan c (d s)
297
- matchDispatch _ s (DispatchSTM c d) = matchSTM c (d s)
299
+ matchDispatch _ s (Dispatch d) = match (d s)
300
+ matchDispatch _ s (DispatchIf d cond) = matchIf (cond s) (d s)
301
+
302
+ instance MessageMatcher ExternDispatcher where
303
+ matchDispatch _ s (DispatchCC c d) = matchChan c (d s)
304
+ matchDispatch _ s (DispatchSTM c d _) = matchSTM c (d s)
305
+
306
+ class ExternMatcher d where
307
+ matchExtern :: UnhandledMessagePolicy -> s -> d s -> Match P. Message
308
+
309
+ instance ExternMatcher ExternDispatcher where
310
+ matchExtern _ _ (DispatchCC c _) = matchChan c (return . unsafeWrapMessage)
311
+ matchExtern _ _ (DispatchSTM _ _ m) = m
298
312
299
313
-- | Maps handlers to a dynamic action that can take place outside of a
300
314
-- expect/recieve block.
@@ -308,8 +322,10 @@ class DynMessageHandler d where
308
322
instance DynMessageHandler Dispatcher where
309
323
dynHandleMessage _ s (Dispatch d) msg = handleMessage msg (d s)
310
324
dynHandleMessage _ s (DispatchIf d c) msg = handleMessageIf msg (c s) (d s)
311
- dynHandleMessage _ _ (DispatchCC _ _) _ = error " ThisCanNeverHappen"
312
- dynHandleMessage _ _ (DispatchSTM _ _) _ = error " ThisCanNeverHappen"
325
+
326
+ instance DynMessageHandler ExternDispatcher where
327
+ dynHandleMessage _ s (DispatchCC _ d) msg = handleMessage msg (d s)
328
+ dynHandleMessage _ s (DispatchSTM _ d _) msg = handleMessage msg (d s)
313
329
314
330
instance DynMessageHandler DeferredDispatcher where
315
331
dynHandleMessage _ s (DeferredDispatcher d) = d s
@@ -368,9 +384,10 @@ data UnhandledMessagePolicy =
368
384
-- | Stores the functions that determine runtime behaviour in response to
369
385
-- incoming messages and a policy for responding to unhandled messages.
370
386
data ProcessDefinition s = ProcessDefinition {
371
- apiHandlers :: [Dispatcher s ] -- ^ functions that handle call/cast messages
372
- , infoHandlers :: [DeferredDispatcher s ] -- ^ functions that handle non call/cast messages
373
- , exitHandlers :: [ExitSignalDispatcher s ] -- ^ functions that handle exit signals
387
+ apiHandlers :: [Dispatcher s ] -- ^ functions that handle call/cast messages
388
+ , infoHandlers :: [DeferredDispatcher s ] -- ^ functions that handle non call/cast messages
389
+ , externHandlers :: [ExternDispatcher s ] -- ^ functions that handle control channel and STM inputs
390
+ , exitHandlers :: [ExitSignalDispatcher s ] -- ^ functions that handle exit signals
374
391
, timeoutHandler :: TimeoutHandler s -- ^ a function that handles timeouts
375
392
, shutdownHandler :: ShutdownHandler s -- ^ a function that is run just before the process exits
376
393
, unhandledMessagePolicy :: UnhandledMessagePolicy -- ^ how to deal with unhandled messages
0 commit comments