|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE GADTs #-} |
| 3 | +{-# LANGUAGE KindSignatures #-} |
| 4 | +{-# LANGUAGE LambdaCase #-} |
| 5 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 6 | +{-# LANGUAGE TypeApplications #-} |
| 7 | + |
| 8 | +-- | A view of the object diffusion protocol from the point of view of |
| 9 | +-- the inbound. |
| 10 | +-- |
| 11 | +-- This provides a view that uses less complex types and should be easier to |
| 12 | +-- use than the underlying typed protocol itself. |
| 13 | +-- |
| 14 | +-- For execution, a conversion into the typed protocol is provided. |
| 15 | +module Ouroboros.Network.Protocol.ObjectDiffusion.Inbound |
| 16 | + ( -- * Protocol type for the inbound |
| 17 | + |
| 18 | + -- | The protocol states from the point of view of the inbound. |
| 19 | + ObjectDiffusionInboundPipelined (..), |
| 20 | + InboundStIdle (..), |
| 21 | + Collect (..), |
| 22 | + |
| 23 | + -- * Execution as a typed protocol |
| 24 | + objectDiffusionClientInboundPeerPipelined, |
| 25 | + objectDiffusionServerInboundPeerPipelined, |
| 26 | + ) |
| 27 | +where |
| 28 | + |
| 29 | +import Data.List.NonEmpty (NonEmpty) |
| 30 | +import Network.TypedProtocol.Core |
| 31 | +import Network.TypedProtocol.Peer |
| 32 | +import Ouroboros.Network.Protocol.ObjectDiffusion.Type |
| 33 | + |
| 34 | +data ObjectDiffusionInboundPipelined objectId object m a where |
| 35 | + ObjectDiffusionInboundPipelined :: |
| 36 | + m (InboundStIdle Z objectId object m a) -> |
| 37 | + ObjectDiffusionInboundPipelined objectId object m a |
| 38 | + |
| 39 | +-- | This is the type of the pipelined results, collected by 'CollectPipelined'. |
| 40 | +-- This protocol can pipeline requests for object ids and objects, |
| 41 | +-- so we use a sum of either for collecting the responses. |
| 42 | +data Collect objectId object |
| 43 | + = -- | The result of 'SendMsgRequestObjectIdsPipelined'. It also carries |
| 44 | + -- the number of objectIds originally requested. |
| 45 | + CollectObjectIds NumObjectIdsToReq [(objectId, SizeInBytes)] |
| 46 | + | -- | The result of 'SendMsgRequestObjectsPipelined'. The actual reply only |
| 47 | + -- contains the objects sent, but this pairs them up with the |
| 48 | + -- objects requested. This is because the peer can determine that |
| 49 | + -- some objects are no longer needed. |
| 50 | + CollectObjects [objectId] [object] |
| 51 | + |
| 52 | +data InboundStIdle (n :: N) objectId object m a where |
| 53 | + SendMsgRequestObjectIdsBlocking :: |
| 54 | + -- | number of objectIds to acknowledge |
| 55 | + NumObjectIdsToAck -> |
| 56 | + -- | number of objectIds to request |
| 57 | + NumObjectIdsToReq -> |
| 58 | + -- | Result if done |
| 59 | + m a -> |
| 60 | + ( NonEmpty (objectId, SizeInBytes) -> |
| 61 | + m (InboundStIdle Z objectId object m a) |
| 62 | + ) -> |
| 63 | + InboundStIdle Z objectId object m a |
| 64 | + SendMsgRequestObjectIdsPipelined :: |
| 65 | + NumObjectIdsToAck -> |
| 66 | + NumObjectIdsToReq -> |
| 67 | + m (InboundStIdle (S n) objectId object m a) -> |
| 68 | + InboundStIdle n objectId object m a |
| 69 | + SendMsgRequestObjectsPipelined :: |
| 70 | + [objectId] -> |
| 71 | + m (InboundStIdle (S n) objectId object m a) -> |
| 72 | + InboundStIdle n objectId object m a |
| 73 | + -- | Collect a pipelined result. |
| 74 | + CollectPipelined :: |
| 75 | + Maybe (InboundStIdle (S n) objectId object m a) -> |
| 76 | + (Collect objectId object -> m (InboundStIdle n objectId object m a)) -> |
| 77 | + InboundStIdle (S n) objectId object m a |
| 78 | + |
| 79 | +inboundRun :: forall (pr :: PeerRole) (n :: N) objectId object m a. |
| 80 | + (Functor m) => |
| 81 | + InboundStIdle n objectId object m a -> |
| 82 | + Peer (ObjectDiffusion objectId object) pr (Pipelined n (Collect objectId object)) StIdle m a |
| 83 | + |
| 84 | +inboundRun (SendMsgRequestObjectIdsBlocking ackNo reqNo kDone k) = |
| 85 | + Yield undefined |
| 86 | + (MsgRequestObjectIds SingBlocking ackNo reqNo) |
| 87 | + $ Await undefined |
| 88 | + $ \case |
| 89 | + MsgDone -> Effect (Done undefined <$> kDone) |
| 90 | + MsgReplyObjectIds (BlockingReply objectIds) -> Effect (inboundRun <$> k objectIds) |
| 91 | +inboundRun (SendMsgRequestObjectIdsPipelined ackNo reqNo k) = |
| 92 | + YieldPipelined undefined |
| 93 | + (MsgRequestObjectIds SingNonBlocking ackNo reqNo) |
| 94 | + (ReceiverAwait undefined $ \(MsgReplyObjectIds (NonBlockingReply objectIds)) -> ReceiverDone (CollectObjectIds reqNo objectIds)) |
| 95 | + (Effect (inboundRun <$> k)) |
| 96 | +inboundRun (SendMsgRequestObjectsPipelined objectIds k) = |
| 97 | + YieldPipelined undefined |
| 98 | + (MsgRequestObjects objectIds) |
| 99 | + (ReceiverAwait undefined $ \(MsgReplyObjects objects) -> ReceiverDone (CollectObjects objectIds objects)) |
| 100 | + (Effect (inboundRun <$> k)) |
| 101 | +inboundRun (CollectPipelined mNone collect) = |
| 102 | + Collect |
| 103 | + (fmap inboundRun mNone) |
| 104 | + (Effect . fmap inboundRun . collect) |
| 105 | + |
| 106 | +-- | Transform a 'ObjectDiffusionInboundPipelined' into a 'PeerPipelined'. |
| 107 | +objectDiffusionClientInboundPeerPipelined :: |
| 108 | + forall objectId object m a. |
| 109 | + (Functor m) => |
| 110 | + ObjectDiffusionInboundPipelined objectId object m a -> |
| 111 | + PeerPipelined (ObjectDiffusion objectId object) 'AsClient StInit m a |
| 112 | +objectDiffusionClientInboundPeerPipelined (ObjectDiffusionInboundPipelined inboundSt) = |
| 113 | + PeerPipelined $ |
| 114 | + Yield undefined MsgInit $ |
| 115 | + Effect $ |
| 116 | + inboundRun <$> inboundSt |
| 117 | + |
| 118 | +objectDiffusionServerInboundPeerPipelined :: |
| 119 | + forall objectId object m a. |
| 120 | + (Functor m) => |
| 121 | + ObjectDiffusionInboundPipelined objectId object m a -> |
| 122 | + PeerPipelined (ObjectDiffusion objectId object) 'AsServer StInit m a |
| 123 | +objectDiffusionServerInboundPeerPipelined (ObjectDiffusionInboundPipelined inboundSt) = |
| 124 | + PeerPipelined $ |
| 125 | + Await @_ @_ @(Pipelined Z (Collect objectId object)) undefined |
| 126 | + (\MsgInit -> Effect (inboundRun <$> inboundSt)) |
0 commit comments