msg_queue_manager.cpp
Go to the documentation of this file.
1 
24 
25 namespace micros_swarm{
26 
27  OutMsgQueue::OutMsgQueue(const std::string& name, int size, MsgQueueManager *manager_ptr): name_(name), size_(size), queue_manager_ptr_(manager_ptr)
28  {
29  queue_.reset(new cqueue<std::vector<uint8_t> >(size_));
30  }
31 
33  {
34  queue_.reset();
35  }
36 
38  {
39  boost::shared_lock<boost::shared_mutex> lock(mutex_);
40  return queue_->full();
41  }
42 
44  {
45  boost::shared_lock<boost::shared_mutex> lock(mutex_);
46  return queue_->empty();
47  }
48 
50  {
51  boost::shared_lock<boost::shared_mutex> lock(mutex_);
52  return queue_->size();
53  }
54 
55  const std::vector<uint8_t>& OutMsgQueue::front()
56  {
57  boost::shared_lock<boost::shared_mutex> lock(mutex_);
58  return queue_->front();
59  }
60 
62  {
63  boost::unique_lock<boost::shared_mutex> lock(mutex_);
64  queue_->pop();
65  }
66 
67  void OutMsgQueue::push(const std::vector<uint8_t>& msg)
68  {
69  boost::unique_lock<boost::shared_mutex> lock(mutex_);
70  queue_->push(msg);
72  }
73 
75  {
76  queue_map_.clear();
77  }
78 
80  {
81  std::map<std::string, OutMsgQueue*>::iterator it;
82  for(it = queue_map_.begin(); it != queue_map_.end(); it++) {
83  delete (it->second);
84  }
85  queue_map_.clear();
86  }
87 
88  void MsgQueueManager::createOutMsgQueue(std::string name, int size)
89  {
90  OutMsgQueue *queue = new OutMsgQueue(name, size, this);
91  queue_map_.insert(std::pair<std::string, OutMsgQueue*>(name, queue));
92  }
93 
95  {
96  std::map<std::string, OutMsgQueue*>::iterator it = queue_map_.find(name);
97  if(it != queue_map_.end()) {
98  return it->second;
99  }
100  else {
101  std::cout<<"get out msg queue "<<name<<" failed!"<<std::endl;
102  exit(-1);
103  }
104  }
105 
106  void MsgQueueManager::spinAllOutMsgQueueOnce(std::vector<std::vector<uint8_t> >& msg_vec)
107  {
108  boost::shared_lock<boost::shared_mutex> lock(msg_queue_mutex);
109  std::map<std::string, OutMsgQueue*>::iterator it;
110  for(it = queue_map_.begin(); it != queue_map_.end(); it++) {
111  std::string name = it->first;
112  OutMsgQueue *msg_queue = getOutMsgQueue(name);
113  if(name == "scds_pso") {
114  for(int i = 0; i < msg_queue->size(); i++) {
115  msg_vec.push_back(msg_queue->front());
116  msg_queue->pop();
117  }
118  }
119  else {
120  if (!msg_queue->empty()) {
121  msg_vec.push_back(msg_queue->front());
122  msg_queue->pop();
123  }
124  }
125  }
126  while(allOutMsgQueueEmpty()) {
127  msg_queue_condition.wait(lock);
128  }
129  }
130 
132  {
133  std::map<std::string, OutMsgQueue*>::iterator it;
134  for(it = queue_map_.begin(); it != queue_map_.end(); it++) {
135  if(!it->second->empty()) {
136  return false;
137  }
138  }
139  return true;
140  }
141 };
boost::condition_variable_any msg_queue_condition
OutMsgQueue(const std::string &name, int size, MsgQueueManager *manager_ptr)
void notify_one() BOOST_NOEXCEPT
MsgQueueManager * queue_manager_ptr_
boost::shared_mutex mutex_
OutMsgQueue * getOutMsgQueue(std::string name)
boost::shared_ptr< cqueue< std::vector< uint8_t > > > queue_
void createOutMsgQueue(std::string name, int size)
void push(const std::vector< uint8_t > &msg)
const std::vector< uint8_t > & front()
void spinAllOutMsgQueueOnce(std::vector< std::vector< uint8_t > > &msg_vec)


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