-
Notifications
You must be signed in to change notification settings - Fork 730
/
Copy pathAcceptor.hs
113 lines (102 loc) · 3.92 KB
/
Acceptor.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
module Cardano.Tracer.Test.Acceptor
( AcceptorsMode (..)
, launchAcceptorsSimple
) where
import Control.Concurrent.STM.TVar (newTVarIO, readTVarIO)
import Control.Concurrent.Async.Extra (sequenceConcurrently)
import Control.Concurrent.Extra (newLock)
import Control.Monad (forever, forM_, void)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.List.NonEmpty as NE
import qualified Data.Map.Strict as M
import qualified Data.Text as T
import System.Time.Extra (sleep)
import Cardano.Tracer.Environment
import Cardano.Tracer.Acceptors.Run
import Cardano.Tracer.Configuration
import Cardano.Tracer.Handlers.RTView.Run
import Cardano.Tracer.Handlers.RTView.State.Historical
import Cardano.Tracer.MetaTrace
import Cardano.Tracer.Types
import Cardano.Tracer.Utils
import Trace.Forward.Utils.DataPoint
data AcceptorsMode = Initiator | Responder
launchAcceptorsSimple
:: AcceptorsMode
-> FilePath
-> String
-> IO ()
launchAcceptorsSimple mode localSock dpName = do
protocolsBrake <- initProtocolsBrake
dpRequestors <- initDataPointRequestors
connectedNodes <- initConnectedNodes
connectedNodesNames <- initConnectedNodesNames
acceptedMetrics <- initAcceptedMetrics
savedTO <- initSavedTraceObjects
currentLogLock <- newLock
currentDPLock <- newLock
eventsQueues <- initEventsQueues Nothing connectedNodesNames dpRequestors currentDPLock
chainHistory <- initBlockchainHistory
resourcesHistory <- initResourcesHistory
txHistory <- initTransactionsHistory
rtViewPageOpened <- newTVarIO False
tr <- mkTracerTracer $ SeverityF $ Just Warning
let tracerEnv =
TracerEnv
{ teConfig = mkConfig
, teConnectedNodes = connectedNodes
, teConnectedNodesNames = connectedNodesNames
, teAcceptedMetrics = acceptedMetrics
, teSavedTO = savedTO
, teBlockchainHistory = chainHistory
, teResourcesHistory = resourcesHistory
, teTxHistory = txHistory
, teCurrentLogLock = currentLogLock
, teCurrentDPLock = currentDPLock
, teEventsQueues = eventsQueues
, teDPRequestors = dpRequestors
, teProtocolsBrake = protocolsBrake
, teRTViewPageOpened = rtViewPageOpened
, teRTViewStateDir = Nothing
, teTracer = tr
}
void . sequenceConcurrently $
[ runAcceptors tracerEnv
, runDataPointsPrinter dpName dpRequestors
]
where
mkConfig = TracerConfig
{ networkMagic = 764824073
, network = case mode of
Initiator -> ConnectTo $ NE.fromList [LocalSocket localSock]
Responder -> AcceptAt (LocalSocket localSock)
, loRequestNum = Just 1
, ekgRequestFreq = Just 1.0
, hasEKG = Nothing
, hasPrometheus = Nothing
, hasRTView = Nothing
, logging = NE.fromList [LoggingParams "/tmp/demo-acceptor" FileMode ForHuman]
, rotation = Nothing
, verbosity = Just Minimum
, metricsComp = Nothing
}
-- | To be able to ask any 'DataPoint' by the name without knowing the actual type,
-- we print it out as a raw 'ByteString'.
runDataPointsPrinter
:: String
-> DataPointRequestors
-> IO ()
runDataPointsPrinter dpName dpRequestors = forever $ do
sleep 1.0
dpReqs <- M.toList <$> readTVarIO dpRequestors
forM_ dpReqs $ \(_, dpReq) -> do
dpValues <- askForDataPoints dpReq [T.pack dpName]
forM_ dpValues $ \(dpName', dpValue) ->
case dpValue of
Nothing -> return ()
Just rawDPValue -> do
putStr $ "DataPoint, name: " <> T.unpack dpName' <> ", raw value: "
LBS.putStr rawDPValue
putStrLn ""