00001 00023 #ifndef NEIGHBOR_COMM_H_ 00024 #define NEIGHBOR_COMM_H_ 00025 00026 #include <iostream> 00027 00028 #include "micros_swarm/runtime_handle.h" 00029 #include "micros_swarm/listener_helper.h" 00030 #include "micros_swarm/msg_queue_manager.h" 00031 #include "gsdf_msgs/NeighborBroadcastKeyValue.h" 00032 00033 namespace micros_swarm{ 00034 00035 template<class Type> 00036 class Broadcaster{ 00037 public: 00038 Broadcaster(const std::string& key) 00039 { 00040 key_ = key; 00041 rth_ = Singleton<RuntimeHandle>::getSingleton(); 00042 mqm_ = Singleton<MsgQueueManager>::getSingleton(); 00043 } 00044 00045 ~Broadcaster() 00046 { 00047 rth_.reset(); 00048 mqm_.reset(); 00049 } 00050 00051 void broadcast(const Type& value) 00052 { 00053 std::vector<uint8_t> value_vec = serialize_ros(value); 00054 gsdf_msgs::NeighborBroadcastKeyValue nbkv; 00055 00056 nbkv.key = key_; 00057 nbkv.value = value_vec; 00058 std::vector<uint8_t> nbkv_vec = serialize_ros(nbkv); 00059 00060 gsdf_msgs::CommPacket p; 00061 p.header.source = rth_->getRobotID(); 00062 p.header.type = NEIGHBOR_BROADCAST_KEY_VALUE; 00063 p.header.data_len = nbkv_vec.size(); 00064 p.header.version = 1; 00065 p.header.checksum = 0; 00066 p.content.buf = nbkv_vec; 00067 std::vector<uint8_t> msg_data = serialize_ros(p); 00068 mqm_->getOutMsgQueue("nc")->push(msg_data); 00069 } 00070 private: 00071 boost::shared_ptr<RuntimeHandle> rth_; 00072 boost::shared_ptr<micros_swarm::MsgQueueManager> mqm_; 00073 std::string key_; 00074 }; 00075 00076 template<typename Type> 00077 class Listener{ 00078 public: 00079 Listener(const std::string& key, const boost::function<void(const Type&)>& callback) 00080 { 00081 key_ = key; 00082 rth_ = Singleton<RuntimeHandle>::getSingleton(); 00083 00084 helper_.reset(new ListenerHelperT<Type>(key, callback)); 00085 rth_->insertOrUpdateListenerHelper(key_, helper_); 00086 } 00087 00088 ~Listener() 00089 { 00090 rth_.reset(); 00091 helper_.reset(); 00092 } 00093 00094 void ignore() 00095 { 00096 rth_->deleteListenerHelper(key_); 00097 } 00098 private: 00099 std::string key_; 00100 boost::shared_ptr<RuntimeHandle> rth_; 00101 boost::shared_ptr<ListenerHelper> helper_; 00102 }; 00103 }; 00104 #endif