YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
rpc_monitorTest.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026-2026 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 <yarp/os/Time.h>
10#include <yarp/os/RpcClient.h>
11#include <yarp/os/RpcServer.h>
12#include <yarp/os/Port.h>
13#include <yarp/os/Bottle.h>
14#include <yarp/os/Carriers.h>
15
17
18#include <array>
19#include <thread>
20
21#include <catch2/catch_amalgamated.hpp>
22#include <harness.h>
23
24TEST_CASE("pm::rpc_monitorTest", "[yarp::pm]")
25{
26 YARP_REQUIRE_PLUGIN("rpc_monitor", "portmonitor")
27
29
31
32 REQUIRE(yarp::os::Carriers::getCarrierTemplate("portmonitor") != nullptr);
33
35
36 struct TestCase {
37 std::string carrier;
38 bool use_monitor;
39 };
40
41 auto tc = GENERATE(
42 TestCase {"fast_tcp", false},
43 TestCase {"fast_tcp+send.portmonitor+file.rpc_monitor+monitor_name./monitor_data_receiver+type.dll", true}
44 );
45
46 SECTION("Quick Test RPC monitoring - monitor = " + std::string(tc.use_monitor ? "enabled" : "disabled"))
47 {
48 yarp::os::Port sender;
49 yarp::os::Port receiver;
50 yarp::os::Port monitorport;
51
52 sender.open("/send");
53 receiver.open("/recv");
54 if (tc.use_monitor) {
55
56 monitorport.open("/monitor_data_receiver");
57 }
58
59 yarp::os::Network::connect("/send", "/recv", tc.carrier);
60
61 sender.write(yarp::os::Value(100));
62
64
65 yarp::os::Value rval;
66 receiver.read(rval);
67 CHECK(rval.asInt32()==100);
68
69 receiver.close();
70 sender.close();
71 if (tc.use_monitor) {
72 monitorport.close();
73 }
74 }
75
76 SECTION("Advanced Test RPC monitoring - monitor = " + std::string(tc.use_monitor ? "enabled" : "disabled"))
77 {
80 yarp::os::Port monitor;
81
82 client.open("/rpc_client");
83 server.open("/rpc_server");
84
85 if (tc.use_monitor) {
86 monitor.open("/monitor_data_receiver");
87 }
88
89 bool b = yarp::os::Network::connect("/rpc_client", "/rpc_server", tc.carrier);
90 REQUIRE(b);
91
93
94 // Send RPC command in a separate thread
95 std::thread server_thread([&server]() {
97 yarp::os::Bottle reply;
98 server.read(cmd, true);
99 reply.addString("response");
100 reply.addInt32(42);
101 server.reply(reply);
102 });
103
104 // Send command from client
106 cmd.addString("test_command");
107 cmd.addInt32(123);
108
109 yarp::os::Bottle reply;
110 client.write(cmd, reply);
111
113
114 server_thread.join();
115
116 // Verify the RPC worked
117 REQUIRE(reply.size() == 2);
118 CHECK(reply.get(0).asString() == "response");
119 CHECK(reply.get(1).asInt32() == 42);
120
121 if (tc.use_monitor) {
122 // Check if monitor received the command/reply
124
125 // The monitor port should have received messages
126 // Note: In actual implementation, you would check monitor.getPendingReads()
127 // but this basic test just verifies the connection works
128 CHECK(true); // Placeholder for actual monitor verification
129 }
130
131 monitor.close();
132 server.close();
133 client.close();
134 }
135
137}
bool reply(PortWriter &writer) override
Send an object as a reply to an object read from the port.
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.
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
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 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
static Carrier * getCarrierTemplate(const std::string &name)
Get template for carrier.
Definition Carriers.cpp:238
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 setLocalMode(bool flag)
Chooses whether communication is process-local.
Definition Network.cpp:1137
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:718
A mini-server for network communication.
Definition Port.h:46
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition Port.cpp:395
bool read(PortReader &reader, bool willReply=false) override
Read an object from the port.
Definition Port.cpp:440
void close() override
Stop port activity.
Definition Port.cpp:330
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
A port that is specialized as an RPC client.
Definition RpcClient.h:22
A port that is specialized as an RPC server.
Definition RpcServer.h:23
bool read(PortReader &reader, bool willReply=true) override
Read an object from the port.
Definition RpcServer.cpp:49
A single value (typically within a Bottle).
Definition Value.h:44
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition Value.cpp:204
virtual std::string asString() const
Get string value.
Definition Value.cpp:240
void delay(double seconds)
Wait for a certain number of seconds.
Definition Time.cpp:111
@ YARP_CLOCK_SYSTEM
Definition Time.h:28
The main, catch-all namespace for YARP.
Definition dirs.h:16
TEST_CASE("pm::rpc_monitorTest", "[yarp::pm]")