orca-robotics INTRODUCTION Overview Download and Install Quick Start Documentation Publications REPOSITORY Interfaces Components Libraries Utilities Software Map DEVELOPER Tutorials Examples Dev Guide Dashboard Wiki login/pass: orca/orca PEOPLE Contributors Users Project Download Mailing lists
|
icestormelement.h00001 /* 00002 * Orca-Robotics Project: Components for robotics 00003 * http://orca-robotics.sf.net/ 00004 * Copyright (c) 2004-2008 Alex Brooks, Alexei Makarenko, Tobias Kaupp 00005 * 00006 * This copy of Orca is licensed to you under the terms described in 00007 * the LICENSE file included in this distribution. 00008 * 00009 */ 00010 00011 #ifndef ORCAGUI_ICESTORM_ELEMENT_H 00012 #define ORCAGUI_ICESTORM_ELEMENT_H 00013 00014 #include <assert.h> 00015 #include <Ice/Ice.h> 00016 #include <IceStorm/IceStorm.h> 00017 #include <orcaqguielementutil/icestormlistener.h> 00018 #include <hydroqgui/hydroqgui.h> 00019 #include <hydroqguielementutil/guielement2d.h> 00020 00021 namespace orcaqguielementutil { 00022 00030 template<class PainterType, class DataType, class ProxyType, class ConsumerType, class ConsumerPrxType> 00031 class IceStormElement : public hydroqguielementutil::GuiElement2d 00032 { 00033 00034 public: 00035 00038 IceStormElement( const orcaice::Context &context, 00039 const std::string &proxyString, 00040 PainterType &painter, 00041 const double timeoutMs=5000.0 ) 00042 : context_(context), 00043 listener_(context,proxyString), 00044 painter_(painter), 00045 timeoutMs_(timeoutMs), 00046 isConnected_(false) 00047 { 00048 }; 00049 00051 virtual void actionOnConnection()=0; 00052 00054 virtual void update(); 00055 00057 virtual void paint( QPainter *p, int z ) 00058 { painter_.paint( p, z ); } 00059 00060 bool paintThisLayer( int z ) const 00061 { return painter_.paintThisLayer( z ); } 00062 00063 protected: 00064 00065 orcaice::Context context_; 00066 00067 DataType data_; 00068 00069 bool needToUpdate(); 00070 00072 orcaqguielementutil::IceStormListener<DataType, 00073 ProxyType, 00074 ConsumerType, 00075 ConsumerPrxType> listener_; 00076 00078 PainterType &painter_; 00079 00080 double timeoutMs_; 00081 bool isConnected_; 00082 }; 00083 00084 template<class PainterType, class DataType, class ProxyType, class ConsumerType, class ConsumerPrxType> 00085 bool 00086 IceStormElement<PainterType,DataType,ProxyType,ConsumerType,ConsumerPrxType>::needToUpdate() 00087 { 00088 if ( !isConnected_ ) 00089 { 00090 if ( listener_.connect() == 0 ) 00091 { 00092 isConnected_ = true; 00093 actionOnConnection(); 00094 } 00095 return false; 00096 } 00097 00098 if ( !listener_.buffer().isEmpty() ) 00099 { 00100 // An object has arrived in our buffer. We need to update. 00101 return true; 00102 } 00103 00104 // The buffer is empty. How long since we last received something? 00105 if ( timeoutMs_ != -1 && listener_.msSinceReceipt() >= timeoutMs_ ) 00106 { 00107 std::cout<<"TRACE(icestormelement.h): Haven't received anything from " 00108 << listener_.interfaceName() << " for " << listener_.msSinceReceipt() << "ms" << std::endl; 00109 std::cout<<"TRACE(icestormelement.h): Timing out..." << std::endl; 00110 00111 painter_.clear(); 00112 listener_.resetTimer(); 00113 if ( listener_.connect() == 0 ) 00114 actionOnConnection(); 00115 } 00116 return false; 00117 } 00118 00119 template<class PainterType, class DataType, class ProxyType, class ConsumerType, class ConsumerPrxType> 00120 void 00121 IceStormElement<PainterType,DataType,ProxyType,ConsumerType,ConsumerPrxType>::update() 00122 { 00123 if ( !needToUpdate() ) { 00124 return; 00125 } 00126 00127 assert( !listener_.buffer().isEmpty() ); 00128 00129 // get data from the buffer 00130 listener_.buffer().getAndPop( data_ ); 00131 00132 // transfer data into painter 00133 painter_.setData( data_ ); 00134 } 00135 00136 } // namespace 00137 00138 #endif |
Webmaster: Tobias Kaupp (tobasco at users.sourceforge.net)