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
16
17#include <array>
18#include <thread>
19
20#include <catch2/catch_amalgamated.hpp>
21#include <harness.h>
22
23TEST_CASE("pm::rpc_monitorTest", "[yarp::pm]")
24{
25 YARP_REQUIRE_PLUGIN("rpc_monitor", "portmonitor")
26
28
31
32 struct TestCase {
33 std::string carrier;
34 bool use_monitor;
35 };
36
37 auto tc = GENERATE(
38 TestCase {"fast_tcp", false},
39 TestCase {"fast_tcp+send.portmonitor+file.rpc_monitor+monitor_name./monitor_data_receiver+type.dll", true}
40 );
41
42 SECTION("Quick Test RPC monitoring - monitor = " + std::string(tc.use_monitor ? "enabled" : "disabled"))
43 {
44 yarp::os::Port sender;
45 yarp::os::Port receiver;
46 yarp::os::Port monitorport;
47
48 sender.open("/send");
49 receiver.open("/recv");
50 if (tc.use_monitor) {
51
52 monitorport.open("/monitor_data_receiver");
53 }
54
55 yarp::os::Network::connect("/send", "/recv", tc.carrier);
56
57 sender.write(yarp::os::Value(100));
58
60
61 yarp::os::Value rval;
62 receiver.read(rval);
63 CHECK(rval.asInt32()==100);
64
65 receiver.close();
66 sender.close();
67 if (tc.use_monitor) {
68 monitorport.close();
69 }
70 }
71
72 SECTION("Advanced Test RPC monitoring - monitor = " + std::string(tc.use_monitor ? "enabled" : "disabled"))
73 {
76 yarp::os::Port monitor;
77
78 client.open("/rpc_client");
79 server.open("/rpc_server");
80
81 if (tc.use_monitor) {
82 monitor.open("/monitor_data_receiver");
83 }
84
85 bool b = yarp::os::Network::connect("/rpc_client", "/rpc_server", tc.carrier);
86 REQUIRE(b);
87
89
90 // Send RPC command in a separate thread
91 std::thread server_thread([&server]() {
93 yarp::os::Bottle reply;
94 server.read(cmd, true);
95 reply.addString("response");
96 reply.addInt32(42);
97 server.reply(reply);
98 });
99
100 // Send command from client
102 cmd.addString("test_command");
103 cmd.addInt32(123);
104
105 yarp::os::Bottle reply;
106 client.write(cmd, reply);
107
109
110 server_thread.join();
111
112 // Verify the RPC worked
113 REQUIRE(reply.size() == 2);
114 CHECK(reply.get(0).asString() == "response");
115 CHECK(reply.get(1).asInt32() == 42);
116
117 if (tc.use_monitor) {
118 // Check if monitor received the command/reply
120
121 // The monitor port should have received messages
122 // Note: In actual implementation, you would check monitor.getPendingReads()
123 // but this basic test just verifies the connection works
124 CHECK(true); // Placeholder for actual monitor verification
125 }
126
127 monitor.close();
128 server.close();
129 client.close();
130 }
131
133}
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:65
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 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 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 setLocalMode(bool flag)
Chooses whether communication is process-local.
Definition Network.cpp:1118
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:210
virtual std::string asString() const
Get string value.
Definition Value.cpp:246
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]")