Program Listing for File StateNotifier.hpp

Return to documentation for file (src/mimir/control/StateNotifier.hpp)

#pragma once

#include <memory>
#include <boost/log/trivial.hpp>
#include "mimir/FkinDds.hpp"

namespace mimir
{
  namespace detail
  {
    inline dds::pub::qos::DataWriterQos NotifierQos(const dds::pub::Publisher& p)
    {
      auto qos = p.default_datawriter_qos();
      qos << dds::core::policy::Durability::TransientLocal();
      // should TransientLocal be removed?
      return qos;
    }
  }

  namespace control
  {

    class StateNotifier
    {
    public:
      StateNotifier(
          const std::string& notifyTopicName,
          const std::string& notifyIdentifier,
          dds::pub::Publisher publisher) :
        m_notifyWriter(dds::core::null),
        m_identifier(notifyIdentifier)
      {
        try
        {
          m_notifyWriter = dds::pub::DataWriter<fkin::ProcessStateAutomaton>(
              publisher,
              dds::topic::Topic<fkin::ProcessStateAutomaton>(
                  publisher.participant(),
                  notifyTopicName),
              detail::NotifierQos(publisher));
        }
        catch (const dds::core::Exception& e) {
          BOOST_LOG_TRIVIAL(fatal) << "DDS exception: " + std::string(e.what());
          throw std::runtime_error("StateNotifier failed to construct");
        }
      }

      StateNotifier(const StateNotifier&) = delete;
      StateNotifier& operator=(const StateNotifier&) = delete;
      StateNotifier(StateNotifier&&) = default;
      StateNotifier& operator=(StateNotifier&&) = default;
      ~StateNotifier() = default;

      void NotifyState(fkin::ProcessStateKind state)
      {
        // Notify state
        auto notification = fkin::ProcessStateAutomaton(m_identifier, state);
        m_notifyWriter << notification;
      }
    private:

      StateNotifier() = delete;
      dds::pub::DataWriter<fkin::ProcessStateAutomaton> m_notifyWriter;
      std::string m_identifier;
    };


  }
}