YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
RpcMonitor.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
6#include "RpcMonitor.h"
7
8#include <yarp/os/Bottle.h>
11#include <yarp/os/LogStream.h>
12#include <yarp/os/Things.h>
13
14#include <map>
15
16// Monitor: log component
17namespace {
18YARP_LOG_COMPONENT(RPCMONITOR,
19 "yarp.portmonitor.rpcMonitor",
23 nullptr)
24
25bool split(const std::string &s, std::map<std::string, std::string>& parameters) {
26 std::istringstream iss(s);
27 std::string item;
28 while (std::getline(iss, item, '+')) {
29 const auto point = item.find('.');
30 parameters[item.substr(0, point)] = item.substr(point + 1);
31 }
32 return true;
33}
34} // namespace
35
36// Monitor: create
38{
39 sender = options.find("sender_side").asBool();
40 source = options.find("source").asString();
41 destination = options.find("destination").asString();
42 const std::string source_port = (sender ? source : destination) + "/monitor";
43 // Check the 'monitor' parameter
44 const std::string carrier = options.find("carrier").asString();
45 std::map<std::string, std::string> parameters;
46 if (!split(carrier, parameters)) {
47 yCError(RPCMONITOR, "Error parsing the parameters.");
48 return false;
49 }
50 const std::string monitor_port = ((parameters.find("monitor") != parameters.end()) ? parameters["monitor"] : "/monitor");
51
52 if (!sender) {
53 yCError(RPCMONITOR, "Attaching on receiver side is not supported yet.");
54 return false;
55 }
56 if (!port.openFake(source_port)) {
57 yCError(RPCMONITOR, "Could not open port %s.", source_port.c_str());
58 return false;
59 }
60 if (!port.addOutput(monitor_port)) {
61 yCError(RPCMONITOR, "Could not connect to port %s.", monitor_port.c_str());
62 return false;
63 }
64 return true;
65}
66
67// Monitor: update
69{
72 msg.addString(source);
73 msg.addString(destination);
74 msg.addString("command");
75 msg.addString(sender ? "client" : "server");
76 msg.addString("RpcMonitor");
77 auto& bcmd = msg.addList();
78 auto& bargs [[maybe_unused]] = msg.addList();
79 auto& breply [[maybe_unused]] = msg.addList();
80
81 if (const auto* reply = thing.cast_as<yarp::os::CommandBottle>()) {
82 bcmd.addString(reply->cmd.get(0).toString());
83 yarp::os::Portable::copyPortable(reply->cmd, bargs);
84 } else if (const auto* cmd = thing.cast_as<yarp::os::Bottle>()) {
85 bcmd.addString(cmd->get(0).toString());
87 } else {
88 yCWarning(RPCMONITOR) << "Sending unknown command";
89 bcmd.addString("[unknown]");
90 }
91 yCDebug(RPCMONITOR, "Writing: %s", msg.toString().c_str());
92 port.write(msg);
93 return thing;
94}
95
96// Monitor: updateReply
98{
101 msg.addString(source);
102 msg.addString(destination);
103 msg.addString("reply");
104 msg.addString(sender ? "rpc client" : "rpc server");
105 msg.addString("RpcMonitor");
106 auto& bcmd = msg.addList();
107 auto& bargs [[maybe_unused]] = msg.addList();
108 auto& breply [[maybe_unused]] = msg.addList();
109
110 if (const auto* reply = thing.cast_as<yarp::os::CommandBottle>()) {
111 bcmd.addString(reply->cmd.get(0).toString());
112 yarp::os::Portable::copyPortable(reply->cmd, bargs);
113 yarp::os::Portable::copyPortable(reply->reply, breply);
114 } else if (const auto* reply = thing.cast_as<yarp::os::Bottle>()) {
115 yarp::os::Portable::copyPortable(*reply, breply);
116 } else {
117 yCWarning(RPCMONITOR) << "Received unknown reply";
118 bcmd.addString("[unknown]");
119 }
120 yCDebug(RPCMONITOR, "Writing: %s", msg.toString().c_str());
121 port.write(msg);
122 return thing;
123}
yarp::os::Things & updateReply(yarp::os::Things &thing) override
The updateReply makes it possible to modify a reply from a port when the portmonitor object is attach...
bool create(const yarp::os::Property &options) override
This will be called when the dll is properly loaded by the portmonitor carrier.
yarp::os::Things & update(yarp::os::Things &thing) override
After data get accpeted in the accept() callback, an instance of that is given to the update function...
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:182
void addFloat64(yarp::conf::float64_t x)
Places a 64-bit floating point number in the bottle, at the end of the list.
Definition Bottle.cpp:158
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:170
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition Bottle.cpp:211
static LogCallback printCallback()
Get current print callback.
Definition Log.cpp:873
static LogType minimumPrintLevel()
Get current minimum print level.
Definition Log.cpp:833
@ LogTypeReserved
Definition Log.h:98
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition Port.cpp:436
bool addOutput(const std::string &name) override
Add an output connection to the specified port.
Definition Port.cpp:353
bool openFake(const std::string &name)
Start port without making it accessible from the network.
Definition Port.cpp:74
static bool copyPortable(const PortWriter &writer, PortReader &reader)
Copy one portable to another, via writing and reading.
Definition Portable.cpp:16
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.
static double nowSystem()
Base class for generic things.
Definition Things.h:18
T * cast_as()
Definition Things.h:53
virtual bool asBool() const
Get boolean value.
Definition Value.cpp:186
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
STL namespace.