{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module Capnp.New.Rpc.Server
( CallHandler,
MethodHandler,
UntypedMethodHandler,
Export (..),
export,
findMethod,
SomeServer (..),
handleParsed,
handleRaw,
methodUnimplemented,
toUntypedMethodHandler,
MethodHandlerTree (..),
)
where
import qualified Capnp.Basics as B
import qualified Capnp.Classes as C
import Capnp.Convert (parsedToRaw)
import Capnp.Message (Mutability (..))
import qualified Capnp.Repr as R
import Capnp.Repr.Methods (Client (..))
import Capnp.Rpc.Errors
( eFailed,
eMethodUnimplemented,
wrapException,
)
import Capnp.Rpc.Promise
( Fulfiller,
breakPromise,
fulfill,
newCallback,
)
import qualified Capnp.Rpc.Server as Legacy
import qualified Capnp.Rpc.Untyped as URpc
import Capnp.TraversalLimit (defaultLimit, evalLimitT)
import qualified Capnp.Untyped as U
import Control.Exception.Safe (withException)
import Control.Monad.STM.Class (MonadSTM (..))
import Data.Function ((&))
import Data.Kind (Constraint, Type)
import qualified Data.Map.Strict as M
import Data.Maybe (fromMaybe)
import Data.Proxy (Proxy (..))
import Data.Typeable (Typeable)
import qualified Data.Vector as V
import Data.Word
import GHC.Prim (coerce)
import Internal.BuildPure (createPure)
import Supervisors (Supervisor)
type CallHandler = M.Map Word64 (V.Vector UntypedMethodHandler)
type MethodHandler p r =
R.Raw p 'Const ->
Fulfiller (R.Raw r 'Const) ->
IO ()
type UntypedMethodHandler = MethodHandler B.AnyStruct B.AnyStruct
class SomeServer a where
shutdown :: a -> IO ()
shutdown a
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
unwrap :: Typeable b => a -> Maybe b
unwrap a
_ = forall a. Maybe a
Nothing
class (R.IsCap i, C.HasTypeId i) => Export i where
type Server i :: Type -> Constraint
methodHandlerTree :: Server i s => Proxy i -> s -> MethodHandlerTree
data MethodHandlerTree = MethodHandlerTree
{
MethodHandlerTree -> Word64
mhtId :: Word64,
MethodHandlerTree -> [UntypedMethodHandler]
mhtHandlers :: [UntypedMethodHandler],
MethodHandlerTree -> [MethodHandlerTree]
mhtParents :: [MethodHandlerTree]
}
mhtToCallHandler :: MethodHandlerTree -> CallHandler
mhtToCallHandler :: MethodHandlerTree -> CallHandler
mhtToCallHandler = CallHandler -> [MethodHandlerTree] -> CallHandler
go forall k a. Map k a
M.empty forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
where
go :: CallHandler -> [MethodHandlerTree] -> CallHandler
go CallHandler
accum [] = CallHandler
accum
go CallHandler
accum (MethodHandlerTree
t : [MethodHandlerTree]
ts)
| MethodHandlerTree -> Word64
mhtId MethodHandlerTree
t forall k a. Ord k => k -> Map k a -> Bool
`M.member` CallHandler
accum = CallHandler -> [MethodHandlerTree] -> CallHandler
go CallHandler
accum [MethodHandlerTree]
ts
| Bool
otherwise =
CallHandler -> [MethodHandlerTree] -> CallHandler
go (forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert (MethodHandlerTree -> Word64
mhtId MethodHandlerTree
t) (forall a. [a] -> Vector a
V.fromList (MethodHandlerTree -> [UntypedMethodHandler]
mhtHandlers MethodHandlerTree
t)) CallHandler
accum) (MethodHandlerTree -> [MethodHandlerTree]
mhtParents MethodHandlerTree
t forall a. [a] -> [a] -> [a]
++ [MethodHandlerTree]
ts)
export :: forall i s m. (MonadSTM m, Export i, Server i s, SomeServer s) => Supervisor -> s -> m (Client i)
export :: forall i s (m :: * -> *).
(MonadSTM m, Export i, Server i s, SomeServer s) =>
Supervisor -> s -> m (Client i)
export Supervisor
sup s
srv =
let h :: CallHandler
h = MethodHandlerTree -> CallHandler
mhtToCallHandler (forall i s.
(Export i, Server i s) =>
Proxy i -> s -> MethodHandlerTree
methodHandlerTree (forall {k} (t :: k). Proxy t
Proxy @i) s
srv)
in forall (m :: * -> *) a. MonadSTM m => STM a -> m a
liftSTM forall a b. (a -> b) -> a -> b
$ forall a. Client -> Client a
Client forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *).
MonadSTM m =>
Supervisor -> ServerOps -> m Client
URpc.export Supervisor
sup (forall a. SomeServer a => a -> CallHandler -> ServerOps
toLegacyServerOps s
srv CallHandler
h)
findMethod :: Word64 -> Word16 -> CallHandler -> Maybe UntypedMethodHandler
findMethod :: Word64 -> Word16 -> CallHandler -> Maybe UntypedMethodHandler
findMethod Word64
interfaceId Word16
methodId CallHandler
handler = do
Vector UntypedMethodHandler
iface <- forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Word64
interfaceId CallHandler
handler
Vector UntypedMethodHandler
iface forall a. Vector a -> Int -> Maybe a
V.!? forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
methodId
toLegacyCallHandler ::
CallHandler ->
Word64 ->
Word16 ->
Legacy.MethodHandler (Maybe (U.Ptr 'Const)) (Maybe (U.Ptr 'Const))
toLegacyCallHandler :: CallHandler
-> Word64
-> Word16
-> MethodHandler (Maybe (Ptr 'Const)) (Maybe (Ptr 'Const))
toLegacyCallHandler CallHandler
callHandler Word64
interfaceId Word16
methodId =
Word64 -> Word16 -> CallHandler -> Maybe UntypedMethodHandler
findMethod Word64
interfaceId Word16
methodId CallHandler
callHandler
forall a b. a -> (a -> b) -> b
& forall a. a -> Maybe a -> a
fromMaybe forall p r. MethodHandler p r
methodUnimplemented
forall a b. a -> (a -> b) -> b
& UntypedMethodHandler
-> MethodHandler (Maybe (Ptr 'Const)) (Maybe (Ptr 'Const))
toLegacyMethodHandler
toUntypedMethodHandler ::
forall p r.
(R.IsStruct p, R.IsStruct r) =>
MethodHandler p r ->
UntypedMethodHandler
toUntypedMethodHandler :: forall p r.
(IsStruct p, IsStruct r) =>
MethodHandler p r -> UntypedMethodHandler
toUntypedMethodHandler = coerce :: forall a b. Coercible a b => a -> b
coerce
toLegacyMethodHandler :: UntypedMethodHandler -> Legacy.MethodHandler (Maybe (U.Ptr 'Const)) (Maybe (U.Ptr 'Const))
toLegacyMethodHandler :: UntypedMethodHandler
-> MethodHandler (Maybe (Ptr 'Const)) (Maybe (Ptr 'Const))
toLegacyMethodHandler UntypedMethodHandler
handler =
(Maybe (Ptr 'Const) -> Fulfiller (Maybe (Ptr 'Const)) -> IO ())
-> MethodHandler (Maybe (Ptr 'Const)) (Maybe (Ptr 'Const))
Legacy.untypedHandler forall a b. (a -> b) -> a -> b
$ \Maybe (Ptr 'Const)
args Fulfiller (Maybe (Ptr 'Const))
respond -> do
Fulfiller (Raw AnyStruct 'Const)
respond' <- forall (m :: * -> *) a.
MonadSTM m =>
(Either (Parsed Exception) a -> STM ()) -> m (Fulfiller a)
newCallback forall a b. (a -> b) -> a -> b
$ \case
Left Parsed Exception
e ->
forall (m :: * -> *) a.
MonadSTM m =>
Fulfiller a -> Parsed Exception -> m ()
breakPromise Fulfiller (Maybe (Ptr 'Const))
respond Parsed Exception
e
Right (R.Raw Unwrapped (Untyped (ReprFor AnyStruct) 'Const)
s) ->
forall (m :: * -> *) a. MonadSTM m => Fulfiller a -> a -> m ()
fulfill Fulfiller (Maybe (Ptr 'Const))
respond (forall a. a -> Maybe a
Just (forall (mut :: Mutability). Struct mut -> Ptr mut
U.PtrStruct Unwrapped (Untyped (ReprFor AnyStruct) 'Const)
s))
case Maybe (Ptr 'Const)
args of
Just (U.PtrStruct Struct 'Const
argStruct) ->
UntypedMethodHandler
handler (forall a (mut :: Mutability).
Unwrapped (Untyped (ReprFor a) mut) -> Raw a mut
R.Raw Struct 'Const
argStruct) Fulfiller (Raw AnyStruct 'Const)
respond'
Maybe (Ptr 'Const)
_ ->
forall (m :: * -> *) a.
MonadSTM m =>
Fulfiller a -> Parsed Exception -> m ()
breakPromise Fulfiller (Maybe (Ptr 'Const))
respond forall a b. (a -> b) -> a -> b
$ Text -> Parsed Exception
eFailed Text
"Argument was not a struct"
toLegacyServerOps :: SomeServer a => a -> CallHandler -> Legacy.ServerOps
toLegacyServerOps :: forall a. SomeServer a => a -> CallHandler -> ServerOps
toLegacyServerOps a
srv CallHandler
callHandler =
Legacy.ServerOps
{ handleStop :: IO ()
handleStop = forall a. SomeServer a => a -> IO ()
shutdown a
srv,
handleCast :: forall a. Typeable a => Maybe a
handleCast = forall a b. (SomeServer a, Typeable b) => a -> Maybe b
unwrap a
srv,
handleCall :: Word64
-> Word16
-> MethodHandler (Maybe (Ptr 'Const)) (Maybe (Ptr 'Const))
handleCall = CallHandler
-> Word64
-> Word16
-> MethodHandler (Maybe (Ptr 'Const)) (Maybe (Ptr 'Const))
toLegacyCallHandler CallHandler
callHandler
}
handleParsed ::
( C.Parse p pp,
R.IsStruct p,
C.Parse r pr,
R.IsStruct r
) =>
(pp -> IO pr) ->
MethodHandler p r
handleParsed :: forall p pp r pr.
(Parse p pp, IsStruct p, Parse r pr, IsStruct r) =>
(pp -> IO pr) -> MethodHandler p r
handleParsed pp -> IO pr
handler Raw p 'Const
param = forall a b. (Fulfiller a -> IO b) -> Fulfiller a -> IO b
propagateExceptions forall a b. (a -> b) -> a -> b
$ \Fulfiller (Raw r 'Const)
f -> do
pp
p <- forall (m :: * -> *) a.
MonadThrow m =>
WordCount -> LimitT m a -> m a
evalLimitT WordCount
defaultLimit forall a b. (a -> b) -> a -> b
$ forall t p (m :: * -> *).
(Parse t p, ReadCtx m 'Const) =>
Raw t 'Const -> m p
C.parse Raw p 'Const
param
pr
r <- pp -> IO pr
handler pp
p
Struct 'Const
struct <- forall (m :: * -> *) (f :: Mutability -> *).
(MonadThrow m, MaybeMutable f) =>
WordCount -> (forall s. PureBuilder s (f ('Mut s))) -> m (f 'Const)
createPure forall a. Bounded a => a
maxBound forall a b. (a -> b) -> a -> b
$ forall a (mut :: Mutability).
Raw a mut -> Unwrapped (Untyped (ReprFor a) mut)
R.fromRaw forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (m :: * -> *) pa s.
(RWCtx m s, IsStruct a, Parse a pa) =>
pa -> m (Raw a ('Mut s))
parsedToRaw pr
r
forall (m :: * -> *) a. MonadSTM m => Fulfiller a -> a -> m ()
fulfill Fulfiller (Raw r 'Const)
f (forall a (mut :: Mutability).
Unwrapped (Untyped (ReprFor a) mut) -> Raw a mut
R.Raw Struct 'Const
struct)
handleRaw ::
(R.IsStruct p, R.IsStruct r) =>
(R.Raw p 'Const -> IO (R.Raw r 'Const)) ->
MethodHandler p r
handleRaw :: forall p r.
(IsStruct p, IsStruct r) =>
(Raw p 'Const -> IO (Raw r 'Const)) -> MethodHandler p r
handleRaw Raw p 'Const -> IO (Raw r 'Const)
handler Raw p 'Const
param = forall a b. (Fulfiller a -> IO b) -> Fulfiller a -> IO b
propagateExceptions forall a b. (a -> b) -> a -> b
$ \Fulfiller (Raw r 'Const)
f ->
Raw p 'Const -> IO (Raw r 'Const)
handler Raw p 'Const
param forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. MonadSTM m => Fulfiller a -> a -> m ()
fulfill Fulfiller (Raw r 'Const)
f
propagateExceptions :: (Fulfiller a -> IO b) -> Fulfiller a -> IO b
propagateExceptions :: forall a b. (Fulfiller a -> IO b) -> Fulfiller a -> IO b
propagateExceptions Fulfiller a -> IO b
h Fulfiller a
f =
Fulfiller a -> IO b
h Fulfiller a
f forall (m :: * -> *) e a b.
(MonadMask m, Exception e) =>
m a -> (e -> m b) -> m a
`withException` (forall (m :: * -> *) a.
MonadSTM m =>
Fulfiller a -> Parsed Exception -> m ()
breakPromise Fulfiller a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> SomeException -> Parsed Exception
wrapException Bool
False)
methodUnimplemented :: MethodHandler p r
methodUnimplemented :: forall p r. MethodHandler p r
methodUnimplemented Raw p 'Const
_ Fulfiller (Raw r 'Const)
f = forall (m :: * -> *) a.
MonadSTM m =>
Fulfiller a -> Parsed Exception -> m ()
breakPromise Fulfiller (Raw r 'Const)
f Parsed Exception
eMethodUnimplemented