YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
main.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#include <yarp/os/Network.h>
8#include <yarp/os/LogStream.h>
9#include<fstream>
10
11using namespace std;
12
13namespace
14{
15 YARP_LOG_COMPONENT(YARPCONNINFO, "yarp.yarpconnectionsinfo")
16}
17
19{
20 yCInfo(YARPCONNINFO) << "This is the tool yarpconnectionsinfo.";
21 yCInfo(YARPCONNINFO) << "By default it prints on screen all the connections found on the yarp network.";
22 yCInfo(YARPCONNINFO) << "use the --to_dotfile <filename> to write the list of connections to a file in the dot format.";
23 yCInfo(YARPCONNINFO) << "use an online dot editor (e.g. https://dreampuf.github.io/GraphvizOnline) to visualize the graph.";
24 yCInfo(YARPCONNINFO) << " ";
25 yCInfo(YARPCONNINFO) << "Filter options (filters are added using AND logic):";
26 yCInfo(YARPCONNINFO) << "filters on the ip (default value: *):";
27 yCInfo(YARPCONNINFO) << "--from_ip <ip>";
28 yCInfo(YARPCONNINFO) << "--to_ip <ip>";
29 yCInfo(YARPCONNINFO) << "filters on the portnumbers (default value: *):";
30 yCInfo(YARPCONNINFO) << "--from_portnumber <ip>";
31 yCInfo(YARPCONNINFO) << "--to_portnumber <ip>";
32 yCInfo(YARPCONNINFO) << "filters on the port names (default value: *):";
33 yCInfo(YARPCONNINFO) << "--from_portname <name>";
34 yCInfo(YARPCONNINFO) << "--to_portname name>";
35 yCInfo(YARPCONNINFO) << " ";
36 yCInfo(YARPCONNINFO) << "Additional options (default value: false):";
37 yCInfo(YARPCONNINFO) << "--display_unconnnected_ports";
38 yCInfo(YARPCONNINFO) << "--display_log_ports";
39 yCInfo(YARPCONNINFO) << "--display_stats_ports";
40 yCInfo(YARPCONNINFO) << "--display_clock_ports";
41 yCInfo(YARPCONNINFO) << "--display_yarprun_processes";
42}
43
44int main(int argc, char *argv[])
45{
47
49 if (argc > 1) {
50 p.fromCommand(argc, argv);
51 }
52
53 std::vector <string> color_map;
54 color_map.push_back ("cadetblue1"); //0
55 color_map.push_back ("antiquewhite1");
56 color_map.push_back ("darkolivegreen2");
57 color_map.push_back ("gold2");
58 color_map.push_back ("darkorange2");
59 color_map.push_back ("cadetblue4");
60 color_map.push_back ("coral2");
61 color_map.push_back ("firebrick3");
62 color_map.push_back ("forestgreen");
63 color_map.push_back ("cornflowerblue");
64
65 std::string from_ip = p.check("from_ip",yarp::os::Value(std::string("*")), "").asString();
66 std::string to_ip = p.check("to_ip",yarp::os::Value(std::string("*")), "").asString();
67 std::string from_portnumber = p.check("from_portnumber",yarp::os::Value(std::string("*")), "").asString();
68 std::string to_portnumber = p.check("to_portnumber",yarp::os::Value(std::string("*")), "").asString();
69 std::string from_portname = p.check("from_portname",yarp::os::Value(std::string("*")), "").asString();
70 std::string to_portname = p.check("to_portname",yarp::os::Value(std::string("*")), "").asString();
71 std::string to_dotfile = p.check("to_dotfile", yarp::os::Value(std::string("")), "").asString();
72 bool display_yarprun_processes = false;
73 bool display_log_ports = false;
74 bool display_stats_ports = false;
75 bool display_clock_ports = false;
76 bool display_unconnected_ports = false;
77
78 if (p.check("help")) {display_help(); return 1; }
79 if (p.check("display_yarprun_processes")) { display_yarprun_processes = true;}
80 if (p.check("display_log_ports")) { display_log_ports = true; }
81 if (p.check("display_stats_ports")) { display_stats_ports = true; }
82 if (p.check("display_clock_ports")) { display_clock_ports = true; }
83 if (p.check("display_unconnnected_ports")) { display_unconnected_ports = true; }
84
90
91 prof.getPortsList(ports_list);
92 prof.getConnectionsList(conns_orig);
93 conns=conns_orig;
94
95 if (from_ip != "*" || to_ip != "*")
96 {
97 prof.filterConnectionListByIp(conns, conns_filtered, from_ip,to_ip);
98 conns= conns_filtered;
99 }
100 if (from_portnumber != "*" || to_portnumber != "*")
101 {
102 prof.filterConnectionListByPortNumber(conns, conns_filtered, from_portnumber, to_portnumber);
103 conns = conns_filtered;
104 }
105 if (from_portname != "*" || to_portname != "*")
106 {
107 prof.filterConnectionListByName(conns, conns_filtered, from_portname, to_portname);
108 conns = conns_filtered;
109 }
110
111 std::string sss = "Connections:\n";
112 if (conns.size() !=0)
113 for (auto it = conns.begin(); it!=conns.end(); it++)
114 {
115 sss += it->src.name + " (" + it->src.ip + ":" + it->src.port_number + ") -> " +
116 it->dst.name + " (" + it->dst.ip + ":" + it->dst.port_number + ") with carrier: (" +
117 it->carrier + ")\n";
118 }
119 else
120 {
121 sss +="Empty list";
122 }
123
124 yInfo() << sss;
125
126 if (to_dotfile != "")
127 {
128 //saveToFile
129 std::ofstream file;
130 file.open(to_dotfile.c_str(), std::ios::out);
131 file << "digraph G{" <<endl;
132 file << "splines=\"compound\"" << endl;
133
134 yarp::profiler::NetworkProfiler::ports_detail_set detailed_ports_filtered_by_ip;
135 yarp::profiler::NetworkProfiler::ports_detail_set detailed_ports_filtered_by_ip_and_process;
137 prof.getPortsDetailedList(detailed_ports);
139 prof.getMachinesList(detailed_ports, ml);
141 prof.getProcessesList(detailed_ports, pl);
142
143 file << "subgraph cluster_net{" << endl;
144 size_t machine_counter = 0;
145 size_t color_map_index = 0;
146 for (auto machines_ip_it = ml.begin(); machines_ip_it != ml.end(); machines_ip_it++, machine_counter++)
147 {
148 prof.filterPortsListByIp(detailed_ports, detailed_ports_filtered_by_ip, *machines_ip_it);
149 file << "subgraph cluster_machine"<<machine_counter <<" {" << endl;
150
151 size_t process_counter = 0;
152 for (auto process_name_it = pl.begin(); process_name_it != pl.end(); process_name_it++, process_counter++)
153 {
154 //this option skips all yarprun processes
155 if (display_yarprun_processes == false)
156 {
157 std::size_t found = process_name_it->find("yarprun");
158 if (found != std::string::npos) { process_counter--; continue;}
159 }
160
161 file << "subgraph cluster_process" << process_counter<<" {" << endl;
162 prof.filterPortsListByProcess(detailed_ports_filtered_by_ip, detailed_ports_filtered_by_ip_and_process, *process_name_it);
163
164 for (auto ports_it = detailed_ports_filtered_by_ip_and_process.begin(); ports_it != detailed_ports_filtered_by_ip_and_process.end(); ports_it++)
165 {
166 //this option skips all logger ports
167 if (display_log_ports == false)
168 {
169 std::size_t found = ports_it->info.name.find("/log");
170 if (found != std::string::npos) { continue; }
171 }
172
173 //this option skips all stats ports (opened by `statsMonitor` portmonitor)
174 if (display_stats_ports == false)
175 {
176 std::size_t found = ports_it->info.name.find("/stats:o");
177 if (found != std::string::npos) { continue; }
178 }
179
180 //this option skips all clock ports
181 if (display_clock_ports == false)
182 {
183 std::size_t found = ports_it->info.name.find("/clock:i");
184 if (found != std::string::npos) { continue; }
185 }
186
187 //this option skips all unconnected ports
188 if (display_unconnected_ports == false)
189 {
190 if (ports_it->inputs.size() == 0 &&
191 ports_it->outputs.size() == 0)
192 {
193 continue;
194 }
195 }
196
197 file << "\"" << ports_it->info.name << "\";" << endl;
198 }
199 file << "label = \""<< *process_name_it << "\";" << endl;
200 file << "}" << endl;
201 }
202 file << "label = \"" << *machines_ip_it << "\";" << endl;
203 file << "style = filled" << endl;
204 file << "color = " << color_map[color_map_index++] << endl;
205 if (color_map_index >=10) color_map_index=0;
206 file << "}" << endl;
207 }
208 file << "}" << endl;
209
210 for (auto connection_it = conns.begin(); connection_it != conns.end(); connection_it++)
211 {
212 //this option skips all logger ports
213 if (display_log_ports == false)
214 {
215 std::size_t founds = connection_it->src.name.find("/log");
216 std::size_t foundd = connection_it->dst.name.find("/log");
217 if (founds != std::string::npos ||
218 foundd != std::string::npos) { continue; }
219 }
220
221 //this option skips all clock ports
222 if (display_clock_ports == false)
223 {
224 std::size_t founds = connection_it->src.name.find("/clock:i");
225 std::size_t foundd = connection_it->dst.name.find("/clock:i");
226 if (founds != std::string::npos ||
227 foundd != std::string::npos) {
228 continue;
229 }
230 }
231
232 std::string protocol_name = connection_it->carrier; //"unknown"
233 std::string color_connection = "black";
234 if (protocol_name == "tcp") { color_connection = "blue";}
235 else if (protocol_name == "udp") { color_connection = "red"; }
236 else if (protocol_name == "fast_tcp") { color_connection = "green"; }
237 else if (protocol_name == "mjpeg") { color_connection = "darkorange2"; }
238 else if (protocol_name == "tcpros") { color_connection = "darkgray"; }
239
240 file << "\"" << connection_it->src.name << "\"" << " -> "
241 << "\"" << connection_it->dst.name << "\"" << " [label = \""<< protocol_name << "\" color = "<< color_connection <<"]" << endl;
242 }
243
244 file << "}" << endl;
245 file.close();
246 }
247
248 return 1;
249}
250
251/********************
252* dot file example
253*********************
254
255digraph G {
256
257 splines="compound"
258
259 rankdir="TB"
260 subgraph cluster_net {
261
262 subgraph cluster_1 {
263
264 subgraph cluster_1a {
265
266 node [style=filled];
267 a0;
268 a1;
269 a2;
270 a3;
271 label = "process a";
272
273 color=blue
274 }
275 subgraph cluster_1b {
276 node [style=filled];
277 b0;
278 b1;
279 b2;
280 b3;
281 label = "process b";
282 color=blue
283 }
284 label = "machine1";
285 }
286
287 subgraph cluster_2 {
288 subgraph cluster_2a {
289 node [style=filled];
290 c0 [label="/c0/kkk11"];
291 c1;
292 c2;
293 c3;
294 label = "process c";
295 color=blue
296 }
297 subgraph cluster_2b {
298 node [style=filled];
299 d0;
300 d1;
301 d2;
302 d3;
303 label = "process d";
304 color=blue
305 }
306 label = "machine";
307 }
308
309 a1 -> c1 [label="tcp" color=blue];
310 c1 -> a1 [label="tcp" color=blue];
311 b2 -> c3 [label="tcp" color=blue];
312 a3 -> c0 [label="tcp" color=green];
313 a3 -> d0 [label="tcp" color=green];
314 b3 -> d3 [label="tcp" color=blue];
315}
316*/
#define yInfo(...)
Definition Log.h:319
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:718
A class for storing options and configuration information.
Definition Property.h:33
bool check(const std::string &key) const override
Check if there exists a property of the given name.
void fromCommand(int argc, char *argv[], bool skipFirst=true, bool wipe=true)
Interprets a list of command arguments as a list of properties.
A single value (typically within a Bottle).
Definition Value.h:44
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 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 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 getPortsDetailedList(ports_detail_set &ports, bool complete=false)
std::vector< std::string > machines_list
int main(int argc, char *argv[])
Definition main.cpp:44
void display_help()
Definition main.cpp:18
#define yCInfo(component,...)
#define YARP_LOG_COMPONENT(name,...)
STL namespace.
The main, catch-all namespace for YARP.
Definition dirs.h:16