Skip to content

Commit

Permalink
iox-eclipse-iceoryx#2251 Use loop to take all samples in the listener…
Browse files Browse the repository at this point in the history
… examples
  • Loading branch information
elBoberido committed Apr 14, 2024
1 parent 829578f commit c41adf0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
18 changes: 12 additions & 6 deletions iceoryx_examples/callbacks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,17 @@ void heartbeatCallback(iox::popo::UserTrigger*)
}
```
The `onSampleReceivedCallback` is more complex. We first acquire the received
sample and check which subscriber signaled the event by acquiring the subscriber's
The `onSampleReceivedCallback` is more complex. We first acquires all the received
samples and checks which subscriber signaled the event by acquiring the subscriber's
service description. If the instance is equal to `FrontLeft` we store the sample
in the `leftCache` otherwise in the `rightCache`.
<!--[geoffrey][iceoryx_examples/callbacks/ice_callbacks_subscriber.cpp][[subscriber callback][get data]]-->
```cpp
void onSampleReceivedCallback(iox::popo::Subscriber<CounterTopic>* subscriber)
{
subscriber->take().and_then([subscriber](auto& sample) {
// take all samples from the subscriber queue
while (subscriber->take().and_then([subscriber](auto& sample) {
auto instanceString = subscriber->getServiceDescription().getInstanceIDString();
// store the sample in the corresponding cache
Expand All @@ -188,7 +189,9 @@ void onSampleReceivedCallback(iox::popo::Subscriber<CounterTopic>* subscriber)
}
std::cout << "received: " << sample->counter << std::endl;
});
}))
{
}
// ...
}
```
Expand Down Expand Up @@ -304,7 +307,8 @@ argument, the pointer to the object itself, called `self`.
```cpp
static void onSampleReceivedCallback(iox::popo::Subscriber<CounterTopic>* subscriber, CounterService* self)
{
subscriber->take().and_then([subscriber, self](auto& sample) {
// take all samples from the subscriber queue
while (subscriber->take().and_then([subscriber, self](auto& sample) {
auto instanceString = subscriber->getServiceDescription().getInstanceIDString();
// store the sample in the corresponding cache
Expand All @@ -318,7 +322,9 @@ static void onSampleReceivedCallback(iox::popo::Subscriber<CounterTopic>* subscr
}
std::cout << "received: " << sample->counter << std::endl;
});
}))
{
}
// if both caches are filled we can process them
if (self->m_leftCache && self->m_rightCache)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class CounterService
//! [callback]
static void onSampleReceivedCallback(iox::popo::Subscriber<CounterTopic>* subscriber, CounterService* self)
{
subscriber->take().and_then([subscriber, self](auto& sample) {
// take all samples from the subscriber queue
while (subscriber->take().and_then([subscriber, self](auto& sample) {
auto instanceString = subscriber->getServiceDescription().getInstanceIDString();

// store the sample in the corresponding cache
Expand All @@ -82,7 +83,9 @@ class CounterService
}

std::cout << "received: " << sample->counter << std::endl;
});
}))
{
}

// if both caches are filled we can process them
if (self->m_leftCache && self->m_rightCache)
Expand Down
7 changes: 5 additions & 2 deletions iceoryx_examples/callbacks/ice_callbacks_subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ void heartbeatCallback(iox::popo::UserTrigger*)
void onSampleReceivedCallback(iox::popo::Subscriber<CounterTopic>* subscriber)
{
//! [get data]
subscriber->take().and_then([subscriber](auto& sample) {
// take all samples from the subscriber queue
while (subscriber->take().and_then([subscriber](auto& sample) {
auto instanceString = subscriber->getServiceDescription().getInstanceIDString();

// store the sample in the corresponding cache
Expand All @@ -56,7 +57,9 @@ void onSampleReceivedCallback(iox::popo::Subscriber<CounterTopic>* subscriber)
}

std::cout << "received: " << sample->counter << std::endl;
});
}))
{
}
//! [get data]

//! [process data]
Expand Down
7 changes: 4 additions & 3 deletions iceoryx_examples/callbacks_in_c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,18 @@ void heartbeatCallback(iox_user_trigger_t userTrigger)
}
```
The `onSampleReceivedCallback` is a little bit more complex. First we acquire
the chunk and then we have to find out which subscriber received the chunk. For that
The `onSampleReceivedCallback` is a little bit more complex. First we acquire all
the chunks and also have to find out which subscriber received the chunk. For that
we acquire the service description of the subscriber and if its instance equals
`FrontLeft` we store the chunk value in the `leftCache` otherwise in the `rightCache`.
<!--[geoffrey][iceoryx_examples/callbacks_in_c/ice_c_callbacks_subscriber.c][[subscriber callback][get data]]-->
```c
void onSampleReceivedCallback(iox_sub_t subscriber)
{
// take all samples from the subscriber queue
const struct CounterTopic* userPayload;
if (iox_sub_take_chunk(subscriber, (const void**)&userPayload) == ChunkReceiveResult_SUCCESS)
while (iox_sub_take_chunk(subscriber, (const void**)&userPayload) == ChunkReceiveResult_SUCCESS)
{
iox_service_description_t serviceDescription = iox_sub_get_service_description(subscriber);
if (strcmp(serviceDescription.instanceString, "FrontLeft") == 0)
Expand Down
3 changes: 2 additions & 1 deletion iceoryx_examples/callbacks_in_c/ice_c_callbacks_subscriber.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ void* cyclicHeartbeatTrigger(void* dontCare)
void onSampleReceivedCallback(iox_sub_t subscriber)
{
//! [get data]
// take all samples from the subscriber queue
const struct CounterTopic* userPayload;
if (iox_sub_take_chunk(subscriber, (const void**)&userPayload) == ChunkReceiveResult_SUCCESS)
while (iox_sub_take_chunk(subscriber, (const void**)&userPayload) == ChunkReceiveResult_SUCCESS)
{
iox_service_description_t serviceDescription = iox_sub_get_service_description(subscriber);
if (strcmp(serviceDescription.instanceString, "FrontLeft") == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ void onSampleReceivedCallback(iox_sub_t subscriber, void* contextData)

//! [get data]
const struct CounterTopic* userPayload;
if (iox_sub_take_chunk(subscriber, (const void**)&userPayload) == ChunkReceiveResult_SUCCESS)
// take all samples from the subscriber queue
while (iox_sub_take_chunk(subscriber, (const void**)&userPayload) == ChunkReceiveResult_SUCCESS)
{
iox_service_description_t serviceDescription = iox_sub_get_service_description(subscriber);
if (strcmp(serviceDescription.instanceString, "FrontLeft") == 0)
Expand Down
2 changes: 2 additions & 0 deletions iceoryx_examples/ice_access_control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ Do the following to configure shared memory segments when building a custom RouD
<!--[geoffrey][iceoryx_examples/ice_access_control/roudi_main_static_segments.cpp][config]-->
```cpp
iox::IceoryxConfig config;
static_cast<iox::config::RouDiConfig&>(config) = cmdLineArgs.value().roudiConfig;


// Create Mempool Config
iox::mepoo::MePooConfig mepooConfig;
Expand Down

0 comments on commit c41adf0

Please sign in to comment.