s-net-Tools
Statistic.h
Go to the documentation of this file.
1 
22 #ifndef STATISTIC_H
23 #define STATISTIC_H
24 
25 #include <bitset>
26 
27 class Statistic {
28 public:
29  Statistic(std::string a_Identifier): m_Identifier(a_Identifier) {
30  Reset();
31  }
32 
33  void Reset() {
34  m_SeqNbrsSeen[0].reset();
35  m_SeqNbrsSeen[1].reset();
36  m_Generation = 0;
37  m_BankIndex = 0;
38  m_TotalReceived = 0;
39  m_TotalMissing = 0;
40  m_Resets = 0;
41  }
42 
43  void Update(uint32_t a_SeqNbr) {
44  uint32_t l_Generation = ((a_SeqNbr & 0xFFFFFF00) >> 8);
45  uint8_t l_Nbr = (a_SeqNbr & 0x000000FF);
46  if (((l_Generation + 1) & 0x00FFFFFFF) == m_Generation) {
47  // Ignore
48  } else if (l_Generation == m_Generation) {
49  // Copy
50  m_SeqNbrsSeen[m_BankIndex].set(l_Nbr);
51  } else if (l_Generation == ((m_Generation + 1) & 0x00FFFFFF)) {
52  // Copy
53  uint8_t l_BankIndex = ((m_BankIndex + 1) & 0x01);
54  m_SeqNbrsSeen[l_BankIndex].set(l_Nbr);
55  } else if (l_Generation == ((m_Generation + 2) & 0x00FFFFFF)) {
56  // One generation is complete now
57  PrintReport(m_Generation, m_SeqNbrsSeen[m_BankIndex].count());
58  ++m_Generation;
59  m_SeqNbrsSeen[m_BankIndex].reset();
60  m_SeqNbrsSeen[m_BankIndex].set(l_Nbr);
61  m_BankIndex = ((m_BankIndex + 1) & 0x01);
62  } else if (l_Generation == ((m_Generation + 3) & 0x00FFFFFF)) {
63  // Both generations are complete now
64  PrintReport(m_Generation, m_SeqNbrsSeen[m_BankIndex].count());
65  ++m_Generation;
66  uint8_t l_BankIndex = ((m_BankIndex + 1) & 0x01);
67 
68  PrintReport(m_Generation, m_SeqNbrsSeen[m_BankIndex].count());
69  ++m_Generation;
70  m_SeqNbrsSeen[0].reset();
71  m_SeqNbrsSeen[1].reset();
72  m_SeqNbrsSeen[l_BankIndex].set(l_Nbr);
73  } else {
74  // Start from scratch
75  m_TotalReceived += m_SeqNbrsSeen[0].count();
76  m_TotalReceived += m_SeqNbrsSeen[1].count();
77  m_SeqNbrsSeen[0].reset();
78  m_SeqNbrsSeen[1].reset();
79  m_BankIndex = 0;
80  m_Generation = l_Generation;
81  m_SeqNbrsSeen[0].set(l_Nbr);
82  ++m_Resets;
83  } // else
84  }
85 
86 private:
87  // Helpers
88  void PrintReport(uint32_t a_Generation, size_t a_AmountOfPackets) {
89  // Update statistic
90  m_TotalReceived += a_AmountOfPackets;
91  m_TotalMissing += (256 - a_AmountOfPackets);
92 
93  std::cout << "Report " << std::dec << m_Identifier << ": Gen = " << a_Generation << ", Amount = " << a_AmountOfPackets;
94  if (a_AmountOfPackets == 256) {
95  std::cout << " ";
96  } else {
97  std::cout << " (!!!) ";
98  } // else
99 
100  double l_DeliveryRate = ((m_TotalReceived * 100) / (m_TotalReceived + m_TotalMissing));
101  std::cout << "resets: " << m_Resets
102  << ", total RX: " << m_TotalReceived
103  << ", estimated loss (lower bound): " << m_TotalMissing
104  << ", delivery rate: " << l_DeliveryRate << std::endl;
105  }
106 
107  // Members
108  std::string m_Identifier;
109  uint32_t m_Generation;
110  uint8_t m_BankIndex;
111  std::bitset<256> m_SeqNbrsSeen[2];
112  unsigned long long m_TotalReceived;
113  unsigned long long m_TotalMissing;
114  unsigned long long m_Resets;
115 };
116 
117 #endif // STATISTIC_H
Statistic(std::string a_Identifier)
Definition: Statistic.h:29
void Update(uint32_t a_SeqNbr)
Definition: Statistic.h:43
void Reset()
Definition: Statistic.h:33