Skip to content

Commit 213de72

Browse files
committed
iox-#27 Add example page for req/res example to website
Signed-off-by: Dietrich Krönke <[email protected]>
1 parent 61b13a8 commit 213de72

File tree

9 files changed

+27
-38
lines changed

9 files changed

+27
-38
lines changed

doc/website/examples/.pages

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ nav:
1414
- ice_access_control.md
1515
- iceperf.md
1616
- icecrystal.md
17+
- request_response.md
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: Communication pattern which delivers data on demand (Client/Server)
3+
---
4+
5+
{! ../iceoryx/iceoryx_examples/request_response/README.md !}

iceoryx_examples/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
|[waitset](./waitset/) | Waiting for events like arrival of data using C++ | :star::star: |
1414
|[waitset_in_c](./waitset_in_c/) | Waiting for events like arrival of data using C | :star::star: |
1515
|[iceensemble](./iceensemble/) | Using multiple publishers for one topic | :star::star: |
16-
|[request_response](./request_response/) | Communication pattern which delivers data on demand (Server/Client) | :star::star: |
16+
|[request_response](./request_response/) | Communication pattern which delivers data on demand (Client/Server) | :star::star: |
1717
|[singleprocess](./singleprocess/) | Communicating in a single process between threads | :star::star: |
1818
|[user_header](./user_header/) | Using a user-header for additional meta-information like timestamps | :star::star: |
1919
|[icediscovery](./icediscovery) | Searching for currently available services | :star::star: |

iceoryx_examples/request_response/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ target_compile_options(
4444
${ICEORYX_SANITIZER_FLAGS}
4545
)
4646

