YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
RunClient.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/run/Run.h>
14
17
18#include <yarp/os/Network.h>
19#include <yarp/os/Os.h>
20#include <yarp/os/LogStream.h>
21#include <yarp/os/RpcClient.h>
22#include <yarp/os/RpcServer.h>
23#include <yarp/os/Semaphore.h>
24#include <yarp/os/SystemInfo.h>
25#include <yarp/os/Time.h>
26
30
31#include <cstdio>
32#include <string>
33#include <cstring>
34#include <random>
35
36// CLIENT
38{
39 // WITH STDIO
40 //
41 if (config.check("cmd") && config.check("stdio"))
42 {
44 // syntax check
45 if (config.find("stdio").asString()=="")
46 {
47 Help("SYNTAX ERROR: missing remote stdio server\n");
48 return YARPRUN_ERROR;
49 }
50 if (config.find("cmd").asString()=="")
51 {
52 Help("SYNTAX ERROR: missing command\n");
53 return YARPRUN_ERROR;
54 }
55 if (!config.check("as") || config.find("as").asString()=="")
56 {
57 Help("SYNTAX ERROR: missing tag\n");
58 return YARPRUN_ERROR;
59 }
60 if (!config.check("on") || config.find("on").asString()=="")
61 {
62 Help("SYNTAX ERROR: missing remote server\n");
63 return YARPRUN_ERROR;
64 }
65 //
67
68 printf("*********** %s ************\n", config.toString().c_str());
69
71 msg.addList()=config.findGroup("stdio");
72 msg.addList()=config.findGroup("cmd");
73 msg.addList()=config.findGroup("as");
74 msg.addList()=config.findGroup("on");
75
76 if (config.check("workdir")) {
77 msg.addList() = config.findGroup("workdir");
78 }
79 if (config.check("geometry")) {
80 msg.addList() = config.findGroup("geometry");
81 }
82 if (config.check("hold")) {
83 msg.addList() = config.findGroup("hold");
84 }
85 if (config.check("env")) {
86 msg.addList() = config.findGroup("env");
87 }
88 if (config.check("log")) {
89 msg.addList() = config.findGroup("log");
90 }
91 /*
92 {
93 yarp::os::Bottle log;
94 log.addString("log");
95 log.addString("log");
96 msg.addList()=log;
97 }
98 */
99
100 std::string on=config.find("on").asString();
101
102 yarp::os::Bottle response=sendMsg(msg, on);
103
104 if (!response.size()) {
105 return YARPRUN_ERROR;
106 }
107
108 if (response.get(0).asInt32() <= 0) {
109 return 2;
110 }
111
112 return 0;
113 }
114
115 // NO STDIO
116 //
117 if (config.check("cmd"))
118 {
120 // syntax check
121 if (config.find("cmd").asString()=="")
122 {
123 Help("SYNTAX ERROR: missing command\n");
124 return YARPRUN_ERROR;
125 }
126 if (!config.check("as") || config.find("as").asString()=="")
127 {
128 Help("SYNTAX ERROR: missing tag\n");
129 return YARPRUN_ERROR;
130 }
131 if (!config.check("on") || config.find("on").asString()=="")
132 {
133 Help("SYNTAX ERROR: missing remote server\n");
134 return YARPRUN_ERROR;
135 }
136 //
138
140 msg.addList()=config.findGroup("cmd");
141 msg.addList()=config.findGroup("as");
142
143 if (config.check("workdir")) {
144 msg.addList() = config.findGroup("workdir");
145 }
146 if (config.check("log")) {
147 msg.addList() = config.findGroup("log");
148 }
149 /*
150 {
151 yarp::os::Bottle log;
152 log.addString("log");
153 log.addString("log");
154 msg.addList()=log;
155 }
156 */
157 if (config.check("env")) {
158 msg.addList() = config.findGroup("env");
159 }
160
161 yarp::os::Bottle response=sendMsg(msg, config.find("on").asString());
162
163 if (!response.size()) {
164 return YARPRUN_ERROR;
165 }
166
167 if (response.get(0).asInt32() <= 0) {
168 return 2;
169 }
170
171 return 0;
172 }
173
174
175
176
177
178 //----------------------------
179 if (config.check("kill"))
180 {
181 if (!config.check("on") || config.find("on").asString()=="")
182 {
183 Help("SYNTAX ERROR: missing remote server\n");
184 return YARPRUN_ERROR;
185 }
186 if (config.findGroup("kill").get(1).asString()=="")
187 {
188 Help("SYNTAX ERROR: missing tag\n");
189 return YARPRUN_ERROR;
190 }
191 if (config.findGroup("kill").get(2).asInt32()==0)
192 {
193 Help("SYNTAX ERROR: missing signum\n");
194 return YARPRUN_ERROR;
195 }
196
197 int ret = kill(config.find("on").asString(),
198 config.findGroup("kill").get(1).asString(),
199 config.findGroup("kill").get(2).asInt32());
200
201 return ret?0:2;
202 }
203
204 //----------------------------
205 if (config.check("sigterm"))
206 {
207 if (config.find("sigterm").asString()=="")
208 {
209 Help("SYNTAX ERROR: missing tag");
210 return YARPRUN_ERROR;
211 }
212 if (!config.check("on") || config.find("on").asString()=="")
213 {
214 Help("SYNTAX ERROR: missing remote server\n");
215 return YARPRUN_ERROR;
216 }
217
218 sigterm(config.find("on").asString(),
219 config.find("sigterm").asString());
220
221 return 0;
222 }
223
224 //----------------------------
225 if (config.check("sigtermall"))
226 {
227 if (!config.check("on") || config.find("on").asString()=="")
228 {
229 Help("SYNTAX ERROR: missing remote server\n");
230 return YARPRUN_ERROR;
231 }
232
233 sigtermall(config.find("on").asString());
234
235 return 0;
236 }
237
238 //----------------------------
239 if (config.check("ps"))
240 {
241 if (!config.check("on") || config.find("on").asString()=="")
242 {
243 Help("SYNTAX ERROR: missing remote server\n");
244 return YARPRUN_ERROR;
245 }
246
247 std::vector<processInfo> processes;
248 ps(config.find("on").asString(),processes);
249
250 return 0;
251 }
252
253 //----------------------------
254 if (config.check("isrunning"))
255 {
256 if (!config.check("on") || config.find("on").asString()=="")
257 {
258 Help("SYNTAX ERROR: missing remote server\n");
259 return YARPRUN_ERROR;
260 }
261
262 if (config.find("isrunning").asString()=="")
263 {
264 Help("SYNTAX ERROR: missing tag\n");
265 return YARPRUN_ERROR;
266 }
267
268 bool running = isRunning(config.find("on").asString(),
269 config.find("isrunning").asString());
270
271 return running?0:2;
272 }
273
274 //----------------------------
275 if (config.check("sysinfo"))
276 {
277 if (!config.check("on") || config.find("on").asString()=="")
278 {
279 Help("SYNTAX ERROR: missing remote server\n");
280 return YARPRUN_ERROR;
281 }
282
284 bool ret = sysinfo(config.find("on").asString(),info);
285 if (!ret)
286 {
287 fprintf(stdout, "No response. (timeout)\n");
288 return YARPRUN_ERROR;
289 }
290
291 fprintf(stdout, "Platform name : %s\n", info.platform.name.c_str());
292 fprintf(stdout, "Platform dist : %s\n", info.platform.distribution.c_str());
293 fprintf(stdout, "Platform release : %s\n", info.platform.release.c_str());
294 fprintf(stdout, "Platform code : %s\n", info.platform.codename.c_str());
295 fprintf(stdout, "Platform kernel : %s\n\n", info.platform.kernel.c_str());
296
297 fprintf(stdout, "User Id : %d\n", info.user.userID);
298 fprintf(stdout, "User name : %s\n", info.user.userName.c_str());
299 fprintf(stdout, "User real name : %s\n", info.user.realName.c_str());
300 fprintf(stdout, "User home dir : %s\n\n", info.user.homeDir.c_str());
301
302 fprintf(stdout, "Cpu load Ins.: %d\n", info.load.cpuLoadInstant);
303 fprintf(stdout, "Cpu load 1 : %.2lf\n", info.load.cpuLoad1);
304 fprintf(stdout, "Cpu load 5 : %.2lf\n", info.load.cpuLoad5);
305 fprintf(stdout, "Cpu load 15 : %.2lf\n\n", info.load.cpuLoad15);
306
307 fprintf(stdout, "Memory total : %dM\n", info.memory.totalSpace);
308 fprintf(stdout, "Memory free : %dM\n\n", info.memory.freeSpace);
309
310 fprintf(stdout, "Storage total : %dM\n", info.storage.totalSpace);
311 fprintf(stdout, "Storage free : %dM\n\n", info.storage.freeSpace);
312
313 fprintf(stdout, "Processor model : %s\n", info.processor.model.c_str());
314 fprintf(stdout, "Processor model num : %d\n", info.processor.modelNumber);
315 fprintf(stdout, "Processor family : %d\n", info.processor.family);
316 fprintf(stdout, "Processor vendor : %s\n", info.processor.vendor.c_str());
317 fprintf(stdout, "Processor arch : %s\n", info.processor.architecture.c_str());
318 fprintf(stdout, "Processor cores : %d\n", info.processor.cores);
319 fprintf(stdout, "Processor siblings : %d\n", info.processor.siblings);
320 fprintf(stdout, "Processor Mhz : %.2lf\n\n", info.processor.frequency);
321
322 fprintf(stdout, "Environment variables :\n%s\n", info.platform.environmentVars.c_str());
323 //fprintf(stdout, "Network IP4 : %s\n", info.network.ip4.c_str());
324 //fprintf(stdout, "Network IP6 : %s\n", info.network.ip6.c_str());
325 //fprintf(stdout, "Network mac : %s\n\n", info.network.mac.c_str());
326
327 return 0;
328 }
329
330 //----------------------------
331 if (config.check("which"))
332 {
333 if (!config.check("on") || config.find("on").asString()=="")
334 {
335 Help("SYNTAX ERROR: missing remote server\n");
336
337 return YARPRUN_ERROR;
338 }
339
340 bool ret = which(config.find("on").asString(), config.find("which").asString());
341
342 return ret?0:2;
343 }
344
345 //----------------------------
346 if (config.check("exit"))
347 {
348 if (!config.check("on") || config.find("on").asString()=="")
349 {
350 Help("SYNTAX ERROR: missing remote server\n");
351
352 return YARPRUN_ERROR;
353 }
354
355 bool ret = exit(config.find("on").asString());
356
357 return ret?0:2;
358 }
359
360 return 0;
361}
362
363void yarp::run::Run::Help(const char *msg)
364{
365 fprintf(stderr, "%s", msg);
366 fprintf(stderr, "\nUSAGE:\n\n");
367 fprintf(stderr, "yarp run --server SERVERPORT\nrun a server on the local machine\n\n");
368 fprintf(stderr, "yarp run --on SERVERPORT --as TAG --cmd COMMAND [ARGLIST] [--workdir WORKDIR] [--env ENVIRONMENT]\nrun a command on SERVERPORT server\n\n");
369 fprintf(stderr, "yarp run --on SERVERPORT --as TAG --stdio STDIOSERVERPORT [--hold] [--geometry WxH+X+Y] --cmd COMMAND [ARGLIST] [--workdir WORKDIR] [--env ENVIRONMENT]\n");
370 fprintf(stderr, "run a command on SERVERPORT server sending I/O to STDIOSERVERPORT server\n\n");
371 fprintf(stderr, "yarp run --on SERVERPORT --kill TAG SIGNUM\nsend SIGNUM signal to TAG command\n\n");
372 fprintf(stderr, "yarp run --on SERVERPORT --sigterm TAG\nterminate TAG command\n\n");
373 fprintf(stderr, "yarp run --on SERVERPORT --sigtermall\nterminate all commands\n\n");
374 fprintf(stderr, "yarp run --on SERVERPORT --ps\nreport commands running on SERVERPORT\n\n");
375 fprintf(stderr, "yarp run --on SERVERPORT --isrunning TAG\nTAG command is running?\n\n");
376 fprintf(stderr, "yarp run --on SERVERPORT --sysinfo\nreport system information of SERVERPORT\n\n");
377 fprintf(stderr, "yarp run --on SERVERPORT --exit\nstop SERVERPORT server\n\n");
378 fprintf(stderr, "yarp run --on SERVERPORT --which\nsearch for path of provided filename\n\n");
379}
380
382// API
384
385bool yarp::run::Run::isRunning(const std::string &node, const std::string &keyv)
386{
387 yarp::os::Bottle msg, grp, response;
388
389 grp.clear();
390 grp.addString("on");
391 grp.addString(node.c_str());
392 msg.addList()=grp;
393
394 grp.clear();
395 grp.addString("isrunning");
396 grp.addString(keyv.c_str());
397 msg.addList()=grp;
398
399 printf(":: %s\n", msg.toString().c_str());
400
401 response=sendMsg(msg, node);
402
403 if (!response.size()) {
404 return false;
405 }
406
407 return response.get(0).asString()=="running";
408}
409
410
411bool yarp::run::Run::sysinfo(const std::string &node, yarp::os::SystemInfo& info)
412{
413 yarp::os::Bottle msg, grp, response;
414
415 grp.clear();
416 grp.addString("on");
417 grp.addString(node.c_str());
418 msg.addList()=grp;
419
420 grp.clear();
421 grp.addString("sysinfo");
422 msg.addList()=grp;
423
424 printf(":: %s\n", msg.toString().c_str());
426 //port.setTimeout(5.0);
427 if (!port.open("..."))
428 {
429 fprintf(stderr, "RESPONSE:\n=========\n");
430 fprintf(stderr, "Cannot open port, aborting...\n");
431 return false;
432 }
433 bool connected = yarp::os::Network::connect(port.getName(), node);
434 if (!connected)
435 {
436 fprintf(stderr, "RESPONSE:\n=========\n");
437 fprintf(stderr, "Cannot connect to remote server, aborting...\n");
438 port.close();
439 //yarp::os::Network::unregisterName(port.getName());
440 return false;
441 }
442 RUNLOG("<<<port.write(msg, info)")
443 int ret = port.write(msg, info);
444 RUNLOG(">>>port.write(msg, info)")
446 port.close();
447 //yarp::os::Network::unregisterName(port.getName());
448 fprintf(stdout, "RESPONSE:\n=========\n\n");
449 if (!ret)
450 {
451 fprintf(stdout, "No response. (timeout)\n");
452 return false;
453 }
454
455 return true;
456}
457
458bool yarp::run::Run::start(const std::string &node, yarp::os::Property &command, std::string &keyv)
459{
460 yarp::os::Bottle msg, grp, response;
461
462 grp.clear();
463 grp.addString("on");
464 grp.addString(node.c_str());
465 msg.addList()=grp;
466
467 std::string dest_srv=node;
468
469 if (command.check("stdio"))
470 {
471 dest_srv=std::string(command.find("stdio").asString());
472
473 grp.clear();
474 grp.addString("stdio");
475 grp.addString(dest_srv.c_str());
476 msg.addList()=grp;
477
478 if (command.check("geometry"))
479 {
480 grp.clear();
481 grp.addString("geometry");
482 grp.addString(command.find("geometry").asString().c_str());
483 msg.addList()=grp;
484 }
485
486 if (command.check("hold"))
487 {
488 grp.clear();
489 grp.addString("hold");
490 msg.addList()=grp;
491 }
492 }
493
494 grp.clear();
495 grp.addString("as");
496 grp.addString(keyv.c_str());
497 msg.addList()=grp;
498
499 grp.clear();
500 grp.addString("cmd");
501 grp.addString(command.find("name").asString().c_str());
502 grp.addString(command.find("parameters").asString().c_str());
503 msg.addList()=grp;
504
505 printf(":: %s\n", msg.toString().c_str());
506
507 response=sendMsg(msg, dest_srv);
508
509 char buff[16];
510 snprintf(buff, sizeof(buff), "%d", response.get(0).asInt32());
511 keyv=std::string(buff);
512
513 return response.get(0).asInt32()>0?true:false;
514}
515
516bool yarp::run::Run::sigterm(const std::string &node, const std::string &keyv)
517{
518 yarp::os::Bottle msg, grp, response;
519
520 grp.clear();
521 grp.addString("on");
522 grp.addString(node.c_str());
523 msg.addList()=grp;
524
525 grp.clear();
526 grp.addString("sigterm");
527 grp.addString(keyv.c_str());
528 msg.addList()=grp;
529
530 printf(":: %s\n", msg.toString().c_str());
531
532 response=sendMsg(msg, node);
533
534 return response.get(0).asString()=="sigterm OK"?true:false;
535}
536
537bool yarp::run::Run::sigtermall(const std::string &node)
538{
539 yarp::os::Bottle msg, grp, response;
540
541 grp.clear();
542 grp.addString("on");
543 grp.addString(node.c_str());
544 msg.addList()=grp;
545
546 grp.clear();
547 grp.addString("sigtermall");
548 msg.addList()=grp;
549
550 printf(":: %s\n", msg.toString().c_str());
551
552 response=sendMsg(msg, node);
553
554 return response.get(0).asString()=="sigtermall OK"?true:false;
555}
556
557bool yarp::run::Run::kill(const std::string &node, const std::string &keyv, int s)
558{
559 yarp::os::Bottle msg, grp, response;
560
561 grp.clear();
562 grp.addString("on");
563 grp.addString(node.c_str());
564 msg.addList()=grp;
565
566 grp.clear();
567 grp.addString("kill");
568 grp.addString(keyv.c_str());
569 grp.addInt32(s);
570 msg.addList()=grp;
571
572 printf(":: %s\n", msg.toString().c_str());
573
574 response=sendMsg(msg, node);
575
576 return response.get(0).asString()=="kill OK"?true:false;
577}
578
579bool yarp::run::Run::ps(const std::string &node, std::vector<processInfo>& processes)
580{
581 yarp::os::Bottle msg, grp, response;
582 grp.clear();
583 grp.addString("on");
584 grp.addString(node.c_str());
585 msg.addList()=grp;
586
587 grp.clear();
588 grp.addString("ps");
589 msg.addList()=grp;
590
591 printf(":: %s\n", msg.toString().c_str());
592
593 response=sendMsg(msg, node);
594 for (size_t i=0; i<response.size(); i++)
595 {
596 response.get(i).toString();
597 processInfo temp;
598 yarp::os::Bottle* b = response.get(i).asList();
599
600 temp.pid = b->get(0).asList()->get(1).asInt32();
601 temp.tag = b->get(1).asList()->get(1).asString();
602 temp.status = b->get(2).asList()->get(1).asString();
603 temp.command = b->get(3).asList()->get(1).asString();
604 temp.env = b->get(4).asList()->get(1).asString();
605 processes.push_back(temp);
606 }
607 return true;
608}
609
610bool yarp::run::Run::which(const std::string &node, const std::string &keyv)
611{
612 yarp::os::Bottle msg, grp, response;
613
614 grp.clear();
615 grp.addString("on");
616 grp.addString(node.c_str());
617 msg.addList()=grp;
618
619 grp.clear();
620 grp.addString("which");
621 grp.addString(keyv.c_str());
622 msg.addList()=grp;
623
624 printf(":: %s\n", msg.toString().c_str());
625
626 response=sendMsg(msg, node);
627
628 if (!response.size()) {
629 return false;
630 }
631
632 return true;
633}
634
635bool yarp::run::Run::exit(const std::string &node)
636{
637 yarp::os::Bottle msg, grp, response;
638
639 grp.clear();
640 grp.addString("on");
641 grp.addString(node.c_str());
642 msg.addList()=grp;
643
644 grp.clear();
645 grp.addString("exit");
646 msg.addList()=grp;
647
648 printf(":: %s\n", msg.toString().c_str());
649
650 response=sendMsg(msg, node);
651
652 if (!response.size()) {
653 return false;
654 }
655
656 return true;
657}
#define RUNLOG(msg)
#define YARPRUN_ERROR
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
std::string getName() const override
Get name of port.
void close() override
Stop port activity.
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition Bottle.cpp:188
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
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition Bottle.cpp:140
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:176
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition Bottle.cpp:212
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 disconnect(const std::string &src, const std::string &dest, bool quiet)
Request that an output port disconnect from an input port.
Definition Network.cpp:802
A class for storing options and configuration information.
Definition Property.h:32
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
std::string toString() const override
Return a standard text representation of the content of the object.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
A port that is specialized as an RPC client.
Definition RpcClient.h:22
A class to get the system (platform) status such as available memory, storage, CPU load and etc.
Definition SystemInfo.h:25
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition Value.cpp:204
virtual Bottle * asList() const
Get list value.
Definition Value.cpp:246
std::string toString() const override
Return a standard text representation of the content of the object.
Definition Value.cpp:347
virtual std::string asString() const
Get string value.
Definition Value.cpp:240
static bool ps(const std::string &node, std::vector< processInfo > &processes)
Get a report of all applications running on a yarprun server.
static bool start(const std::string &node, yarp::os::Property &command, std::string &keyv)
Launch a yarprun server.
static bool sigtermall(const std::string &node)
Terminate all applications running on a yarprun server.
static bool which(const std::string &node, const std::string &keyv)
Display the path of a file on a yarprun server.
static int client(yarp::os::Property &config)
Send a property object to a run server, bundling up all the settings usually specified on the command...
Definition RunClient.cpp:37
static bool kill(const std::string &node, const std::string &keyv, int s)
Send a SIGNAL to an application running on a yarprun server (Linux only).
static bool isRunning(const std::string &node, const std::string &keyv)
Report if an application is still running on a yarprun server.
static bool sysinfo(const std::string &node, yarp::os::SystemInfo &info)
Get a report of system information of a yarprun server.
static bool sigterm(const std::string &node, const std::string &keyv)
Terminate an application running on a yarprun server.
static bool exit(const std::string &node)
Exit a yarprun server.
std::string command
Definition Run.h:89
std::string status
Definition Run.h:88