// Copyright 2016 Open Source Robotics Foundation, Inc.
// Copyright 2021 Real-Time Innovations, Inc. (RTI)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <memory>

#include "rclcpp_dds/rclcpp_dds.hpp"

// Include type support code generated by rtiddsgen
#include "std_msgs/msg/String.hpp"

using std::placeholders::_1;

namespace rclcpp_dds_examples
{

class DdsListener : public rclcpp_dds::DDSNode
{
public:
  DdsListener()
  : DDSNode("dds_listener")
  {
    using std_msgs::msg::String;
    auto reader_qos = this->get_default_datareader_qos();
    reader_qos << dds::core::policy::Reliability::Reliable();
    reader_ = this->create_datareader<String>("chatter", reader_qos);
    this->set_data_callback<String>(
      reader_, [this](const String & msg)
      {
        RCLCPP_INFO(
          this->get_logger(),
          "I heard from Connext: [%s]", msg.data().c_str());
      });
  }

private:
  dds::sub::DataReader<std_msgs::msg::String> reader_{nullptr};
};

}  // namespace rclcpp_dds_examples

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<rclcpp_dds_examples::DdsListener>());
  rclcpp::shutdown();
  return 0;
}
