-
Notifications
You must be signed in to change notification settings - Fork 86
/
ping-pong.hs
293 lines (252 loc) · 8.56 KB
/
ping-pong.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}
module Main where
import Data.ByteString.Lazy qualified as LBS
import Data.Functor (void)
import Data.Void (Void)
import Control.Concurrent.Async
import Control.Monad (when)
import Control.Tracer
import GHC.Clock (getMonotonicTime)
import System.Directory
import System.Environment
import System.Exit
import System.IO
import Text.Printf (printf)
import Network.Mux qualified as Mx
import Ouroboros.Network.ErrorPolicy
import Ouroboros.Network.IOManager
import Ouroboros.Network.Mux
import Ouroboros.Network.Snocket
import Ouroboros.Network.Snocket qualified as Snocket
import Ouroboros.Network.Socket
import Ouroboros.Network.Util.ShowProxy (ShowProxy (..))
import Ouroboros.Network.Protocol.Handshake.Codec
import Ouroboros.Network.Protocol.Handshake.Unversioned
import Ouroboros.Network.Protocol.Handshake.Version
import Network.TypedProtocol.PingPong.Client as PingPong
import Network.TypedProtocol.PingPong.Codec.CBOR as PingPong
import Network.TypedProtocol.PingPong.Examples
import Network.TypedProtocol.PingPong.Server as PingPong
import Network.TypedProtocol.PingPong.Type (PingPong)
main :: IO ()
main = do
args <- getArgs
case args of
"pingpong":"client":[] -> clientPingPong False
"pingpong":"client-pipelined":[] -> clientPingPong True
"pingpong":"server":[] -> do
rmIfExists defaultLocalSocketAddrPath
void serverPingPong
"pingpong2":"client":[] -> clientPingPong2
"pingpong2":"server":[] -> do
rmIfExists defaultLocalSocketAddrPath
void serverPingPong2
_ -> usage
instance ShowProxy PingPong where
showProxy _ = "PingPong"
usage :: IO ()
usage = do
hPutStrLn stderr "usage: demo-ping-pong [pingpong|pingpong2] {client|server} [addr]"
exitFailure
defaultLocalSocketAddrPath :: FilePath
defaultLocalSocketAddrPath = "./demo-ping-pong.sock"
defaultLocalSocketAddr :: LocalAddress
defaultLocalSocketAddr = localAddressFromPath defaultLocalSocketAddrPath
rmIfExists :: FilePath -> IO ()
rmIfExists path = do
b <- doesFileExist path
when b (removeFile path)
-- TODO: provide sensible limits
-- https://github.com/intersectmbo/ouroboros-network/issues/575
maximumMiniProtocolLimits :: MiniProtocolLimits
maximumMiniProtocolLimits =
MiniProtocolLimits {
maximumIngressQueue = maxBound
}
tracer :: Show a => Tracer IO a
tracer = f `contramapM` stdoutTracer
where
f a = do
t <- getMonotonicTime
return (printf "%-20f %s" t (show a))
--
-- Ping pong demo
--
demoProtocol0 :: RunMiniProtocolWithMinimalCtx appType addr bytes m a b
-> OuroborosApplicationWithMinimalCtx appType addr bytes m a b
demoProtocol0 pingPong =
OuroborosApplication [
MiniProtocol {
miniProtocolNum = MiniProtocolNum 2,
miniProtocolLimits = maximumMiniProtocolLimits,
miniProtocolRun = pingPong
}
]
clientPingPong :: Bool -> IO ()
clientPingPong pipelined =
withIOManager $ \iomgr ->
void $
connectToNode
(Snocket.localSnocket iomgr)
makeLocalBearer
ConnectToArgs {
ctaHandshakeCodec = unversionedHandshakeCodec,
ctaHandshakeTimeLimits = noTimeLimitsHandshake,
ctaVersionDataCodec = unversionedProtocolDataCodec,
ctaConnectTracers = nullNetworkConnectTracers,
ctaHandshakeCallbacks = HandshakeCallbacks acceptableVersion queryVersion
}
mempty
(unversionedProtocol app)
Nothing
defaultLocalSocketAddr
where
app :: OuroborosApplicationWithMinimalCtx
Mx.InitiatorMode LocalAddress LBS.ByteString IO () Void
app = demoProtocol0 pingPongInitiator
pingPongInitiator | pipelined =
InitiatorProtocolOnly $
mkMiniProtocolCbFromPeerPipelined $ \_ctx ->
( contramap show stdoutTracer
, codecPingPong
, void $ pingPongClientPeerPipelined (pingPongClientPipelinedMax 5)
)
| otherwise =
InitiatorProtocolOnly $
mkMiniProtocolCbFromPeer $ \_ctx ->
( contramap show stdoutTracer
, codecPingPong
, pingPongClientPeer (pingPongClientCount 5)
)
serverPingPong :: IO Void
serverPingPong =
withIOManager $ \iomgr -> do
networkState <- newNetworkMutableState
_ <- async $ cleanNetworkMutableState networkState
withServerNode
(Snocket.localSnocket iomgr)
makeLocalBearer
mempty
nullNetworkServerTracers
networkState
(AcceptedConnectionsLimit maxBound maxBound 0)
defaultLocalSocketAddr
unversionedHandshakeCodec
noTimeLimitsHandshake
unversionedProtocolDataCodec
(HandshakeCallbacks acceptableVersion queryVersion)
(unversionedProtocol (SomeResponderApplication app))
nullErrorPolicies
$ \_ serverAsync ->
wait serverAsync -- block until async exception
where
app :: OuroborosApplicationWithMinimalCtx
Mx.ResponderMode LocalAddress LBS.ByteString IO Void ()
app = demoProtocol0 pingPongResponder
pingPongResponder =
ResponderProtocolOnly $
mkMiniProtocolCbFromPeer $ \_ctx ->
( tracer
, codecPingPong
, pingPongServerPeer pingPongServerStandard
)
--
-- Ping pong demo2
--
demoProtocol1 :: RunMiniProtocolWithMinimalCtx appType addr bytes m a b
-> RunMiniProtocolWithMinimalCtx appType addr bytes m a b
-> OuroborosApplicationWithMinimalCtx appType addr bytes m a b
demoProtocol1 pingPong pingPong' =
OuroborosApplication [
MiniProtocol {
miniProtocolNum = MiniProtocolNum 2,
miniProtocolLimits = maximumMiniProtocolLimits,
miniProtocolRun = pingPong
}
, MiniProtocol {
miniProtocolNum = MiniProtocolNum 3,
miniProtocolLimits = maximumMiniProtocolLimits,
miniProtocolRun = pingPong'
}
]
clientPingPong2 :: IO ()
clientPingPong2 =
withIOManager $ \iomgr -> void $ do
connectToNode
(Snocket.localSnocket iomgr)
makeLocalBearer
ConnectToArgs {
ctaHandshakeCodec = unversionedHandshakeCodec,
ctaHandshakeTimeLimits = noTimeLimitsHandshake,
ctaVersionDataCodec = unversionedProtocolDataCodec,
ctaConnectTracers = nullNetworkConnectTracers,
ctaHandshakeCallbacks = HandshakeCallbacks acceptableVersion queryVersion
}
mempty
(unversionedProtocol app)
Nothing
defaultLocalSocketAddr
where
app :: OuroborosApplicationWithMinimalCtx
Mx.InitiatorMode addr LBS.ByteString IO () Void
app = demoProtocol1 pingpong pingpong'
pingpong =
InitiatorProtocolOnly $
mkMiniProtocolCbFromPeer $ \_ctx ->
( contramap (show . (,) (1 :: Int)) tracer
, codecPingPong
, pingPongClientPeer (pingPongClientCount 5)
)
pingpong'=
InitiatorProtocolOnly $
mkMiniProtocolCbFromPeer $ \_ctx ->
( contramap (show . (,) (2 :: Int)) tracer
, codecPingPong
, pingPongClientPeer (pingPongClientCount 5)
)
serverPingPong2 :: IO Void
serverPingPong2 =
withIOManager $ \iomgr -> do
networkState <- newNetworkMutableState
_ <- async $ cleanNetworkMutableState networkState
withServerNode
(Snocket.localSnocket iomgr)
makeLocalBearer
mempty
nullNetworkServerTracers
networkState
(AcceptedConnectionsLimit maxBound maxBound 0)
defaultLocalSocketAddr
unversionedHandshakeCodec
noTimeLimitsHandshake
unversionedProtocolDataCodec
(HandshakeCallbacks acceptableVersion queryVersion)
(unversionedProtocol (SomeResponderApplication app))
nullErrorPolicies
$ \_ serverAsync ->
wait serverAsync -- block until async exception
where
app :: OuroborosApplicationWithMinimalCtx
Mx.ResponderMode addr LBS.ByteString IO Void ()
app = demoProtocol1 pingpong pingpong'
pingpong =
ResponderProtocolOnly $
mkMiniProtocolCbFromPeer $ \_ctx ->
( contramap (show . (,) (1 :: Int)) tracer
, codecPingPong
, pingPongServerPeer pingPongServerStandard
)
pingpong' =
ResponderProtocolOnly $
mkMiniProtocolCbFromPeer $ \_ctx ->
( contramap (show . (,) (2 :: Int)) tracer
, codecPingPong
, pingPongServerPeer pingPongServerStandard
)