/
Mux.hs
355 lines (294 loc) · 11.5 KB
/
Mux.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
module Ouroboros.Network.Mux
( MuxMode (..)
, OuroborosApplication (..)
, MuxProtocolBundle
, ProtocolTemperature (..)
, TokProtocolTemperature (..)
, WithProtocolTemperature (..)
, withoutProtocolTemperature
, Bundle (..)
, projectBundle
, OuroborosBundle
, MuxBundle
, MiniProtocol (..)
, MiniProtocolNum (..)
, MiniProtocolLimits (..)
, RunMiniProtocol (..)
, MuxPeer (..)
, runMuxPeer
, toApplication
, mkMuxApplicationBundle
, ControlMessage (..)
, ControlMessageSTM
, continueForever
-- * Re-exports
-- | from "Network.Mux"
, MuxError(..)
, MuxErrorType(..)
, HasInitiator
, HasResponder
) where
import Control.Monad.Class.MonadAsync
import Control.Monad.Class.MonadSTM
import Control.Monad.Class.MonadThrow
import Control.Tracer (Tracer)
import Data.Typeable (Typeable)
import Data.Void (Void)
import qualified Data.ByteString.Lazy as LBS
import Network.TypedProtocol.Core
import Network.TypedProtocol.Pipelined
import qualified Network.Mux.Compat as Mux
import Network.Mux
( MuxMode(..), HasInitiator, HasResponder
, MiniProtocolNum, MiniProtocolLimits(..)
, MuxError(..), MuxErrorType(..) )
import Ouroboros.Network.Channel
import Ouroboros.Network.ConnectionId
import Ouroboros.Network.Codec
import Ouroboros.Network.Driver
import Ouroboros.Network.Util.ShowProxy (ShowProxy)
-- | Control signal sent to a mini-protocol. expected to exit, on 'Continue' it
-- should continue its operation
--
data ControlMessage =
-- | Continue operation.
Continue
-- | Hold on, e.g. do not sent messages until resumed. This is not used for
-- any hot protocol.
--
| Quiesce
-- | The client is expected to terminate as soon as possible.
--
| Terminate
deriving (Eq, Show)
-- | 'ControlMessageSTM' should depend on `muxMode` (we only need to shedule
-- stop for intiator side). This is not done only because this would break
-- tests, but once the old api is removed it should be possible.
--
type ControlMessageSTM m = STM m ControlMessage
continueForever :: Applicative (STM m)
=> proxy m
-> ControlMessageSTM m
continueForever _ = pure Continue
-- | Like 'MuxApplication' but using a 'MuxPeer' rather than a raw
-- @Channel -> m a@ action.
--
newtype OuroborosApplication (mode :: MuxMode) addr bytes m a b =
OuroborosApplication
(ConnectionId addr -> ControlMessageSTM m -> [MiniProtocol mode bytes m a b])
-- | There are three kinds of applications: warm, hot and established (ones
-- that run in for both warm and hot peers).
--
data ProtocolTemperature = Hot | Warm | Established
-- | Singletons for 'AppKind'
--
data TokProtocolTemperature (pt :: ProtocolTemperature) where
TokHot :: TokProtocolTemperature Hot
TokWarm :: TokProtocolTemperature Warm
TokEstablished :: TokProtocolTemperature Established
-- | We keep hot and warm application (or their context) distinct. It's only
-- needed for a handly 'projectBundle' map.
--
data WithProtocolTemperature (pt :: ProtocolTemperature) a where
WithHot :: !a -> WithProtocolTemperature Hot a
WithWarm :: !a -> WithProtocolTemperature Warm a
WithEstablished :: !a -> WithProtocolTemperature Established a
deriving instance Eq a => Eq (WithProtocolTemperature pt a)
deriving instance Show a => Show (WithProtocolTemperature pt a)
deriving instance (Functor (WithProtocolTemperature pt))
instance Semigroup a => Semigroup (WithProtocolTemperature pt a) where
WithHot a <> WithHot b = WithHot (a <> b)
WithWarm a <> WithWarm b = WithWarm (a <> b)
WithEstablished a <> WithEstablished b = WithEstablished (a <> b)
instance Monoid a => Monoid (WithProtocolTemperature Hot a) where
mempty = WithHot mempty
instance Monoid a => Monoid (WithProtocolTemperature Warm a) where
mempty = WithWarm mempty
instance Monoid a => Monoid (WithProtocolTemperature Established a) where
mempty = WithEstablished mempty
withoutProtocolTemperature :: WithProtocolTemperature pt a -> a
withoutProtocolTemperature (WithHot a) = a
withoutProtocolTemperature (WithWarm a) = a
withoutProtocolTemperature (WithEstablished a) = a
-- | A bundle of 'HotApp', 'WarmApp' and 'EstablishedApp'.
--
data Bundle a =
Bundle {
-- | hot mini-protocols
--
withHot
:: !(WithProtocolTemperature Hot a),
-- | warm mini-protocols
--
withWarm
:: !(WithProtocolTemperature Warm a),
-- | established mini-protocols
--
withEstablished
:: !(WithProtocolTemperature Established a)
}
deriving (Eq, Show, Functor)
instance Semigroup a => Semigroup (Bundle a) where
Bundle hot warm established <> Bundle hot' warm' established' =
Bundle (hot <> hot')
(warm <> warm')
(established <> established')
instance Monoid a => Monoid (Bundle a) where
mempty = Bundle mempty mempty mempty
projectBundle :: TokProtocolTemperature pt -> Bundle a -> a
projectBundle TokHot = withoutProtocolTemperature . withHot
projectBundle TokWarm = withoutProtocolTemperature . withWarm
projectBundle TokEstablished = withoutProtocolTemperature . withEstablished
instance Applicative Bundle where
pure a = Bundle (WithHot a) (WithWarm a) (WithEstablished a)
Bundle (WithHot hotFn)
(WithWarm warmFn)
(WithEstablished establishedFn)
<*> Bundle (WithHot hot)
(WithWarm warm)
(WithEstablished established) =
Bundle (WithHot $ hotFn hot)
(WithWarm $ warmFn warm)
(WithEstablished $ establishedFn established)
--
-- Useful type synonyms
--
type MuxProtocolBundle (mode :: MuxMode) addr bytes m a b
= ConnectionId addr
-> ControlMessageSTM m
-> [MiniProtocol mode bytes m a b]
type OuroborosBundle (mode :: MuxMode) addr bytes m a b =
Bundle (MuxProtocolBundle mode addr bytes m a b)
data MiniProtocol (mode :: MuxMode) bytes m a b =
MiniProtocol {
miniProtocolNum :: !MiniProtocolNum,
miniProtocolLimits :: !MiniProtocolLimits,
miniProtocolRun :: !(RunMiniProtocol mode bytes m a b)
}
type MuxBundle (mode :: MuxMode) bytes m a b =
Bundle [MiniProtocol mode bytes m a b]
data RunMiniProtocol (mode :: MuxMode) bytes m a b where
InitiatorProtocolOnly
:: MuxPeer bytes m a
-> RunMiniProtocol InitiatorMode bytes m a Void
ResponderProtocolOnly
:: MuxPeer bytes m b
-> RunMiniProtocol ResponderMode bytes m Void b
InitiatorAndResponderProtocol
:: MuxPeer bytes m a
-> MuxPeer bytes m b
-> RunMiniProtocol InitiatorResponderMode bytes m a b
data MuxPeer bytes m a where
MuxPeer :: forall (pr :: PeerRole) ps (st :: ps) failure bytes m a.
( Typeable ps
, Typeable failure
, Show failure
, forall (st' :: ps). Show (ClientHasAgency st')
, forall (st' :: ps). Show (ServerHasAgency st')
, ShowProxy ps
)
=> Tracer m (TraceSendRecv ps)
-> Codec ps failure m bytes
-> Peer ps pr st m a
-> MuxPeer bytes m a
MuxPeerPipelined
:: forall (pr :: PeerRole) ps (st :: ps) failure bytes m a.
( Typeable ps
, Typeable failure
, Show failure
, forall (st' :: ps). Show (ClientHasAgency st')
, forall (st' :: ps). Show (ServerHasAgency st')
, ShowProxy ps
)
=> Tracer m (TraceSendRecv ps)
-> Codec ps failure m bytes
-> PeerPipelined ps pr st m a
-> MuxPeer bytes m a
MuxPeerRaw
:: (Channel m bytes -> m (a, Maybe bytes))
-> MuxPeer bytes m a
toApplication :: (MonadCatch m, MonadAsync m)
=> ConnectionId addr
-> ControlMessageSTM m
-> OuroborosApplication mode addr LBS.ByteString m a b
-> Mux.MuxApplication mode m a b
toApplication connectionId controlMessageSTM (OuroborosApplication ptcls) =
Mux.MuxApplication
[ Mux.MuxMiniProtocol {
Mux.miniProtocolNum = miniProtocolNum ptcl,
Mux.miniProtocolLimits = miniProtocolLimits ptcl,
Mux.miniProtocolRun = toMuxRunMiniProtocol (miniProtocolRun ptcl)
}
| ptcl <- ptcls connectionId controlMessageSTM ]
mkMuxApplicationBundle
:: forall mode addr bytes m a b.
ConnectionId addr
-> Bundle (ControlMessageSTM m)
-> OuroborosBundle mode addr bytes m a b
-> MuxBundle mode bytes m a b
mkMuxApplicationBundle connectionId
(Bundle
hotControlMessageSTM
warmControlMessageSTM
establishedControlMessageSTM)
(Bundle
hotApp
warmApp
establishedApp) =
Bundle {
withHot =
mkApplication hotControlMessageSTM hotApp,
withWarm =
mkApplication warmControlMessageSTM warmApp,
withEstablished =
mkApplication establishedControlMessageSTM establishedApp
}
where
mkApplication :: WithProtocolTemperature pt (ControlMessageSTM m)
-> WithProtocolTemperature pt (MuxProtocolBundle mode addr bytes m a b)
-> WithProtocolTemperature pt [MiniProtocol mode bytes m a b]
mkApplication (WithHot scheduleStop) (WithHot app) =
WithHot $ app connectionId scheduleStop
mkApplication (WithWarm scheduleStop) (WithWarm app) =
WithWarm $ app connectionId scheduleStop
mkApplication (WithEstablished scheduleStop) (WithEstablished app) =
WithEstablished $ app connectionId scheduleStop
toMuxRunMiniProtocol :: forall mode m a b.
(MonadCatch m, MonadAsync m)
=> RunMiniProtocol mode LBS.ByteString m a b
-> Mux.RunMiniProtocol mode m a b
toMuxRunMiniProtocol (InitiatorProtocolOnly i) =
Mux.InitiatorProtocolOnly (runMuxPeer i . fromChannel)
toMuxRunMiniProtocol (ResponderProtocolOnly r) =
Mux.ResponderProtocolOnly (runMuxPeer r . fromChannel)
toMuxRunMiniProtocol (InitiatorAndResponderProtocol i r) =
Mux.InitiatorAndResponderProtocol (runMuxPeer i . fromChannel)
(runMuxPeer r . fromChannel)
-- |
-- Run a @'MuxPeer'@ using either @'runPeer'@ or @'runPipelinedPeer'@.
--
runMuxPeer
:: ( MonadCatch m
, MonadAsync m
)
=> MuxPeer bytes m a
-> Channel m bytes
-> m (a, Maybe bytes)
runMuxPeer (MuxPeer tracer codec peer) channel =
runPeer tracer codec channel peer
runMuxPeer (MuxPeerPipelined tracer codec peer) channel =
runPipelinedPeer tracer codec channel peer
runMuxPeer (MuxPeerRaw action) channel =
action channel