ApplicationNetworkStats.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore::core
19  * @author Manfred Kroehnert (manfred dot kroehnert at kit dot edu)
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
26 
27 #include <atomic>
28 
29 #include <Ice/Connection.h>
30 #include <Ice/Endpoint.h>
31 #include <IceSSL/EndpointInfo.h>
32 
33 #include "../exceptions/local/ExpressionException.h"
34 #include "../services/tasks/TaskUtil.h"
35 
36 namespace armarx::detail
37 {
38  struct ArmarXConnectionObserver : public Ice::Instrumentation::ConnectionObserver
39  {
41  {
42  }
43 
44  void
45  attach() override
46  {
47  }
48 
49  void
50  detach() override
51  {
52  }
53 
54  void
55  failed(const std::string&) override
56  {
57  }
58 
59  void
60  sentBytes(int num) override
61  {
62  sent += num;
63  }
64 
65  void
66  receivedBytes(int num) override
67  {
68  received += num;
69  }
70 
71  void
72  report(ProfilerListenerPrx& profiler, const std::string& applicationName)
73  {
74  profiler->reportNetworkTraffic(
75  applicationName, protocol, received.exchange(0), sent.exchange(0));
76  }
77 
78  const std::string protocol;
79  std::atomic<Ice::Int> sent{0};
80  std::atomic<Ice::Int> received{0};
81  };
82 } // namespace armarx::detail
83 
84 namespace armarx
85 {
87  {
88  observerTCP = new detail::ArmarXConnectionObserver{"tcp"};
89  observerUDP = new detail::ArmarXConnectionObserver{"udp"};
90  observerSSL = new detail::ArmarXConnectionObserver{"ssl"};
91  observerUnknown = new detail::ArmarXConnectionObserver{"Unknown"};
92  }
93 
94  void
95  ApplicationNetworkStats::start(ProfilerListenerPrx profiler, const std::string& applicationName)
96  {
97  ARMARX_CHECK_EXPRESSION(!applicationName.empty());
98  ARMARX_CHECK_EXPRESSION(profiler);
99  reportNetworkTrafficTask =
100  new SimplePeriodicTask<>{[this, profiler, applicationName]() mutable
101  {
102  observerTCP->report(profiler, applicationName);
103  observerUDP->report(profiler, applicationName);
104  observerSSL->report(profiler, applicationName);
105  observerUnknown->report(profiler, applicationName);
106  },
107  REPORT_TIME_MS};
108  reportNetworkTrafficTask->start();
109  }
110 
111  void
113  {
114  reportNetworkTrafficTask->stop();
115  }
116 
118  {
119  stopTask();
120  }
121 
122  Ice::Instrumentation::ConnectionObserverPtr
124  const Ice::ConnectionInfoPtr& /*c*/,
125  const Ice::EndpointPtr& e,
126  Ice::Instrumentation::ConnectionState /*s*/,
127  const Ice::Instrumentation::ConnectionObserverPtr& /*previous*/)
128  {
129  const auto protocol = e->getInfo()->type();
130  switch (protocol)
131  {
132  case Ice::TCPEndpointType:
133  return observerTCP;
134  case Ice::UDPEndpointType:
135  return observerUDP;
136  case Ice::SSLEndpointType:
137  return observerSSL;
138  //TODO maybe implement more endpointypes introduced in ice3.7
139  default:
140  ARMARX_WARNING << deactivateSpam(10, to_string(protocol))
141  << "Unknown protocol with type " << protocol;
142  return observerUnknown;
143  }
144  }
145 
146  Ice::Instrumentation::ObserverPtr
148  const std::string&)
149  {
150  return {};
151  }
152 
153  Ice::Instrumentation::ObserverPtr
155  {
156  return {};
157  }
158 
159  Ice::Instrumentation::ThreadObserverPtr
161  const std::string&,
162  Ice::Instrumentation::ThreadState,
163  const Ice::Instrumentation::ThreadObserverPtr&)
164  {
165  return {};
166  }
167 
168  Ice::Instrumentation::InvocationObserverPtr
170  const std::string&,
171  const Ice::Context&)
172  {
173  return {};
174  }
175 
176  Ice::Instrumentation::DispatchObserverPtr
178  {
179  return {};
180  }
181 
182  void
184  {
185  }
186 } // namespace armarx
armarx::ApplicationNetworkStats::getDispatchObserver
Ice::Instrumentation::DispatchObserverPtr getDispatchObserver(const Ice::Current &, Ice::Int) override
Definition: ApplicationNetworkStats.cpp:177
armarx::detail::ArmarXConnectionObserver::sent
std::atomic< Ice::Int > sent
Definition: ApplicationNetworkStats.cpp:79
IceStorm::Instrumentation::ObserverUpdaterPtr
::IceInternal::Handle<::IceStorm::Instrumentation::ObserverUpdater > ObserverUpdaterPtr
Definition: Instrumentation.h:192
armarx::detail::ArmarXConnectionObserver::sentBytes
void sentBytes(int num) override
Definition: ApplicationNetworkStats.cpp:60
armarx::ApplicationNetworkStats::getConnectionEstablishmentObserver
Ice::Instrumentation::ObserverPtr getConnectionEstablishmentObserver(const Ice::EndpointPtr &, const std::string &) override
Definition: ApplicationNetworkStats.cpp:147
armarx::ApplicationNetworkStats::getEndpointLookupObserver
Ice::Instrumentation::ObserverPtr getEndpointLookupObserver(const Ice::EndpointPtr &) override
Definition: ApplicationNetworkStats.cpp:154
armarx::ApplicationNetworkStats::ApplicationNetworkStats
ApplicationNetworkStats()
Definition: ApplicationNetworkStats.cpp:86
armarx::ApplicationNetworkStats::setObserverUpdater
void setObserverUpdater(const Ice::Instrumentation::ObserverUpdaterPtr &upd) override
Definition: ApplicationNetworkStats.cpp:183
armarx::detail::ArmarXConnectionObserver::failed
void failed(const std::string &) override
Definition: ApplicationNetworkStats.cpp:55
armarx::detail::ArmarXConnectionObserver::report
void report(ProfilerListenerPrx &profiler, const std::string &applicationName)
Definition: ApplicationNetworkStats.cpp:72
armarx::detail::ArmarXConnectionObserver::protocol
const std::string protocol
Definition: ApplicationNetworkStats.cpp:78
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
armarx::detail::ArmarXConnectionObserver::ArmarXConnectionObserver
ArmarXConnectionObserver(std::string protocol)
Definition: ApplicationNetworkStats.cpp:40
armarx::ApplicationNetworkStats::getInvocationObserver
Ice::Instrumentation::InvocationObserverPtr getInvocationObserver(const Ice::ObjectPrx &, const std::string &, const Ice::Context &) override
Definition: ApplicationNetworkStats.cpp:169
ApplicationNetworkStats.h
armarx::detail
Definition: ApplicationNetworkStats.cpp:36
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::detail::ArmarXConnectionObserver
Definition: ApplicationNetworkStats.cpp:38
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::detail::ArmarXConnectionObserver::detach
void detach() override
Definition: ApplicationNetworkStats.cpp:50
armarx::detail::ArmarXConnectionObserver::receivedBytes
void receivedBytes(int num) override
Definition: ApplicationNetworkStats.cpp:66
armarx::ApplicationNetworkStats::getThreadObserver
Ice::Instrumentation::ThreadObserverPtr getThreadObserver(const std::string &, const std::string &, Ice::Instrumentation::ThreadState, const Ice::Instrumentation::ThreadObserverPtr &) override
Definition: ApplicationNetworkStats.cpp:160
armarx::detail::ArmarXConnectionObserver::received
std::atomic< Ice::Int > received
Definition: ApplicationNetworkStats.cpp:80
armarx::detail::ArmarXConnectionObserver::attach
void attach() override
Definition: ApplicationNetworkStats.cpp:45
armarx::ApplicationNetworkStats::start
void start(ProfilerListenerPrx profiler, const std::string &applicationName)
Definition: ApplicationNetworkStats.cpp:95
armarx::ApplicationNetworkStats::stopTask
void stopTask()
Definition: ApplicationNetworkStats.cpp:112
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:917
armarx::ApplicationNetworkStats::getConnectionObserver
Ice::Instrumentation::ConnectionObserverPtr getConnectionObserver(const Ice::ConnectionInfoPtr &c, const Ice::EndpointPtr &e, Ice::Instrumentation::ConnectionState s, const Ice::Instrumentation::ConnectionObserverPtr &previous) override
Definition: ApplicationNetworkStats.cpp:123
armarx::ApplicationNetworkStats::~ApplicationNetworkStats
~ApplicationNetworkStats() override
Definition: ApplicationNetworkStats.cpp:117
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::SimplePeriodicTask
Usage:
Definition: ApplicationNetworkStats.h:32