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.yarpconnectioninfo")
16}
17
19{
20 yCInfo(YARPCONNINFO) << "This is the tool yarpconnectioninfo.";
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_clock_ports";
40 yCInfo(YARPCONNINFO) << "--display_yarprun_processes";
41}
42
43int main(int argc, char *argv[])
44{
46
48 if (argc > 1) {
49 p.fromCommand(argc, argv);
50 }
51
52 std::vector <string> color_map;
53 color_map.push_back ("cadetblue1"); //0
54 color_map.push_back ("antiquewhite1");
55 color_map.push_back ("darkolivegreen2");
56 color_map.push_back ("gold2");
57 color_map.push_back ("darkorange2");
58 color_map.push_back ("cadetblue4");
59 color_map.push_back ("coral2");
60 color_map.push_back ("firebrick3");
61 color_map.push_back ("forestgreen");
62 color_map.push_back ("cornflowerblue");
63
64 std::string from_ip = p.check("from_ip",yarp::os::Value(std::string("*")), "").asString();
65 std::string to_ip = p.check("to_ip",yarp::os::Value(std::string("*")), "").asString();
66 std::string from_portnumber = p.check("from_portnumber",yarp::os::Value(std::string("*")), "").asString();
67 std::string to_portnumber = p.check("to_portnumber",yarp::os::Value(std::string("*")), "").asString();
68 std::string from_portname = p.check("from_portname",yarp::os::Value(std::string("*")), "").asString();
69 std::string to_portname = p.check("to_portname",yarp::os::Value(std::string("*")), "").asString();
70 std::string to_dotfile = p.check("to_dotfile", yarp::os::Value(std::string("")), "").asString();
71 bool display_yarprun_processes = false;
72 bool display_log_ports = false;
73 bool display_clock_ports = false;
74 bool display_unconnected_ports = false;
75
76 if (p.check("help")) {display_help(); return 1; }
77 if (p.check("display_yarprun_processes")) { display_yarprun_processes = true;}
78 if (p.check("display_log_ports")) { display_log_ports = true; }
79 if (p.check("display_clock_ports")) { display_clock_ports = true; }
80 if (p.check("display_unconnnected_ports")) { display_unconnected_ports = true; }
81
87
88 prof.getPortsList(ports_list);
89 prof.getConnectionsList(conns_orig);
90 conns=conns_orig;
91
92 if (from_ip != "*" || to_ip != "*")
93 {
94 prof.filterConnectionListByIp(conns, conns_filtered, from_ip,to_ip);
95 conns= conns_filtered;
96 }
97 if (from_portnumber != "*" || to_portnumber != "*")
98 {
99 prof.filterConnectionListByPortNumber(conns, conns_filtered, from_portnumber, to_portnumber);
100 conns = conns_filtered;
101 }
102 if (from_portname != "*" || to_portname != "*")
103 {
104 prof.filterConnectionListByName(conns, conns_filtered, from_portname, to_portname);
105 conns = conns_filtered;
106 }
107
108 std::string sss = "Connections:\n";
109 if (conns.size() !=0)
110 for (auto it = conns.begin(); it!=conns.end(); it++)
111 {
112 sss += it->src.name + " (" + it->src.ip + ":" + it->src.port_number + ") -> " +
113 it->dst.name + " (" + it->dst.ip + ":" + it->dst.port_number + ") with carrier: (" +
114 it->carrier + ")\n";
115 }
116 else
117 {
118 sss +="Empty list";
119 }
120
121 yInfo() << sss;
122
123 if (to_dotfile != "")
124 {
125 //saveToFile
126 std::ofstream file;
127 file.open(to_dotfile.c_str(), std::ios::out);
128 file << "digraph G{" <<endl;
129 file << "splines=\"compound\"" << endl;
130
131 yarp::profiler::NetworkProfiler::ports_detail_set detailed_ports_filtered_by_ip;
132 yarp::profiler::NetworkProfiler::ports_detail_set detailed_ports_filtered_by_ip_and_process;
134 prof.getPortsDetailedList(detailed_ports);
136 prof.getMachinesList(detailed_ports, ml);
138 prof.getProcessesList(detailed_ports, pl);
139
140 file << "subgraph cluster_net{" << endl;
141 size_t machine_counter = 0;
142 size_t color_map_index = 0;
143 for (auto machines_ip_it = ml.begin(); machines_ip_it != ml.end(); machines_ip_it++, machine_counter++)
144 {
145 prof.filterPortsListByIp(detailed_ports, detailed_ports_filtered_by_ip, *machines_ip_it);
146 file << "subgraph cluster_machine"<<machine_counter <<" {" << endl;
147
148 size_t process_counter = 0;
149 for (auto process_name_it = pl.begin(); process_name_it != pl.end(); process_name_it++, process_counter++)
150 {
151 //this option skips all yarprun processes
152 if (display_yarprun_processes == false)
153 {
154 std::size_t found = process_name_it->find("yarprun");
155 if (found != std::string::npos) { process_counter--; continue;}
156 }
157
158 file << "subgraph cluster_process" << process_counter<<" {" << endl;
159 prof.filterPortsListByProcess(detailed_ports_filtered_by_ip, detailed_ports_filtered_by_ip_and_process, *process_name_it);
160
161 for (auto ports_it = detailed_ports_filtered_by_ip_and_process.begin(); ports_it != detailed_ports_filtered_by_ip_and_process.end(); ports_it++)
162 {
163 //this option skips all logger ports
164 if (display_log_ports == false)
165 {
166 std::size_t found = ports_it->info.name.find("/log");
167 if (found != std::string::npos) { continue; }
168 }
169
170 //this option skips all clock ports
171 if (display_clock_ports == false)
172 {
173 std::size_t found = ports_it->info.name.find("/clock:i");
174 if (found != std::string::npos) { continue; }
175 }
176
177 //this option skips all unconnected ports
178 if (display_unconnected_ports == false)
179 {
180 if (ports_it->inputs.size() == 0 &&
181 ports_it->outputs.size() == 0)
182 {
183 continue;
184 }
185 }
186
187 file << "\"" << ports_it->info.name << "\";" << endl;
188 }
189 file << "label = \""<< *process_name_it << "\";" << endl;
190 file << "}" << endl;
191 }
192 file << "label = \"" << *machines_ip_it << "\";" << endl;
193 file << "style = filled" << endl;
194 file << "color = " << color_map[color_map_index++] << endl;
195 if (color_map_index >=10) color_map_index=0;
196 file << "}" << endl;
197 }
198 file << "}" << endl;
199
200 for (auto connection_it = conns.begin(); connection_it != conns.end(); connection_it++)
201 {
202 //this option skips all logger ports
203 if (display_log_ports == false)
204 {
205 std::size_t founds = connection_it->src.name.find("/log");
206 std::size_t foundd = connection_it->dst.name.find("/log");
207 if (founds != std::string::npos ||
208 foundd != std::string::npos) { continue; }
209 }
210
211 //this option skips all clock ports
212 if (display_clock_ports == false)
213 {
214 std::size_t founds = connection_it->src.name.find("/clock:i");
215 std::size_t foundd = connection_it->dst.name.find("/clock:i");
216 if (founds != std::string::npos ||
217 foundd != std::string::npos) {
218 continue;
219 }
220 }
221
222 std::string protocol_name = connection_it->carrier; //"unknown"
223 std::string color_connection = "black";
224 if (protocol_name == "tcp") { color_connection = "blue";}
225 else if (protocol_name == "udp") { color_connection = "red"; }
226 else if (protocol_name == "fast_tcp") { color_connection = "green"; }
227 else if (protocol_name == "mjpeg") { color_connection = "darkorange2"; }
228 else if (protocol_name == "tcpros") { color_connection = "darkgray"; }
229
230 file << "\"" << connection_it->src.name << "\"" << " -> "
231 << "\"" << connection_it->dst.name << "\"" << " [label = \""<< protocol_name << "\" color = "<< color_connection <<"]" << endl;
232 }
233
234 file << "}" << endl;
235 file.close();
236 }
237
238 return 1;
239}
240
241/********************
242* dot file example
243*********************
244
245digraph G {
246
247 splines="compound"
248
249 rankdir="TB"
250 subgraph cluster_net {
251
252 subgraph cluster_1 {
253
254 subgraph cluster_1a {
255
256 node [style=filled];
257 a0;
258 a1;
259 a2;
260 a3;
261 label = "process a";
262
263 color=blue
264 }
265 subgraph cluster_1b {
266 node [style=filled];
267 b0;
268 b1;
269 b2;
270 b3;
271 label = "process b";
272 color=blue
273 }
274 label = "machine1";
275 }
276
277 subgraph cluster_2 {
278 subgraph cluster_2a {
279 node [style=filled];
280 c0 [label="/c0/kkk11"];
281 c1;
282 c2;
283 c3;
284 label = "process c";
285 color=blue
286 }
287 subgraph cluster_2b {
288 node [style=filled];
289 d0;
290 d1;
291 d2;
292 d3;
293 label = "process d";
294 color=blue
295 }
296 label = "machine";
297 }
298
299 a1 -> c1 [label="tcp" color=blue];
300 c1 -> a1 [label="tcp" color=blue];
301 b2 -> c3 [label="tcp" color=blue];
302 a3 -> c0 [label="tcp" color=green];
303 a3 -> d0 [label="tcp" color=green];
304 b3 -> d3 [label="tcp" color=blue];
305}
306*/
#define yInfo(...)
Definition Log.h:319
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:706
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:43
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