runtime_handle.cpp
Go to the documentation of this file.
1 
24 
25 namespace micros_swarm{
26 
28  {
29  robot_id_ = 0;
30  robot_base_ = Base(0,0,0,0,0,0,-1);
31  neighbors_.clear();
32  swarms_.clear();
33  neighbor_swarms_.clear();
34  virtual_stigmergy_.clear();
35  blackboard_.clear();
36  listener_helpers_.clear();
37  listener_helpers_.insert(std::pair<std::string, boost::shared_ptr<ListenerHelper> >("" , NULL));
38  barrier_.clear();
39  scds_pso_tuple_.clear();
40  }
41 
43  {
44  boost::shared_lock<boost::shared_mutex> lock(id_mutex_);
45  return robot_id_;
46  }
47 
48  void RuntimeHandle::setRobotID(int robot_id)
49  {
50  boost::unique_lock<boost::shared_mutex> lock(id_mutex_);
51  robot_id_ = robot_id;
52  }
53 
55  {
56  boost::shared_lock<boost::shared_mutex> lock(type_mutex_);
57  return robot_type_;
58  }
59 
60  void RuntimeHandle::setRobotType(int robot_type)
61  {
62  boost::unique_lock<boost::shared_mutex> lock(type_mutex_);
63  robot_type_ = robot_type;
64  }
65 
67  {
68  boost::shared_lock<boost::shared_mutex> lock(status_mutex_);
69  return robot_status_;
70  }
71 
72  void RuntimeHandle::setRobotStatus(int robot_status)
73  {
74  boost::unique_lock<boost::shared_mutex> lock(status_mutex_);
75  robot_status_ = robot_status;
76  }
77 
79  {
80  boost::shared_lock<boost::shared_mutex> lock(base_mutex_);
81  return robot_base_;
82  }
83 
84  void RuntimeHandle::setRobotBase(const Base& robot_base)
85  {
86  boost::unique_lock<boost::shared_mutex> lock(base_mutex_);
87  robot_base_ = robot_base;
88  if(robot_base.valid == -1) {
89  robot_base_.valid = 1;
90  }
91  }
92 
94  {
95  boost::shared_lock<boost::shared_mutex> lock(base_mutex_);
96  std::cout<<"robot base: "<<robot_base_.x<<", "<<robot_base_.y<<", "<<\
97  robot_base_.z<<", "<<robot_base_.vx<<", "<<robot_base_.vy<<", "<<\
98  robot_base_.vz<<std::endl;
99  }
100 
101  void RuntimeHandle::getNeighbors(std::map<int, NeighborBase>& neighbors)
102  {
103  boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
104  neighbors = neighbors_;
105  }
106 
108  {
109  boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
110  std::map<int, NeighborBase>::iterator n_it = neighbors_.find(robot_id);
111 
112  if(n_it != neighbors_.end()) {
113  nb = n_it->second;
114  return true;
115  }
116 
117  return false;
118  }
119 
121  {
122  boost::upgrade_lock<boost::shared_mutex> lock(neighbor_mutex_);
123  if(neighbors_.size() > 0) {
124  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
125  neighbors_.clear();
126  }
127  }
128 
129  std::map<int, NeighborBase> RuntimeHandle::getNeighbors()
130  {
131  boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
132  return neighbors_;
133  };
134 
136  {
137  boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
138  return neighbors_.size();
139  }
140 
141  void RuntimeHandle::insertOrUpdateNeighbor(int robot_id, float distance, float azimuth, float elevation, float x, float y, float z, float vx, float vy, float vz)
142  {
143  boost::upgrade_lock<boost::shared_mutex> lock(neighbor_mutex_);
144  std::map<int, NeighborBase>::iterator n_it = neighbors_.find(robot_id);
145 
146  if(n_it != neighbors_.end()) {
147  NeighborBase new_neighbor_base(distance, azimuth, elevation, x, y, z,vx, vy, vz);
148  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
149  n_it->second = new_neighbor_base;
150  }
151  else {
152  NeighborBase new_neighbor_base(distance, azimuth, elevation, x, y, z, vx, vy, vz);
153  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
154  neighbors_.insert(std::pair<int, NeighborBase>(robot_id ,new_neighbor_base));
155  }
156  }
157 
158  void RuntimeHandle::deleteNeighbor(int robot_id)
159  {
160  boost::unique_lock<boost::shared_mutex> lock(neighbor_mutex_);
161  neighbors_.erase(robot_id);
162  }
163 
164  bool RuntimeHandle::inNeighbors(int robot_id)
165  {
166  boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
167  std::map<int, NeighborBase>::iterator n_it = neighbors_.find(robot_id);
168 
169  if(n_it != neighbors_.end()) {
170  return true;
171  }
172 
173  return false;
174  }
175 
177  {
178  std::map<int, NeighborBase>::iterator n_it;
179  boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
180  for (n_it = neighbors_.begin(); n_it != neighbors_.end(); n_it++) {
181  std::cout<<n_it->first<<": ";
182  std::cout<<n_it->second.distance<<","<<n_it->second.azimuth<<","<<n_it->second.elevation<<","<<\
183  n_it->second.x<<","<<n_it->second.y<<","<<n_it->second.z<<", "<<
184  n_it->second.vx<<","<<n_it->second.vy<<","<<n_it->second.vz;
185  std::cout<<std::endl;
186  }
187  }
188 
189  void RuntimeHandle::insertOrUpdateSwarm(int swarm_id, bool value)
190  {
191  boost::upgrade_lock<boost::shared_mutex> lock(swarm_mutex_);
192  std::map<int, bool>::iterator s_it = swarms_.find(swarm_id);
193 
194  if(s_it != swarms_.end()) {
195  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
196  s_it->second = value;
197  }
198  else {
199  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
200  swarms_.insert(std::pair<int, bool>(swarm_id, value));
201  }
202  }
203 
204  bool RuntimeHandle::getSwarmFlag(int swarm_id)
205  {
206  boost::shared_lock<boost::shared_mutex> lock(swarm_mutex_);
207  std::map<int, bool>::iterator s_it = swarms_.find(swarm_id);
208 
209  if(s_it != swarms_.end()) {
210  return s_it->second;
211  }
212 
213  return false;
214  }
215 
216  void RuntimeHandle::getSwarmList(std::vector<int>& swarm_list)
217  {
218  swarm_list.clear();
219  std::map<int, bool>::iterator s_it;
220  boost::shared_lock<boost::shared_mutex> lock(swarm_mutex_);
221  for(s_it = swarms_.begin(); s_it != swarms_.end(); s_it++) {
222  if(s_it->second) {
223  swarm_list.push_back(s_it->first);
224  }
225  }
226  }
227 
228  void RuntimeHandle::deleteSwarm(int swarm_id)
229  {
230  boost::unique_lock<boost::shared_mutex> lock(swarm_mutex_);
231  swarms_.erase(swarm_id);
232  }
233 
235  {
236  std::map<int, bool>::iterator s_it;
237  boost::shared_lock<boost::shared_mutex> lock(swarm_mutex_);
238  for(s_it = swarms_.begin(); s_it != swarms_.end(); s_it++) {
239  std::cout<<s_it->first<<": ";
240  std::cout<<s_it->second;
241  std::cout<<std::endl;
242  }
243  }
244 
245  bool RuntimeHandle::inNeighborSwarm(int robot_id, int swarm_id)
246  {
247  std::map<int, NeighborSwarmTuple>::iterator os_it;
248  boost::shared_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
249  os_it = neighbor_swarms_.find(robot_id);
250  if(os_it != neighbor_swarms_.end()) {
251  if(os_it->second.swarmIDExist(swarm_id)) {
252  return true;
253  }
254  else {
255  return false;
256  }
257  }
258  else {
259  return false;
260  }
261  }
262 
263  void RuntimeHandle::joinNeighborSwarm(int robot_id, int swarm_id)
264  {
265  std::map<int, NeighborSwarmTuple>::iterator os_it;
266  boost::upgrade_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
267  os_it = neighbor_swarms_.find(robot_id);
268 
269  if(os_it != neighbor_swarms_.end()) {
270  if(os_it->second.swarmIDExist(swarm_id)) {
271  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
272  os_it->second.age = 0;
273  }
274  else {
275  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
276  os_it->second.addSwarmID(swarm_id);
277  os_it->second.age = 0;
278  }
279  }
280  else {
281  std::vector<int> swarm_list;
282  swarm_list.push_back(swarm_id);
283  NeighborSwarmTuple new_neighbor_swarm(swarm_list, 0);
284 
285  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
286  neighbor_swarms_.insert(std::pair<int, NeighborSwarmTuple>(robot_id, new_neighbor_swarm));
287  }
288  }
289 
290  void RuntimeHandle::leaveNeighborSwarm(int robot_id, int swarm_id)
291  {
292  std::map<int, NeighborSwarmTuple>::iterator os_it;
293  boost::upgrade_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
294  os_it = neighbor_swarms_.find(robot_id);
295 
296  if(os_it != neighbor_swarms_.end()) {
297  if(os_it->second.swarmIDExist(swarm_id)) {
298  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
299  os_it->second.removeSwarmID(swarm_id);
300  os_it->second.age = 0;
301  }
302  else {
303  std::cout<<"robot"<<robot_id<<" is not in swarm"<<swarm_id<<"."<<std::endl;
304  }
305  }
306  else { //not exist
307  std::cout<<"robot_id "<<robot_id<<" neighbor_swarm tuple is not exist."<<std::endl;
308  return;
309  }
310  }
311 
312  void RuntimeHandle::insertOrRefreshNeighborSwarm(int robot_id, const std::vector<int>& swarm_list)
313  {
314  std::map<int, NeighborSwarmTuple>::iterator os_it;
315  boost::upgrade_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
316  os_it = neighbor_swarms_.find(robot_id);
317 
318  if(os_it != neighbor_swarms_.end()) {
319  NeighborSwarmTuple new_neighbor_swarm(swarm_list, 0);
320  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
321  os_it->second = new_neighbor_swarm;
322  }
323  else {
324  NeighborSwarmTuple new_neighbor_swarm(swarm_list, 0);
325  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
326  neighbor_swarms_.insert(std::pair<int, NeighborSwarmTuple>(robot_id ,new_neighbor_swarm));
327  }
328  }
329 
330  void RuntimeHandle::getSwarmMembers(int swarm_id, std::set<int>& swarm_members)
331  {
332  std::map<int, NeighborSwarmTuple>::iterator os_it;
333  swarm_members.clear();
334  if(getSwarmFlag(swarm_id)) {
335  swarm_members.insert(robot_id_);
336  }
337  boost::shared_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
338  for(os_it = neighbor_swarms_.begin(); os_it != neighbor_swarms_.end(); os_it++) {
339  if(os_it->second.swarmIDExist(swarm_id)) {
340  swarm_members.insert(os_it->first);
341  }
342  }
343  }
344 
346  {
347  boost::unique_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
348  neighbor_swarms_.erase(robot_id);
349  }
350 
352  {
353  std::map<int, NeighborSwarmTuple>::iterator os_it;
354  boost::shared_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
355  for(os_it = neighbor_swarms_.begin(); os_it != neighbor_swarms_.end(); os_it++) {
356  std::cout<<"neighbor swarm "<<os_it->first<<": ";
357  std::vector<int> temp = os_it->second.swarm_id_vector;
358  for(int i=0; i< temp.size(); i++) {
359  std::cout<<temp[i]<<",";
360  }
361  std::cout<<"age: "<<os_it->second.age;
362  std::cout<<std::endl;
363  }
364  }
365 
367  {
368  std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
369  boost::upgrade_lock<boost::shared_mutex> lock(vstig_mutex_);
370  vst_it = virtual_stigmergy_.find(id);
371  if(vst_it != virtual_stigmergy_.end()) {
372  return;
373  }
374  else {
375  std::map<std::string, VirtualStigmergyTuple> vst;
376  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
377  virtual_stigmergy_.insert(std::pair<int, std::map<std::string, VirtualStigmergyTuple> >(id, vst));
378  }
379  }
380 
381  void RuntimeHandle::insertOrUpdateVirtualStigmergy(int id, const std::string& key, const std::vector<uint8_t>& value, \
382  unsigned int lclock, time_t wtime, unsigned int rcount, int robot_id)
383  {
384  std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
385  boost::upgrade_lock<boost::shared_mutex> lock(vstig_mutex_);
386  vst_it = virtual_stigmergy_.find(id);
387 
388  if(vst_it != virtual_stigmergy_.end()) {
389  std::map<std::string, VirtualStigmergyTuple>::iterator svstt_it = vst_it->second.find(key);
390  if(svstt_it != vst_it->second.end()) {
391  VirtualStigmergyTuple new_tuple(value, lclock, wtime, rcount, robot_id);
392  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
393  svstt_it->second = new_tuple;
394  }
395  else {
396  VirtualStigmergyTuple new_tuple(value, lclock, wtime, rcount, robot_id);
397  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
398  vst_it->second.insert(std::pair<std::string, VirtualStigmergyTuple>(key ,new_tuple));
399  }
400  }
401  else {
402  std::cout<<"ID "<<id<<" VirtualStigmergy is not exist."<<std::endl;
403  return;
404  }
405  }
406 
407  bool RuntimeHandle::isVirtualStigmergyTupleExist(int id, const std::string& key)
408  {
409  std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
410  boost::shared_lock<boost::shared_mutex> lock(vstig_mutex_);
411  vst_it = virtual_stigmergy_.find(id);
412  if(vst_it != virtual_stigmergy_.end()) {
413  std::map<std::string, VirtualStigmergyTuple>::iterator svstt_it = vst_it->second.find(key);
414  if(svstt_it != vst_it->second.end()) {
415  return true;
416  }
417  }
418  return false;
419  }
420 
421  bool RuntimeHandle::getVirtualStigmergyTuple(int id, const std::string& key, VirtualStigmergyTuple& vstig_tuple)
422  {
423  std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
424  boost::shared_lock<boost::shared_mutex> lock(vstig_mutex_);
425  vst_it = virtual_stigmergy_.find(id);
426  if(vst_it != virtual_stigmergy_.end()) {
427  std::map<std::string, VirtualStigmergyTuple>::iterator svstt_it = vst_it->second.find(key);
428  if(svstt_it != vst_it->second.end()) {
429  vstig_tuple = svstt_it->second;
430  return true;
431  }
432  }
433  return false;
434  }
435 
436  void RuntimeHandle::updateVirtualStigmergyTupleReadCount(int id, const std::string& key, int count)
437  {
438  std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
439  boost::upgrade_lock<boost::shared_mutex> lock(vstig_mutex_);
440  vst_it = virtual_stigmergy_.find(id);
441 
442  if(vst_it != virtual_stigmergy_.end()) {
443  std::map<std::string, VirtualStigmergyTuple>::iterator svstt_it = vst_it->second.find(key);
444  if(svstt_it != vst_it->second.end()) {
445  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
446  svstt_it->second.read_count = count;
447  }
448  else {
449  std::cout<<"ID: "<<id<<" VirtualStigmergy, key: "<<key<<" is not exist."<<std::endl;
450  }
451  }
452  else {
453  std::cout<<"ID "<<id<<" VirtualStigmergy is not exist."<<std::endl;
454  return;
455  }
456  }
457 
459  {
460  std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
461  boost::shared_lock<boost::shared_mutex> lock(vstig_mutex_);
462  vst_it = virtual_stigmergy_.find(id);
463  if(vst_it != virtual_stigmergy_.end()) {
464  return vst_it->second.size();
465  }
466 
467  return 0;
468  }
469 
471  {
472  boost::unique_lock<boost::shared_mutex> lock(vstig_mutex_);
473  virtual_stigmergy_.erase(id);
474  }
475 
476  void RuntimeHandle::deleteVirtualStigmergyValue(int id, const std::string& key)
477  {
478  std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
479  boost::upgrade_lock<boost::shared_mutex> lock(vstig_mutex_);
480  vst_it = virtual_stigmergy_.find(id);
481  if(vst_it != virtual_stigmergy_.end()) {
482  std::map<std::string, VirtualStigmergyTuple>::iterator svstt_it = vst_it->second.find(key);
483  if(svstt_it != vst_it->second.end()) {
484  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
485  vst_it->second.erase(key);
486  }
487  else {
488  return;
489  }
490  }
491  else {
492  return;
493  }
494  }
495 
497  {
498  std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
499  std::map<std::string, VirtualStigmergyTuple>::iterator svstt_it;
500  boost::shared_lock<boost::shared_mutex> lock(vstig_mutex_);
501  for (vst_it = virtual_stigmergy_.begin(); vst_it != virtual_stigmergy_.end(); vst_it++) {
502  std::cout<<"["<<vst_it->first<<":"<<std::endl;
503  std::map<std::string, VirtualStigmergyTuple>* svstt_pointer = &(vst_it->second);
504  for (svstt_it = svstt_pointer->begin(); svstt_it != svstt_pointer->end(); svstt_it++) {
505  std::cout<<svstt_it->first<<" ";
506  /*std::cout<<"("<<svstt_it->first<<", "<< \
507  svstt_it->second.vstig_value<<", "<<svstt_it->second.lamport_clock<<", "<<\
508  svstt_it->second.write_timestamp<<", "<<svstt_it->second.read_count<<", "<<\
509  svstt_it->second.robot_id<<")"<<std::endl;*/
510  }
511  std::cout<<"]"<<std::endl;
512  std::cout<<std::endl;
513  }
514  }
515 
517  {
518  if(inNeighbors(robot_id)) {
519  boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
520  NeighborBase nb;
521  if(!getNeighborBase(robot_id, nb)) {
522  return false;
523  }
524  Base msg_src_neighbor(nb.x, nb.y, nb.z, nb.vx, nb.vy, nb.vz, 1);
525  std::map<int, NeighborBase>::iterator it = neighbors_.begin();
526  for(it = neighbors_.begin(); it != neighbors_.end(); it++) {
527  if(it->first == robot_id) {
528  continue;
529  }
530  Base neighbor(it->second.x, it->second.y, it->second.z, it->second.vx, it->second.vy, it->second.vz, 1);
531  if(!cni_->isNeighbor(msg_src_neighbor, neighbor)) {
532  return false;
533  }
534  }
535  return true;
536  }
537  return false;
538  }
539 
541  {
542  std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
543  boost::upgrade_lock<boost::shared_mutex> lock(bb_mutex_);
544  bb_it = blackboard_.find(id);
545 
546  if(bb_it != blackboard_.end()) {
547  return;
548  }
549  else {
550  std::map<std::string, BlackBoardTuple> bb;
551  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
552  blackboard_.insert(std::pair<int, std::map<std::string, BlackBoardTuple> >(id, bb));
553  }
554  }
555 
556  void RuntimeHandle::insertOrUpdateBlackBoard(int id, const std::string& key, const std::vector<uint8_t>& value, const ros::Time& timestamp, int robot_id)
557  {
558  std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
559  boost::upgrade_lock<boost::shared_mutex> lock(bb_mutex_);
560  bb_it = blackboard_.find(id);
561 
562  if(bb_it != blackboard_.end()) {
563  std::map<std::string, BlackBoardTuple>::iterator sbbt_it = bb_it->second.find(key);
564  if(sbbt_it != bb_it->second.end()) {
565  BlackBoardTuple new_tuple(value, timestamp, robot_id);
566  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
567  sbbt_it->second = new_tuple;
568  }
569  else {
570  BlackBoardTuple new_tuple(value, timestamp, robot_id);
571  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
572  bb_it->second.insert(std::pair<std::string, BlackBoardTuple>(key ,new_tuple));
573  }
574  }
575  else {
576  std::cout<<"ID "<<id<<" BlackBoard is not exist."<<std::endl;
577  return;
578  }
579  }
580 
581  bool RuntimeHandle::isBlackBoardTupleExist(int id, const std::string& key)
582  {
583  std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
584  boost::shared_lock<boost::shared_mutex> lock(bb_mutex_);
585  bb_it = blackboard_.find(id);
586  if(bb_it != blackboard_.end()) {
587  std::map<std::string, BlackBoardTuple>::iterator sbbt_it = bb_it->second.find(key);
588  if(sbbt_it != bb_it->second.end()) {
589  return true;
590  }
591  }
592  return false;
593  }
594 
595  void RuntimeHandle::getBlackBoardTuple(int id, const std::string& key, BlackBoardTuple& bb_tuple)
596  {
597  std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
598  boost::shared_lock<boost::shared_mutex> lock(bb_mutex_);
599  bb_it = blackboard_.find(id);
600  if(bb_it != blackboard_.end()) {
601  std::map<std::string, BlackBoardTuple>::iterator sbbt_it = bb_it->second.find(key);
602  if(sbbt_it != bb_it->second.end()) {
603  bb_tuple = sbbt_it->second;
604  }
605  }
606  }
607 
609  {
610  std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
611  boost::shared_lock<boost::shared_mutex> lock(bb_mutex_);
612  bb_it = blackboard_.find(id);
613 
614  if(bb_it != blackboard_.end()) {
615  return bb_it->second.size();
616  }
617 
618  return 0;
619  }
620 
622  {
623  boost::unique_lock<boost::shared_mutex> lock(bb_mutex_);
624  blackboard_.erase(id);
625  }
626 
627  void RuntimeHandle::deleteBlackBoardValue(int id, const std::string& key)
628  {
629  std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
630  boost::upgrade_lock<boost::shared_mutex> lock(bb_mutex_);
631  bb_it = blackboard_.find(id);
632 
633  if(bb_it != blackboard_.end()) {
634  std::map<std::string, BlackBoardTuple>::iterator sbbt_it=bb_it->second.find(key);
635  if(sbbt_it != bb_it->second.end()) {
636  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
637  bb_it->second.erase(key);
638  }
639  else {
640  return;
641  }
642  }
643  else {
644  return;
645  }
646  }
647 
649  {
650  std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
651  std::map<std::string, BlackBoardTuple>::iterator sbbt_it;
652 
653  boost::shared_lock<boost::shared_mutex> lock(bb_mutex_);
654  for (bb_it = blackboard_.begin(); bb_it != blackboard_.end(); bb_it++) {
655  std::cout<<"["<<bb_it->first<<":"<<std::endl;
656  std::map<std::string, BlackBoardTuple>* sbbt_pointer = &(bb_it->second);
657  for (sbbt_it = sbbt_pointer->begin(); sbbt_it != sbbt_pointer->end(); sbbt_it++) {
658  /*std::cout<<"("<<sbbt_it->first<<","<< \
659  sbbt_it->second.bb_value<<","<<sbbt_it->second.timestamp.sec<<","<<\
660  sbbt_it->second.robot_id<<")"<<std::endl;*/
661  }
662  std::cout<<"]"<<std::endl;
663  std::cout<<std::endl;
664  }
665  }
666 
668  {
669  boost::shared_lock<boost::shared_mutex> lock(neighbor_distance_mutex_);
670  return neighbor_distance_;
671  }
672 
673  void RuntimeHandle::setNeighborDistance(float neighbor_distance)
674  {
675  boost::unique_lock<boost::shared_mutex> lock(neighbor_distance_mutex_);
676  neighbor_distance_ = neighbor_distance;
678  std::cout<<"neighbor distance is set to "<<neighbor_distance_<<std::endl;
679  clearNeighbors();
680  }
681 
683  {
684  std::map<std::string, boost::shared_ptr<ListenerHelper> >::iterator lh_it;
685  boost::upgrade_lock<boost::shared_mutex> lock(listener_helpers_mutex_);
686  lh_it = listener_helpers_.find(key);
687 
688  if(lh_it != listener_helpers_.end()) {
689  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
690  lh_it->second = helper;
691  }
692  else {
693  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
694  listener_helpers_.insert(std::pair<std::string, boost::shared_ptr<ListenerHelper> >(key ,helper));
695  }
696  }
697 
699  {
700  std::map<std::string, boost::shared_ptr<ListenerHelper> >::iterator lh_it;
701  boost::shared_lock<boost::shared_mutex> lock(listener_helpers_mutex_);
702  lh_it = listener_helpers_.find(key);
703 
704  if(lh_it != listener_helpers_.end()) {
705  return lh_it->second;
706  }
707 
708  std::cout<<"could not get the callback function which has the key "<<key<<"!"<<std::endl;
709  return NULL;
710  }
711 
712  void RuntimeHandle::deleteListenerHelper(const std::string& key)
713  {
714  boost::unique_lock<boost::shared_mutex> lock(listener_helpers_mutex_);
715  listener_helpers_.erase(key);
716  }
717 
718  void RuntimeHandle::insertBarrier(int robot_id)
719  {
720  boost::unique_lock<boost::shared_mutex> lock(barrier_mutex_);
721  barrier_.insert(robot_id);
722  }
723 
725  {
726  boost::shared_lock<boost::shared_mutex> lock(barrier_mutex_);
727  return barrier_.size();
728  }
729 
730  bool RuntimeHandle::getSCDSPSOValue(const std::string& aKey, SCDSPSODataTuple& aT)
731  {
732  boost::shared_lock<boost::shared_mutex> lock(scds_pso_tuple_mutex_);
733  std::map<std::string, SCDSPSODataTuple>::iterator iter = scds_pso_tuple_.find(aKey);
734  if (iter != scds_pso_tuple_.end()) {
735  aT = iter->second;
736  return true;
737  }
738  else return false;
739  }
740 
741  void RuntimeHandle::insertOrUpdateSCDSPSOValue(const std::string& aKey, const SCDSPSODataTuple& aT)
742  {
743  boost::upgrade_lock<boost::shared_mutex> lock(scds_pso_tuple_mutex_);
744  std::map<std::string, SCDSPSODataTuple>::iterator iter = scds_pso_tuple_.find(aKey);
745  /*if (iter!=scds_pso_tuple_.end()) {
746  //if (iter->second.val < aT.val)
747  scds_pso_tuple_[aKey]=aT;
748  }
749  else
750  scds_pso_tuple_[aKey]=aT;*/
751  if(iter != scds_pso_tuple_.end()) {
752  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
753  iter->second = aT;
754  }
755  else {
756  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
757  scds_pso_tuple_[aKey] = aT;
758  }
759  }
760 };
boost::shared_mutex status_mutex_
boost::shared_mutex type_mutex_
std::map< int, std::map< std::string, BlackBoardTuple > > blackboard_
bool isBlackBoardTupleExist(int id, const std::string &key)
void setRobotType(int robot_type)
boost::shared_mutex bb_mutex_
void joinNeighborSwarm(int robot_id, int swarm_id)
void getSwarmList(std::vector< int > &swarm_list)
boost::shared_mutex neighbor_distance_mutex_
std::map< std::string, SCDSPSODataTuple > scds_pso_tuple_
bool checkNeighborsOverlap(int robot_id)
bool getVirtualStigmergyTuple(int id, const std::string &key, VirtualStigmergyTuple &vstig_tuple)
void setRobotID(int robot_id)
void deleteBlackBoardValue(int id, const std::string &key)
bool getNeighborBase(int robot_id, NeighborBase &nb)
void deleteNeighborSwarm(int robot_id)
boost::shared_mutex neighbor_mutex_
void setRobotBase(const Base &robot_base)
void setNeighborDistance(float neighbor_distance)
void insertOrUpdateNeighbor(int robot_id, float distance, float azimuth, float elevation, float x, float y, float z, float vx, float vy, float vz)
void insertOrUpdateSwarm(int swarm_id, bool value)
void deleteSwarm(int swarm_id)
void insertOrUpdateVirtualStigmergy(int id, const std::string &key, const std::vector< uint8_t > &value, unsigned int lclock, time_t wtime, unsigned int rcount, int robot_id)
boost::shared_mutex scds_pso_tuple_mutex_
boost::shared_mutex barrier_mutex_
void insertOrUpdateListenerHelper(const std::string &key, const boost::shared_ptr< ListenerHelper > helper)
bool inNeighbors(int robot_id)
bool inNeighborSwarm(int robot_id, int swarm_id)
boost::shared_mutex vstig_mutex_
void deleteNeighbor(int robot_id)
const float & getNeighborDistance()
void insertOrUpdateBlackBoard(int id, const std::string &key, const std::vector< uint8_t > &value, const ros::Time &timestamp, int robot_id)
const boost::shared_ptr< ListenerHelper > getListenerHelper(const std::string &key)
bool getSwarmFlag(int swarm_id)
boost::shared_mutex listener_helpers_mutex_
void setRobotStatus(int robot_status)
std::map< int, NeighborSwarmTuple > neighbor_swarms_
void deleteListenerHelper(const std::string &key)
boost::shared_ptr< CheckNeighborInterface > cni_
std::map< int, bool > swarms_
void insertOrRefreshNeighborSwarm(int robot_id, const std::vector< int > &swarm_list)
std::map< int, NeighborBase > neighbors_
void getSwarmMembers(int swarm_id, std::set< int > &swarm_members)
boost::shared_mutex swarm_mutex_
boost::shared_mutex id_mutex_
std::map< int, std::map< std::string, VirtualStigmergyTuple > > virtual_stigmergy_
void leaveNeighborSwarm(int robot_id, int swarm_id)
std::map< int, NeighborBase > getNeighbors()
boost::shared_mutex base_mutex_
boost::shared_mutex neighbor_swarm_mutex_
void updateVirtualStigmergyTupleReadCount(int id, const std::string &key, int count)
void deleteVirtualStigmergyValue(int id, const std::string &key)
bool isVirtualStigmergyTupleExist(int id, const std::string &key)
void getBlackBoardTuple(int id, const std::string &key, BlackBoardTuple &bb_tuple)
void insertOrUpdateSCDSPSOValue(const std::string &aKey, const SCDSPSODataTuple &aT)
void insertBarrier(int robot_id)
std::map< std::string, boost::shared_ptr< ListenerHelper > > listener_helpers_
bool getSCDSPSOValue(const std::string &aKey, SCDSPSODataTuple &aT)


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