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