47-
## C++ typed API server
47+
## C++ untyped API server
4848
add_executable(
4949
iox-cpp-request-response-untyped-server
5050
server_cxx_untpyed.cpp

iceoryx_examples/request_response/README.md

+16-32
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ using the request-response communication pattern. The client sends a request wit
77
two consecutive fibonacci numbers and the server responds with the next number in
88
the sequence.
99

10+
We provide three examples, the very basic typed and untyped examples
11+
and the most natural setup combining a Server with a Listener and a Client using a Waitset.
12+
Since you can find the general setup and functionality of the client and the server
13+
also in the Listener/Waitset example, we will focus now on this.
14+
1015
## Expected output basic server-client example
1116

1217
[![asciicast](https://asciinema.org/a/469913.svg)](https://asciinema.org/a/469913)
@@ -42,17 +47,10 @@ iox::runtime::PoshRuntime::initRuntime(APP_NAME);
4247
```
4348
4449
After creating the runtime, the client port is created and attached to the Waitset.
45-
The `options` can be used to alter the behavior of the client, like setting the response
50+
The [options](https://iceoryx.io/latest/getting-started/examples/iceoptions/) can be used to alter the behavior of the client, like setting the response
4651
queue capacity or blocking behavior when the response queue is full or the server is too slow.
4752
The `ClientOptions` are similar to `PublisherOptions`/`SubscriberOptions`.
4853
49-
<!--[geoffrey][iceoryx_examples/request_response/client_cxx_waitset.cpp][create client]-->
50-
```cpp
51-
iox::popo::ClientOptions options;
52-
options.responseQueueCapacity = 2U;
53-
iox::popo::Client<AddRequest, AddResponse> client({"Example", "Request-Response", "Add"}, options);
54-
```
55-
5654
<!--[geoffrey][iceoryx_examples/request_response/client_cxx_waitset.cpp][create waitset]-->
5755
```cpp
5856
iox::popo::WaitSet<> waitset;
@@ -69,30 +67,11 @@ waitset.attachState(client, iox::popo::ClientState::HAS_RESPONSE).or_else([](aut
6967
```
7068

7169
The main goal of the client is to request from the server the sum of two numbers that the
72-
client sends. When to sum is received from the server, the received sum is re-used to insert
70+
client sends. When the sum is received from the server, the received sum is re-used to insert
7371
it to the `addend` of the next request to send.
7472
This calculates a Fibonacci sequence.
75-
<!-- [geoffrey] [iceoryx_examples/request_response/client_cxx_waitset.cpp] [[mainloop]] -->
76-
```cpp
77-
while (!iox::posix::hasTerminationRequested())
78-
{
79-
// ...
80-
// We block and wait for samples to arrive, when the time is up we send the request again
81-
auto notificationVector = waitset.timedWait(iox::units::Duration::fromSeconds(5));
8273

83-
for (auto& notification : notificationVector)
84-
{
85-
if (notification->doesOriginateFrom(&client))
86-
{
87-
// ...
88-
}
89-
}
90-
constexpr std::chrono::milliseconds SLEEP_TIME{950U};
91-
std::this_thread::sleep_for(SLEEP_TIME);
92-
}
93-
```
94-
95-
In the main loop, the client prepares a request using the `loan()` API.
74+
In the main loop, the client prepares first a request using the `loan()` API.
9675
The request is a sample consisting of two numbers `augend` and `addend` that the server shall sum up.
9776
Additionally, the sample is marked with a sequence id that is incremented before
9877
every send cycle to ensure a correct ordering of the messages
@@ -115,7 +94,12 @@ client.loan()
11594
.or_else([](auto& error) { std::cout << "Could not allocate Request! Error: " << error << std::endl; });
11695
```
11796
118-
In the same while loop the client receives the responses from the server using `take()`
97+
Once the request is send, the client do the following:
98+
99+
- wait for samples to arrive via timedWait
100+
- iterate over the notification vector to check if we were triggered from our client
101+
102+
The client receives the responses from the server using `take()`
119103
and extract the sequence id with `response.getResponseHeader().getSequenceId()`.
120104
When the server response comes in the correct order, the received sum is re-used to
121105
insert it to the `addend` of the next request to send.
@@ -221,9 +205,9 @@ iox::posix::waitForTerminationRequest();
221205
```
222206

223207
Once the user wants to shutdown the server, the server event is detached from the listener:
224-
<!-- [geoffrey] [iceoryx_examples/request_response/server_cxx_listener.cpp][wait for termination] -->
208+
<!-- [geoffrey] [iceoryx_examples/request_response/server_cxx_listener.cpp][cleanup] -->
225209
```cpp
226-
iox::posix::waitForTerminationRequest();
210+
listener.detachEvent(server, iox::popo::ServerEvent::REQUEST_RECEIVED);
227211
```
228212

229213
<center>

iceoryx_examples/request_response/client_cxx_untyped.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int main()
4343
while (!iox::posix::hasTerminationRequested())
4444
{
4545
//! [send request]
46-
client.loan(sizeof(AddRequest), alignof(AddResponse))
46+
client.loan(sizeof(AddRequest), alignof(AddRequest))
4747
.and_then([&](auto& requestPayload) {
4848
auto requestHeader = iox::popo::RequestHeader::fromPayload(requestPayload);
4949
requestHeader->setSequenceId(requestSequenceId);

iceoryx_examples/request_response/server_cxx_listener.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include "iceoryx_hoofs/posix_wrapper/signal_watcher.hpp"
2121
#include "iceoryx_posh/popo/listener.hpp"
22-
#include "iceoryx_posh/popo/notification_callback.hpp"
2322
#include "iceoryx_posh/popo/server.hpp"
2423
#include "iceoryx_posh/runtime/posh_runtime.hpp"
2524
//! [iceoryx includes]

iceoryx_integrationtest/iceoryx_integrationtest/test_request_response_listener.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
import pytest
2828

29-
# @brief Test goal: "Integrationtest for the request response basic example of iceoryx"
29+
# @brief Test goal: "Integrationtest for the request response listener and waitset example of iceoryx"
3030
# @pre setup ROS2 launch executables for RouDi (debug mode) and the example processes
3131
# @post check if all applications return exitcode 0 (success) after test run
3232
@pytest.mark.launch_test

iceoryx_integrationtest/iceoryx_integrationtest/test_request_response_untyped.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
import pytest
2828

29-
# @brief Test goal: "Integrationtest for the request response basic example of iceoryx"
29+
# @brief Test goal: "Integrationtest for the request response untyped example of iceoryx"
3030
# @pre setup ROS2 launch executables for RouDi (debug mode) and the example processes
3131
# @post check if all applications return exitcode 0 (success) after test run
3232
@pytest.mark.launch_test

0 commit comments

Comments
 (0)