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

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

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