@@ -7,6 +7,11 @@ using the request-response communication pattern. The client sends a request wit
7
7
two consecutive fibonacci numbers and the server responds with the next number in
8
8
the sequence.
9
9
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
+
10
15
## Expected output basic server-client example
11
16
12
17
[ ![ asciicast] ( https://asciinema.org/a/469913.svg )] ( https://asciinema.org/a/469913 )
@@ -42,17 +47,10 @@ iox::runtime::PoshRuntime::initRuntime(APP_NAME);
42
47
```
43
48
44
49
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
46
51
queue capacity or blocking behavior when the response queue is full or the server is too slow.
47
52
The `ClientOptions` are similar to `PublisherOptions`/`SubscriberOptions`.
48
53
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
-
56
54
<!--[geoffrey][iceoryx_examples/request_response/client_cxx_waitset.cpp][create waitset]-->
57
55
```cpp
58
56
iox::popo::WaitSet<> waitset;
@@ -69,30 +67,11 @@ waitset.attachState(client, iox::popo::ClientState::HAS_RESPONSE).or_else([](aut
69
67
```
70
68
71
69
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
73
71
it to the ` addend ` of the next request to send.
74
72
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));
82
73
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.
96
75
The request is a sample consisting of two numbers ` augend ` and ` addend ` that the server shall sum up.
97
76
Additionally, the sample is marked with a sequence id that is incremented before
98
77
every send cycle to ensure a correct ordering of the messages
@@ -115,7 +94,12 @@ client.loan()
115
94
.or_else([](auto& error) { std::cout << "Could not allocate Request! Error: " << error << std::endl; });
116
95
```
117
96
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()`
119
103
and extract the sequence id with `response.getResponseHeader().getSequenceId()`.
120
104
When the server response comes in the correct order, the received sum is re-used to
121
105
insert it to the `addend` of the next request to send.
@@ -221,9 +205,9 @@ iox::posix::waitForTerminationRequest();
221
205
```
222
206
223
207
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 ] -->
225
209
``` cpp
226
- iox::posix::waitForTerminationRequest ( );
210
+ listener.detachEvent(server, iox::popo::ServerEvent::REQUEST_RECEIVED );
227
211
```
228
212
229
213
<center >
0 commit comments