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

Go to the documentation of this file.
00001 
00029 #include <McpRequest.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 McpRequest::McpRequest(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 McpRequest::McpRequest(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 McpRequest::McpRequest(const std::string &streamname,const std::string &gblName){
00059        init(streamname,STRAT_NORMAL,gblName);
00060 }
00061 
00062 void McpRequest::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 McpRequest::~McpRequest(){
00103   myManager=NULL;
00104   if(myCOMObject){
00105 
00106          //      delete myCOMObject;
00107   }
00108 }
00109 
00110 
00111 void McpRequest::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::CMUIPCMcpRequest(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         if(m_supplier){
00142                 info.strategy.setCMode(C_SUPPLIER);
00143         }else{
00144                 info.strategy.setCMode(C_CONSUMER);
00145         }
00146         if(m_supplier){
00147                 info.status.setCMode(C_SUPPLIER);
00148         }else{
00149                 info.status.setCMode(C_CONSUMER);
00150         }
00151         if(m_supplier){
00152                 info.stream.setCMode(C_SUPPLIER);
00153         }else{
00154                 info.stream.setCMode(C_CONSUMER);
00155         }
00156         if(m_supplier){
00157                 info.robotModule.setCMode(C_SUPPLIER);
00158         }else{
00159                 info.robotModule.setCMode(C_CONSUMER);
00160         }
00161         if(m_supplier){
00162                 info.type.setCMode(C_SUPPLIER);
00163         }else{
00164                 info.type.setCMode(C_CONSUMER);
00165         }
00166         if(m_supplier){
00167                 info.gblname.setCMode(C_SUPPLIER);
00168         }else{
00169                 info.gblname.setCMode(C_CONSUMER);
00170         }
00171        }else{
00172               //this shouldn't happen, but there's no real problem with it.
00173               //it'd just be dumb.
00174               m_initialize=true;
00175               m_supplier=true;
00176        }
00177        
00178        requests=info;
00179 
00180 }
00181 
00182 void McpRequest::activateConsumer(){
00183        std::string localname;
00184        
00185        if(m_haveStream){
00186               m_initialize=false;
00187               m_supplier=false;
00188               if(myCOMObject&&myManager){
00189                       /* global name so we don't have conflicts with the naming service */
00190                       if(!m_haveGlobalname&&m_strategy==STRAT_NORMAL){
00191                               m_globalname=myManager->getName()+MCPREQUEST+m_streamname+"Consumer";
00192                               m_haveGlobalname=true;
00193                       }
00194 
00195                      myCOMObject->setGlobalName(m_globalname);
00196                      myCOMObject->setStreamName(m_streamname);
00197                      requests.source=m_globalname;
00198                      data.source=m_streamname;
00199                      info.source=m_streamname;
00200                      myCOMObject->setInfo(&info);
00201                      myCOMObject->setRequests(&requests);
00202                      myCOMObject->setStaging(&data);
00203                      myCOMObject->setAutoPing(m_doAutoPing);
00204                      myCOMObject->activateConsumer();
00205                      myCOMObject->setInfoVector(&m_infoVector);
00206                      myCOMObject->setRequestVector(&m_requestVector);
00207                      myCOMObject->setDataVector(&m_dataVector);
00208 
00209                      if(m_queue){
00210                             myCOMObject->setQueueMode(true);
00211                      }
00212               }else{
00213                       g_fatal("Attempted to activateConsumer before setting a COMObject and CommsManager. Please use registerConsumer instead.");
00214                      exit(-1);
00215               }
00216 
00217         if(m_supplier){
00218                 info.strategy.setCMode(C_SUPPLIER);
00219         }else{
00220                 info.strategy.setCMode(C_CONSUMER);
00221         }
00222         if(m_supplier){
00223                 info.status.setCMode(C_SUPPLIER);
00224         }else{
00225                 info.status.setCMode(C_CONSUMER);
00226         }
00227         if(m_supplier){
00228                 info.stream.setCMode(C_SUPPLIER);
00229         }else{
00230                 info.stream.setCMode(C_CONSUMER);
00231         }
00232         if(m_supplier){
00233                 info.robotModule.setCMode(C_SUPPLIER);
00234         }else{
00235                 info.robotModule.setCMode(C_CONSUMER);
00236         }
00237         if(m_supplier){
00238                 info.type.setCMode(C_SUPPLIER);
00239         }else{
00240                 info.type.setCMode(C_CONSUMER);
00241         }
00242         if(m_supplier){
00243                 info.gblname.setCMode(C_SUPPLIER);
00244         }else{
00245                 info.gblname.setCMode(C_CONSUMER);
00246         }
00247        }else{
00248               //no stream, so we can't connect yet. tell the system 
00249               //to initialize as soon as we've got one
00250               m_initialize=true;
00251        }
00252 
00253 }
00254 
00255 void McpRequest::runUpdate(){
00256        if(myCOMObject){
00257 
00258               myCOMObject->runUpdate(m_doPublishData,m_doPublishInfo);
00259               
00260               m_doPublishData=false;
00261               m_doPublishInfo=false;
00262               
00263        }
00264 }
00265 
00266 void McpRequest::setManager(CommsManager *myManager_){
00267 
00268   if(!myCOMObject){
00269     myManager=myManager_;
00270   }
00271 
00272 }
00273 
00274 void McpRequest::setQueueMode(const bool & val){
00275 
00276        if(!myCOMObject){
00277               m_queue=val;
00278        }else if(!m_queue&&val){
00279               m_queue=val;
00280               myCOMObject->setDataVector(&m_dataVector);
00281               myCOMObject->setQueueMode(true);
00282 
00283        }else if(!val&&m_queue){
00284               m_queue=val;
00285               myCOMObject->setQueueMode(false);
00286               while(!m_dataVector.empty()){
00287                      m_dataVector.pop();
00288               }
00289               while(!m_infoVector.empty()){
00290                      m_infoVector.pop();
00291               }
00292               while(!m_requestVector.empty()){
00293                      m_requestVector.pop();
00294               }
00295        }
00296 }
00297 
00298 bool McpRequest::newData(){
00299        if(m_queue){
00300               return !m_dataVector.empty();
00301        }else{
00302               if(myCOMObject){
00303                      return myCOMObject->newData();
00304               }
00305        }
00306 
00307        return false;
00308 }
00309 
00310 bool McpRequest::newInfo(){
00311        if(m_queue){
00312               if(m_supplier){
00313                      return !m_requestVector.empty();
00314               }
00315 
00316               return !m_infoVector.empty();
00317        }else{
00318               if(myCOMObject){
00319                      return myCOMObject->newInfo();
00320               }
00321        }
00322 
00323 
00324        return false;
00325 }
00326 
00327 void McpRequest::publishData(){
00328        if(m_supplier){
00329               if(m_doTimestamp){
00330                      data.timestamp.now();
00331               }
00332               if(m_doTag){
00333                      data.source=m_globalname;
00334               }
00335               if(m_queue){
00336                      McpRequestDataStruct temp;
00337                      temp.source=data.source;
00338 temp.target=data.target;
00339 temp.timestamp=data.timestamp;
00340 temp.state=data.state;
00341  
00342                      m_dataVector.push(temp);
00343                               //                     m_dataVector.push(data);
00344               }
00345               m_doPublishData=true;
00346        }
00347 }
00348 
00349 void McpRequest::publishInfo(){
00350        if(m_supplier){
00351 
00352               info.mType=MESSAGE_INFO;
00353               if(m_doTimestamp){
00354                      info.timestamp.now();
00355               }
00356 
00357               if(m_doTag){
00358                      info.source=m_globalname;
00359                      info.target="Any";
00360               }
00361 
00362               if(m_queue){
00363                      McpRequestInfoStruct temp;
00364                      
00365                     
00366                      temp.source=info.source;
00367 temp.strategy=info.strategy;
00368 temp.status=info.status;
00369 temp.stream=info.stream;
00370 temp.mType=info.mType;
00371 temp.target=info.target;
00372 temp.timestamp=info.timestamp;
00373 temp.robotModule=info.robotModule;
00374 temp.type=info.type;
00375 temp.gblname=info.gblname;
00376 
00377                      m_infoVector.push(temp);
00378                     
00379                      //              m_infoVector.push(info);
00380               }
00381 
00382               m_doPublishInfo=true;
00383        }else{
00384 
00385               requests.mType=MESSAGE_REQUEST;
00386               
00387               if(m_doTimestamp){
00388                      requests.timestamp.now();
00389               }
00390 
00391               if(m_doTag){
00392                      requests.source=m_globalname;
00393                      requests.target=m_streamname;
00394               }
00395 
00396               if(m_queue){
00397                      McpRequestInfoStruct temp;
00398         if(m_supplier){
00399                 temp.strategy.setCMode(C_SUPPLIER);
00400         }else{
00401                 temp.strategy.setCMode(C_CONSUMER);
00402         }
00403         if(m_supplier){
00404                 temp.status.setCMode(C_SUPPLIER);
00405         }else{
00406                 temp.status.setCMode(C_CONSUMER);
00407         }
00408         if(m_supplier){
00409                 temp.stream.setCMode(C_SUPPLIER);
00410         }else{
00411                 temp.stream.setCMode(C_CONSUMER);
00412         }
00413         if(m_supplier){
00414                 temp.robotModule.setCMode(C_SUPPLIER);
00415         }else{
00416                 temp.robotModule.setCMode(C_CONSUMER);
00417         }
00418         if(m_supplier){
00419                 temp.type.setCMode(C_SUPPLIER);
00420         }else{
00421                 temp.type.setCMode(C_CONSUMER);
00422         }
00423         if(m_supplier){
00424                 temp.gblname.setCMode(C_SUPPLIER);
00425         }else{
00426                 temp.gblname.setCMode(C_CONSUMER);
00427         }
00428                      temp.source=requests.source;
00429 temp.strategy=requests.strategy;
00430 temp.status=requests.status;
00431 temp.stream=requests.stream;
00432 temp.mType=requests.mType;
00433 temp.target=requests.target;
00434 temp.timestamp=requests.timestamp;
00435 temp.robotModule=requests.robotModule;
00436 temp.type=requests.type;
00437 temp.gblname=requests.gblname;
00438 
00439                      m_requestVector.push(temp);
00440 
00441                      //              m_requestVector.push(requests);
00442               }
00443 
00444               m_doPublishInfo=true;
00445        }
00446 }
00447 
00448 McpRequestDataStruct McpRequest::getNextData(){
00449        if(m_queue){
00450               McpRequestDataStruct retval;
00451               retval.source="NULL";
00452               if(m_dataVector.empty()){
00453                      return data;
00454               }
00455              
00456               retval.source=m_dataVector.front().source;
00457 retval.state=m_dataVector.front().state;
00458 retval.target=m_dataVector.front().target;
00459 retval.timestamp=m_dataVector.front().timestamp;
00460 
00461                        //             retval=m_dataVector.front();
00462               m_dataVector.pop();
00463               data=retval;
00464        
00465               return retval;
00466        }else{
00467               myCOMObject->resetData();
00468               return data;
00469        }
00470 }
00471 
00472 McpRequestInfoStruct McpRequest::getNextInfo(){
00473        if(m_queue){
00474               McpRequestInfoStruct retval;
00475         if(m_supplier){
00476                 retval.strategy.setCMode(C_SUPPLIER);
00477         }else{
00478                 retval.strategy.setCMode(C_CONSUMER);
00479         }
00480         if(m_supplier){
00481                 retval.status.setCMode(C_SUPPLIER);
00482         }else{
00483                 retval.status.setCMode(C_CONSUMER);
00484         }
00485         if(m_supplier){
00486                 retval.stream.setCMode(C_SUPPLIER);
00487         }else{
00488                 retval.stream.setCMode(C_CONSUMER);
00489         }
00490         if(m_supplier){
00491                 retval.robotModule.setCMode(C_SUPPLIER);
00492         }else{
00493                 retval.robotModule.setCMode(C_CONSUMER);
00494         }
00495         if(m_supplier){
00496                 retval.type.setCMode(C_SUPPLIER);
00497         }else{
00498                 retval.type.setCMode(C_CONSUMER);
00499         }
00500         if(m_supplier){
00501                 retval.gblname.setCMode(C_SUPPLIER);
00502         }else{
00503                 retval.gblname.setCMode(C_CONSUMER);
00504         }
00505               retval.source="NULL";
00506               if(m_supplier){
00507                      if(m_requestVector.empty()){
00508                             return requests;
00509                      }
00510                      retval.source=m_requestVector.front().source;
00511 retval.strategy=m_requestVector.front().strategy;
00512 retval.status=m_requestVector.front().status;
00513 retval.stream=m_requestVector.front().stream;
00514 retval.mType=m_requestVector.front().mType;
00515 retval.target=m_requestVector.front().target;
00516 retval.timestamp=m_requestVector.front().timestamp;
00517 retval.robotModule=m_requestVector.front().robotModule;
00518 retval.type=m_requestVector.front().type;
00519 retval.gblname=m_requestVector.front().gblname;
00520 
00521                               //                     retval=m_requestVector.front();
00522         if(m_supplier){
00523                 retval.strategy.setCMode(C_SUPPLIER);
00524         }else{
00525                 retval.strategy.setCMode(C_CONSUMER);
00526         }
00527         if(m_supplier){
00528                 retval.status.setCMode(C_SUPPLIER);
00529         }else{
00530                 retval.status.setCMode(C_CONSUMER);
00531         }
00532         if(m_supplier){
00533                 retval.stream.setCMode(C_SUPPLIER);
00534         }else{
00535                 retval.stream.setCMode(C_CONSUMER);
00536         }
00537         if(m_supplier){
00538                 retval.robotModule.setCMode(C_SUPPLIER);
00539         }else{
00540                 retval.robotModule.setCMode(C_CONSUMER);
00541         }
00542         if(m_supplier){
00543                 retval.type.setCMode(C_SUPPLIER);
00544         }else{
00545                 retval.type.setCMode(C_CONSUMER);
00546         }
00547         if(m_supplier){
00548                 retval.gblname.setCMode(C_SUPPLIER);
00549         }else{
00550                 retval.gblname.setCMode(C_CONSUMER);
00551         }
00552                      m_requestVector.pop();
00553                      requests=retval;
00554                      return retval;
00555               }else{
00556                      if(m_infoVector.empty()){
00557                             return info;
00558                      }
00559                      retval.source=m_infoVector.front().source;
00560 retval.strategy=m_infoVector.front().strategy;
00561 retval.status=m_infoVector.front().status;
00562 retval.stream=m_infoVector.front().stream;
00563 retval.mType=m_infoVector.front().mType;
00564 retval.target=m_infoVector.front().target;
00565 retval.timestamp=m_infoVector.front().timestamp;
00566 retval.robotModule=m_infoVector.front().robotModule;
00567 retval.type=m_infoVector.front().type;
00568 retval.gblname=m_infoVector.front().gblname;
00569 
00570         if(m_supplier){
00571                 retval.strategy.setCMode(C_SUPPLIER);
00572         }else{
00573                 retval.strategy.setCMode(C_CONSUMER);
00574         }
00575         if(m_supplier){
00576                 retval.status.setCMode(C_SUPPLIER);
00577         }else{
00578                 retval.status.setCMode(C_CONSUMER);
00579         }
00580         if(m_supplier){
00581                 retval.stream.setCMode(C_SUPPLIER);
00582         }else{
00583                 retval.stream.setCMode(C_CONSUMER);
00584         }
00585         if(m_supplier){
00586                 retval.robotModule.setCMode(C_SUPPLIER);
00587         }else{
00588                 retval.robotModule.setCMode(C_CONSUMER);
00589         }
00590         if(m_supplier){
00591                 retval.type.setCMode(C_SUPPLIER);
00592         }else{
00593                 retval.type.setCMode(C_CONSUMER);
00594         }
00595         if(m_supplier){
00596                 retval.gblname.setCMode(C_SUPPLIER);
00597         }else{
00598                 retval.gblname.setCMode(C_CONSUMER);
00599         }
00600                      m_infoVector.pop();
00601                      info=retval;
00602                      // the requests struct really should get the values in 
00603                      // info
00604                      requests=info;
00605                      
00606                      return retval;
00607               }
00608        }else{
00609               myCOMObject->resetInfo();
00610               if(m_supplier){
00611         if(m_supplier){
00612                 requests.strategy.setCMode(C_SUPPLIER);
00613         }else{
00614                 requests.strategy.setCMode(C_CONSUMER);
00615         }
00616         if(m_supplier){
00617                 requests.status.setCMode(C_SUPPLIER);
00618         }else{
00619                 requests.status.setCMode(C_CONSUMER);
00620         }
00621         if(m_supplier){
00622                 requests.stream.setCMode(C_SUPPLIER);
00623         }else{
00624                 requests.stream.setCMode(C_CONSUMER);
00625         }
00626         if(m_supplier){
00627                 requests.robotModule.setCMode(C_SUPPLIER);
00628         }else{
00629                 requests.robotModule.setCMode(C_CONSUMER);
00630         }
00631         if(m_supplier){
00632                 requests.type.setCMode(C_SUPPLIER);
00633         }else{
00634                 requests.type.setCMode(C_CONSUMER);
00635         }
00636         if(m_supplier){
00637                 requests.gblname.setCMode(C_SUPPLIER);
00638         }else{
00639                 requests.gblname.setCMode(C_CONSUMER);
00640         }
00641                      return requests;
00642               }else{
00643         if(m_supplier){
00644                 info.strategy.setCMode(C_SUPPLIER);
00645         }else{
00646                 info.strategy.setCMode(C_CONSUMER);
00647         }
00648         if(m_supplier){
00649                 info.status.setCMode(C_SUPPLIER);
00650         }else{
00651                 info.status.setCMode(C_CONSUMER);
00652         }
00653         if(m_supplier){
00654                 info.stream.setCMode(C_SUPPLIER);
00655         }else{
00656                 info.stream.setCMode(C_CONSUMER);
00657         }
00658         if(m_supplier){
00659                 info.robotModule.setCMode(C_SUPPLIER);
00660         }else{
00661                 info.robotModule.setCMode(C_CONSUMER);
00662         }
00663         if(m_supplier){
00664                 info.type.setCMode(C_SUPPLIER);
00665         }else{
00666                 info.type.setCMode(C_CONSUMER);
00667         }
00668         if(m_supplier){
00669                 info.gblname.setCMode(C_SUPPLIER);
00670         }else{
00671                 info.gblname.setCMode(C_CONSUMER);
00672         }
00673                      // the requests struct really should get the values in 
00674                      // info
00675                      requests=info;
00676                      return info;
00677               }
00678        }
00679 }
00680 
00681 void McpRequest::setStreamName(const std::string & name){
00682 
00683        if(m_supplier&&name=="Any"){
00684               return;
00685        }
00686 
00687        m_streamname=name;
00688        m_haveStream=true;
00689 
00690        if(m_supplier){
00691               m_globalname=name;
00692        }
00693        
00694        if(myCOMObject){
00695               myCOMObject->setStreamName(name);
00696        }
00697 
00698        if(m_initialize){
00699               if(m_supplier){
00700                      activateSupplier();
00701               }else{
00702                      activateConsumer();
00703               }
00704        }
00705 
00706        m_streamRequest=STAT_OKAY;
00707 }
00708 
00709 void McpRequest::setGlobalName(const std::string & name){
00710        
00711        if(name=="Any"){
00712               return;
00713        }
00714 
00715        m_globalname=name;
00716        m_haveGlobalname=true;
00717        if(m_supplier){
00718               m_streamname=name;
00719        }
00720 
00721        if(myCOMObject){
00722               myCOMObject->setGlobalName(name);
00723        }
00724 }
00725 
00726 void McpRequest::doPing(){
00727        if(myCOMObject){
00728               myCOMObject->doPing();
00729        }
00730 }
00731 
00732 void McpRequest::setAutoPing(const bool & val){
00733        m_doAutoPing=val; 
00734        if(myCOMObject){
00735               myCOMObject->setAutoPing(val);
00736        }
00737 }
00738 
00739 

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