This commit is contained in:
Dmitry Zuikov 2023-01-16 11:34:06 +03:00
parent aad1dbea28
commit 9c9a3aa86b
1 changed files with 16 additions and 36 deletions

View File

@ -12,25 +12,6 @@ import Data.Map (Map)
import Control.Monad.Reader import Control.Monad.Reader
import Control.Monad.Identity import Control.Monad.Identity
data ProtocolA = ProtocolA
data ProtocolB = ProtocolB
instance HasProtocol ProtocolA where
type instance ProtocolId ProtocolA = 1
type instance Encoded ProtocolA = String
decode = undefined
encode = undefined
instance HasProtocol ProtocolB where
type instance ProtocolId ProtocolB = 2
type instance Encoded ProtocolB = String
decode = undefined
encode = undefined
-- class Response p (m :: Type -> Type) where
-- answer :: p -> m ()
data AnyProtocol m = forall a . ( HasProtocol a data AnyProtocol m = forall a . ( HasProtocol a
, KnownNat (ProtocolId a) , KnownNat (ProtocolId a)
@ -49,7 +30,7 @@ data PingPong = Ping Int
instance HasProtocol PingPong where instance HasProtocol PingPong where
type instance ProtocolId PingPong = 3 type instance ProtocolId PingPong = 1
type instance Encoded PingPong = PingPong type instance Encoded PingPong = PingPong
decode = undefined decode = undefined
encode = undefined encode = undefined
@ -58,29 +39,31 @@ instance HasProtocol PingPong where
class Response p (m :: Type -> Type) where class Response p (m :: Type -> Type) where
answer :: p -> m () answer :: p -> m ()
anyProtocol :: forall p m . ( MonadIO m makeProtocol :: forall p m . ( MonadIO m
, Response p m , Response p (EngineM m)
, HasProtocol p , HasProtocol p
, KnownNat (ProtocolId p) , KnownNat (ProtocolId p)
) )
=> (p -> m ()) -> AnyProtocol m => (p -> EngineM m ()) -> AnyProtocol (EngineM m)
anyProtocol h = AnyProtocol { getProtoId = natVal (Proxy @(ProtocolId p)) makeProtocol h = AnyProtocol { getProtoId = natVal (Proxy @(ProtocolId p))
, protoDecode = decode @p , protoDecode = decode @p
, protoEncode = encode @p , protoEncode = encode @p
, handle = h , handle = h
} }
newtype EngineM m a = EngineM { fromEngine :: ReaderT () m a } newtype EngineM m a = EngineM { fromEngine :: ReaderT () m a }
deriving ( Functor, Applicative, Monad, MonadTrans, MonadIO ) deriving ( Functor, Applicative, Monad, MonadTrans, MonadIO )
runEngineM :: EngineM m a -> m a runEngineM :: EngineM m a -> m a
runEngineM f = runReaderT (fromEngine f) () runEngineM f = runReaderT (fromEngine f) ()
instance (Monad m, HasProtocol p) => Response p m where instance (Monad m, HasProtocol p) => Response p (EngineM m) where
answer = undefined answer _ = do
-- TODO: get bus
-- TODO: encode
-- TODO: sendTo
undefined
testUniqiProtoId :: IO () testUniqiProtoId :: IO ()
testUniqiProtoId = do testUniqiProtoId = do
@ -91,19 +74,16 @@ testUniqiProtoId = do
-- TODO: GET RECIPIENT -- TODO: GET RECIPIENT
-- TODO: GET PROTO-ID FROM MESSAGE -- TODO: GET PROTO-ID FROM MESSAGE
let pingpong = anyProtocol @PingPong @(EngineM IO) let pingpong = makeProtocol @PingPong @IO
\case \case
Ping c -> lift (print "effect: PING") >> answer (Pong c) Ping c -> lift (print "effect: PING") >> answer (Pong c)
Pong _ -> lift (print "effect: PONG") Pong _ -> lift (print "effect: PONG")
-- FIXME: dispatcher! -- FIXME: dispatcher!
case Map.lookup 3 decoders of case Map.lookup 3 decoders of
Just (AnyProtocol {protoDecode = decoder, handle = h}) -> maybe (pure ()) (runEngineM . h) (decoder "AAA") Just (AnyProtocol {protoDecode = decoder, handle = h}) -> maybe (pure ()) (runEngineM . h) (decoder "AAA")
Nothing -> pure () Nothing -> pure ()
-- let qq = natVal (Proxy @(ProtocolId ProtocolA))
pure () pure ()