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
|
icestormlistenerdetail.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_LISTENER_DETAIL_H 00012 #define ORCAGUI_ICESTORM_LISTENER_DETAIL_H 00013 00014 #include <orcaice/ptrbuffer.h> 00015 00016 namespace orcaqguielementutil 00017 { 00018 00019 // non-member utility stuff 00020 namespace detail { 00021 00022 // A little bit of magic to get around the fact that different (un)subscription 00023 // functions may have different names 00024 template< class ProxyType, class ConsumerPrxType > 00025 class DefaultSubscriptionMaker 00026 { 00027 public: 00028 DefaultSubscriptionMaker( ProxyType proxy, ConsumerPrxType callbackPrx ) 00029 { proxy->subscribe( callbackPrx ); } 00030 }; 00031 00032 template< class ProxyType, class ConsumerPrxType > 00033 class DefaultUnSubscriptionMaker 00034 { 00035 public: 00036 DefaultUnSubscriptionMaker( ProxyType proxy, ConsumerPrxType callbackPrx ) 00037 { proxy->unsubscribe( callbackPrx ); } 00038 }; 00039 00040 // 00041 // ================ Definition ===================== 00042 // 00043 00044 // Subscribe/unsubscribe IceStormListeners 00045 template< class ProxyType, 00046 class ConsumerType, 00047 class ConsumerPrxType, 00048 class SubscriptionMakerType > 00049 void 00050 subscribeListener( orcaice::Context &context, 00051 const std::string &proxyString, 00052 ConsumerType *consumer, 00053 ConsumerPrxType &callbackPrx, 00054 ProxyType &proxy); 00055 00056 template< class ProxyType, 00057 class ConsumerType, 00058 class ConsumerPrxType, 00059 class UnSubscriptionMakerType > 00060 void 00061 unSubscribeListener( orcaice::Context &context, 00062 const std::string &proxyString, 00063 ConsumerType *consumer, 00064 ConsumerPrxType &callbackPrx, 00065 ProxyType &proxy); 00066 00067 // 00068 // ================ Implementation ===================== 00069 // 00070 00071 template< class ProxyType, 00072 class ConsumerType, 00073 class ConsumerPrxType, 00074 class SubscriptionMakerType > 00075 void 00076 subscribeListener( orcaice::Context &context, 00077 const std::string &proxyString, 00078 ConsumerType *consumer, 00079 ConsumerPrxType &callbackPrx, 00080 ProxyType &proxy ) 00081 { 00082 try { 00083 // Connect to remote interface 00084 orcaice::connectToInterfaceWithString( context, proxy, proxyString ); 00085 00086 // Ask the remote object to subscribe us to the topic. 00087 SubscriptionMakerType s( proxy, callbackPrx ); 00088 } 00089 // Ignore all exceptions, and try again next time. 00090 catch ( Ice::ConnectionRefusedException &e ) { 00091 std::cout<<"TRACE(PtrIceStormListener::subscribeListener): Caught exception: " << e << std::endl; 00092 throw; 00093 } 00094 catch ( Ice::Exception &e ) { 00095 std::cout<<"TRACE(PtrIceStormListener::subscribeListener): Caught ice exception: " << e << std::endl; 00096 throw; 00097 } 00098 catch ( std::exception &e ) { 00099 std::cout<<"TRACE(PtrIceStormListener::subscribeListener): Caught std exception: " << e.what() << std::endl; 00100 throw; 00101 } 00102 catch ( ... ) { 00103 std::cout<<"TRACE(PtrIceStormListener::subscribeListener): Caught unknown exception." << std::endl; 00104 throw; 00105 } 00106 } 00107 template< class ProxyType, 00108 class ConsumerType, 00109 class ConsumerPrxType, 00110 class UnSubscriptionMakerType > 00111 void 00112 unSubscribeListener( orcaice::Context &context, 00113 const std::string &proxyString, 00114 ConsumerType *consumer, 00115 ConsumerPrxType &callbackPrx, 00116 ProxyType &proxy) 00117 { 00118 try { 00119 // Connect to remote object 00120 orcaice::connectToInterfaceWithString( context, proxy, proxyString ); 00121 00122 // Ask the remote object to unsubscribe us from the topic. 00123 UnSubscriptionMakerType s( proxy, callbackPrx ); 00124 } 00125 // Ignore all exceptions, and try again next time. 00126 catch ( Ice::ConnectionRefusedException &e ) { 00127 std::cout<<"TRACE(PtrIceStormListener::unSubscribeListener): Caught exception: " << e << std::endl; 00128 throw; 00129 } 00130 catch ( Ice::Exception &e ) { 00131 std::cout<<"TRACE(PtrIceStormListener::unSubscribeListener): Caught ice exception: " << e << std::endl; 00132 throw; 00133 } 00134 catch ( std::exception &e ) { 00135 std::cout<<"TRACE(PtrIceStormListener::unSubscribeListener): Caught std exception: " << e.what() << std::endl; 00136 throw; 00137 } 00138 catch ( ... ) { 00139 std::cout<<"TRACE(PtrIceStormListener::unSubscribeListener): Caught unknown exception." << std::endl; 00140 throw; 00141 } 00142 } 00143 00145 00146 /* 00147 * @brief A convenience class for consumers with setData() operation. 00148 * 00149 * Relies on the fact that the Consumer objects has only one operation 00150 * to implement and it's called setData(). 00151 * 00152 */ 00153 template<class ConsumerType, class ObjectType> 00154 class BufferedTimedConsumerI : public ConsumerType 00155 { 00156 public: 00157 00158 BufferedTimedConsumerI( int depth=1, 00159 gbxsickacfr::gbxiceutilacfr::BufferType bufferType = gbxsickacfr::gbxiceutilacfr::BufferTypeCircular ) 00160 : buffer_( depth, bufferType ) {}; 00161 00162 void setData( const ObjectType& data, const Ice::Current& ) 00163 { 00164 buffer_.push( data ); 00165 handleData( data ); 00166 } 00167 00168 double msSinceReceipt() { return timer_.elapsedMs(); } 00169 void resetTimer() { return timer_.restart(); } 00170 00171 // buffer_ is public so that guielements can access it directly 00172 gbxsickacfr::gbxiceutilacfr::Buffer<ObjectType> buffer_; 00173 00174 protected: 00175 00176 void handleData( const ObjectType & obj ) 00177 { 00178 timer_.restart(); 00179 } 00180 00181 gbxsickacfr::gbxiceutilacfr::Timer timer_; 00182 }; 00183 /* 00184 * @brief A convenience class for consumers with setData() operation. 00185 * 00186 * Relies on the fact that the Consumer objects has only one operation 00187 * to implement and it's called setData(). 00188 * 00189 */ 00190 template<class ConsumerType, class ObjectType> 00191 class PtrBufferedTimedConsumerI : public ConsumerType 00192 { 00193 public: 00194 00195 PtrBufferedTimedConsumerI( int depth=1, gbxsickacfr::gbxiceutilacfr::BufferType bufferType = gbxsickacfr::gbxiceutilacfr::BufferTypeCircular ) 00196 : buffer_( depth, bufferType ) {}; 00197 00198 void setData( const ObjectType& data, const Ice::Current& ) 00199 { 00200 buffer_.push( data ); 00201 handleData( data ); 00202 } 00203 00204 double msSinceReceipt() { return timer_.elapsedMs(); } 00205 void resetTimer() { return timer_.restart(); } 00206 00207 // buffer_ is public so that guielements can access it directly 00208 orcaice::PtrBuffer<ObjectType> buffer_; 00209 00210 protected: 00211 00212 void handleData( const ObjectType & obj ) 00213 { 00214 timer_.restart(); 00215 } 00216 00217 gbxsickacfr::gbxiceutilacfr::Timer timer_; 00218 }; 00219 00220 00221 } // namespace 00222 } // namespace 00223 00224 #endif |
Webmaster: Tobias Kaupp (tobasco at users.sourceforge.net)