MRXT: The Multi-Robot eXploration Tool
Multi-Robot autonomous exploration and mapping simulator.
include/mylib/ComThread.h
00001 /*
00002 *
00003 * Author: Miguel Julia <mjulia@umh.es> 
00004 * 
00005 * Date:   2009
00006 * 
00007 * Class ComThead
00008 *
00009 */
00010 
00011 #pragma once
00012 #ifndef __CLASS__COMMUNICATIVE__THREAD__
00013 #define __CLASS__COMMUNICATIVE__THREAD__
00014 
00015 #include "ClThread.h"
00016 #include "ClMutex.h"
00017 #include <list>
00018 #include <map>
00019 #include <string>
00020 
00021 
00022 /**
00023 * @brief Implements communication between threads
00024 *
00025 * Objects of classes derived from ComThread can send (to a specific address) or broadcast (to everybody) messages to other ComThread derived objects
00026 *
00027 * The messages received are cummulated in a FIFO queue that can be read at any moment
00028 *
00029 */
00030 class ComThread : public ClThread{
00031 public:
00032         /// constructor
00033         ComThread();
00034         /// destroyer
00035         virtual ~ComThread();
00036 
00037 protected:
00038         /// Sends a message to a specific address (dir < 0 to broadcast)
00039         void sendMessage(const std::string& msg, int dir);
00040         /// Reads the oldest message in the queue
00041         int getMessage(std::string& msg);
00042         /// Returns the communication address of the object
00043         int getDir(){return mydir;};
00044 
00045 private:
00046 
00047         static int nextId;
00048         static std::map<int,std::list<std::string>*> idmap;
00049         static ClMutex mut;
00050 
00051         std::list<std::string> messageQueue;
00052         int mydir;
00053 
00054         /// this virtual method will be executed when trying to start the thead, must return 0 if ok
00055         virtual int setup(){return 0;};
00056 
00057         /// this virtual method will be executed when the stop method is called
00058         virtual void onStop(){};
00059 
00060         /// virtual method for the thread main process. 
00061         virtual void execute(){};
00062 
00063 };
00064 
00065 
00066 #endif
00067 
 All Classes Functions Variables Typedefs