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

Go to the documentation of this file.
00001 
00029 #include <Power.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 Power::Power(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 Power::Power(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 Power::Power(const std::string &streamname,const std::string &gblName){
00059        init(streamname,STRAT_NORMAL,gblName);
00060 }
00061 
00062 void Power::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 Power::~Power(){
00103   myManager=NULL;
00104   if(myCOMObject){
00105 
00106          //      delete myCOMObject;
00107   }
00108 }
00109 
00110 
00111 void Power::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::CMUIPCPower(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         data.min_volts=0;
00142         data.max_volts=0;
00143         data.volts=0;
00144         data.ac_online=0;
00145        }else{
00146               //this shouldn't happen, but there's no real problem with it.
00147               //it'd just be dumb.
00148               m_initialize=true;
00149               m_supplier=true;
00150        }
00151        
00152        requests=info;
00153 
00154 }
00155 
00156 void Power::activateConsumer(){
00157        std::string localname;
00158        
00159        if(m_haveStream){
00160               m_initialize=false;
00161               m_supplier=false;
00162               if(myCOMObject&&myManager){
00163                       /* global name so we don't have conflicts with the naming service */
00164                       if(!m_haveGlobalname&&m_strategy==STRAT_NORMAL){
00165                               m_globalname=myManager->getName()+POWER+m_streamname+"Consumer";
00166                               m_haveGlobalname=true;
00167                       }
00168 
00169                      myCOMObject->setGlobalName(m_globalname);
00170                      myCOMObject->setStreamName(m_streamname);
00171                      requests.source=m_globalname;
00172                      data.source=m_streamname;
00173                      info.source=m_streamname;
00174                      myCOMObject->setInfo(&info);
00175                      myCOMObject->setRequests(&requests);
00176                      myCOMObject->setStaging(&data);
00177                      myCOMObject->setAutoPing(m_doAutoPing);
00178                      myCOMObject->activateConsumer();
00179                      myCOMObject->setInfoVector(&m_infoVector);
00180                      myCOMObject->setRequestVector(&m_requestVector);
00181                      myCOMObject->setDataVector(&m_dataVector);
00182 
00183                      if(m_queue){
00184                             myCOMObject->setQueueMode(true);
00185                      }
00186               }else{
00187                       g_fatal("Attempted to activateConsumer before setting a COMObject and CommsManager. Please use registerConsumer instead.");
00188                      exit(-1);
00189               }
00190 
00191         data.min_volts=0;
00192         data.max_volts=0;
00193         data.volts=0;
00194         data.ac_online=0;
00195        }else{
00196               //no stream, so we can't connect yet. tell the system 
00197               //to initialize as soon as we've got one
00198               m_initialize=true;
00199        }
00200 
00201 }
00202 
00203 void Power::runUpdate(){
00204        if(myCOMObject){
00205 
00206               myCOMObject->runUpdate(m_doPublishData,m_doPublishInfo);
00207               
00208               m_doPublishData=false;
00209               m_doPublishInfo=false;
00210               
00211        }
00212 }
00213 
00214 void Power::setManager(CommsManager *myManager_){
00215 
00216   if(!myCOMObject){
00217     myManager=myManager_;
00218   }
00219 
00220 }
00221 
00222 void Power::setQueueMode(const bool & val){
00223 
00224        if(!myCOMObject){
00225               m_queue=val;
00226        }else if(!m_queue&&val){
00227               m_queue=val;
00228               myCOMObject->setDataVector(&m_dataVector);
00229               myCOMObject->setQueueMode(true);
00230 
00231        }else if(!val&&m_queue){
00232               m_queue=val;
00233               myCOMObject->setQueueMode(false);
00234               while(!m_dataVector.empty()){
00235                      m_dataVector.pop();
00236               }
00237               while(!m_infoVector.empty()){
00238                      m_infoVector.pop();
00239               }
00240               while(!m_requestVector.empty()){
00241                      m_requestVector.pop();
00242               }
00243        }
00244 }
00245 
00246 bool Power::newData(){
00247        if(m_queue){
00248               return !m_dataVector.empty();
00249        }else{
00250               if(myCOMObject){
00251                      return myCOMObject->newData();
00252               }
00253        }
00254 
00255        return false;
00256 }
00257 
00258 bool Power::newInfo(){
00259        if(m_queue){
00260               if(m_supplier){
00261                      return !m_requestVector.empty();
00262               }
00263 
00264               return !m_infoVector.empty();
00265        }else{
00266               if(myCOMObject){
00267                      return myCOMObject->newInfo();
00268               }
00269        }
00270 
00271 
00272        return false;
00273 }
00274 
00275 void Power::publishData(){
00276        if(m_supplier){
00277               if(m_doTimestamp){
00278                      data.timestamp.now();
00279               }
00280               if(m_doTag){
00281                      data.source=m_globalname;
00282               }
00283               if(m_queue){
00284                      PowerDataStruct temp;
00285         temp.min_volts=0;
00286         temp.max_volts=0;
00287         temp.volts=0;
00288         temp.ac_online=0;
00289                      temp.source=data.source;
00290 temp.min_volts=data.min_volts;
00291 temp.max_volts=data.max_volts;
00292 temp.volts=data.volts;
00293 temp.timestamp=data.timestamp;
00294 temp.ac_online=data.ac_online;
00295  
00296                      m_dataVector.push(temp);
00297                               //                     m_dataVector.push(data);
00298               }
00299               m_doPublishData=true;
00300        }
00301 }
00302 
00303 void Power::publishInfo(){
00304        if(m_supplier){
00305 
00306               info.mType=MESSAGE_INFO;
00307               if(m_doTimestamp){
00308                      info.timestamp.now();
00309               }
00310 
00311               if(m_doTag){
00312                      info.source=m_globalname;
00313                      info.target="Any";
00314               }
00315 
00316               if(m_queue){
00317                      PowerInfoStruct temp;
00318                      
00319                     
00320                      temp.source=info.source;
00321 temp.target=info.target;
00322 temp.timestamp=info.timestamp;
00323 temp.mType=info.mType;
00324 
00325                      m_infoVector.push(temp);
00326                     
00327                      //              m_infoVector.push(info);
00328               }
00329 
00330               m_doPublishInfo=true;
00331        }else{
00332 
00333               requests.mType=MESSAGE_REQUEST;
00334               
00335               if(m_doTimestamp){
00336                      requests.timestamp.now();
00337               }
00338 
00339               if(m_doTag){
00340                      requests.source=m_globalname;
00341                      requests.target=m_streamname;
00342               }
00343 
00344               if(m_queue){
00345                      PowerInfoStruct temp;
00346                      temp.source=requests.source;
00347 temp.target=requests.target;
00348 temp.timestamp=requests.timestamp;
00349 temp.mType=requests.mType;
00350 
00351                      m_requestVector.push(temp);
00352 
00353                      //              m_requestVector.push(requests);
00354               }
00355 
00356               m_doPublishInfo=true;
00357        }
00358 }
00359 
00360 PowerDataStruct Power::getNextData(){
00361        if(m_queue){
00362               PowerDataStruct retval;
00363         retval.min_volts=0;
00364         retval.max_volts=0;
00365         retval.volts=0;
00366         retval.ac_online=0;
00367               retval.source="NULL";
00368               if(m_dataVector.empty()){
00369                      return data;
00370               }
00371              
00372               retval.source=m_dataVector.front().source;
00373 retval.min_volts=m_dataVector.front().min_volts;
00374 retval.max_volts=m_dataVector.front().max_volts;
00375 retval.volts=m_dataVector.front().volts;
00376 retval.timestamp=m_dataVector.front().timestamp;
00377 retval.ac_online=m_dataVector.front().ac_online;
00378 
00379                        //             retval=m_dataVector.front();
00380               m_dataVector.pop();
00381               data=retval;
00382        
00383               return retval;
00384        }else{
00385               myCOMObject->resetData();
00386               return data;
00387        }
00388 }
00389 
00390 PowerInfoStruct Power::getNextInfo(){
00391        if(m_queue){
00392               PowerInfoStruct retval;
00393               retval.source="NULL";
00394               if(m_supplier){
00395                      if(m_requestVector.empty()){
00396                             return requests;
00397                      }
00398                      retval.source=m_requestVector.front().source;
00399 retval.target=m_requestVector.front().target;
00400 retval.timestamp=m_requestVector.front().timestamp;
00401 retval.mType=m_requestVector.front().mType;
00402 
00403                               //                     retval=m_requestVector.front();
00404                      m_requestVector.pop();
00405                      requests=retval;
00406                      return retval;
00407               }else{
00408                      if(m_infoVector.empty()){
00409                             return info;
00410                      }
00411                      retval.source=m_infoVector.front().source;
00412 retval.target=m_infoVector.front().target;
00413 retval.timestamp=m_infoVector.front().timestamp;
00414 retval.mType=m_infoVector.front().mType;
00415 
00416                      m_infoVector.pop();
00417                      info=retval;
00418                      // the requests struct really should get the values in 
00419                      // info
00420                      requests=info;
00421                      
00422                      return retval;
00423               }
00424        }else{
00425               myCOMObject->resetInfo();
00426               if(m_supplier){
00427                      return requests;
00428               }else{
00429                      // the requests struct really should get the values in 
00430                      // info
00431                      requests=info;
00432                      return info;
00433               }
00434        }
00435 }
00436 
00437 void Power::setStreamName(const std::string & name){
00438 
00439        if(m_supplier&&name=="Any"){
00440               return;
00441        }
00442 
00443        m_streamname=name;
00444        m_haveStream=true;
00445 
00446        if(m_supplier){
00447               m_globalname=name;
00448        }
00449        
00450        if(myCOMObject){
00451               myCOMObject->setStreamName(name);
00452        }
00453 
00454        if(m_initialize){
00455               if(m_supplier){
00456                      activateSupplier();
00457               }else{
00458                      activateConsumer();
00459               }
00460        }
00461 
00462        m_streamRequest=STAT_OKAY;
00463 }
00464 
00465 void Power::setGlobalName(const std::string & name){
00466        
00467        if(name=="Any"){
00468               return;
00469        }
00470 
00471        m_globalname=name;
00472        m_haveGlobalname=true;
00473        if(m_supplier){
00474               m_streamname=name;
00475        }
00476 
00477        if(myCOMObject){
00478               myCOMObject->setGlobalName(name);
00479        }
00480 }
00481 
00482 void Power::doPing(){
00483        if(myCOMObject){
00484               myCOMObject->doPing();
00485        }
00486 }
00487 
00488 void Power::setAutoPing(const bool & val){
00489        m_doAutoPing=val; 
00490        if(myCOMObject){
00491               myCOMObject->setAutoPing(val);
00492        }
00493 }
00494 
00495 

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