Skip to content

Commit

Permalink
iox-eclipse-iceoryx#751 Fix rpc c++ example by signalling waitset SIG…
Browse files Browse the repository at this point in the history
…TERM

Signed-off-by: Christian Eltzschig <me@elchris.org>
  • Loading branch information
elfenpiff committed Jun 23, 2022
1 parent 4866c4b commit 68b1ace
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
23 changes: 18 additions & 5 deletions iceoryx_examples/request_response/client_cxx_waitset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
//! [iceoryx includes]
#include "request_and_response_types.hpp"

#include "iceoryx_hoofs/posix_wrapper/signal_handler.hpp"
#include "iceoryx_hoofs/posix_wrapper/signal_watcher.hpp"
#include "iceoryx_posh/popo/client.hpp"
#include "iceoryx_posh/popo/wait_set.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
//! [iceoryx includes]

#include <atomic>
#include <iostream>

constexpr char APP_NAME[] = "iox-cpp-request-response-client-waitset";

std::atomic_bool keepRunning = {true};
iox::cxx::optional<iox::popo::WaitSet<>> waitset;

//! [context data to store Fibonacci numbers and sequence ids]
struct ContextData
Expand All @@ -38,16 +41,25 @@ struct ContextData
};
//! [context data to store Fibonacci numbers and sequence ids]

void signalHandler(int)
{
keepRunning = false;
waitset.and_then([&](auto& w) { w.markForDestruction(); });
}

int main()
{
auto sigTermGuard = iox::posix::registerSignalHandler(iox::posix::Signal::TERM, signalHandler);
auto sigIntGuard = iox::posix::registerSignalHandler(iox::posix::Signal::INT, signalHandler);

//! [initialize runtime]
iox::runtime::PoshRuntime::initRuntime(APP_NAME);
//! [initialize runtime]

ContextData ctx;

//! [create waitset]
iox::popo::WaitSet<> waitset;
waitset.emplace();

//! [create client]
iox::popo::ClientOptions options;
Expand All @@ -56,14 +68,14 @@ int main()
//! [create client]

// attach client to waitset
waitset.attachState(client, iox::popo::ClientState::HAS_RESPONSE).or_else([](auto) {
waitset->attachState(client, iox::popo::ClientState::HAS_RESPONSE).or_else([](auto) {
std::cerr << "failed to attach client" << std::endl;
std::exit(EXIT_FAILURE);
});
//! [create waitset]

//! [mainloop]
while (!iox::posix::hasTerminationRequested())
while (keepRunning)
{
//! [send request]
client.loan()
Expand All @@ -84,7 +96,7 @@ int main()

// We block and wait for samples to arrive, when the time is up we send the request again
//! [wait and check if the client triggered]
auto notificationVector = waitset.timedWait(iox::units::Duration::fromSeconds(5));
auto notificationVector = waitset->timedWait(iox::units::Duration::fromSeconds(5));

for (auto& notification : notificationVector)
{
Expand Down Expand Up @@ -117,6 +129,7 @@ int main()
}
//! [mainloop]

waitset.reset();
std::cout << "shutting down" << std::endl;

return (EXIT_SUCCESS);
Expand Down
9 changes: 3 additions & 6 deletions iceoryx_examples/request_response_in_c/client_c_waitset.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int main()
int64_t expectedResponseSequenceId = requestSequenceId;

//! [create waitset and attach client]
iox_ws_t waitset = iox_ws_init(&waitsetStorage);
waitset = iox_ws_init(&waitsetStorage);

if (iox_ws_attach_client_state(waitset, client, ClientState_HAS_RESPONSE, 0U, NULL) != WaitSetResult_SUCCESS)
{
Expand Down Expand Up @@ -136,11 +136,8 @@ int main()
}
//! [process responses]

if (keepRunning)
{
const uint32_t SLEEP_TIME_IN_MS = 950U;
sleep_for(SLEEP_TIME_IN_MS);
}
const uint32_t SLEEP_TIME_IN_MS = 950U;
sleep_for(SLEEP_TIME_IN_MS);
}

//! [cleanup]
Expand Down

0 comments on commit 68b1ace

Please sign in to comment.