00001
00023 #include "micros_swarm/runtime_handle.h"
00024
00025 namespace micros_swarm{
00026
00027 RuntimeHandle::RuntimeHandle()
00028 {
00029 robot_id_ = 0;
00030 robot_base_ = Base(0,0,0,0,0,0,-1);
00031 neighbors_.clear();
00032 swarms_.clear();
00033 neighbor_swarms_.clear();
00034 virtual_stigmergy_.clear();
00035 blackboard_.clear();
00036 listener_helpers_.clear();
00037 listener_helpers_.insert(std::pair<std::string, boost::shared_ptr<ListenerHelper> >("" , NULL));
00038 barrier_.clear();
00039 scds_pso_tuple_.clear();
00040 }
00041
00042 int RuntimeHandle::getRobotID()
00043 {
00044 boost::shared_lock<boost::shared_mutex> lock(id_mutex_);
00045 return robot_id_;
00046 }
00047
00048 void RuntimeHandle::setRobotID(int robot_id)
00049 {
00050 boost::unique_lock<boost::shared_mutex> lock(id_mutex_);
00051 robot_id_ = robot_id;
00052 }
00053
00054 int RuntimeHandle::getRobotType()
00055 {
00056 boost::shared_lock<boost::shared_mutex> lock(type_mutex_);
00057 return robot_type_;
00058 }
00059
00060 void RuntimeHandle::setRobotType(int robot_type)
00061 {
00062 boost::unique_lock<boost::shared_mutex> lock(type_mutex_);
00063 robot_type_ = robot_type;
00064 }
00065
00066 int RuntimeHandle::getRobotStatus()
00067 {
00068 boost::shared_lock<boost::shared_mutex> lock(status_mutex_);
00069 return robot_status_;
00070 }
00071
00072 void RuntimeHandle::setRobotStatus(int robot_status)
00073 {
00074 boost::unique_lock<boost::shared_mutex> lock(status_mutex_);
00075 robot_status_ = robot_status;
00076 }
00077
00078 const Base& RuntimeHandle::getRobotBase()
00079 {
00080 boost::shared_lock<boost::shared_mutex> lock(base_mutex_);
00081 return robot_base_;
00082 }
00083
00084 void RuntimeHandle::setRobotBase(const Base& robot_base)
00085 {
00086 boost::unique_lock<boost::shared_mutex> lock(base_mutex_);
00087 robot_base_ = robot_base;
00088 if(robot_base.valid == -1) {
00089 robot_base_.valid = 1;
00090 }
00091 }
00092
00093 void RuntimeHandle::printRobotBase()
00094 {
00095 boost::shared_lock<boost::shared_mutex> lock(base_mutex_);
00096 std::cout<<"robot base: "<<robot_base_.x<<", "<<robot_base_.y<<", "<<\
00097 robot_base_.z<<", "<<robot_base_.vx<<", "<<robot_base_.vy<<", "<<\
00098 robot_base_.vz<<std::endl;
00099 }
00100
00101 void RuntimeHandle::getNeighbors(std::map<int, NeighborBase>& neighbors)
00102 {
00103 boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
00104 neighbors = neighbors_;
00105 }
00106
00107 bool RuntimeHandle::getNeighborBase(int robot_id, NeighborBase& nb)
00108 {
00109 boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
00110 std::map<int, NeighborBase>::iterator n_it = neighbors_.find(robot_id);
00111
00112 if(n_it != neighbors_.end()) {
00113 nb = n_it->second;
00114 return true;
00115 }
00116
00117 return false;
00118 }
00119
00120 void RuntimeHandle::clearNeighbors()
00121 {
00122 boost::upgrade_lock<boost::shared_mutex> lock(neighbor_mutex_);
00123 if(neighbors_.size() > 0) {
00124 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00125 neighbors_.clear();
00126 }
00127 }
00128
00129 std::map<int, NeighborBase> RuntimeHandle::getNeighbors()
00130 {
00131 boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
00132 return neighbors_;
00133 };
00134
00135 int RuntimeHandle::getNeighborSize()
00136 {
00137 boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
00138 return neighbors_.size();
00139 }
00140
00141 void RuntimeHandle::insertOrUpdateNeighbor(int robot_id, float distance, float azimuth, float elevation, float x, float y, float z, float vx, float vy, float vz)
00142 {
00143 boost::upgrade_lock<boost::shared_mutex> lock(neighbor_mutex_);
00144 std::map<int, NeighborBase>::iterator n_it = neighbors_.find(robot_id);
00145
00146 if(n_it != neighbors_.end()) {
00147 NeighborBase new_neighbor_base(distance, azimuth, elevation, x, y, z,vx, vy, vz);
00148 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00149 n_it->second = new_neighbor_base;
00150 }
00151 else {
00152 NeighborBase new_neighbor_base(distance, azimuth, elevation, x, y, z, vx, vy, vz);
00153 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00154 neighbors_.insert(std::pair<int, NeighborBase>(robot_id ,new_neighbor_base));
00155 }
00156 }
00157
00158 void RuntimeHandle::deleteNeighbor(int robot_id)
00159 {
00160 boost::unique_lock<boost::shared_mutex> lock(neighbor_mutex_);
00161 neighbors_.erase(robot_id);
00162 }
00163
00164 bool RuntimeHandle::inNeighbors(int robot_id)
00165 {
00166 boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
00167 std::map<int, NeighborBase>::iterator n_it = neighbors_.find(robot_id);
00168
00169 if(n_it != neighbors_.end()) {
00170 return true;
00171 }
00172
00173 return false;
00174 }
00175
00176 void RuntimeHandle::printNeighbor()
00177 {
00178 std::map<int, NeighborBase>::iterator n_it;
00179 boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
00180 for (n_it = neighbors_.begin(); n_it != neighbors_.end(); n_it++) {
00181 std::cout<<n_it->first<<": ";
00182 std::cout<<n_it->second.distance<<","<<n_it->second.azimuth<<","<<n_it->second.elevation<<","<<\
00183 n_it->second.x<<","<<n_it->second.y<<","<<n_it->second.z<<", "<<
00184 n_it->second.vx<<","<<n_it->second.vy<<","<<n_it->second.vz;
00185 std::cout<<std::endl;
00186 }
00187 }
00188
00189 void RuntimeHandle::insertOrUpdateSwarm(int swarm_id, bool value)
00190 {
00191 boost::upgrade_lock<boost::shared_mutex> lock(swarm_mutex_);
00192 std::map<int, bool>::iterator s_it = swarms_.find(swarm_id);
00193
00194 if(s_it != swarms_.end()) {
00195 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00196 s_it->second = value;
00197 }
00198 else {
00199 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00200 swarms_.insert(std::pair<int, bool>(swarm_id, value));
00201 }
00202 }
00203
00204 bool RuntimeHandle::getSwarmFlag(int swarm_id)
00205 {
00206 boost::shared_lock<boost::shared_mutex> lock(swarm_mutex_);
00207 std::map<int, bool>::iterator s_it = swarms_.find(swarm_id);
00208
00209 if(s_it != swarms_.end()) {
00210 return s_it->second;
00211 }
00212
00213 return false;
00214 }
00215
00216 void RuntimeHandle::getSwarmList(std::vector<int>& swarm_list)
00217 {
00218 swarm_list.clear();
00219 std::map<int, bool>::iterator s_it;
00220 boost::shared_lock<boost::shared_mutex> lock(swarm_mutex_);
00221 for(s_it = swarms_.begin(); s_it != swarms_.end(); s_it++) {
00222 if(s_it->second) {
00223 swarm_list.push_back(s_it->first);
00224 }
00225 }
00226 }
00227
00228 void RuntimeHandle::deleteSwarm(int swarm_id)
00229 {
00230 boost::unique_lock<boost::shared_mutex> lock(swarm_mutex_);
00231 swarms_.erase(swarm_id);
00232 }
00233
00234 void RuntimeHandle::printSwarm()
00235 {
00236 std::map<int, bool>::iterator s_it;
00237 boost::shared_lock<boost::shared_mutex> lock(swarm_mutex_);
00238 for(s_it = swarms_.begin(); s_it != swarms_.end(); s_it++) {
00239 std::cout<<s_it->first<<": ";
00240 std::cout<<s_it->second;
00241 std::cout<<std::endl;
00242 }
00243 }
00244
00245 bool RuntimeHandle::inNeighborSwarm(int robot_id, int swarm_id)
00246 {
00247 std::map<int, NeighborSwarmTuple>::iterator os_it;
00248 boost::shared_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
00249 os_it = neighbor_swarms_.find(robot_id);
00250 if(os_it != neighbor_swarms_.end()) {
00251 if(os_it->second.swarmIDExist(swarm_id)) {
00252 return true;
00253 }
00254 else {
00255 return false;
00256 }
00257 }
00258 else {
00259 return false;
00260 }
00261 }
00262
00263 void RuntimeHandle::joinNeighborSwarm(int robot_id, int swarm_id)
00264 {
00265 std::map<int, NeighborSwarmTuple>::iterator os_it;
00266 boost::upgrade_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
00267 os_it = neighbor_swarms_.find(robot_id);
00268
00269 if(os_it != neighbor_swarms_.end()) {
00270 if(os_it->second.swarmIDExist(swarm_id)) {
00271 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00272 os_it->second.age = 0;
00273 }
00274 else {
00275 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00276 os_it->second.addSwarmID(swarm_id);
00277 os_it->second.age = 0;
00278 }
00279 }
00280 else {
00281 std::vector<int> swarm_list;
00282 swarm_list.push_back(swarm_id);
00283 NeighborSwarmTuple new_neighbor_swarm(swarm_list, 0);
00284
00285 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00286 neighbor_swarms_.insert(std::pair<int, NeighborSwarmTuple>(robot_id, new_neighbor_swarm));
00287 }
00288 }
00289
00290 void RuntimeHandle::leaveNeighborSwarm(int robot_id, int swarm_id)
00291 {
00292 std::map<int, NeighborSwarmTuple>::iterator os_it;
00293 boost::upgrade_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
00294 os_it = neighbor_swarms_.find(robot_id);
00295
00296 if(os_it != neighbor_swarms_.end()) {
00297 if(os_it->second.swarmIDExist(swarm_id)) {
00298 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00299 os_it->second.removeSwarmID(swarm_id);
00300 os_it->second.age = 0;
00301 }
00302 else {
00303 std::cout<<"robot"<<robot_id<<" is not in swarm"<<swarm_id<<"."<<std::endl;
00304 }
00305 }
00306 else {
00307 std::cout<<"robot_id "<<robot_id<<" neighbor_swarm tuple is not exist."<<std::endl;
00308 return;
00309 }
00310 }
00311
00312 void RuntimeHandle::insertOrRefreshNeighborSwarm(int robot_id, const std::vector<int>& swarm_list)
00313 {
00314 std::map<int, NeighborSwarmTuple>::iterator os_it;
00315 boost::upgrade_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
00316 os_it = neighbor_swarms_.find(robot_id);
00317
00318 if(os_it != neighbor_swarms_.end()) {
00319 NeighborSwarmTuple new_neighbor_swarm(swarm_list, 0);
00320 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00321 os_it->second = new_neighbor_swarm;
00322 }
00323 else {
00324 NeighborSwarmTuple new_neighbor_swarm(swarm_list, 0);
00325 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00326 neighbor_swarms_.insert(std::pair<int, NeighborSwarmTuple>(robot_id ,new_neighbor_swarm));
00327 }
00328 }
00329
00330 void RuntimeHandle::getSwarmMembers(int swarm_id, std::set<int>& swarm_members)
00331 {
00332 std::map<int, NeighborSwarmTuple>::iterator os_it;
00333 swarm_members.clear();
00334 if(getSwarmFlag(swarm_id)) {
00335 swarm_members.insert(robot_id_);
00336 }
00337 boost::shared_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
00338 for(os_it = neighbor_swarms_.begin(); os_it != neighbor_swarms_.end(); os_it++) {
00339 if(os_it->second.swarmIDExist(swarm_id)) {
00340 swarm_members.insert(os_it->first);
00341 }
00342 }
00343 }
00344
00345 void RuntimeHandle::deleteNeighborSwarm(int robot_id)
00346 {
00347 boost::unique_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
00348 neighbor_swarms_.erase(robot_id);
00349 }
00350
00351 void RuntimeHandle::printNeighborSwarm()
00352 {
00353 std::map<int, NeighborSwarmTuple>::iterator os_it;
00354 boost::shared_lock<boost::shared_mutex> lock(neighbor_swarm_mutex_);
00355 for(os_it = neighbor_swarms_.begin(); os_it != neighbor_swarms_.end(); os_it++) {
00356 std::cout<<"neighbor swarm "<<os_it->first<<": ";
00357 std::vector<int> temp = os_it->second.swarm_id_vector;
00358 for(int i=0; i< temp.size(); i++) {
00359 std::cout<<temp[i]<<",";
00360 }
00361 std::cout<<"age: "<<os_it->second.age;
00362 std::cout<<std::endl;
00363 }
00364 }
00365
00366 void RuntimeHandle::createVirtualStigmergy(int id)
00367 {
00368 std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
00369 boost::upgrade_lock<boost::shared_mutex> lock(vstig_mutex_);
00370 vst_it = virtual_stigmergy_.find(id);
00371 if(vst_it != virtual_stigmergy_.end()) {
00372 return;
00373 }
00374 else {
00375 std::map<std::string, VirtualStigmergyTuple> vst;
00376 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00377 virtual_stigmergy_.insert(std::pair<int, std::map<std::string, VirtualStigmergyTuple> >(id, vst));
00378 }
00379 }
00380
00381 void RuntimeHandle::insertOrUpdateVirtualStigmergy(int id, const std::string& key, const std::vector<uint8_t>& value, \
00382 unsigned int lclock, time_t wtime, unsigned int rcount, int robot_id)
00383 {
00384 std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
00385 boost::upgrade_lock<boost::shared_mutex> lock(vstig_mutex_);
00386 vst_it = virtual_stigmergy_.find(id);
00387
00388 if(vst_it != virtual_stigmergy_.end()) {
00389 std::map<std::string, VirtualStigmergyTuple>::iterator svstt_it = vst_it->second.find(key);
00390 if(svstt_it != vst_it->second.end()) {
00391 VirtualStigmergyTuple new_tuple(value, lclock, wtime, rcount, robot_id);
00392 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00393 svstt_it->second = new_tuple;
00394 }
00395 else {
00396 VirtualStigmergyTuple new_tuple(value, lclock, wtime, rcount, robot_id);
00397 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00398 vst_it->second.insert(std::pair<std::string, VirtualStigmergyTuple>(key ,new_tuple));
00399 }
00400 }
00401 else {
00402 std::cout<<"ID "<<id<<" VirtualStigmergy is not exist."<<std::endl;
00403 return;
00404 }
00405 }
00406
00407 bool RuntimeHandle::isVirtualStigmergyTupleExist(int id, const std::string& key)
00408 {
00409 std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
00410 boost::shared_lock<boost::shared_mutex> lock(vstig_mutex_);
00411 vst_it = virtual_stigmergy_.find(id);
00412 if(vst_it != virtual_stigmergy_.end()) {
00413 std::map<std::string, VirtualStigmergyTuple>::iterator svstt_it = vst_it->second.find(key);
00414 if(svstt_it != vst_it->second.end()) {
00415 return true;
00416 }
00417 }
00418 return false;
00419 }
00420
00421 bool RuntimeHandle::getVirtualStigmergyTuple(int id, const std::string& key, VirtualStigmergyTuple& vstig_tuple)
00422 {
00423 std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
00424 boost::shared_lock<boost::shared_mutex> lock(vstig_mutex_);
00425 vst_it = virtual_stigmergy_.find(id);
00426 if(vst_it != virtual_stigmergy_.end()) {
00427 std::map<std::string, VirtualStigmergyTuple>::iterator svstt_it = vst_it->second.find(key);
00428 if(svstt_it != vst_it->second.end()) {
00429 vstig_tuple = svstt_it->second;
00430 return true;
00431 }
00432 }
00433 return false;
00434 }
00435
00436 void RuntimeHandle::updateVirtualStigmergyTupleReadCount(int id, const std::string& key, int count)
00437 {
00438 std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
00439 boost::upgrade_lock<boost::shared_mutex> lock(vstig_mutex_);
00440 vst_it = virtual_stigmergy_.find(id);
00441
00442 if(vst_it != virtual_stigmergy_.end()) {
00443 std::map<std::string, VirtualStigmergyTuple>::iterator svstt_it = vst_it->second.find(key);
00444 if(svstt_it != vst_it->second.end()) {
00445 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00446 svstt_it->second.read_count = count;
00447 }
00448 else {
00449 std::cout<<"ID: "<<id<<" VirtualStigmergy, key: "<<key<<" is not exist."<<std::endl;
00450 }
00451 }
00452 else {
00453 std::cout<<"ID "<<id<<" VirtualStigmergy is not exist."<<std::endl;
00454 return;
00455 }
00456 }
00457
00458 int RuntimeHandle::getVirtualStigmergySize(int id)
00459 {
00460 std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
00461 boost::shared_lock<boost::shared_mutex> lock(vstig_mutex_);
00462 vst_it = virtual_stigmergy_.find(id);
00463 if(vst_it != virtual_stigmergy_.end()) {
00464 return vst_it->second.size();
00465 }
00466
00467 return 0;
00468 }
00469
00470 void RuntimeHandle::deleteVirtualStigmergy(int id)
00471 {
00472 boost::unique_lock<boost::shared_mutex> lock(vstig_mutex_);
00473 virtual_stigmergy_.erase(id);
00474 }
00475
00476 void RuntimeHandle::deleteVirtualStigmergyValue(int id, const std::string& key)
00477 {
00478 std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
00479 boost::upgrade_lock<boost::shared_mutex> lock(vstig_mutex_);
00480 vst_it = virtual_stigmergy_.find(id);
00481 if(vst_it != virtual_stigmergy_.end()) {
00482 std::map<std::string, VirtualStigmergyTuple>::iterator svstt_it = vst_it->second.find(key);
00483 if(svstt_it != vst_it->second.end()) {
00484 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00485 vst_it->second.erase(key);
00486 }
00487 else {
00488 return;
00489 }
00490 }
00491 else {
00492 return;
00493 }
00494 }
00495
00496 void RuntimeHandle::printVirtualStigmergy()
00497 {
00498 std::map<int, std::map<std::string, VirtualStigmergyTuple> >::iterator vst_it;
00499 std::map<std::string, VirtualStigmergyTuple>::iterator svstt_it;
00500 boost::shared_lock<boost::shared_mutex> lock(vstig_mutex_);
00501 for (vst_it = virtual_stigmergy_.begin(); vst_it != virtual_stigmergy_.end(); vst_it++) {
00502 std::cout<<"["<<vst_it->first<<":"<<std::endl;
00503 std::map<std::string, VirtualStigmergyTuple>* svstt_pointer = &(vst_it->second);
00504 for (svstt_it = svstt_pointer->begin(); svstt_it != svstt_pointer->end(); svstt_it++) {
00505 std::cout<<svstt_it->first<<" ";
00506
00507
00508
00509
00510 }
00511 std::cout<<"]"<<std::endl;
00512 std::cout<<std::endl;
00513 }
00514 }
00515
00516 bool RuntimeHandle::checkNeighborsOverlap(int robot_id)
00517 {
00518 if(inNeighbors(robot_id)) {
00519 boost::shared_lock<boost::shared_mutex> lock(neighbor_mutex_);
00520 NeighborBase nb;
00521 if(!getNeighborBase(robot_id, nb)) {
00522 return false;
00523 }
00524 Base msg_src_neighbor(nb.x, nb.y, nb.z, nb.vx, nb.vy, nb.vz, 1);
00525 std::map<int, NeighborBase>::iterator it = neighbors_.begin();
00526 for(it = neighbors_.begin(); it != neighbors_.end(); it++) {
00527 if(it->first == robot_id) {
00528 continue;
00529 }
00530 Base neighbor(it->second.x, it->second.y, it->second.z, it->second.vx, it->second.vy, it->second.vz, 1);
00531 if(!cni_->isNeighbor(msg_src_neighbor, neighbor)) {
00532 return false;
00533 }
00534 }
00535 return true;
00536 }
00537 return false;
00538 }
00539
00540 void RuntimeHandle::createBlackBoard(int id)
00541 {
00542 std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
00543 boost::upgrade_lock<boost::shared_mutex> lock(bb_mutex_);
00544 bb_it = blackboard_.find(id);
00545
00546 if(bb_it != blackboard_.end()) {
00547 return;
00548 }
00549 else {
00550 std::map<std::string, BlackBoardTuple> bb;
00551 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00552 blackboard_.insert(std::pair<int, std::map<std::string, BlackBoardTuple> >(id, bb));
00553 }
00554 }
00555
00556 void RuntimeHandle::insertOrUpdateBlackBoard(int id, const std::string& key, const std::vector<uint8_t>& value, const ros::Time& timestamp, int robot_id)
00557 {
00558 std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
00559 boost::upgrade_lock<boost::shared_mutex> lock(bb_mutex_);
00560 bb_it = blackboard_.find(id);
00561
00562 if(bb_it != blackboard_.end()) {
00563 std::map<std::string, BlackBoardTuple>::iterator sbbt_it = bb_it->second.find(key);
00564 if(sbbt_it != bb_it->second.end()) {
00565 BlackBoardTuple new_tuple(value, timestamp, robot_id);
00566 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00567 sbbt_it->second = new_tuple;
00568 }
00569 else {
00570 BlackBoardTuple new_tuple(value, timestamp, robot_id);
00571 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00572 bb_it->second.insert(std::pair<std::string, BlackBoardTuple>(key ,new_tuple));
00573 }
00574 }
00575 else {
00576 std::cout<<"ID "<<id<<" BlackBoard is not exist."<<std::endl;
00577 return;
00578 }
00579 }
00580
00581 bool RuntimeHandle::isBlackBoardTupleExist(int id, const std::string& key)
00582 {
00583 std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
00584 boost::shared_lock<boost::shared_mutex> lock(bb_mutex_);
00585 bb_it = blackboard_.find(id);
00586 if(bb_it != blackboard_.end()) {
00587 std::map<std::string, BlackBoardTuple>::iterator sbbt_it = bb_it->second.find(key);
00588 if(sbbt_it != bb_it->second.end()) {
00589 return true;
00590 }
00591 }
00592 return false;
00593 }
00594
00595 void RuntimeHandle::getBlackBoardTuple(int id, const std::string& key, BlackBoardTuple& bb_tuple)
00596 {
00597 std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
00598 boost::shared_lock<boost::shared_mutex> lock(bb_mutex_);
00599 bb_it = blackboard_.find(id);
00600 if(bb_it != blackboard_.end()) {
00601 std::map<std::string, BlackBoardTuple>::iterator sbbt_it = bb_it->second.find(key);
00602 if(sbbt_it != bb_it->second.end()) {
00603 bb_tuple = sbbt_it->second;
00604 }
00605 }
00606 }
00607
00608 int RuntimeHandle::getBlackBoardSize(int id)
00609 {
00610 std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
00611 boost::shared_lock<boost::shared_mutex> lock(bb_mutex_);
00612 bb_it = blackboard_.find(id);
00613
00614 if(bb_it != blackboard_.end()) {
00615 return bb_it->second.size();
00616 }
00617
00618 return 0;
00619 }
00620
00621 void RuntimeHandle::deleteBlackBoard(int id)
00622 {
00623 boost::unique_lock<boost::shared_mutex> lock(bb_mutex_);
00624 blackboard_.erase(id);
00625 }
00626
00627 void RuntimeHandle::deleteBlackBoardValue(int id, const std::string& key)
00628 {
00629 std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
00630 boost::upgrade_lock<boost::shared_mutex> lock(bb_mutex_);
00631 bb_it = blackboard_.find(id);
00632
00633 if(bb_it != blackboard_.end()) {
00634 std::map<std::string, BlackBoardTuple>::iterator sbbt_it=bb_it->second.find(key);
00635 if(sbbt_it != bb_it->second.end()) {
00636 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00637 bb_it->second.erase(key);
00638 }
00639 else {
00640 return;
00641 }
00642 }
00643 else {
00644 return;
00645 }
00646 }
00647
00648 void RuntimeHandle::printBlackBoard()
00649 {
00650 std::map<int, std::map<std::string, BlackBoardTuple> >::iterator bb_it;
00651 std::map<std::string, BlackBoardTuple>::iterator sbbt_it;
00652
00653 boost::shared_lock<boost::shared_mutex> lock(bb_mutex_);
00654 for (bb_it = blackboard_.begin(); bb_it != blackboard_.end(); bb_it++) {
00655 std::cout<<"["<<bb_it->first<<":"<<std::endl;
00656 std::map<std::string, BlackBoardTuple>* sbbt_pointer = &(bb_it->second);
00657 for (sbbt_it = sbbt_pointer->begin(); sbbt_it != sbbt_pointer->end(); sbbt_it++) {
00658
00659
00660
00661 }
00662 std::cout<<"]"<<std::endl;
00663 std::cout<<std::endl;
00664 }
00665 }
00666
00667 const float& RuntimeHandle::getNeighborDistance()
00668 {
00669 boost::shared_lock<boost::shared_mutex> lock(neighbor_distance_mutex_);
00670 return neighbor_distance_;
00671 }
00672
00673 void RuntimeHandle::setNeighborDistance(float neighbor_distance)
00674 {
00675 boost::unique_lock<boost::shared_mutex> lock(neighbor_distance_mutex_);
00676 neighbor_distance_ = neighbor_distance;
00677 cni_.reset(new CheckNeighbor(neighbor_distance_));
00678 std::cout<<"neighbor distance is set to "<<neighbor_distance_<<std::endl;
00679 clearNeighbors();
00680 }
00681
00682 void RuntimeHandle::insertOrUpdateListenerHelper(const std::string& key, const boost::shared_ptr<ListenerHelper> helper)
00683 {
00684 std::map<std::string, boost::shared_ptr<ListenerHelper> >::iterator lh_it;
00685 boost::upgrade_lock<boost::shared_mutex> lock(listener_helpers_mutex_);
00686 lh_it = listener_helpers_.find(key);
00687
00688 if(lh_it != listener_helpers_.end()) {
00689 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00690 lh_it->second = helper;
00691 }
00692 else {
00693 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00694 listener_helpers_.insert(std::pair<std::string, boost::shared_ptr<ListenerHelper> >(key ,helper));
00695 }
00696 }
00697
00698 const boost::shared_ptr<ListenerHelper> RuntimeHandle::getListenerHelper(const std::string& key)
00699 {
00700 std::map<std::string, boost::shared_ptr<ListenerHelper> >::iterator lh_it;
00701 boost::shared_lock<boost::shared_mutex> lock(listener_helpers_mutex_);
00702 lh_it = listener_helpers_.find(key);
00703
00704 if(lh_it != listener_helpers_.end()) {
00705 return lh_it->second;
00706 }
00707
00708 std::cout<<"could not get the callback function which has the key "<<key<<"!"<<std::endl;
00709 return NULL;
00710 }
00711
00712 void RuntimeHandle::deleteListenerHelper(const std::string& key)
00713 {
00714 boost::unique_lock<boost::shared_mutex> lock(listener_helpers_mutex_);
00715 listener_helpers_.erase(key);
00716 }
00717
00718 void RuntimeHandle::insertBarrier(int robot_id)
00719 {
00720 boost::unique_lock<boost::shared_mutex> lock(barrier_mutex_);
00721 barrier_.insert(robot_id);
00722 }
00723
00724 int RuntimeHandle::getBarrierSize()
00725 {
00726 boost::shared_lock<boost::shared_mutex> lock(barrier_mutex_);
00727 return barrier_.size();
00728 }
00729
00730 bool RuntimeHandle::getSCDSPSOValue(const std::string& aKey, SCDSPSODataTuple& aT)
00731 {
00732 boost::shared_lock<boost::shared_mutex> lock(scds_pso_tuple_mutex_);
00733 std::map<std::string, SCDSPSODataTuple>::iterator iter = scds_pso_tuple_.find(aKey);
00734 if (iter != scds_pso_tuple_.end()) {
00735 aT = iter->second;
00736 return true;
00737 }
00738 else return false;
00739 }
00740
00741 void RuntimeHandle::insertOrUpdateSCDSPSOValue(const std::string& aKey, const SCDSPSODataTuple& aT)
00742 {
00743 boost::upgrade_lock<boost::shared_mutex> lock(scds_pso_tuple_mutex_);
00744 std::map<std::string, SCDSPSODataTuple>::iterator iter = scds_pso_tuple_.find(aKey);
00745
00746
00747
00748
00749
00750
00751 if(iter != scds_pso_tuple_.end()) {
00752 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00753 iter->second = aT;
00754 }
00755 else {
00756 boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00757 scds_pso_tuple_[aKey] = aT;
00758 }
00759 }
00760 };