EMANE  1.0.1
otastatisticpublisher.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 - Adjacent Link LLC, Bridgewater, New Jersey
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * * Neither the name of Adjacent Link LLC nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "otastatisticpublisher.h"
35 
36 // specialized hash for PacketCounTable
37 namespace std
38 {
39  template<>
40  struct hash<std::pair<std::string,EMANE::NEMId>>
41  {
42  typedef std::pair<std::string,EMANE::NEMId> argument_type;
43  typedef std::size_t result_type;
44 
45  result_type operator()(argument_type const& s) const
46  {
47  result_type const h1{std::hash<std::string>()(s.first)};
48  result_type const h2{std::hash<EMANE::NEMId>()(s.second)};
49  return h1 ^ (h2 << 1);
50  }
51  };
52 }
53 
54 namespace
55 {
56  const EMANE::StatisticTableLabels PacketCountLabels =
57  {
58  "Src",
59  "Emulator UUID",
60  "Num Pkts Tx",
61  "Num Pkts Rx"
62  };
63 
65  {
66  PACKET_COUNT_COLUMN_NEM = 0,
67  PACKET_COUNT_COLUMN_UUID = 1,
68  PACKET_COUNT_COLUMN_NUM_PACKETS_TX = 2,
69  PACKET_COUNT_COLUMN_NUM_PACKETS_RX = 3,
70  };
71 }
72 
74  rowLimit_{0}
75 {
76  auto statisticRegistrar = StatisticRegistrarProxy{*StatisticServiceSingleton::instance(),0};
77 
78  pNumOTAChannelDownstreamPackets_ =
79  statisticRegistrar.registerNumeric<std::uint64_t>("numOTAChannelDownstreamPackets",
81  pNumOTAChannelUpstreamPackets_ =
82  statisticRegistrar.registerNumeric<std::uint64_t>("numOTAChannelUpstreamPackets",
84  pPacketCountTable_ =
85  statisticRegistrar.registerTable<PacketCountTableKey>("OTAChannelPacketCountTable",
86  PacketCountLabels,
87  [this](StatisticTablePublisher * pTable)
88  {
89  std::lock_guard<std::mutex> m(mutexPacketCountTable_);
90  packetCountInfo_.clear();
91  pTable->clear();
92  },
93  "OTA packet count table.");
94 }
95 
96 void EMANE::OTAStatisticPublisher::update(Type type, const uuid_t & uuid, NEMId nemId)
97 {
98  char buf[37];
99  uuid_unparse(uuid,buf);
100  auto key = PacketCountTableKey{buf,nemId};
101 
102  std::lock_guard<std::mutex> m(mutexPacketCountTable_);
103 
104  auto iter = packetCountInfo_.find(key);
105 
106  if(iter == packetCountInfo_.end())
107  {
108  if(packetCountInfo_.size() < rowLimit_)
109  {
110  iter = packetCountInfo_.insert({key,std::make_tuple(0,0)}).first;
111 
112  pPacketCountTable_->addRow(key,
113  {Any{nemId},
114  Any{buf},
115  Any{0L},
116  Any{0L}});
117  }
118  }
119 
120  if(type == Type::TYPE_UPSTREAM)
121  {
122  ++*pNumOTAChannelUpstreamPackets_;
123 
124  if(iter != packetCountInfo_.end())
125  {
126  auto & packets = std::get<PACKET_COUNT_COLUMN_NUM_PACKETS_RX-2>(iter->second);
127 
128  packets += 1;
129 
130  pPacketCountTable_->setCell(key,
131  PACKET_COUNT_COLUMN_NUM_PACKETS_RX,
132  Any{packets});
133  }
134 
135  }
136  else if(type == Type::TYPE_DOWNSTREAM)
137  {
138  ++*pNumOTAChannelDownstreamPackets_;
139 
140  if(iter != packetCountInfo_.end())
141  {
142  auto & packets = std::get<PACKET_COUNT_COLUMN_NUM_PACKETS_TX-2>(iter->second);
143 
144  packets += 1;
145 
146  pPacketCountTable_->setCell(key,
147  PACKET_COUNT_COLUMN_NUM_PACKETS_TX,
148  Any{packets});
149  }
150  }
151 }
152 
154 {
155  rowLimit_ = rows;
156 }
A StatisticTablePublisher produces two dimensional tables of Anys.
void setCell(const Key &key, std::size_t columnIndex, const Any &any)
void update(Type type, const uuid_t &uuid, NEMId nemId)
std::vector< std::string > StatisticTableLabels
std::uint16_t NEMId
Definition: types.h:52
void addRow(const Key &key, const std::vector< Any > &anys={})
static StatisticService * instance()
Definition: singleton.h:56
The Any class can contain an instance of one of any type in its support type set. ...
Definition: any.h:49