Debian Codegraph @

apps/statswriter.hpp

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2018 Haivision Systems Inc.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11
12 #ifndef INC_SRT_APPS_STATSWRITER_H
13 #define INC_SRT_APPS_STATSWRITER_H
14
15 #include <string>
16 #include <map>
17 #include <vector>
18 #include <memory>
19
20 #include "srt.h"
21 #include "utilities.h"
22
23 enum SrtStatsPrintFormat
24 {
25 SRTSTATS_PROFMAT_INVALID = -1,
26 SRTSTATS_PROFMAT_2COLS = 0,
27 SRTSTATS_PROFMAT_JSON,
28 SRTSTATS_PROFMAT_CSV
29 };
30
31 SrtStatsPrintFormat ParsePrintFormat(std::string pf, std::string& w_extras);
32
33 enum SrtStatCat
34 {
35 SSC_GEN, //< General
36 SSC_WINDOW, // flow/congestion window
37 SSC_LINK, //< Link data
38 SSC_SEND, //< Sending
39 SSC_RECV //< Receiving
40 };
41
42 struct SrtStatData
43 {
44 SrtStatCat category;
45 std::string name;
46 std::string longname;
47
48 SrtStatData(SrtStatCat cat, std::string n, std::string l): category(cat), name(n), longname(l) {}
49 virtual ~SrtStatData() {}
50
51 virtual void PrintValue(std::ostream& str, const CBytePerfMon& mon) = 0;
52 };
53
54 template <class TYPE>
55 struct SrtStatDataType: public SrtStatData
56 {
57 typedef TYPE CBytePerfMon::*pfield_t;
58 pfield_t pfield;
59
60 SrtStatDataType(SrtStatCat cat, const std::string& name, const std::string& longname, pfield_t field)
61 : SrtStatData (cat, name, longname), pfield(field)
62 {
63 }
64
65 void PrintValue(std::ostream& str, const CBytePerfMon& mon) override
66 {
67 str << mon.*pfield;
68 }
69 };
70
71 class SrtStatsWriter
72 {
73 public:
74 virtual std::string WriteStats(int sid, const CBytePerfMon& mon) = 0;
75 virtual std::string WriteBandwidth(double mbpsBandwidth) = 0;
76 virtual ~SrtStatsWriter() {}
77
78 // Only if HAS_PUT_TIME. Specified in the imp file.
79 std::string print_timestamp();
80
81 void Option(const std::string& key, const std::string& val)
82 {
83 options[key] = val;
84 }
85
86 bool Option(const std::string& key, std::string* rval = nullptr)
87 {
88 const std::string* out = map_getp(options, key);
89 if (!out)
90 return false;
91
92 if (rval)
93 *rval = *out;
94 return true;
95 }
96
97 protected:
98 std::map<std::string, std::string> options;
99 };
100
101 extern std::vector<std::unique_ptr<SrtStatData>> g_SrtStatsTable;
102
103 std::shared_ptr<SrtStatsWriter> SrtStatsWriterFactory(SrtStatsPrintFormat printformat);
104
105
106
107 #endif