|
| 1 | +/* |
| 2 | + * (c) Copyright, Real-Time Innovations, 2023. All rights reserved. |
| 3 | + * RTI grants Licensee a license to use, modify, compile, and create derivative |
| 4 | + * works of the software solely for use with RTI Connext DDS. Licensee may |
| 5 | + * redistribute copies of the software provided that all such copies are subject |
| 6 | + * to this license. The software is provided "as is", with no warranty of any |
| 7 | + * type, including any warranty for fitness for any purpose. RTI is under no |
| 8 | + * obligation to maintain or support the software. RTI shall not be liable for |
| 9 | + * any incidental or consequential damages arising out of the use or inability |
| 10 | + * to use the software. |
| 11 | + */ |
| 12 | + |
| 13 | +#include <algorithm> |
| 14 | +#include <iostream> |
| 15 | + |
| 16 | +#include <dds/sub/ddssub.hpp> |
| 17 | +#include <dds/core/ddscore.hpp> |
| 18 | +#include <rti/config/Logger.hpp> // for logging |
| 19 | + |
| 20 | +#include "CDS.hpp" |
| 21 | +#include "application.hpp" // for command line parsing and ctrl-c |
| 22 | + |
| 23 | +void process_data(dds::sub::DataReader<Example> reader) |
| 24 | +{ |
| 25 | + // Take all samples |
| 26 | + dds::sub::LoanedSamples<Example> samples = reader.take(); |
| 27 | + for (auto sample : samples) { |
| 28 | + if (sample.info().valid()) { |
| 29 | + std::cout << sample.data() << std::endl; |
| 30 | + } else { |
| 31 | + std::cout << "Instance state changed to " |
| 32 | + << sample.info().state().instance_state() << std::endl; |
| 33 | + } |
| 34 | + } |
| 35 | +} // The LoanedSamples destructor returns the loan |
| 36 | + |
| 37 | +void run_subscriber_application( |
| 38 | + unsigned int domain_id, |
| 39 | + unsigned int sample_count) |
| 40 | +{ |
| 41 | + // DDS objects behave like shared pointers or value types |
| 42 | + // (see |
| 43 | + // https://community.rti.com/best-practices/use-modern-c-types-correctly) |
| 44 | + |
| 45 | + // Start communicating in a domain, usually one participant per application |
| 46 | + dds::domain::DomainParticipant participant( |
| 47 | + domain_id, |
| 48 | + dds::core::QosProvider::Default().participant_qos( |
| 49 | + "lite_library::lite_peer")); |
| 50 | + |
| 51 | + // Create a Topic with a name and a datatype |
| 52 | + dds::topic::Topic<Example> topic(participant, "CDS LWS Example"); |
| 53 | + |
| 54 | + // Create a Subscriber and DataReader with default Qos |
| 55 | + dds::sub::Subscriber subscriber(participant); |
| 56 | + dds::sub::DataReader<Example> reader(subscriber, topic); |
| 57 | + |
| 58 | + // Create a ReadCondition for any data received on this reader and set a |
| 59 | + // handler to process the data |
| 60 | + dds::sub::cond::ReadCondition read_condition( |
| 61 | + reader, |
| 62 | + dds::sub::status::DataState::any(), |
| 63 | + [reader]() { process_data(reader); }); |
| 64 | + |
| 65 | + // WaitSet will be woken when the attached condition is triggered |
| 66 | + dds::core::cond::WaitSet waitset; |
| 67 | + waitset += read_condition; |
| 68 | + |
| 69 | + while (!application::shutdown_requested) { |
| 70 | + std::cout << "Example subscriber sleeping up to 1 sec..." << std::endl; |
| 71 | + |
| 72 | + // Run the handlers of the active conditions. Wait for up to 1 second. |
| 73 | + waitset.dispatch(dds::core::Duration(1)); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +int main(int argc, char *argv[]) |
| 78 | +{ |
| 79 | + using namespace application; |
| 80 | + |
| 81 | + // Parse arguments and handle control-C |
| 82 | + auto arguments = parse_arguments(argc, argv); |
| 83 | + if (arguments.parse_result == ParseReturn::exit) { |
| 84 | + return EXIT_SUCCESS; |
| 85 | + } else if (arguments.parse_result == ParseReturn::failure) { |
| 86 | + return EXIT_FAILURE; |
| 87 | + } |
| 88 | + setup_signal_handlers(); |
| 89 | + |
| 90 | + // Sets Connext verbosity to help debugging |
| 91 | + rti::config::Logger::instance().verbosity(arguments.verbosity); |
| 92 | + |
| 93 | + try { |
| 94 | + run_subscriber_application(arguments.domain_id, arguments.sample_count); |
| 95 | + } catch (const std::exception &ex) { |
| 96 | + // This will catch DDS exceptions |
| 97 | + std::cerr << "Exception in run_subscriber_application(): " << ex.what() |
| 98 | + << std::endl; |
| 99 | + return EXIT_FAILURE; |
| 100 | + } |
| 101 | + |
| 102 | + // Releases the memory used by the participant factory. Optional at |
| 103 | + // application exit |
| 104 | + dds::domain::DomainParticipant::finalize_participant_factory(); |
| 105 | + |
| 106 | + return EXIT_SUCCESS; |
| 107 | +} |
0 commit comments