Program Listing for File graph_listener.hpp

Return to documentation for file (include/rclcpp/graph_listener.hpp)

// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// 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.

#ifndef RCLCPP__GRAPH_LISTENER_HPP_
#define RCLCPP__GRAPH_LISTENER_HPP_

#include <atomic>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>

#include "rcl/guard_condition.h"
#include "rcl/wait.h"
#include "rclcpp/context.hpp"
#include "rclcpp/guard_condition.hpp"
#include "rclcpp/macros.hpp"
#include "rclcpp/node_interfaces/node_graph_interface.hpp"
#include "rclcpp/visibility_control.hpp"

namespace rclcpp
{

namespace graph_listener
{

class GraphListenerShutdownError : public std::runtime_error
{
public:
  GraphListenerShutdownError()
  : std::runtime_error("GraphListener already shutdown") {}
};

class NodeAlreadyAddedError : public std::runtime_error
{
public:
  NodeAlreadyAddedError()
  : std::runtime_error("node already added") {}
};

class NodeNotFoundError : public std::runtime_error
{
public:
  NodeNotFoundError()
  : std::runtime_error("node not found") {}
};

class GraphListener : public std::enable_shared_from_this<GraphListener>
{
public:
  RCLCPP_PUBLIC
  explicit GraphListener(const rclcpp::Context::SharedPtr & parent_context);

  RCLCPP_PUBLIC
  virtual ~GraphListener();


  RCLCPP_PUBLIC
  virtual
  void
  start_if_not_started();


  RCLCPP_PUBLIC
  virtual
  void
  add_node(rclcpp::node_interfaces::NodeGraphInterface * node_graph);


  RCLCPP_PUBLIC
  virtual
  bool
  has_node(rclcpp::node_interfaces::NodeGraphInterface * node_graph);


  RCLCPP_PUBLIC
  virtual
  void
  remove_node(rclcpp::node_interfaces::NodeGraphInterface * node_graph);


  RCLCPP_PUBLIC
  virtual
  void
  shutdown();

  RCLCPP_PUBLIC
  virtual
  void
  shutdown(const std::nothrow_t &) noexcept;

  RCLCPP_PUBLIC
  virtual
  bool
  is_shutdown();

protected:
  RCLCPP_PUBLIC
  virtual
  void
  run();

  RCLCPP_PUBLIC
  virtual
  void
  run_loop();

  RCLCPP_PUBLIC
  void
  init_wait_set();

  RCLCPP_PUBLIC
  void
  cleanup_wait_set();

private:
  RCLCPP_DISABLE_COPY(GraphListener)


  void
  __shutdown();

  std::weak_ptr<rclcpp::Context> weak_parent_context_;
  std::shared_ptr<rcl_context_t> rcl_parent_context_;

  std::thread listener_thread_;
  bool is_started_;
  std::atomic_bool is_shutdown_;
  mutable std::mutex shutdown_mutex_;

  mutable std::mutex node_graph_interfaces_barrier_mutex_;
  mutable std::mutex node_graph_interfaces_mutex_;
  std::vector<rclcpp::node_interfaces::NodeGraphInterface *> node_graph_interfaces_;

  rclcpp::GuardCondition interrupt_guard_condition_;
  rcl_wait_set_t wait_set_ = rcl_get_zero_initialized_wait_set();
};

}  // namespace graph_listener
}  // namespace rclcpp

#endif  // RCLCPP__GRAPH_LISTENER_HPP_