|
| 1 | +#ifndef TMS_COMMON_CONFIGURABLE |
| 2 | +#define TMS_COMMON_CONFIGURABLE |
| 3 | + |
| 4 | +#include <dds/DCPS/ConfigStoreImpl.h> |
| 5 | +#include <dds/DCPS/InternalDataReaderListener.h> |
| 6 | +#include <dds/DCPS/Service_Participant.h> |
| 7 | + |
| 8 | +#include <mutex> |
| 9 | + |
| 10 | +class Configurable { |
| 11 | +public: |
| 12 | + using Mutex = std::recursive_mutex; |
| 13 | + using Guard = std::lock_guard<Mutex>; |
| 14 | + |
| 15 | + Configurable(const std::string& prefix) |
| 16 | + : config_prefix_(prefix) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + virtual ~Configurable() |
| 21 | + { |
| 22 | + Guard g(config_lock_); |
| 23 | + |
| 24 | + if (config_reader_) { |
| 25 | + TheServiceParticipant->config_topic()->disconnect(config_reader_); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + void setup_config() |
| 30 | + { |
| 31 | + Guard g(config_lock_); |
| 32 | + |
| 33 | + if (!config_reader_) { |
| 34 | + config_listener_ = OpenDDS::DCPS::make_rch<ConfigListener>(this, config_prefix_ + "_"); |
| 35 | + config_reader_ = OpenDDS::DCPS::make_rch<OpenDDS::DCPS::ConfigReader>( |
| 36 | + TheServiceParticipant->config_store()->datareader_qos(), config_listener_); |
| 37 | + TheServiceParticipant->config_topic()->connect(config_reader_); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + const std::string& config_prefix() const |
| 42 | + { |
| 43 | + return config_prefix_; |
| 44 | + } |
| 45 | + |
| 46 | + static bool convert_bool(const OpenDDS::DCPS::ConfigPair& pair, bool& value) |
| 47 | + { |
| 48 | + DDS::Boolean x = 0; |
| 49 | + if (pair.value() == "true") { |
| 50 | + value = true; |
| 51 | + return true; |
| 52 | + } else if (pair.value() == "false") { |
| 53 | + value = false; |
| 54 | + return true; |
| 55 | + } else if (OpenDDS::DCPS::convertToInteger(pair.value(), x)) { |
| 56 | + value = x; |
| 57 | + return true; |
| 58 | + } else { |
| 59 | + ACE_ERROR((LM_WARNING, "(%P|%t) WARNING: Configurable::convert_bool: failed to parse boolean for %C=%C\n", |
| 60 | + pair.key().c_str(), pair.value().c_str())); |
| 61 | + return false; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + virtual bool got_config(const std::string& name, const OpenDDS::DCPS::ConfigPair& pair) = 0; |
| 66 | + |
| 67 | +private: |
| 68 | + class ConfigListener : public OpenDDS::DCPS::ConfigListener { |
| 69 | + public: |
| 70 | + explicit ConfigListener(Configurable* configurable, const std::string& prefix) |
| 71 | + : OpenDDS::DCPS::ConfigListener(TheServiceParticipant->job_queue()) |
| 72 | + , configurable_(*configurable) |
| 73 | + , prefix_(prefix) |
| 74 | + { |
| 75 | + } |
| 76 | + |
| 77 | + void on_data_available(InternalDataReader_rch reader) override |
| 78 | + { |
| 79 | + OpenDDS::DCPS::ConfigReader::SampleSequence samples; |
| 80 | + OpenDDS::DCPS::InternalSampleInfoSequence infos; |
| 81 | + reader->read(samples, infos, DDS::LENGTH_UNLIMITED, |
| 82 | + DDS::NOT_READ_SAMPLE_STATE, DDS::ANY_VIEW_STATE, DDS::ANY_INSTANCE_STATE); |
| 83 | + for (size_t idx = 0; idx != samples.size(); ++idx) { |
| 84 | + const auto& info = infos[idx]; |
| 85 | + if (info.valid_data) { |
| 86 | + const auto& pair = samples[idx]; |
| 87 | + // Match key to prefix and pass rest of key as a short name |
| 88 | + if (pair.key().substr(0, prefix_.length()) == prefix_) { |
| 89 | + const auto name = pair.key().substr(prefix_.length()); |
| 90 | + if (!configurable_.got_config(name, pair)) { |
| 91 | + ACE_ERROR((LM_WARNING, "(%P|%t) WARNING: Configurable::ConfigListener::on_data_available: " |
| 92 | + "%C is not a valid property for %C\n", |
| 93 | + name.c_str(), configurable_.config_prefix().c_str())); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private: |
| 101 | + Configurable& configurable_; |
| 102 | + const std::string prefix_; |
| 103 | + }; |
| 104 | + |
| 105 | + mutable Mutex config_lock_; |
| 106 | + const std::string config_prefix_; |
| 107 | + OpenDDS::DCPS::RcHandle<OpenDDS::DCPS::ConfigReader> config_reader_; |
| 108 | + OpenDDS::DCPS::RcHandle<ConfigListener> config_listener_; |
| 109 | +}; |
| 110 | + |
| 111 | +#endif |
0 commit comments