YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
NetworkProfilerBasic.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
8#include <yarp/os/Network.h>
9#include <yarp/os/LogStream.h>
11#include <yarp/os/Port.h>
13#include <yarp/os/Carrier.h>
15#include <yarp/companion/impl/Companion.h>
16#include <algorithm>
17
21
22using namespace yarp::os;
23using namespace yarp::profiler;
24
26{
27 l.clear(); //clear the output
28
29 for (auto it = ports.begin(); it != ports.end(); it++)
30 {
31 std::string ip = it->owner_process.owner_machine.ip;
32 if (std::find(l.begin(), l.end(), ip) == l.end())
33 {
34 l.push_back(ip);
35 }
36 }
37 return true;
38}
39
41{
42 l.clear(); //clear the output
43
44 for (auto it = ports.begin(); it != ports.end(); it++)
45 {
46 //int pid = it->owner.pid;
47 std::string pid = it->owner_process.process_fullname;
48 if (std::find(l.begin(), l.end(), pid) == l.end())
49 {
50 l.push_back(pid);
51 }
52 }
53 return true;
54}
55
57{
58 ports.clear(); //clear the output
59
61 bool r = getPortsList(port_names, complete);
62 if (!r) return false;
63
65 for (auto it = port_names.begin(); it != port_names.end(); it++)
66 {
68 r = getPortDetails(it->name, details, timeout);
69 if (!r) { yError() << "getPortDetails of `" << it->name << "`failed"; }
70 ports.push_back(details);
71 }
72 return r;
73}
74
76{
77 filtered_out.clear(); //clear the output
78
79 for (auto it = in.begin(); it != in.end(); it++)
80 {
81 if (ip != "*") {
82 if (it->owner_process.owner_machine.hostname == ip) { filtered_out.push_back(*it); }
83 }
84 }
85}
86
88{
89 filtered_out.clear(); //clear the output
90
91 for (auto it = in.begin(); it != in.end(); it++)
92 {
93 if (process_fullname != "*") {
94 if (it->owner_process.process_fullname == process_fullname) { filtered_out.push_back(*it); }
95 }
96 }
97}
98
100{
101 filtered_out.clear(); //clear the output
102
103 for (auto it = in.begin(); it != in.end(); it++)
104 {
105 if (src_name!="*" && dst_name != "*") {
106 if (it->src.name == src_name && it->src.name == dst_name) { filtered_out.push_back(*it);}
107 }
108 else if (src_name == "*") {
109 if (it->dst.name == dst_name) { filtered_out.push_back(*it); }
110 }
111 else if (dst_name == "*") {
112 if (it->src.name == src_name) {filtered_out.push_back(*it); }
113 }
114 }
115}
116
118{
119 filtered_out.clear(); //clear the output
120
121 for (auto it = in.begin(); it != in.end(); it++)
122 {
123 if (src_portnumber != "*" && dst_portnumber != "*") {
124 if (it->src.ip == src_portnumber && it->src.ip == dst_portnumber) { filtered_out.push_back(*it); }
125 }
126 else if (src_portnumber == "*") {
127 if (it->dst.ip == dst_portnumber) { filtered_out.push_back(*it); }
128 }
129 else if (dst_portnumber == "*") {
130 if (it->src.ip == src_portnumber) { filtered_out.push_back(*it); }
131 }
132 }
133}
134
136{
137 filtered_out.clear(); //clear the output
138
139 for (auto it = in.begin(); it != in.end(); it++)
140 {
141 if (src_ip != "*" && dst_ip != "*") {
142 if (it->src.port_number == src_ip && it->src.port_number == dst_ip) { filtered_out.push_back(*it); }
143 }
144 else if (src_ip == "*") {
145 if (it->dst.port_number == dst_ip) { filtered_out.push_back(*it); }
146 }
147 else if (dst_ip == "*") {
148 if (it->src.port_number == src_ip) { filtered_out.push_back(*it); }
149 }
150 }
151}
152
153bool NetworkProfilerBasic::getPortInfo (const std::string& name, const ports_name_set& ports, PortInfo& p)
154{
155 p = PortInfo(); // clear the output
156
157 for (auto it = ports.begin(); it != ports.end(); it++)
158 {
159 if (name == it->name)
160 {
161 p.name = it->name;
162 p.ip = it->ip;
163 p.port_number = it->port_number;
164 return true;
165 }
166 }
167 return false;
168}
169
171{
172 connections.clear(); // clear the output
173
174 //get the list of all the ports
177
178 //for each port, get the list of its connections...
179 for (auto it = ports.begin(); it != ports.end(); it++)
180 {
182 getPortDetails(it->name, info);
184 /*
185 //I will not process incoming connections since probably everything in the system
186 //is determined just looking at the outgoing connections
187 for (auto it2 = info.inputs.begin(); it2 != info.inputs.end(); it2++)
188 {
189 PortInfo p; getPortInfo(it2->name, ports, p);
190 conn.src.name = it2->name;
191 conn.src.ip = p.ip;
192 conn.src.port_number = p.port_number;
193 conn.dst.name = it->name;
194 conn.dst.ip = it->ip;
195 conn.dst.port_number = it->port_number;
196 conn.carrier = it2->carrier;
197 }*/
198 //process all the outgoing connections and put them into a vector
199 for (auto it2 = info.outputs.begin(); it2 != info.outputs.end(); it2++)
200 {
201 PortInfo p; getPortInfo(it2->port_name, ports, p);
202 conn.src.name = it->name;
203 conn.src.ip = it->ip;
204 conn.src.port_number= it->port_number;
205 conn.dst.name = it2->port_name;
206 conn.dst.ip = p.ip;
207 conn.dst.port_number = p.port_number;
208 conn.carrier = it2->carrier;
209
210 //add the connection to the list if it is valid and it is unique
211 //(no other connections with same src/dst/protocol)
212 if (conn.isValid() &&
213 std::find(connections.begin(), connections.end(), conn) == connections.end())
214 {
215 connections.push_back(conn);
216 }
217 }
218 }
219 return true;
220}
221
223{
224 ports.clear(); //clear the output
225
226 ContactStyle style;
227 style.quiet = true;
228 style.timeout = 3.0;
230 Bottle msg, reply;
231 msg.addString("bot");
232 msg.addString("list");
233 if(!NetworkBase::write(Contact(nameserver), msg, reply, style)) {
234 yError() << "Cannot write to yarp name server";
235 return false;
236 }
237
238 if(reply.size() == 0) {
239 yError() << "Empty reply from yarp name server";
240 return false;
241 }
242
243 for (size_t i=1; i<reply.size(); i++) {
244 Bottle *entry = reply.get(i).asList();
245 if(entry != nullptr) {
246 bool shouldTake = false;
247 std::string portname = entry->check("name", Value("")).asString();
248 if(complete)
249 {
250 shouldTake = portname != "";
251 }
252 else
253 {
254 shouldTake = portname != "" && portname != "fallback" && portname != nameserver;
255 }
256 if (shouldTake) {
258 if (c.getCarrier() != "mcast")
259 {
261 portd.name = entry->find("name").asString();
262 portd.ip = entry->find("ip").asString();
263 portd.port_number = std::to_string(entry->find("port_number").asInt32());
264 ports.push_back(portd);
265 }
266 }
267 }
268 }
269
270 return true;
271}
272
273bool NetworkProfilerBasic::getPortDetails(const std::string& portName, PortDetails& details, double timeout)
274 {
275 details = PortDetails(); //clear the output
276
277 details.info.name = portName;
278 Port ping;
279 ping.open("...");
280 ping.setAdminMode(true);
281 ping.setTimeout(timeout);
282 if(!NetworkBase::connect(ping.getName(), portName)) {
283 yWarning()<<"Cannot connect to"<<portName;
284 ping.close();
285 return false;
286 }
287
289
290 // Getting output connections list
292 if (!outputs.ret) {
293 yError()<<"Cannot get the output connections of"<<portName;
294 ping.close();
295 return false;
296 }
297 for (const auto& name : outputs.connections) {
299 cnn.port_name = name;
301 if (!info.ret) {
302 yWarning()<<"Cannot get the details of the connection from"<<portName<<"to"<<name;
303 } else {
304 cnn.carrier = info.carrier;
305 }
306 details.outputs.push_back(cnn);
307 }
308
309 // Getting input connections list
311 if (!inputs.ret) {
312 yError()<<"Cannot get the input connections of"<<portName;
313 ping.close();
314 return false;
315 }
316 for (const auto& name : inputs.connections) {
317 if (name != ping.getName()) {
319 cnn.port_name = name;
320 details.inputs.push_back(cnn);
321 }
322 }
323
324 // Getting owner info
326 if (!info.ret) {
327 yError()<<"Cannot get the info of"<<portName;
328 ping.close();
329 return false;
330 }
331
332 details.owner_process.process_name = info.process.name;
333 details.owner_process.arguments = info.process.arguments;
334 details.owner_process.pid = info.process.pid;
335 details.owner_process.priority = info.process.schedPriority;
336 details.owner_process.policy = info.process.schedPolicy;
337 details.owner_process.process_fullname = details.owner_process.process_name + "(" + std::to_string(details.owner_process.pid) + ")";
338
339 details.owner_process.owner_machine.os = info.platform.name;
340
341 details.owner_process.owner_machine.hostname = info.port.hostname;
342 details.owner_process.owner_machine.ip = info.port.hostname;
343
344 ping.close();
345 return true;
346}
347
349
351}
352
354{
355 std::ostringstream str;
356 str << "port name: " << info.name << std::endl;
357 str << "port ip: " << info.ip << std::endl;
358 str << "port port number: " << info.port_number << std::endl;
359 str << "outputs:" << std::endl;
360 std::vector<ConnectedPortInfo>::const_iterator itr;
361 for (itr = outputs.begin(); itr != outputs.end(); itr++) {
362 str << " + " << (*itr).port_name << " (" << (*itr).carrier << ")" << std::endl;
363 }
364 str << "inputs:" << std::endl;
365 for (itr = inputs.begin(); itr != inputs.end(); itr++) {
366 str << " + " << (*itr).port_name << " (" << (*itr).carrier << ")" << std::endl;
367 }
368 str << "owner:" << std::endl;
369 str << " + name: " << owner_process.process_name << std::endl;
370 str << " + arguments: " << owner_process.arguments << std::endl;
371 str << " + hostname: " << owner_process.owner_machine.hostname << std::endl;
372 str << " + priority: " << owner_process.priority << std::endl;
373 str << " + policy: " << owner_process.policy << std::endl;
374 str << " + os: " << owner_process.owner_machine.os << std::endl;
375 str << " + pid: " << owner_process.pid << std::endl;
376 return str.str();
377}
#define yError(...)
Definition Log.h:361
#define yWarning(...)
Definition Log.h:340
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:252
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:247
A mini-server for performing network communication in the background.
std::string getName() const override
Get name of port.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Preferences for how to communicate with a contact.
double timeout
Set a timeout for communication (in units of seconds, fractional seconds allowed).
bool quiet
Suppress all outputs and warnings.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
static Contact fromConfig(const Searchable &config)
Factory method.
Definition Contact.cpp:129
static std::string getNameServerName()
Get the name of the port associated with the nameserver (usually "/root", but this can be overwritten...
Definition Network.cpp:1448
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 bool write(const Contact &contact, PortWriter &cmd, PortReader &reply, bool admin=false, bool quiet=false, double timeout=-1)
Send a single command to a port and await a single response.
Definition Network.cpp:1322
static bool cleanUnresponsivePorts(double timeout=-1)
Clean up unresponsive ports from the name server.
Definition Network.cpp:1200
virtual return_getPortInfo getPortInfo()
Give information about this port, its main thread, and the process and the platform on which it is ru...
virtual return_getConnectionInfo getConnectionInfoOut(const std::string &portname)
Give details about the output connection going from this port to another port.
virtual return_listConnections listConnectionsOut()
List the ports connected to the output of this port.
virtual return_listConnections listConnectionsIn()
List the ports connected to the input of this port.
A mini-server for network communication.
Definition Port.h:46
A single value (typically within a Bottle).
Definition Value.h:44
virtual Bottle * asList() const
Get list value.
Definition Value.cpp:246
Client side of the administrative commands defined in PortCoreMsgs.thrift and handled by PortCore::ad...
Reply of PortCoreMsgs::getConnectionInfoIn() and PortCoreMsgs::getConnectionInfoOut().
Reply of PortCoreMsgs::getPortInfo().
Reply of PortCoreMsgs::listConnectionsIn() and PortCoreMsgs::listConnectionsOut().
std::vector< std::string > connections
std::vector< ConnectionDetails > connections_set
static bool getProcessesList(const ports_detail_set &ports, processes_list &l)
static void filterConnectionListByName(const connections_set &in, connections_set &filtered_out, std::string src_name="*", std::string dst_name="*")
static bool getPortsDetailedList(ports_detail_set &ports, bool complete=false, double timeout=2.0)
static bool getPortsList(ports_name_set &ports, bool complete=false)
static void filterConnectionListByPortNumber(const connections_set &in, connections_set &filtered_out, std::string src_name="*", std::string dst_name="*")
static void filterPortsListByProcess(const ports_detail_set &in, ports_detail_set &filtered_out, std::string fullprocess="*")
std::vector< PortDetails > ports_detail_set
std::vector< std::string > processes_list
static bool getPortDetails(const std::string &portName, PortDetails &info, double timeout=2.0)
Get the details (connections, owner process and machine) of a port.
static bool yarpClean(float timeout=0.1)
static void filterConnectionListByIp(const connections_set &in, connections_set &filtered_out, std::string src_name="*", std::string dst_name="*")
static bool getConnectionsList(connections_set &connections)
static bool getMachinesList(const ports_detail_set &ports, machines_list &l)
static void filterPortsListByIp(const ports_detail_set &in, ports_detail_set &filtered_out, std::string ip="*")
static bool getPortInfo(const std::string &name, const ports_name_set &ports, PortInfo &p)
std::vector< std::string > machines_list
An interface to the operating system, including Port based communication.