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

Go to the documentation of this file.
00001 
00029 #include <ImageTransport.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 ImageTransport::ImageTransport(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 ImageTransport::ImageTransport(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 ImageTransport::ImageTransport(const std::string &streamname,const std::string &gblName){
00059        init(streamname,STRAT_NORMAL,gblName);
00060 }
00061 
00062 void ImageTransport::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 ImageTransport::~ImageTransport(){
00103   myManager=NULL;
00104   if(myCOMObject){
00105 
00106          //      delete myCOMObject;
00107   }
00108 }
00109 
00110 
00111 void ImageTransport::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::CMUIPCImageTransport(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.imagesize=0;
00142         data.image=NULL;
00143         if(m_supplier){
00144                 info.width.setCMode(C_SUPPLIER);
00145         }else{
00146                 info.width.setCMode(C_CONSUMER);
00147         }
00148         if(m_supplier){
00149                 info.userChannels.setCMode(C_SUPPLIER);
00150         }else{
00151                 info.userChannels.setCMode(C_CONSUMER);
00152         }
00153         if(m_supplier){
00154                 info.height.setCMode(C_SUPPLIER);
00155         }else{
00156                 info.height.setCMode(C_CONSUMER);
00157         }
00158         if(m_supplier){
00159                 info.compression.setCMode(C_SUPPLIER);
00160         }else{
00161                 info.compression.setCMode(C_CONSUMER);
00162         }
00163         if(m_supplier){
00164                 info.format.setCMode(C_SUPPLIER);
00165         }else{
00166                 info.format.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 ImageTransport::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()+IMAGETRANSPORT+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.imagesize=0;
00215         data.image=NULL;
00216         if(m_supplier){
00217                 info.width.setCMode(C_SUPPLIER);
00218         }else{
00219                 info.width.setCMode(C_CONSUMER);
00220         }
00221         if(m_supplier){
00222                 info.userChannels.setCMode(C_SUPPLIER);
00223         }else{
00224                 info.userChannels.setCMode(C_CONSUMER);
00225         }
00226         if(m_supplier){
00227                 info.height.setCMode(C_SUPPLIER);
00228         }else{
00229                 info.height.setCMode(C_CONSUMER);
00230         }
00231         if(m_supplier){
00232                 info.compression.setCMode(C_SUPPLIER);
00233         }else{
00234                 info.compression.setCMode(C_CONSUMER);
00235         }
00236         if(m_supplier){
00237                 info.format.setCMode(C_SUPPLIER);
00238         }else{
00239                 info.format.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 ImageTransport::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 ImageTransport::setManager(CommsManager *myManager_){
00261 
00262   if(!myCOMObject){
00263     myManager=myManager_;
00264   }
00265 
00266 }
00267 
00268 void ImageTransport::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 ImageTransport::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 ImageTransport::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 ImageTransport::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                      ImageTransportDataStruct temp;
00331         temp.imagesize=0;
00332         temp.image=NULL;
00333                      temp.source=data.source;
00334 temp.timestamp=data.timestamp;
00335 temp.imagesize=data.imagesize;
00336         if(temp.image&&temp.imagesize!=data.imagesize){
00337                                         temp.imagesize=data.imagesize;
00338                                         delete [] temp.image;
00339                                         temp.image=NULL;
00340                                 }
00341                                 if(!temp.image){
00342                                         temp.image=new unsigned char[temp.imagesize];
00343                                 }
00344                                 memcpy(temp.image,data.image,temp.imagesize);
00345  
00346                      m_dataVector.push(temp);
00347                               //                     m_dataVector.push(data);
00348               }
00349               m_doPublishData=true;
00350        }
00351 }
00352 
00353 void ImageTransport::publishInfo(){
00354        if(m_supplier){
00355 
00356               info.mType=MESSAGE_INFO;
00357               if(m_doTimestamp){
00358                      info.timestamp.now();
00359               }
00360 
00361               if(m_doTag){
00362                      info.source=m_globalname;
00363                      info.target="Any";
00364               }
00365 
00366               if(m_queue){
00367                      ImageTransportInfoStruct temp;
00368                      
00369                     
00370                      temp.width=info.width;
00371 temp.source=info.source;
00372 temp.userChannels=info.userChannels;
00373 temp.height=info.height;
00374 temp.mType=info.mType;
00375 temp.compression=info.compression;
00376 temp.target=info.target;
00377 temp.timestamp=info.timestamp;
00378 temp.format=info.format;
00379 
00380                      m_infoVector.push(temp);
00381                     
00382                      //              m_infoVector.push(info);
00383               }
00384 
00385               m_doPublishInfo=true;
00386        }else{
00387 
00388               requests.mType=MESSAGE_REQUEST;
00389               
00390               if(m_doTimestamp){
00391                      requests.timestamp.now();
00392               }
00393 
00394               if(m_doTag){
00395                      requests.source=m_globalname;
00396                      requests.target=m_streamname;
00397               }
00398 
00399               if(m_queue){
00400                      ImageTransportInfoStruct temp;
00401         if(m_supplier){
00402                 temp.width.setCMode(C_SUPPLIER);
00403         }else{
00404                 temp.width.setCMode(C_CONSUMER);
00405         }
00406         if(m_supplier){
00407                 temp.userChannels.setCMode(C_SUPPLIER);
00408         }else{
00409                 temp.userChannels.setCMode(C_CONSUMER);
00410         }
00411         if(m_supplier){
00412                 temp.height.setCMode(C_SUPPLIER);
00413         }else{
00414                 temp.height.setCMode(C_CONSUMER);
00415         }
00416         if(m_supplier){
00417                 temp.compression.setCMode(C_SUPPLIER);
00418         }else{
00419                 temp.compression.setCMode(C_CONSUMER);
00420         }
00421         if(m_supplier){
00422                 temp.format.setCMode(C_SUPPLIER);
00423         }else{
00424                 temp.format.setCMode(C_CONSUMER);
00425         }
00426                      temp.width=requests.width;
00427 temp.source=requests.source;
00428 temp.userChannels=requests.userChannels;
00429 temp.height=requests.height;
00430 temp.mType=requests.mType;
00431 temp.compression=requests.compression;
00432 temp.target=requests.target;
00433 temp.format=requests.format;
00434 temp.timestamp=requests.timestamp;
00435 
00436                      m_requestVector.push(temp);
00437 
00438                      //              m_requestVector.push(requests);
00439               }
00440 
00441               m_doPublishInfo=true;
00442        }
00443 }
00444 
00445 ImageTransportDataStruct ImageTransport::getNextData(){
00446        if(m_queue){
00447               ImageTransportDataStruct retval;
00448         retval.imagesize=0;
00449         retval.image=NULL;
00450               retval.source="NULL";
00451               if(m_dataVector.empty()){
00452                      return data;
00453               }
00454              
00455               retval.source=m_dataVector.front().source;
00456 retval.imagesize=m_dataVector.front().imagesize;
00457         if(retval.image&&retval.imagesize!=m_dataVector.front().imagesize){
00458                                         retval.imagesize=m_dataVector.front().imagesize;
00459                                         delete [] retval.image;
00460                                         retval.image=NULL;
00461                                 }
00462                                 if(!retval.image){
00463                                         retval.image=new unsigned char[retval.imagesize];
00464                                 }
00465                                 memcpy(retval.image,m_dataVector.front().image,retval.imagesize);
00466 retval.timestamp=m_dataVector.front().timestamp;
00467 
00468                        //             retval=m_dataVector.front();
00469               m_dataVector.pop();
00470               data=retval;
00471        
00472               return retval;
00473        }else{
00474               myCOMObject->resetData();
00475               return data;
00476        }
00477 }
00478 
00479 ImageTransportInfoStruct ImageTransport::getNextInfo(){
00480        if(m_queue){
00481               ImageTransportInfoStruct retval;
00482         if(m_supplier){
00483                 retval.width.setCMode(C_SUPPLIER);
00484         }else{
00485                 retval.width.setCMode(C_CONSUMER);
00486         }
00487         if(m_supplier){
00488                 retval.userChannels.setCMode(C_SUPPLIER);
00489         }else{
00490                 retval.userChannels.setCMode(C_CONSUMER);
00491         }
00492         if(m_supplier){
00493                 retval.height.setCMode(C_SUPPLIER);
00494         }else{
00495                 retval.height.setCMode(C_CONSUMER);
00496         }
00497         if(m_supplier){
00498                 retval.compression.setCMode(C_SUPPLIER);
00499         }else{
00500                 retval.compression.setCMode(C_CONSUMER);
00501         }
00502         if(m_supplier){
00503                 retval.format.setCMode(C_SUPPLIER);
00504         }else{
00505                 retval.format.setCMode(C_CONSUMER);
00506         }
00507               retval.source="NULL";
00508               if(m_supplier){
00509                      if(m_requestVector.empty()){
00510                             return requests;
00511                      }
00512                      retval.width=m_requestVector.front().width;
00513 retval.source=m_requestVector.front().source;
00514 retval.userChannels=m_requestVector.front().userChannels;
00515 retval.height=m_requestVector.front().height;
00516 retval.mType=m_requestVector.front().mType;
00517 retval.compression=m_requestVector.front().compression;
00518 retval.target=m_requestVector.front().target;
00519 retval.format=m_requestVector.front().format;
00520 retval.timestamp=m_requestVector.front().timestamp;
00521 
00522                               //                     retval=m_requestVector.front();
00523         if(m_supplier){
00524                 retval.width.setCMode(C_SUPPLIER);
00525         }else{
00526                 retval.width.setCMode(C_CONSUMER);
00527         }
00528         if(m_supplier){
00529                 retval.userChannels.setCMode(C_SUPPLIER);
00530         }else{
00531                 retval.userChannels.setCMode(C_CONSUMER);
00532         }
00533         if(m_supplier){
00534                 retval.height.setCMode(C_SUPPLIER);
00535         }else{
00536                 retval.height.setCMode(C_CONSUMER);
00537         }
00538         if(m_supplier){
00539                 retval.compression.setCMode(C_SUPPLIER);
00540         }else{
00541                 retval.compression.setCMode(C_CONSUMER);
00542         }
00543         if(m_supplier){
00544                 retval.format.setCMode(C_SUPPLIER);
00545         }else{
00546                 retval.format.setCMode(C_CONSUMER);
00547         }
00548                      m_requestVector.pop();
00549                      requests=retval;
00550                      return retval;
00551               }else{
00552                      if(m_infoVector.empty()){
00553                             return info;
00554                      }
00555                      retval.width=m_infoVector.front().width;
00556 retval.source=m_infoVector.front().source;
00557 retval.userChannels=m_infoVector.front().userChannels;
00558 retval.height=m_infoVector.front().height;
00559 retval.mType=m_infoVector.front().mType;
00560 retval.compression=m_infoVector.front().compression;
00561 retval.target=m_infoVector.front().target;
00562 retval.format=m_infoVector.front().format;
00563 retval.timestamp=m_infoVector.front().timestamp;
00564 
00565         if(m_supplier){
00566                 retval.width.setCMode(C_SUPPLIER);
00567         }else{
00568                 retval.width.setCMode(C_CONSUMER);
00569         }
00570         if(m_supplier){
00571                 retval.userChannels.setCMode(C_SUPPLIER);
00572         }else{
00573                 retval.userChannels.setCMode(C_CONSUMER);
00574         }
00575         if(m_supplier){
00576                 retval.height.setCMode(C_SUPPLIER);
00577         }else{
00578                 retval.height.setCMode(C_CONSUMER);
00579         }
00580         if(m_supplier){
00581                 retval.compression.setCMode(C_SUPPLIER);
00582         }else{
00583                 retval.compression.setCMode(C_CONSUMER);
00584         }
00585         if(m_supplier){
00586                 retval.format.setCMode(C_SUPPLIER);
00587         }else{
00588                 retval.format.setCMode(C_CONSUMER);
00589         }
00590                      m_infoVector.pop();
00591                      info=retval;
00592                      // the requests struct really should get the values in 
00593                      // info
00594                      requests=info;
00595                      
00596                      return retval;
00597               }
00598        }else{
00599               myCOMObject->resetInfo();
00600               if(m_supplier){
00601         if(m_supplier){
00602                 requests.width.setCMode(C_SUPPLIER);
00603         }else{
00604                 requests.width.setCMode(C_CONSUMER);
00605         }
00606         if(m_supplier){
00607                 requests.userChannels.setCMode(C_SUPPLIER);
00608         }else{
00609                 requests.userChannels.setCMode(C_CONSUMER);
00610         }
00611         if(m_supplier){
00612                 requests.height.setCMode(C_SUPPLIER);
00613         }else{
00614                 requests.height.setCMode(C_CONSUMER);
00615         }
00616         if(m_supplier){
00617                 requests.compression.setCMode(C_SUPPLIER);
00618         }else{
00619                 requests.compression.setCMode(C_CONSUMER);
00620         }
00621         if(m_supplier){
00622                 requests.format.setCMode(C_SUPPLIER);
00623         }else{
00624                 requests.format.setCMode(C_CONSUMER);
00625         }
00626                      return requests;
00627               }else{
00628         if(m_supplier){
00629                 info.width.setCMode(C_SUPPLIER);
00630         }else{
00631                 info.width.setCMode(C_CONSUMER);
00632         }
00633         if(m_supplier){
00634                 info.userChannels.setCMode(C_SUPPLIER);
00635         }else{
00636                 info.userChannels.setCMode(C_CONSUMER);
00637         }
00638         if(m_supplier){
00639                 info.height.setCMode(C_SUPPLIER);
00640         }else{
00641                 info.height.setCMode(C_CONSUMER);
00642         }
00643         if(m_supplier){
00644                 info.compression.setCMode(C_SUPPLIER);
00645         }else{
00646                 info.compression.setCMode(C_CONSUMER);
00647         }
00648         if(m_supplier){
00649                 info.format.setCMode(C_SUPPLIER);
00650         }else{
00651                 info.format.setCMode(C_CONSUMER);
00652         }
00653                      // the requests struct really should get the values in 
00654                      // info
00655                      requests=info;
00656                      return info;
00657               }
00658        }
00659 }
00660 
00661 void ImageTransport::setStreamName(const std::string & name){
00662 
00663        if(m_supplier&&name=="Any"){
00664               return;
00665        }
00666 
00667        m_streamname=name;
00668        m_haveStream=true;
00669 
00670        if(m_supplier){
00671               m_globalname=name;
00672        }
00673        
00674        if(myCOMObject){
00675               myCOMObject->setStreamName(name);
00676        }
00677 
00678        if(m_initialize){
00679               if(m_supplier){
00680                      activateSupplier();
00681               }else{
00682                      activateConsumer();
00683               }
00684        }
00685 
00686        m_streamRequest=STAT_OKAY;
00687 }
00688 
00689 void ImageTransport::setGlobalName(const std::string & name){
00690        
00691        if(name=="Any"){
00692               return;
00693        }
00694 
00695        m_globalname=name;
00696        m_haveGlobalname=true;
00697        if(m_supplier){
00698               m_streamname=name;
00699        }
00700 
00701        if(myCOMObject){
00702               myCOMObject->setGlobalName(name);
00703        }
00704 }
00705 
00706 void ImageTransport::doPing(){
00707        if(myCOMObject){
00708               myCOMObject->doPing();
00709        }
00710 }
00711 
00712 void ImageTransport::setAutoPing(const bool & val){
00713        m_doAutoPing=val; 
00714        if(myCOMObject){
00715               myCOMObject->setAutoPing(val);
00716        }
00717 }
00718 
00719 

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