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