swarm.cpp
Go to the documentation of this file.
1 
23 #include "micros_swarm/swarm.h"
24 
25 namespace micros_swarm{
26 
28  {
29  swarm_id_ = -1;
30  }
31 
32  Swarm::Swarm(int swarm_id)
33  {
34  swarm_id_ = swarm_id;
37  rth_->insertOrUpdateSwarm(swarm_id_, 0);
38  }
39 
40  Swarm::Swarm(const Swarm& s)
41  {
44  swarm_id_ = s.swarm_id_;
45  }
46 
48  {
49  if(this == &s) {
50  return *this;
51  }
54  swarm_id_ = s.swarm_id_;
55  return *this;
56  }
57 
59  {
60  rth_.reset();
61  mqm_.reset();
62  }
63 
64  const int Swarm::id() const
65  {
66  return swarm_id_;
67  }
68 
69  const std::set<int> Swarm::members()
70  {
71  std::set<int> s;
72  rth_->getSwarmMembers(swarm_id_, s);
73 
74  return s;
75  }
76 
77  void Swarm::join()
78  {
79  int robot_id = rth_->getRobotID();
80  rth_->insertOrUpdateSwarm(swarm_id_, 1);
81 
82  gsdf_msgs::JoinSwarm js;
83  js.robot_id = robot_id;
84  js.swarm_id = swarm_id_;
85  std::vector<uint8_t> js_vec = serialize_ros(js);
86 
87  gsdf_msgs::CommPacket p;
88  p.header.source = robot_id;
89  p.header.type = SINGLE_ROBOT_JOIN_SWARM;
90  p.header.data_len = js_vec.size();
91  p.header.version = 1;
92  p.header.checksum = 0;
93  p.content.buf = js_vec;
94  std::vector<uint8_t> msg_data = serialize_ros(p);
95  mqm_->getOutMsgQueue("swarm")->push(msg_data);
96  }
97 
98  void Swarm::leave()
99  {
100  int robot_id = rth_->getRobotID();
101  rth_->insertOrUpdateSwarm(swarm_id_, 0);
102 
103  gsdf_msgs::LeaveSwarm ls;
104  ls.robot_id = robot_id;
105  ls.swarm_id = swarm_id_;
106  std::vector<uint8_t> ls_vec = serialize_ros(ls);
107 
108  gsdf_msgs::CommPacket p;
109  p.header.source = robot_id;
110  p.header.type = SINGLE_ROBOT_LEAVE_SWARM;
111  p.header.data_len = ls_vec.size();
112  p.header.version = 1;
113  p.header.checksum = 0;
114  p.content.buf = ls_vec;
115  std::vector<uint8_t> msg_data = serialize_ros(p);
116  mqm_->getOutMsgQueue("swarm")->push(msg_data);
117  }
118 
119  void Swarm::select(const boost::function<bool()>& bf)
120  {
121  if(bf()) {
122  join();
123  }
124  else {
125  //do nothiong
126  }
127  }
128 
129  void Swarm::unselect(const boost::function<bool()>& bf)
130  {
131  if(bf()) {
132  leave();
133  }
134  else {
135  //do nothiong
136  }
137  }
138 
139  const bool Swarm::in() const
140  {
141  if(rth_->getSwarmFlag(swarm_id_)) {
142  return true;
143  }
144  return false;
145  }
146 
147  //execute a function
148  void Swarm::execute(const boost::function<void()>& f)
149  {
150  if(in()) {
151  f();
152  }
153  }
154 
156  {
157  if(in()) {
158  leave();
159  }
160  rth_->deleteSwarm(swarm_id_);
161  this->~Swarm();
162  }
163 
164  const Swarm Swarm::intersection(const Swarm& s, int new_swarm_id)
165  {
166  std::set<int> result;
167  result.clear();
168 
169  std::set<int> a;
170  rth_->getSwarmMembers(swarm_id_, a);
171  std::set<int> b;
172  rth_->getSwarmMembers(s.id(), b);
173 
174  std::set_intersection(a.begin(), a.end(), b.begin(), b.end(),
175  std::insert_iterator<std::set<int> >(result, result.begin()));
176 
177  Swarm result_swarm(new_swarm_id);
178 
179  int robot_id=rth_->getRobotID();
180 
181  std::set<int>::iterator it;
182  it = result.find(robot_id);
183  if(it != result.end()) {
184  result_swarm.join();
185  }
186 
187  return result_swarm;
188  }
189 
190  const Swarm Swarm::swarm_union(const Swarm& s, int new_swarm_id)
191  {
192  std::set<int> result;
193  result.clear();
194 
195  std::set<int> a;
196  rth_->getSwarmMembers(swarm_id_, a);
197  std::set<int> b;
198  rth_->getSwarmMembers(s.id(), b);
199 
200  std::set_union(a.begin(), a.end(), b.begin(), b.end(),
201  std::insert_iterator<std::set<int> >(result, result.begin()));
202 
203  Swarm result_swarm(new_swarm_id);
204 
205  int robot_id = rth_->getRobotID();
206 
207  std::set<int>::iterator it;
208  it = result.find(robot_id);
209  if(it != result.end()) {
210  result_swarm.join();
211  }
212 
213  return result_swarm;
214  }
215 
216  const Swarm Swarm::difference(const Swarm& s, int new_swarm_id)
217  {
218  std::set<int> result;
219 
220  std::set<int> a;
221  rth_->getSwarmMembers(swarm_id_, a);
222  std::set<int> b;
223  rth_->getSwarmMembers(s.id(), b);
224 
225  std::set_difference(a.begin(), a.end(), b.begin(), b.end(),
226  std::insert_iterator<std::set<int> >(result, result.begin()));
227 
228  Swarm result_swarm(new_swarm_id);
229 
230  int robot_id = rth_->getRobotID();
231 
232  std::set<int>::iterator it;
233  it = result.find(robot_id);
234  if(it != result.end()) {
235  result_swarm.join();
236  }
237 
238  return result_swarm;
239  }
240 
241  const Swarm Swarm::negation(int new_swarm_id)
242  {
243  Swarm result_swarm(new_swarm_id);
244  std::set<int> a;
245  rth_->getSwarmMembers(swarm_id_, a);
246  int robot_id = rth_->getRobotID();
247 
248  std::set<int>::iterator it;
249  it = a.find(robot_id);
250  if(it == a.end()) {
251  result_swarm.join();
252  }
253 
254  return result_swarm;
255  }
256 
257  void Swarm::print() const
258  {
259  std::set<int> s;
260  rth_->getSwarmMembers(swarm_id_, s);
261 
262  int robot_id = rth_->getRobotID();
263 
264  std::set<int>::iterator it;
265  std::cout<<"swarm "<<swarm_id_<<" members: "<<std::endl;
266  for(it = s.begin(); it != s.end(); it++) {
267  std::cout<<*it<<", ";
268  }
269  std::cout<<std::endl;
270  }
271 };
boost::shared_ptr< micros_swarm::RuntimeHandle > rth_
Definition: swarm.h:66
Swarm & operator=(const Swarm &s)
Definition: swarm.cpp:47
f
const bool in() const
Definition: swarm.cpp:139
XmlRpcServer s
const std::set< int > members()
Definition: swarm.cpp:69
void execute(const boost::function< void()> &f)
Definition: swarm.cpp:148
void unselect(const boost::function< bool()> &bf)
Definition: swarm.cpp:129
void print() const
Definition: swarm.cpp:257
const Swarm negation(int new_swarm_id)
Definition: swarm.cpp:241
std::vector< uint8_t > serialize_ros(T t)
Definition: serialize.h:39
static boost::shared_ptr< T > getSingleton()
Definition: singleton.h:70
const int id() const
Definition: swarm.cpp:64
const Swarm difference(const Swarm &s, int new_swarm_id)
Definition: swarm.cpp:216
boost::shared_ptr< micros_swarm::MsgQueueManager > mqm_
Definition: swarm.h:67
const Swarm swarm_union(const Swarm &s, int new_swarm_id)
Definition: swarm.cpp:190
void select(const boost::function< bool()> &bf)
Definition: swarm.cpp:119
const Swarm intersection(const Swarm &s, int new_swarm_id)
Definition: swarm.cpp:164


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