YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
shmem.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
6#include <yarp/os/Bottle.h>
9#include <yarp/os/Network.h>
10#include <yarp/os/Port.h>
11#include <yarp/os/PortReader.h>
12#include <yarp/os/Time.h>
13
14#include <chrono>
15#include <cstdlib>
16#include <future>
17#include <string>
18
19#if !defined(_WIN32)
20# include <sys/wait.h>
21#endif
22
23#include <catch2/catch_amalgamated.hpp>
24#include <harness.h>
25
26using namespace yarp::os;
27
28namespace {
29
30// Replies to each incoming Bottle with the same Bottle, prefixed by "echo"
31class EchoReader : public PortReader
32{
33public:
34 bool read(ConnectionReader& connection) override
35 {
36 Bottle in;
37 if (!in.read(connection)) {
38 return false;
39 }
40 Bottle out;
41 out.addString("echo");
42 out.append(in);
43 ConnectionWriter* writer = connection.getWriter();
44 if (writer != nullptr) {
45 out.write(*writer);
46 }
47 return true;
48 }
49};
50
51// Wait until the connection is reported (or give up after a timeout)
52bool waitConnection(const std::string& src, const std::string& dest, bool connected)
53{
54 for (int i = 0; i < 50; ++i) {
55 if (Network::isConnected(src, dest, "shmem", true) == connected) {
56 return true;
57 }
58 Time::delay(0.1);
59 }
60 return false;
61}
62
63// Run the shmem_peer helper in a different process, asynchronously.
64// The result is the exit code of the helper.
65std::future<int> runPeer(const std::string& mode, const Contact& rpc, const Contact& stream)
66{
67 std::string cmd = std::string("\"") + SHMEM_PEER_PATH + "\" " + mode + " " + rpc.getHost() + " "
68 + std::to_string(rpc.getPort()) + " " + std::to_string(stream.getPort());
69#if defined(_WIN32)
70 // cmd.exe strips the outer quotes
71 cmd = "\"" + cmd + "\"";
72#endif
73 return std::async(std::launch::async, [cmd]() {
74 int ret = std::system(cmd.c_str());
75#if !defined(_WIN32)
76 if (ret != -1 && WIFEXITED(ret)) {
77 ret = WEXITSTATUS(ret);
78 }
79#endif
80 return ret;
81 });
82}
83
84} // namespace
85
86TEST_CASE("carriers::shmem", "[carriers]")
87{
88 YARP_REQUIRE_PLUGIN("shmem", "carrier");
89
90 Network::setLocalMode(true);
91
92 SECTION("test small messages")
93 {
96 in.setStrict();
97
98 REQUIRE(in.open("/shmem/in"));
99 REQUIRE(out.open("/shmem/out"));
100 REQUIRE(Network::connect(out.getName(), in.getName(), "shmem"));
101 REQUIRE(waitConnection(out.getName(), in.getName(), true));
102
103 for (int i = 0; i < 100; ++i) {
104 Bottle& b = out.prepare();
105 b.clear();
106 b.addInt32(i);
107 b.addString("hello");
108 b.addFloat64(i * 0.5);
109 out.writeStrict();
110
111 Bottle* r = in.read();
112 REQUIRE(r != nullptr);
113 REQUIRE(r->size() == 3);
114 CHECK(r->get(0).asInt32() == i);
115 CHECK(r->get(1).asString() == "hello");
116 CHECK(r->get(2).asFloat64() == i * 0.5);
117 }
118
119 out.interrupt();
120 in.interrupt();
121 out.close();
122 in.close();
123 }
124
125 SECTION("test increasing message size (shared memory resize)")
126 {
129 in.setStrict();
130
131 REQUIRE(in.open("/shmem/in"));
132 REQUIRE(out.open("/shmem/out"));
133 REQUIRE(Network::connect(out.getName(), in.getName(), "shmem"));
134 REQUIRE(waitConnection(out.getName(), in.getName(), true));
135
136 // The default buffer is SHMEM_DEFAULT_SIZE (4096) bytes, larger
137 // messages force the shared memory to be reallocated
138 for (size_t len : {10, 1000, 4000, 5000, 20000, 100000, 1000000}) {
139 std::string payload(len, ' ');
140 for (size_t j = 0; j < len; ++j) {
141 payload[j] = static_cast<char>('a' + (j % 26));
142 }
143
144 Bottle& b = out.prepare();
145 b.clear();
146 b.addInt32(static_cast<int>(len));
148 out.writeStrict();
149
150 Bottle* r = in.read();
151 REQUIRE(r != nullptr);
152 REQUIRE(r->size() == 2);
153 CHECK(r->get(0).asInt32() == static_cast<int>(len));
154 CHECK(r->get(1).asString() == payload);
155 }
156
157 // Small messages must still work after the resize
158 Bottle& b = out.prepare();
159 b.clear();
160 b.addString("after resize");
161 out.writeStrict();
162 Bottle* r = in.read();
163 REQUIRE(r != nullptr);
164 CHECK(r->get(0).asString() == "after resize");
165
166 out.interrupt();
167 in.interrupt();
168 out.close();
169 in.close();
170 }
171
172 SECTION("test disconnect and reconnect")
173 {
176 in.setStrict();
177
178 REQUIRE(in.open("/shmem/in"));
179 REQUIRE(out.open("/shmem/out"));
180
181 for (int k = 0; k < 3; ++k) {
182 REQUIRE(Network::connect(out.getName(), in.getName(), "shmem"));
183 REQUIRE(waitConnection(out.getName(), in.getName(), true));
184
185 Bottle& b = out.prepare();
186 b.clear();
187 b.addInt32(k);
188 out.writeStrict();
189
190 Bottle* r = in.read();
191 REQUIRE(r != nullptr);
192 CHECK(r->get(0).asInt32() == k);
193
194 REQUIRE(Network::disconnect(out.getName(), in.getName()));
195 REQUIRE(waitConnection(out.getName(), in.getName(), false));
196 }
197
198 out.interrupt();
199 in.interrupt();
200 out.close();
201 in.close();
202 }
203
204 Network::setLocalMode(false);
205}
206
207TEST_CASE("carriers::shmem::rpc", "[carriers]")
208{
209 YARP_REQUIRE_PLUGIN("shmem", "carrier");
210
211 Network::setLocalMode(true);
212
213 Port server;
214 Port client;
215 EchoReader echo;
217
218 REQUIRE(server.open("/shmem/server"));
219 REQUIRE(client.open("/shmem/client"));
220 REQUIRE(Network::connect(client.getName(), server.getName(), "shmem"));
221 REQUIRE(waitConnection(client.getName(), server.getName(), true));
222
223 for (int i = 0; i < 20; ++i) {
224 Bottle cmd;
225 Bottle reply;
226 cmd.addInt32(i);
227 cmd.addString(std::string(static_cast<size_t>(i) * 1000, 'x'));
228 REQUIRE(client.write(cmd, reply));
229 REQUIRE(reply.size() == 3);
230 CHECK(reply.get(0).asString() == "echo");
231 CHECK(reply.get(1).asInt32() == i);
232 CHECK(reply.get(2).asString().size() == static_cast<size_t>(i) * 1000);
233 }
234
235 client.close();
236 server.close();
237
238 Network::setLocalMode(false);
239}
240
241// The tests above run both sides of the connection in the same process. These
242// ones run the other side in a different process (see shmem_peer.cpp), so
243// that the inter-process objects (shared memory, semaphores) are really shared
244// between two processes.
245TEST_CASE("carriers::shmem::multiprocess", "[carriers]")
246{
247 YARP_REQUIRE_PLUGIN("shmem", "carrier");
248
249 // The peer addresses the ports directly by host and port number, it does
250 // not need to access the name server
251 Network::setLocalMode(true);
252
253 SECTION("rpc and streaming with another process")
254 {
255 Port server;
256 EchoReader echo;
259 in.setStrict();
260
261 REQUIRE(server.open("/shmem/xproc/server"));
262 REQUIRE(in.open("/shmem/xproc/in"));
263
264 auto peer = runPeer("normal", server.where(), in.where());
265
266 // The peer performs the rpc calls (checking the replies) and then
267 // streams 200 messages
268 int received = 0;
269 size_t bytes = 0;
270 bool order_ok = true;
271 double start = Time::now();
272 while (received < 200 && Time::now() - start < 60.0) {
273 Bottle* b = in.read(false);
274 if (b == nullptr) {
275 if (peer.wait_for(std::chrono::seconds(0)) == std::future_status::ready && in.getPendingReads() == 0) {
276 break;
277 }
278 Time::delay(0.01);
279 continue;
280 }
281 if (b->get(0).asInt32() != received) {
282 order_ok = false;
283 }
284 bytes += b->get(1).asString().size();
285 ++received;
286 }
287
288 CHECK(peer.get() == 0);
289 CHECK(received == 200);
291 CHECK(bytes == 196 * 100 + 4 * 500000);
292
293 in.close();
294 server.close();
295 }
296
297 SECTION("the other process crashes")
298 {
300 in.setStrict();
301 REQUIRE(in.open("/shmem/xproc/in"));
302
303 auto peer = runPeer("crash", in.where(), in.where());
304
305 Bottle* b = in.read();
306 REQUIRE(b != nullptr);
307 CHECK(b->get(0).asString() == "bye");
308 CHECK(peer.get() == 0);
309
310 // The peer exited without closing the connection, this must be
311 // detected and the connection removed
312 bool removed = false;
313 for (int i = 0; i < 100 && !removed; ++i) {
314 removed = (in.getInputCount() == 0);
315 Time::delay(0.1);
316 }
317 CHECK(removed);
318
319 in.close();
320 }
321
322 Network::setLocalMode(false);
323}
FeatureMode mode
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
void append(const Bottle &alt)
Append the content of the given bottle to the current list.
Definition Bottle.cpp:354
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:252
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition Bottle.cpp:241
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
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:247
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition Bottle.cpp:231
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
A mini-server for performing network communication in the background.
Contact where() const override
Returns information about how this port can be reached.
std::string getName() const override
Get name of port.
void writeStrict()
Write the current object being returned by BufferedPort::prepare, waiting until any previous sends ar...
void close() override
Stop port activity.
void setReader(PortReader &reader) override
Set an external reader for port data.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
int getPendingReads() override
Get the number of objects ready to be read.
void interrupt() override
Interrupt any current reads or writes attached to the port.
int getInputCount() override
Determine how many connections are arriving into this port.
T * read(bool shouldWait=true) override
Read an available object from the port.
void setStrict(bool strict=true) override
Call this to strictly keep all messages, or allow old ones to be quietly dropped.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
An interface for reading from a network connection.
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
An interface for writing to a network connection.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
int getPort() const
Get the port number associated with this Contact for socket communication.
Definition Contact.cpp:239
virtual std::string getName() const
Get name of port.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition PortReader.h:24
virtual bool read(ConnectionReader &reader)=0
Read this object from a network connection.
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
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
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition Value.cpp:222
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
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121
void delay(double seconds)
Wait for a certain number of seconds.
Definition Time.cpp:111
An interface to the operating system, including Port based communication.
TEST_CASE("carriers::shmem", "[carriers]")
Definition shmem.cpp:86