diff --git a/include/ros_roah_rsbb.h b/include/ros_roah_rsbb.h index 02459f2..d7c07a6 100644 --- a/include/ros_roah_rsbb.h +++ b/include/ros_roah_rsbb.h @@ -21,10 +21,16 @@ #define __ROS_ROAH_RSBB_H__ #include +#include + +#include #include #include #include +#include +#include +#include #include "roah_rsbb.h" @@ -93,8 +99,213 @@ namespace roah_rsbb } }; - typedef PublicChannel RosPublicChannel; - typedef PrivateChannel RosPrivateChannel; + + + class CallbackItem + : public ros::CallbackInterface + { + boost::function f_; + + public: + CallbackItem (boost::function const& f) + : f_ (f) + { + } + + virtual CallResult call() + { + f_(); + return Success; + } + }; + + + + class RosPublicChannel + : public PublicChannel + { + boost::function) > rsbb_beacon_callback_; + boost::function) > robot_beacon_callback_; + boost::function) > tablet_beacon_callback_; + + void + receive_rsbb_beacon (boost::asio::ip::udp::endpoint& endpoint, + uint16_t comp_id, + uint16_t msg_type, + std::shared_ptr msg) + { + ROS_DEBUG_STREAM ("Received RoahRsbbBeacon from " << endpoint.address().to_string() + << ":" << endpoint.port() + << ", COMP_ID " << comp_id + << ", MSG_TYPE " << msg_type); + + if (rsbb_beacon_callback_) { + ros::getGlobalCallbackQueue()->addCallback (boost::make_shared (boost::bind (rsbb_beacon_callback_, endpoint, comp_id, msg_type, msg))); + } + } + + void + receive_robot_beacon (boost::asio::ip::udp::endpoint& endpoint, + uint16_t comp_id, + uint16_t msg_type, + std::shared_ptr msg) + { + ROS_DEBUG_STREAM ("Received RobotBeacon from " << endpoint.address().to_string() + << ":" << endpoint.port() + << ", COMP_ID " << comp_id + << ", MSG_TYPE " << msg_type + << ", team_name: " << msg->team_name() + << ", robot_name: " << msg->robot_name()); + + if (robot_beacon_callback_) { + ros::getGlobalCallbackQueue()->addCallback (boost::make_shared (boost::bind (robot_beacon_callback_, endpoint, comp_id, msg_type, msg))); + } + } + + void + receive_tablet_beacon (boost::asio::ip::udp::endpoint& endpoint, + uint16_t comp_id, + uint16_t msg_type, + std::shared_ptr msg) + { + ROS_DEBUG_STREAM ("Received TabletBeacon from " << endpoint.address().to_string() + << ":" << endpoint.port() + << ", COMP_ID " << comp_id + << ", MSG_TYPE " << msg_type); + + if (tablet_beacon_callback_) { + ros::getGlobalCallbackQueue()->addCallback (boost::make_shared (boost::bind (tablet_beacon_callback_, endpoint, comp_id, msg_type, msg))); + } + } + + public: + RosPublicChannel (std::string const& host, + unsigned short port) + : PublicChannel (host, port) + { + signal_rsbb_beacon_received().connect (boost::bind (&RosPublicChannel::receive_rsbb_beacon, this, _1, _2, _3, _4)); + signal_robot_beacon_received().connect (boost::bind (&RosPublicChannel::receive_robot_beacon, this, _1, _2, _3, _4)); + signal_tablet_beacon_received().connect (boost::bind (&RosPublicChannel::receive_tablet_beacon, this, _1, _2, _3, _4)); + } + + void + set_rsbb_beacon_callback (boost::function) > rsbb_beacon_callback) + { + rsbb_beacon_callback_ = rsbb_beacon_callback; + } + + template void + set_rsbb_beacon_callback (void (T::*callback) (boost::asio::ip::udp::endpoint, uint16_t, uint16_t, std::shared_ptr), + T* obj) + { + set_rsbb_beacon_callback (boost::bind (callback, obj, _1, _2, _3, _4)); + } + + void + set_robot_beacon_callback (boost::function) > robot_beacon_callback) + { + robot_beacon_callback_ = robot_beacon_callback; + } + + template void + set_robot_beacon_callback (void (T::*callback) (boost::asio::ip::udp::endpoint, uint16_t, uint16_t, std::shared_ptr), + T* obj) + { + set_robot_beacon_callback (boost::bind (callback, obj, _1, _2, _3, _4)); + } + + void + set_tablet_beacon_callback (boost::function) > tablet_beacon_callback) + { + tablet_beacon_callback_ = tablet_beacon_callback; + } + + template void + set_tablet_beacon_callback (void (T::*callback) (boost::asio::ip::udp::endpoint, uint16_t, uint16_t, std::shared_ptr), + T* obj) + { + set_tablet_beacon_callback (boost::bind (callback, obj, _1, _2, _3, _4)); + } + }; + + + + class RosPrivateChannel + : public PrivateChannel + { + boost::function) > benchmark_state_callback_; + boost::function) > robot_state_callback_; + + void + receive_benchmark_state (boost::asio::ip::udp::endpoint& endpoint, + uint16_t comp_id, + uint16_t msg_type, + std::shared_ptr msg) + { + ROS_DEBUG_STREAM ("Received BenchmarkState from " << endpoint.address().to_string() + << ":" << endpoint.port() + << ", COMP_ID " << comp_id + << ", MSG_TYPE " << msg_type + << ", benchmark_type: " << msg->benchmark_type()); + + if (benchmark_state_callback_) { + ros::getGlobalCallbackQueue()->addCallback (boost::make_shared (boost::bind (benchmark_state_callback_, endpoint, comp_id, msg_type, msg))); + } + } + + void + receive_robot_state (boost::asio::ip::udp::endpoint& endpoint, + uint16_t comp_id, + uint16_t msg_type, + std::shared_ptr msg) + { + ROS_DEBUG_STREAM ("Received RobotState from " << endpoint.address().to_string() + << ":" << endpoint.port() + << ", COMP_ID " << comp_id + << ", MSG_TYPE " << msg_type); + + if (robot_state_callback_) { + ros::getGlobalCallbackQueue()->addCallback (boost::make_shared (boost::bind (robot_state_callback_, endpoint, comp_id, msg_type, msg))); + } + } + + public: + RosPrivateChannel (std::string const& host, + unsigned short port, + std::string const& key, + std::string const& cypher) + : PrivateChannel (host, port, key, cypher) + { + signal_benchmark_state_received().connect (boost::bind (&RosPrivateChannel::receive_benchmark_state, this, _1, _2, _3, _4)); + signal_robot_state_received().connect (boost::bind (&RosPrivateChannel::receive_robot_state, this, _1, _2, _3, _4)); + } + + void + set_benchmark_state_callback (boost::function) > benchmark_state_callback) + { + benchmark_state_callback_ = benchmark_state_callback; + } + + template void + set_benchmark_state_callback (void (T::*callback) (boost::asio::ip::udp::endpoint, uint16_t, uint16_t, std::shared_ptr), + T* obj) + { + set_benchmark_state_callback (boost::bind (callback, obj, _1, _2, _3, _4)); + } + + void + set_robot_state_callback (boost::function) > robot_state_callback) + { + robot_state_callback_ = robot_state_callback; + } + + template void + set_robot_state_callback (void (T::*callback) (boost::asio::ip::udp::endpoint, uint16_t, uint16_t, std::shared_ptr), + T* obj) + { + set_robot_state_callback (boost::bind (callback, obj, _1, _2, _3, _4)); + } + }; }