YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Ping.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <yarp/os/Ping.h>
8
9#include <yarp/os/Bottle.h>
12#include <yarp/os/Network.h>
13#include <yarp/os/Port.h>
14#include <yarp/os/Time.h>
15#include <yarp/os/Vocab.h>
16
17#include <cmath>
18#include <cstdio>
19#include <mutex>
20
21using namespace yarp::os;
22
23namespace {
24YARP_OS_LOG_COMPONENT(PING, "yarp.os.Ping")
25}
26
28{
29 clear();
30}
31
33{
34 tot = tot2 = 0;
35 ct = at = 0;
36 mu = 0;
37 sigma = 1e10;
38 // infinity would be better, but methods of getting infinity
39 // require awkward dependencies
40}
41
42void Stat::add(double val)
43{
44 tot += val;
45 tot2 += val * val;
46 ct++;
47}
48
49void Stat::add(const Stat& alt)
50{
51 tot += alt.tot;
52 tot2 += alt.tot2;
53 ct += alt.ct;
54}
55
56double Stat::mean()
57{
58 compute();
59 return mu;
60}
61
63{
64 compute();
65 return sigma;
66}
67
69{
70 return ct;
71}
72
73Stat::operator double()
74{
75 return mean();
76}
77
78void Stat::compute()
79{
80 if (ct != at) {
81 // ct must be > 0
82 mu = tot / ct;
83 sigma = tot2 / ct - mu * mu;
84 if (sigma < 0) {
85 sigma = 0; // round-off error
86 }
87 sigma = sqrt(sigma);
88 at = ct;
89 }
90}
91
92
98
100{
101 totalTime.add(alt.totalTime);
102 targetTime.add(alt.targetTime);
103}
104
105
107{
108 period.clear();
109}
110
112{
113 period.add(alt.period);
114}
115
116
117Ping::Ping(const char* target)
118{
119 if (target != nullptr) {
120 setTarget(target);
121 }
122}
123
124bool Ping::setTarget(const char* target)
125{
126 this->target = target;
127 return true;
128}
129
130
132{
133 lastConnect.clear();
134 double start = SystemClock::nowSystem();
137 if (!c.isValid()) {
138 yCError(PING, "Port not found during ping");
139 }
141 rpc.admin = true;
142 rpc.quiet = true;
144 bool ok = client.getAdminProtocolVersion().ret;
145 if (!ok) {
146 yCError(PING, "Port did not respond as expected");
147 }
148 double stop = SystemClock::nowSystem();
149 lastConnect.totalTime.add(stop - start);
150 lastConnect.targetTime.add(stop - afterQuery);
151 accumConnect.add(lastConnect);
152}
153
155{
156 long int ping = lround(accumConnect.targetTime.count() + 0.5);
157 if (ping > 0) {
158 yCInfo(PING, "Ping #%ld:\n", lround(accumConnect.targetTime.count() + 0.5));
159 int space = 14;
160 int decimal = 5;
161 yCInfo(PING,
162 " %s connection time (%s with name lookup)\n",
163 renderTime(lastConnect.targetTime.mean(), space, decimal).c_str(),
164 renderTime(lastConnect.totalTime.mean(), space, decimal).c_str());
165 if (accumConnect.totalTime.count() > 1) {
166 yCInfo(PING,
167 " %s +/- %s on average (%s +/- %s with name lookup)\n",
168 renderTime(accumConnect.targetTime.mean(), space, decimal).c_str(),
169 renderTime(accumConnect.targetTime.deviation(), space, decimal).c_str(),
170 renderTime(accumConnect.totalTime.mean(), space, decimal).c_str(),
171 renderTime(accumConnect.totalTime.deviation(), space, decimal).c_str());
172 }
173 }
174}
175
176
177std::string Ping::renderTime(double t, int space, int decimal)
178{
179 std::string unit;
180 double times = 1;
181 if (space < 0) {
182 yCError(PING, "Negative space");
183 }
184 if (t >= 1) {
185 unit = "sec";
186 } else if (t > 1e-3) {
187 unit = " ms";
188 times = 1e3;
189 } else if (t > 1e-6) {
190 unit = " us";
191 times = 1e6;
192 } else if (t > 1e-9) {
193 unit = " ns";
194 times = 1e9;
195 }
196 char buf[512];
197 std::snprintf(buf, sizeof(buf), "%.*f%s", decimal, t * times, unit.c_str());
198 return buf;
199}
200
201
203{
204public:
205 std::mutex mutex;
206 int ct{0};
207 double lastTime{0};
209
211 mutex()
212 {
213 }
214
215 bool read(ConnectionReader& connection) override
216 {
217 double now = SystemClock::nowSystem();
218 Bottle b;
219 bool ok = b.read(connection);
220 if (ok) {
221 mutex.lock();
222 ct++;
223 if (ct > 1) {
224 double dt = now - lastTime;
225 period.add(dt);
226 }
227 lastTime = now;
228 yCInfo(PING,
229 "Period is %g +/- %g (%d)\n",
230 period.mean(),
231 period.deviation(),
232 ct);
233 mutex.unlock();
234 }
235 return ok;
236 }
237};
238
240{
241 Port p;
244 p.open("...");
245 yCInfo(PING, "Pausing for 5 seconds...\n");
246 NetworkBase::connect(target, p.getName());
248 p.close();
249 yCInfo(PING,
250 "Period is %g +/- %g (%d)\n",
251 sampler.period.mean(),
252 sampler.period.deviation(),
253 sampler.ct);
254}
255
257{
258 lastConnect.clear();
259 accumConnect.clear();
260}
261
263{
264 return lastConnect;
265}
266
268{
269 return accumConnect;
270}
PingSampler()
Definition Ping.cpp:210
std::mutex mutex
Definition Ping.cpp:205
Stat period
Definition Ping.cpp:208
bool read(ConnectionReader &connection) override
Read this object from a network connection.
Definition Ping.cpp:215
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition Bottle.cpp:241
A mini-server for performing network communication in the background.
void add(const ConnectResult &alt)
Definition Ping.cpp:99
An interface for reading from a network connection.
Preferences for how to communicate with a contact.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
virtual std::string getName() const
Get name of port.
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition Network.cpp:784
static Contact queryName(const std::string &name)
Find out information about a registered name.
Definition Network.cpp:1083
void sample()
Definition Ping.cpp:239
bool setTarget(const char *target)
Definition Ping.cpp:124
Ping(const char *target=nullptr)
Definition Ping.cpp:117
void report()
Definition Ping.cpp:154
void clear()
Definition Ping.cpp:256
static std::string renderTime(double t, int space, int decimal)
Definition Ping.cpp:177
ConnectResult getAverageConnect()
Definition Ping.cpp:267
ConnectResult getLastConnect()
Definition Ping.cpp:262
void connect()
Definition Ping.cpp:131
virtual return_getAdminProtocolVersion getAdminProtocolVersion()
Report the version of the administrative protocol of this port (it is distinct from the version of th...
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition PortReader.h:24
A mini-server for network communication.
Definition Port.h:46
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition Port.cpp:470
void close() override
Stop port activity.
Definition Port.cpp:330
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
void add(const RateResult &alt)
Definition Ping.cpp:111
void add(double val)
Definition Ping.cpp:42
void clear()
Definition Ping.cpp:32
double mean()
Definition Ping.cpp:56
double count()
Definition Ping.cpp:68
double deviation()
Definition Ping.cpp:62
static double nowSystem()
static void delaySystem(double seconds)
Client side of the administrative commands defined in PortCoreMsgs.thrift and handled by PortCore::ad...
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
An interface to the operating system, including Port based communication.