/home/fwph/code/wurde/rde/core/capabilities/MCPControl.cpp

Go to the documentation of this file.
00001 
00029 #include <MCPControl.H>
00030 #include <CommsManager.H>
00031 #include <Logger.H>
00032 
00033 // $Id: Type.cpp 68 2007-02-01 17:56:57Z fwph $
00034 
00035 using namespace WURDE;
00036 
00037 MCPControl::MCPControl(const std::string & streamname){
00038        init(streamname,STRAT_NORMAL,"Any");
00039 
00040        //global name will be set later
00041        m_haveGlobalname=false;
00042 }
00043 
00044 MCPControl::MCPControl(const ConnectionStrategy & strategy,const std::string & gblName){
00045        init("Any",strategy,gblName);
00046 
00047        if(strategy==STRAT_NORMAL){
00048               //then the global name really specifies the streamname,
00049               // and the global name should be assigned automagically once we have
00050               // confirmation that the stream has been started
00051               m_haveGlobalname=false;   
00052        }
00053 
00054        m_haveStream=false;
00055        
00056 }
00057 
00058 MCPControl::MCPControl(const std::string &streamname,const std::string &gblName){
00059        init(streamname,STRAT_NORMAL,gblName);
00060 }
00061 
00062 void MCPControl::init(const std::string &streamname,const ConnectionStrategy &strategy,const std::string &gblName){
00063   m_globalname=gblName;
00064   m_streamname=streamname;
00065 
00066   myCOMObject=NULL;
00067   
00068   //  data.source=streamname.c_str();
00069   requests=info;
00070 
00071   m_queue=false;
00072   m_supplier=false;
00073   m_doTimestamp=true;
00074   m_doTag=true;
00075 
00076   m_doPublishData=false;
00077   m_doPublishInfo=false;
00078 
00079   m_initialize=false;
00080   m_strategy=strategy;
00081 
00082   m_haveStream=true;
00083   m_haveGlobalname=true;
00084   m_doPublishData=false;
00085   m_doPublishInfo=false;
00086 
00087   m_doAutoPing=true;
00088 
00089   info.source=streamname;
00090   requests.source=m_globalname;
00091   info.target="Any";
00092   requests.target=streamname;
00093   info.timestamp.now();
00094   requests.timestamp.now();
00095 
00096   m_streamRequest=STAT_NULL;
00097   
00098   m_sourceModule="Unknown";
00099 }
00100 
00101 
00102 MCPControl::~MCPControl(){
00103   myManager=NULL;
00104   if(myCOMObject){
00105 
00106          //      delete myCOMObject;
00107   }
00108 }
00109 
00110 
00111 void MCPControl::activateSupplier(){
00112        if(m_haveStream){
00113               m_supplier=true;
00114               m_globalname=m_streamname;
00115               m_haveGlobalname=true;
00116               m_initialize=false;
00117               if(myCOMObject){
00118                       //                     myCOMObject=new CMUIPCRobotProtocol::CMUIPCMCPControl(m_globalname);
00119                      myCOMObject->setGlobalName(m_globalname);
00120                      myCOMObject->setStreamName(m_streamname);
00121                      myCOMObject->setInfo(&requests);
00122                      myCOMObject->setRequests(&info);
00123                      myCOMObject->setStaging(&data);
00124                      myCOMObject->activateSupplier();
00125                      myCOMObject->setAutoPing(m_doAutoPing);
00126                      myCOMObject->setInfoVector(&m_requestVector);
00127                      myCOMObject->setRequestVector(&m_infoVector);
00128                      myCOMObject->setDataVector(&m_dataVector);
00129 
00130                      data.source=m_streamname;
00131                      info.source=m_streamname;
00132 
00133                      if(m_queue){
00134                             myCOMObject->setQueueMode(true);
00135                      }
00136 
00137               }else{
00138                       g_fatal("Attempted to activateSupplier before setting a COMObject Please just use registerSupplier.");
00139                       exit(-1);
00140               }
00141        }else{
00142               //this shouldn't happen, but there's no real problem with it.
00143               //it'd just be dumb.
00144               m_initialize=true;
00145               m_supplier=true;
00146        }
00147        
00148        requests=info;
00149 
00150 }
00151 
00152 void MCPControl::activateConsumer(){
00153        std::string localname;
00154        
00155        if(m_haveStream){
00156               m_initialize=false;
00157               m_supplier=false;
00158               if(myCOMObject&&myManager){
00159                       /* global name so we don't have conflicts with the naming service */
00160                       if(!m_haveGlobalname&&m_strategy==STRAT_NORMAL){
00161                               m_globalname=myManager->getName()+MCPCONTROL+m_streamname+"Consumer";
00162                               m_haveGlobalname=true;
00163                       }
00164 
00165                      myCOMObject->setGlobalName(m_globalname);
00166                      myCOMObject->setStreamName(m_streamname);
00167                      requests.source=m_globalname;
00168                      data.source=m_streamname;
00169                      info.source=m_streamname;
00170                      myCOMObject->setInfo(&info);
00171                      myCOMObject->setRequests(&requests);
00172                      myCOMObject->setStaging(&data);
00173                      myCOMObject->setAutoPing(m_doAutoPing);
00174                      myCOMObject->activateConsumer();
00175                      myCOMObject->setInfoVector(&m_infoVector);
00176                      myCOMObject->setRequestVector(&m_requestVector);
00177                      myCOMObject->setDataVector(&m_dataVector);
00178 
00179                      if(m_queue){
00180                             myCOMObject->setQueueMode(true);
00181                      }
00182               }else{
00183                       g_fatal("Attempted to activateConsumer before setting a COMObject and CommsManager. Please use registerConsumer instead.");
00184                      exit(-1);
00185               }
00186 
00187        }else{
00188               //no stream, so we can't connect yet. tell the system 
00189               //to initialize as soon as we've got one
00190               m_initialize=true;
00191        }
00192 
00193 }
00194 
00195 void MCPControl::runUpdate(){
00196        if(myCOMObject){
00197 
00198               myCOMObject->runUpdate(m_doPublishData,m_doPublishInfo);
00199               
00200               m_doPublishData=false;
00201               m_doPublishInfo=false;
00202               
00203        }
00204 }
00205 
00206 void MCPControl::setManager(CommsManager *myManager_){
00207 
00208   if(!myCOMObject){
00209     myManager=myManager_;
00210   }
00211 
00212 }
00213 
00214 void MCPControl::setQueueMode(const bool & val){
00215 
00216        if(!myCOMObject){
00217               m_queue=val;
00218        }else if(!m_queue&&val){
00219               m_queue=val;
00220               myCOMObject->setDataVector(&m_dataVector);
00221               myCOMObject->setQueueMode(true);
00222 
00223        }else if(!val&&m_queue){
00224               m_queue=val;
00225               myCOMObject->setQueueMode(false);
00226               while(!m_dataVector.empty()){
00227                      m_dataVector.pop();
00228               }
00229               while(!m_infoVector.empty()){
00230                      m_infoVector.pop();
00231               }
00232               while(!m_requestVector.empty()){
00233                      m_requestVector.pop();
00234               }
00235        }
00236 }
00237 
00238 bool MCPControl::newData(){
00239        if(m_queue){
00240               return !m_dataVector.empty();
00241        }else{
00242               if(myCOMObject){
00243                      return myCOMObject->newData();
00244               }
00245        }
00246 
00247        return false;
00248 }
00249 
00250 bool MCPControl::newInfo(){
00251        if(m_queue){
00252               if(m_supplier){
00253                      return !m_requestVector.empty();
00254               }
00255 
00256               return !m_infoVector.empty();
00257        }else{
00258               if(myCOMObject){
00259                      return myCOMObject->newInfo();
00260               }
00261        }
00262 
00263 
00264        return false;
00265 }
00266 
00267 void MCPControl::publishData(){
00268        if(m_supplier){
00269               if(m_doTimestamp){
00270                      data.timestamp.now();
00271               }
00272               if(m_doTag){
00273                      data.source=m_globalname;
00274               }
00275               if(m_queue){
00276                      MCPControlDataStruct temp;
00277                      temp.source=data.source;
00278 temp.target=data.target;
00279 temp.timestamp=data.timestamp;
00280 temp.state=data.state;
00281  
00282                      m_dataVector.push(temp);
00283                               //                     m_dataVector.push(data);
00284               }
00285               m_doPublishData=true;
00286        }
00287 }
00288 
00289 void MCPControl::publishInfo(){
00290        if(m_supplier){
00291 
00292               info.mType=MESSAGE_INFO;
00293               if(m_doTimestamp){
00294                      info.timestamp.now();
00295               }
00296 
00297               if(m_doTag){
00298                      info.source=m_globalname;
00299                      info.target="Any";
00300               }
00301 
00302               if(m_queue){
00303                      MCPControlInfoStruct temp;
00304                      
00305                     
00306                      temp.source=info.source;
00307 temp.target=info.target;
00308 temp.timestamp=info.timestamp;
00309 temp.mType=info.mType;
00310 
00311                      m_infoVector.push(temp);
00312                     
00313                      //              m_infoVector.push(info);
00314               }
00315 
00316               m_doPublishInfo=true;
00317        }else{
00318 
00319               requests.mType=MESSAGE_REQUEST;
00320               
00321               if(m_doTimestamp){
00322                      requests.timestamp.now();
00323               }
00324 
00325               if(m_doTag){
00326                      requests.source=m_globalname;
00327                      requests.target=m_streamname;
00328               }
00329 
00330               if(m_queue){
00331                      MCPControlInfoStruct temp;
00332                      temp.source=requests.source;
00333 temp.target=requests.target;
00334 temp.timestamp=requests.timestamp;
00335 temp.mType=requests.mType;
00336 
00337                      m_requestVector.push(temp);
00338 
00339                      //              m_requestVector.push(requests);
00340               }
00341 
00342               m_doPublishInfo=true;
00343        }
00344 }
00345 
00346 MCPControlDataStruct MCPControl::getNextData(){
00347        if(m_queue){
00348               MCPControlDataStruct retval;
00349               retval.source="NULL";
00350               if(m_dataVector.empty()){
00351                      return data;
00352               }
00353              
00354               retval.source=m_dataVector.front().source;
00355 retval.target=m_dataVector.front().target;
00356 retval.timestamp=m_dataVector.front().timestamp;
00357 retval.state=m_dataVector.front().state;
00358 
00359                        //             retval=m_dataVector.front();
00360               m_dataVector.pop();
00361               data=retval;
00362        
00363               return retval;
00364        }else{
00365               myCOMObject->resetData();
00366               return data;
00367        }
00368 }
00369 
00370 MCPControlInfoStruct MCPControl::getNextInfo(){
00371        if(m_queue){
00372               MCPControlInfoStruct retval;
00373               retval.source="NULL";
00374               if(m_supplier){
00375                      if(m_requestVector.empty()){
00376                             return requests;
00377                      }
00378                      retval.source=m_requestVector.front().source;
00379 retval.target=m_requestVector.front().target;
00380 retval.timestamp=m_requestVector.front().timestamp;
00381 retval.mType=m_requestVector.front().mType;
00382 
00383                               //                     retval=m_requestVector.front();
00384                      m_requestVector.pop();
00385                      requests=retval;
00386                      return retval;
00387               }else{
00388                      if(m_infoVector.empty()){
00389                             return info;
00390                      }
00391                      retval.source=m_infoVector.front().source;
00392 retval.target=m_infoVector.front().target;
00393 retval.timestamp=m_infoVector.front().timestamp;
00394 retval.mType=m_infoVector.front().mType;
00395 
00396                      m_infoVector.pop();
00397                      info=retval;
00398                      // the requests struct really should get the values in 
00399                      // info
00400                      requests=info;
00401                      
00402                      return retval;
00403               }
00404        }else{
00405               myCOMObject->resetInfo();
00406               if(m_supplier){
00407                      return requests;
00408               }else{
00409                      // the requests struct really should get the values in 
00410                      // info
00411                      requests=info;
00412                      return info;
00413               }
00414        }
00415 }
00416 
00417 void MCPControl::setStreamName(const std::string & name){
00418 
00419        if(m_supplier&&name=="Any"){
00420               return;
00421        }
00422 
00423        m_streamname=name;
00424        m_haveStream=true;
00425 
00426        if(m_supplier){
00427               m_globalname=name;
00428        }
00429        
00430        if(myCOMObject){
00431               myCOMObject->setStreamName(name);
00432        }
00433 
00434        if(m_initialize){
00435               if(m_supplier){
00436                      activateSupplier();
00437               }else{
00438                      activateConsumer();
00439               }
00440        }
00441 
00442        m_streamRequest=STAT_OKAY;
00443 }
00444 
00445 void MCPControl::setGlobalName(const std::string & name){
00446        
00447        if(name=="Any"){
00448               return;
00449        }
00450 
00451        m_globalname=name;
00452        m_haveGlobalname=true;
00453        if(m_supplier){
00454               m_streamname=name;
00455        }
00456 
00457        if(myCOMObject){
00458               myCOMObject->setGlobalName(name);
00459        }
00460 }
00461 
00462 void MCPControl::doPing(){
00463        if(myCOMObject){
00464               myCOMObject->doPing();
00465        }
00466 }
00467 
00468 void MCPControl::setAutoPing(const bool & val){
00469        m_doAutoPing=val; 
00470        if(myCOMObject){
00471               myCOMObject->setAutoPing(val);
00472        }
00473 }
00474 
00475 

Generated on Thu Feb 1 15:31:53 2007 for WURDE by  doxygen 1.5.1