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/Network.h>
9#include <yarp/os/Bottle.h>
12#include <yarp/os/LogStream.h>
13#include <yarp/os/Things.h>
14
15#include <map>
16
17// Monitor: log component
18namespace {
19YARP_LOG_COMPONENT(RPCMONITOR,
20 "yarp.portmonitor.rpcMonitor",
24 nullptr)
25
26} // namespace
27
28// Monitor: create
30{
31 sender = options.find("sender_side").asBool();
32 source = options.find("source").asString();
33 destination = options.find("destination").asString();
34 const std::string source_port = (sender ? source : destination) + "/monitor";
35
36 const std::string monitor_port = options.check("monitor_name", yarp::os::Value("/monitor")).asString();
37
38 if (!sender) {
39 yCError(RPCMONITOR, "Attaching on receiver side is not supported yet.");
40 return false;
41 }
42 if (!port.open(source_port)) {
43 yCError(RPCMONITOR, "Could not open port %s.", source_port.c_str());
44 return false;
45 }
46 if (!yarp::os::Network::connect(source_port, monitor_port, "fast_tcp"))
47 {
48 yCError(RPCMONITOR, "Could not connect to port %s.", monitor_port.c_str());
49 return false;
50 }
51 return true;
52}
53
54// Monitor: update
56{
59 msg.addString(source);
60 msg.addString(destination);
61 msg.addString("command");
62 msg.addString(sender ? "client" : "server");
63 msg.addString("RpcMonitor");
64 auto& bcmd = msg.addList();
65 auto& bargs [[maybe_unused]] = msg.addList();
66 auto& breply [[maybe_unused]] = msg.addList();
67
68 if (const auto* reply = thing.cast_as<yarp::os::CommandBottle>()) {
69 bcmd.addString(reply->cmd.get(0).toString());
70 yarp::os::Portable::copyPortable(reply->cmd, bargs);
71 } else if (const auto* cmd = thing.cast_as<yarp::os::Bottle>()) {
72 bcmd.addString(cmd->get(0).toString());
74 } else {
75 yCWarning(RPCMONITOR) << "Sending unknown command";
76 bcmd.addString("[unknown]");
77 }
78 yCDebug(RPCMONITOR, "Writing: %s", msg.toString().c_str());
79 if (port.getOutputCount() > 0) {
80 port.write(msg);
81 }
82 return thing;
83}
84
85// Monitor: updateReply
87{
90 msg.addString(source);
91 msg.addString(destination);
92 msg.addString("reply");
93 msg.addString(sender ? "rpc client" : "rpc server");
94 msg.addString("RpcMonitor");
95 auto& bcmd = msg.addList();
96 auto& bargs [[maybe_unused]] = msg.addList();
97 auto& breply [[maybe_unused]] = msg.addList();
98
99 if (const auto* reply = thing.cast_as<yarp::os::CommandBottle>()) {
100 bcmd.addString(reply->cmd.get(0).toString());
101 yarp::os::Portable::copyPortable(reply->cmd, bargs);
102 yarp::os::Portable::copyPortable(reply->reply, breply);
103 } else if (const auto* reply = thing.cast_as<yarp::os::Bottle>()) {
104 yarp::os::Portable::copyPortable(*reply, breply);
105 } else {
106 yCWarning(RPCMONITOR) << "Received unknown reply";
107 bcmd.addString("[unknown]");
108 }
109 yCDebug(RPCMONITOR, "Writing: %s", msg.toString().c_str());
110 if (port.getOutputCount() > 0) {
111 port.write(msg);
112 }
113 return thing;
114}
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:188
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:176
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition Bottle.cpp:212
static LogCallback printCallback()
Get current print callback.
Definition Log.cpp:892
static LogType minimumPrintLevel()
Get current minimum print level.
Definition Log.cpp:852
@ LogTypeReserved
Definition Log.h:98
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
int getOutputCount() override
Determine how many output connections this port has.
Definition Port.cpp:526
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition Port.cpp:395
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
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:32
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
static double nowSystem()
Base class for generic things.
Definition Things.h:18
T * cast_as()
Definition Things.h:53
A single value (typically within a Bottle).
Definition Value.h:44
virtual bool asBool() const
Get boolean value.
Definition Value.cpp:186
virtual std::string asString() const
Get string value.
Definition Value.cpp:240
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)