MRXT: The Multi-Robot eXploration Tool
Multi-Robot autonomous exploration and mapping simulator.
src/mylib/ComThread.cpp
00001 
00002 #include "ComThread.h"
00003 
00004 using namespace std;
00005 
00006 int ComThread::nextId=0;
00007 std::map<int,std::list<string>*> ComThread::idmap;
00008 ClMutex ComThread::mut;
00009 
00010 ComThread::ComThread():
00011         ClThread()
00012 {
00013         mut.lock();
00014         mydir = ++nextId;
00015         idmap[mydir]= &messageQueue;
00016         mut.unlock();
00017 }
00018 
00019 ComThread::~ComThread(){
00020         mut.lock();
00021         idmap.erase(idmap.find(mydir));
00022         mut.unlock();
00023 };
00024 
00025 void ComThread::sendMessage(const string& msg, int dir){
00026         mut.lock();
00027         map<int,std::list<string>*>::iterator it;
00028         if (dir>=0){
00029                 it = idmap.find(dir);
00030                 if (it->second) it->second->push_back(msg);
00031         }
00032         else{
00033                 for ( it=idmap.begin() ; it != idmap.end(); it++ )
00034                         if (it->first != mydir) it->second->push_back(msg);
00035         }
00036         mut.unlock();
00037 };
00038 
00039 int ComThread::getMessage(string& msg){
00040         int res = 0;
00041         mut.lock();
00042         if (messageQueue.size()>0){
00043                 msg = messageQueue.front();
00044                 messageQueue.pop_front();
00045                 res = msg.size();
00046         }
00047         mut.unlock();
00048         return res;
00049 };
 All Classes Functions Variables Typedefs