YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
shmem_peer.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/*
7 * Helper process for the multi-process shmem tests (see shmem.cpp).
8 *
9 * Usage:
10 * shmem_peer <mode> <host> <rpc_port> <stream_port>
11 *
12 * It connects to the ports opened by the test process using the shmem
13 * carrier, bypassing the name server (the ports are addressed directly by
14 * host and port number).
15 *
16 * Parameters:
17 * <mode> what the peer does after connecting:
18 * normal: connects to both ports, performs rpc calls on
19 * <rpc_port> (checking each reply), streams
20 * messages to <stream_port>, then closes cleanly
21 * crash: connects only to <stream_port>, sends one
22 * message, then exits without closing anything,
23 * as if the process crashed
24 * <host> host name or IP address of the machine where the test
25 * process opened its ports (the host part of
26 * Contact::where(), e.g. "localhost")
27 * <rpc_port> TCP port number of the test's rpc server port, i.e. a
28 * yarp::os::Port with a reader that replies to every
29 * message with "echo" followed by the message itself.
30 * Ignored in crash mode (any value is accepted)
31 * <stream_port> TCP port number of the test's input port, i.e. a
32 * yarp::os::BufferedPort<Bottle> receiving the streamed
33 * messages
34 *
35 * Exit code:
36 * 0 success
37 * 1 wrong command line arguments
38 * 2 the ports could not be opened or connected
39 * 3 at least one rpc call or stream write failed (normal mode only)
40 */
41
42#include <yarp/os/Bottle.h>
43#include <yarp/os/Contact.h>
44#include <yarp/os/Network.h>
45#include <yarp/os/Port.h>
46#include <yarp/os/Time.h>
47
48#include <cstdio>
49#include <cstdlib>
50#include <string>
51
52using namespace yarp::os;
53
54namespace {
55
56constexpr int rpc_count = 30;
57constexpr int stream_count = 200;
58
59size_t rpcPayloadSize(int i)
60{
61 // Every 10 messages send a big one, to force the reallocation of the
62 // shared memory in both directions
63 return (i % 10 == 9) ? 2000000 : static_cast<size_t>(i) * 1000;
64}
65
66size_t streamPayloadSize(int i)
67{
68 return (i % 50 == 49) ? 500000 : 100;
69}
70
71bool connectTo(Contactable& port, const std::string& host, int portnum)
72{
73 Contact dest("/shmem/xproc/remote", "shmem", host, portnum);
74 for (int i = 0; i < 20; ++i) {
75 if (port.addOutput(dest)) {
76 return true;
77 }
78 Time::delay(0.1);
79 }
80 return false;
81}
82
83} // namespace
84
85int main(int argc, char* argv[])
86{
87 if (argc != 5) {
88 std::fprintf(stderr, "Usage: %s <normal|crash> <host> <rpc_port> <stream_port>\n", argv[0]);
89 return 1;
90 }
91
92 const std::string mode = argv[1];
93 const std::string host = argv[2];
94 const int rpc_port = std::atoi(argv[3]);
95 const int stream_port = std::atoi(argv[4]);
96
98 // Our ports are only used for output and are not reachable from the
99 // network (openFake), the remote ones are addressed directly, therefore no
100 // name server is needed
101 Network::setLocalMode(true);
102
103 if (mode == "crash") {
104 Port out;
105 if (!out.openFake("/shmem/xproc/peer/out") || !connectTo(out, host, stream_port)) {
106 std::fprintf(stderr, "PEER: cannot connect\n");
107 return 2;
108 }
109 Bottle b;
110 b.addString("bye");
111 out.write(b);
112 Time::delay(0.5);
113 // Exit without closing the ports, as if the process crashed
114 std::fflush(stdout);
115 std::_Exit(0);
116 }
117
118 if (mode != "normal") {
119 std::fprintf(stderr, "PEER: unknown mode %s\n", mode.c_str());
120 return 1;
121 }
122
123 Port client;
124 Port out;
125 if (!client.openFake("/shmem/xproc/peer/client") || !out.openFake("/shmem/xproc/peer/out")) {
126 std::fprintf(stderr, "PEER: cannot open ports\n");
127 return 2;
128 }
129 if (!connectTo(client, host, rpc_port) || !connectTo(out, host, stream_port)) {
130 std::fprintf(stderr, "PEER: cannot connect\n");
131 return 2;
132 }
133
134 int failures = 0;
135
136 for (int i = 0; i < rpc_count; ++i) {
137 const size_t len = rpcPayloadSize(i);
138 Bottle cmd;
139 Bottle reply;
140 cmd.addInt32(i);
141 cmd.addString(std::string(len, static_cast<char>('a' + i % 26)));
142 if (!client.write(cmd, reply)
143 || reply.size() != 3
144 || reply.get(0).asString() != "echo"
145 || reply.get(1).asInt32() != i
146 || reply.get(2).asString() != cmd.get(1).asString()) {
147 std::fprintf(stderr, "PEER: rpc %d failed\n", i);
148 ++failures;
149 }
150 }
151
152 for (int i = 0; i < stream_count; ++i) {
153 Bottle b;
154 b.addInt32(i);
155 b.addString(std::string(streamPayloadSize(i), 'z'));
156 if (!out.write(b)) {
157 std::fprintf(stderr, "PEER: stream write %d failed\n", i);
158 ++failures;
159 }
160 }
161
162 // Tell the test that the stream is over
163 Bottle cmd;
164 Bottle reply;
165 cmd.addString("done");
166 if (!client.write(cmd, reply)) {
167 std::fprintf(stderr, "PEER: final rpc failed\n");
168 ++failures;
169 }
170
171 out.close();
172 client.close();
173
174 std::printf("PEER: %d failures\n", failures);
175 return failures == 0 ? 0 : 3;
176}
FeatureMode mode
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
A mini-server for performing network communication in the background.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
An abstract port.
Definition Contactable.h:28
virtual bool addOutput(const std::string &name)=0
Add an output connection to the specified port.
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 openFake(const std::string &name)
Start port without making it accessible from the network.
Definition Port.cpp:74
void close() override
Stop port activity.
Definition Port.cpp:330
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
An interface to the operating system, including Port based communication.
The main, catch-all namespace for YARP.
Definition dirs.h:16
int main(int argc, char *argv[])