neighbor_comm.h
Go to the documentation of this file.
1 
23 #ifndef NEIGHBOR_COMM_H_
24 #define NEIGHBOR_COMM_H_
25 
26 #include <iostream>
27 
31 #include "gsdf_msgs/NeighborBroadcastKeyValue.h"
32 
33 namespace micros_swarm{
34 
35  template<class Type>
36  class Broadcaster{
37  public:
38  Broadcaster(const std::string& key)
39  {
40  key_ = key;
43  }
44 
46  {
47  rth_.reset();
48  mqm_.reset();
49  }
50 
51  void broadcast(const Type& value)
52  {
53  std::vector<uint8_t> value_vec = serialize_ros(value);
54  gsdf_msgs::NeighborBroadcastKeyValue nbkv;
55 
56  nbkv.key = key_;
57  nbkv.value = value_vec;
58  std::vector<uint8_t> nbkv_vec = serialize_ros(nbkv);
59 
60  gsdf_msgs::CommPacket p;
61  p.header.source = rth_->getRobotID();
62  p.header.type = NEIGHBOR_BROADCAST_KEY_VALUE;
63  p.header.data_len = nbkv_vec.size();
64  p.header.version = 1;
65  p.header.checksum = 0;
66  p.content.buf = nbkv_vec;
67  std::vector<uint8_t> msg_data = serialize_ros(p);
68  mqm_->getOutMsgQueue("nc")->push(msg_data);
69  }
70  private:
73  std::string key_;
74  };
75 
76  template<typename Type>
77  class Listener{
78  public:
79  Listener(const std::string& key, const boost::function<void(const Type&)>& callback)
80  {
81  key_ = key;
83 
84  helper_.reset(new ListenerHelperT<Type>(key, callback));
85  rth_->insertOrUpdateListenerHelper(key_, helper_);
86  }
87 
89  {
90  rth_.reset();
91  helper_.reset();
92  }
93 
94  void ignore()
95  {
96  rth_->deleteListenerHelper(key_);
97  }
98  private:
99  std::string key_;
102  };
103 };
104 #endif
boost::shared_ptr< micros_swarm::MsgQueueManager > mqm_
Definition: neighbor_comm.h:72
Listener(const std::string &key, const boost::function< void(const Type &)> &callback)
Definition: neighbor_comm.h:79
std::vector< uint8_t > serialize_ros(T t)
Definition: serialize.h:39
Broadcaster(const std::string &key)
Definition: neighbor_comm.h:38
static boost::shared_ptr< T > getSingleton()
Definition: singleton.h:70
boost::shared_ptr< ListenerHelper > helper_
boost::shared_ptr< RuntimeHandle > rth_
Definition: neighbor_comm.h:71
boost::shared_ptr< RuntimeHandle > rth_
void broadcast(const Type &value)
Definition: neighbor_comm.h:51


micros_swarm
Author(s):
autogenerated on Mon Jun 10 2019 14:02:06