module Control.Eff.Concurrent.Protocol.Wrapper
( Request(..)
, sendReply
, ReplyTarget(..)
, replyTarget
, replyTargetOrigin
, replyTargetSerializer
, embeddedReplyTarget
, toEmbeddedReplyTarget
, RequestOrigin(..)
, embedRequestOrigin
, toEmbeddedOrigin
, Reply(..)
, embedReplySerializer
, makeRequestOrigin
)
where
import Control.DeepSeq
import Control.Eff
import Control.Eff.Concurrent.Process
import Control.Eff.Concurrent.Protocol
import Control.Lens
import Data.Kind (Type)
import Data.Typeable (Typeable)
import Data.Semigroup
import GHC.Generics
data Request protocol where
Call
:: forall protocol reply.
( Tangible reply
, TangiblePdu protocol ('Synchronous reply)
)
=> RequestOrigin protocol reply
-> Pdu protocol ('Synchronous reply)
-> Request protocol
Cast
:: forall protocol. (TangiblePdu protocol 'Asynchronous, NFData (Pdu protocol 'Asynchronous))
=> Pdu protocol 'Asynchronous
-> Request protocol
deriving (Typeable)
instance Show (Request protocol) where
showsPrec d (Call o r) =
showParen (d >= 10) (showString "call-request: " . showsPrec 11 o . showString ": " . showsPrec 11 r)
showsPrec d (Cast r) =
showParen (d >= 10) (showString "cast-request: " . showsPrec 11 r)
instance NFData (Request protocol) where
rnf (Call o req) = rnf o `seq` rnf req
rnf (Cast req) = rnf req
data Reply protocol reply where
Reply :: (Tangible reply) =>
{ _replyTo :: RequestOrigin protocol reply
, _replyValue :: reply
} -> Reply protocol reply
deriving (Typeable)
instance NFData (Reply p r) where
rnf (Reply i r) = rnf i `seq` rnf r
instance Show r => Show (Reply p r) where
showsPrec d (Reply o r) =
showParen (d >= 10) (showString "request-reply: " . showsPrec 11 o . showString ": " . showsPrec 11 r)
data RequestOrigin (proto :: Type) reply = RequestOrigin
{ _requestOriginPid :: !ProcessId
, _requestOriginCallRef :: !Int
} deriving (Typeable, Generic, Eq, Ord)
instance Show (RequestOrigin p r) where
showsPrec d (RequestOrigin o r) =
showParen (d >= 10) (showString "origin: " . showsPrec 10 o . showChar ' ' . showsPrec 10 r)
makeRequestOrigin :: (Typeable r, NFData r, HasProcesses e q0) => Eff e (RequestOrigin p r)
makeRequestOrigin = RequestOrigin <$> self <*> makeReference
instance NFData (RequestOrigin p r)
toEmbeddedOrigin
:: forall outer inner reply . Embeds outer inner
=> RequestOrigin outer reply
-> RequestOrigin inner reply
toEmbeddedOrigin (RequestOrigin !pid !ref) = RequestOrigin pid ref
embedRequestOrigin :: forall outer inner reply . Embeds outer inner => RequestOrigin inner reply -> RequestOrigin outer reply
embedRequestOrigin (RequestOrigin !pid !ref) = RequestOrigin pid ref
embedReplySerializer :: forall outer inner reply . Embeds outer inner => Serializer (Reply outer reply) -> Serializer (Reply inner reply)
embedReplySerializer = contramap embedReply
embedReply :: forall outer inner reply . Embeds outer inner => Reply inner reply -> Reply outer reply
embedReply (Reply (RequestOrigin !pid !ref) !v) = Reply (RequestOrigin pid ref) v
sendReply
:: ( HasProcesses eff q
, Tangible reply
, Typeable protocol
)
=> ReplyTarget protocol reply
-> reply
-> Eff eff ()
sendReply (MkReplyTarget (Arg o ser)) r =
sendAnyMessage (_requestOriginPid o) $! runSerializer ser $! Reply o r
newtype ReplyTarget p r =
MkReplyTarget (Arg (RequestOrigin p r) (Serializer (Reply p r)))
deriving (Eq, Ord, Typeable)
instance Show (ReplyTarget p r) where
showsPrec d (MkReplyTarget (Arg o _s)) = showParen (d>=10) (showString "reply-target: " . shows o)
instance NFData (ReplyTarget p r) where
rnf (MkReplyTarget (Arg x y)) = rnf x `seq` y `seq` ()
replyTarget :: Serializer (Reply p reply) -> RequestOrigin p reply -> ReplyTarget p reply
replyTarget ser orig = MkReplyTarget (Arg orig ser)
replyTargetOrigin :: Lens' (ReplyTarget p reply) (RequestOrigin p reply)
replyTargetOrigin f (MkReplyTarget (Arg o x)) =
(\o' -> MkReplyTarget (Arg o' x)) <$> f o
replyTargetSerializer :: Lens' (ReplyTarget p reply) (Serializer (Reply p reply))
replyTargetSerializer f (MkReplyTarget (Arg x o)) =
(\o' -> MkReplyTarget (Arg x o')) <$> f o
embeddedReplyTarget :: Embeds outer inner => Serializer (Reply outer reply) -> RequestOrigin outer reply -> ReplyTarget inner reply
embeddedReplyTarget ser orig = toEmbeddedReplyTarget $ replyTarget ser orig
toEmbeddedReplyTarget :: Embeds outer inner => ReplyTarget outer reply -> ReplyTarget inner reply
toEmbeddedReplyTarget (MkReplyTarget (Arg orig ser)) =
MkReplyTarget (Arg (toEmbeddedOrigin orig) (embedReplySerializer ser))