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 #include "../exceptions/local/ExpressionException.h"
27 #include "../services/tasks/TaskUtil.h"
28 
29 #include <atomic>
30 #include <Ice/Connection.h>
31 #include <Ice/Endpoint.h>
32 #include <IceSSL/EndpointInfo.h>
33 
34 namespace armarx::detail
35 {
36  struct ArmarXConnectionObserver : public Ice::Instrumentation::ConnectionObserver
37  {
38  ArmarXConnectionObserver(std::string protocol): protocol {std::move(protocol)} {}
39  void attach() override {}
40  void detach() override {}
41  void failed(const std::string&) override {}
42  void sentBytes(int num) override
43  {
44  sent += num;
45  }
46  void receivedBytes(int num) override
47  {
48  received += num;
49  }
50 
51  void report(ProfilerListenerPrx& profiler, const std::string& applicationName)
52  {
53  profiler->reportNetworkTraffic(
54  applicationName,
55  protocol,
56  received.exchange(0),
57  sent.exchange(0));
58  }
59  const std::string protocol;
60  std::atomic<Ice::Int> sent {0};
61  std::atomic<Ice::Int> received {0};
62  };
63 }
64 
65 namespace armarx
66 {
68  {
69  observerTCP = new detail::ArmarXConnectionObserver {"tcp"};
70  observerUDP = new detail::ArmarXConnectionObserver {"udp"};
71  observerSSL = new detail::ArmarXConnectionObserver {"ssl"};
72  observerUnknown = new detail::ArmarXConnectionObserver {"Unknown"};
73  }
74 
75  void ApplicationNetworkStats::start(ProfilerListenerPrx profiler, const std::string& applicationName)
76  {
77  ARMARX_CHECK_EXPRESSION(!applicationName.empty());
78  ARMARX_CHECK_EXPRESSION(profiler);
79  reportNetworkTrafficTask = new SimplePeriodicTask<>
80  {
81  [this, profiler, applicationName]() mutable
82  {
83  observerTCP->report(profiler, applicationName);
84  observerUDP->report(profiler, applicationName);
85  observerSSL->report(profiler, applicationName);
86  observerUnknown->report(profiler, applicationName);
87  },
88  REPORT_TIME_MS
89  };
90  reportNetworkTrafficTask->start();
91  }
92 
94  {
95  reportNetworkTrafficTask->stop();
96  }
97 
99  {
100  stopTask();
101  }
102 
103  Ice::Instrumentation::ConnectionObserverPtr ApplicationNetworkStats::getConnectionObserver(
104  const Ice::ConnectionInfoPtr& /*c*/,
105  const Ice::EndpointPtr& e,
106  Ice::Instrumentation::ConnectionState /*s*/,
107  const Ice::Instrumentation::ConnectionObserverPtr& /*previous*/)
108  {
109  const auto protocol = e->getInfo()->type();
110  switch (protocol)
111  {
112  case Ice::TCPEndpointType:
113  return observerTCP;
114  case Ice::UDPEndpointType:
115  return observerUDP;
116  case Ice::SSLEndpointType:
117  return observerSSL;
118  //TODO maybe implement more endpointypes introduced in ice3.7
119  default:
120  ARMARX_WARNING << deactivateSpam(10, to_string(protocol)) << "Unknown protocol with type " << protocol;
121  return observerUnknown;
122  }
123  }
124 
125  Ice::Instrumentation::ObserverPtr ApplicationNetworkStats::getConnectionEstablishmentObserver(const Ice::EndpointPtr&, const std::string&)
126  {
127  return {};
128  }
129 
130  Ice::Instrumentation::ObserverPtr ApplicationNetworkStats::getEndpointLookupObserver(const Ice::EndpointPtr&)
131  {
132  return {};
133  }
134 
135  Ice::Instrumentation::ThreadObserverPtr ApplicationNetworkStats::getThreadObserver(const std::string&, const std::string&, Ice::Instrumentation::ThreadState, const Ice::Instrumentation::ThreadObserverPtr&)
136  {
137  return {};
138  }
139 
140  Ice::Instrumentation::InvocationObserverPtr ApplicationNetworkStats::getInvocationObserver(const Ice::ObjectPrx&, const std::string&, const Ice::Context&)
141  {
142  return {};
143  }
144 
145  Ice::Instrumentation::DispatchObserverPtr ApplicationNetworkStats::getDispatchObserver(const Ice::Current&, Ice::Int)
146  {
147  return {};
148  }
149 
151  {
152  }
153 }
armarx::ApplicationNetworkStats::getDispatchObserver
Ice::Instrumentation::DispatchObserverPtr getDispatchObserver(const Ice::Current &, Ice::Int) override
Definition: ApplicationNetworkStats.cpp:145
armarx::detail::ArmarXConnectionObserver::sent
std::atomic< Ice::Int > sent
Definition: ApplicationNetworkStats.cpp:60
armarx::detail::ArmarXConnectionObserver::sentBytes
void sentBytes(int num) override
Definition: ApplicationNetworkStats.cpp:42
armarx::ApplicationNetworkStats::getConnectionEstablishmentObserver
Ice::Instrumentation::ObserverPtr getConnectionEstablishmentObserver(const Ice::EndpointPtr &, const std::string &) override
Definition: ApplicationNetworkStats.cpp:125
armarx::ApplicationNetworkStats::getEndpointLookupObserver
Ice::Instrumentation::ObserverPtr getEndpointLookupObserver(const Ice::EndpointPtr &) override
Definition: ApplicationNetworkStats.cpp:130
armarx::ApplicationNetworkStats::ApplicationNetworkStats
ApplicationNetworkStats()
Definition: ApplicationNetworkStats.cpp:67
armarx::ApplicationNetworkStats::setObserverUpdater
void setObserverUpdater(const Ice::Instrumentation::ObserverUpdaterPtr &upd) override
Definition: ApplicationNetworkStats.cpp:150
armarx::detail::ArmarXConnectionObserver::failed
void failed(const std::string &) override
Definition: ApplicationNetworkStats.cpp:41
armarx::detail::ArmarXConnectionObserver::report
void report(ProfilerListenerPrx &profiler, const std::string &applicationName)
Definition: ApplicationNetworkStats.cpp:51
armarx::detail::ArmarXConnectionObserver::protocol
const std::string protocol
Definition: ApplicationNetworkStats.cpp:59
IceStorm::Instrumentation::ObserverUpdaterPtr
::IceInternal::Handle< ::IceStorm::Instrumentation::ObserverUpdater > ObserverUpdaterPtr
Definition: Instrumentation.h:190
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::detail::ArmarXConnectionObserver::ArmarXConnectionObserver
ArmarXConnectionObserver(std::string protocol)
Definition: ApplicationNetworkStats.cpp:38
armarx::ApplicationNetworkStats::getInvocationObserver
Ice::Instrumentation::InvocationObserverPtr getInvocationObserver(const Ice::ObjectPrx &, const std::string &, const Ice::Context &) override
Definition: ApplicationNetworkStats.cpp:140
ApplicationNetworkStats.h
armarx::detail
Definition: ApplicationNetworkStats.cpp:34
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::detail::ArmarXConnectionObserver
Definition: ApplicationNetworkStats.cpp:36
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:40
armarx::detail::ArmarXConnectionObserver::receivedBytes
void receivedBytes(int num) override
Definition: ApplicationNetworkStats.cpp:46
armarx::ApplicationNetworkStats::getThreadObserver
Ice::Instrumentation::ThreadObserverPtr getThreadObserver(const std::string &, const std::string &, Ice::Instrumentation::ThreadState, const Ice::Instrumentation::ThreadObserverPtr &) override
Definition: ApplicationNetworkStats.cpp:135
armarx::detail::ArmarXConnectionObserver::received
std::atomic< Ice::Int > received
Definition: ApplicationNetworkStats.cpp:61
armarx::detail::ArmarXConnectionObserver::attach
void attach() override
Definition: ApplicationNetworkStats.cpp:39
armarx::ApplicationNetworkStats::start
void start(ProfilerListenerPrx profiler, const std::string &applicationName)
Definition: ApplicationNetworkStats.cpp:75
armarx::ApplicationNetworkStats::stopTask
void stopTask()
Definition: ApplicationNetworkStats.cpp:93
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
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:103
armarx::ApplicationNetworkStats::~ApplicationNetworkStats
~ApplicationNetworkStats() override
Definition: ApplicationNetworkStats.cpp:98
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::SimplePeriodicTask
Usage:
Definition: ApplicationNetworkStats.h:32