|
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
|
icestormlistener.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_H 00012 #define ORCAGUI_ICESTORM_LISTENER_H 00013 00014 #include <iostream> 00015 #include <assert.h> 00016 #include <Ice/Ice.h> 00017 #include <IceStorm/IceStorm.h> 00018 00019 #include <orcaice/icestormutils.h> 00020 #include <orcaice/context.h> 00021 #include <gbxsickacfr/gbxiceutilacfr/timer.h> 00022 00023 #include <orcaqguielementutil/icestormlistenerdetail.h> 00024 00025 namespace orcaqguielementutil 00026 { 00027 00035 template< class DataType, 00036 class ProxyType, 00037 class ConsumerType, 00038 class ConsumerPrxType, 00039 class SubscriptionMakerType=detail::DefaultSubscriptionMaker<ProxyType,ConsumerPrxType>, 00040 class UnSubscriptionMakerType=detail::DefaultUnSubscriptionMaker<ConsumerPrxType> > 00041 class IceStormListener 00042 { 00043 00044 public: 00045 00046 IceStormListener( const orcaice::Context &context, 00047 const std::string &proxyStr ) 00048 : context_(context), 00049 proxyString_(proxyStr), 00050 isSubscribed_(false) 00051 { 00052 // Register with the adapter 00053 consumer_ = new detail::BufferedTimedConsumerI<ConsumerType,DataType>; 00054 registerWithAdapter(); 00055 } 00056 00057 ~IceStormListener() 00058 { 00059 shutdown(); 00060 // do not delete consumer_, it's deleted when the smart pointers fall out of scope. 00061 } 00062 00063 gbxiceutilacfr::Buffer<DataType> &buffer() { return consumer_->buffer_; } 00064 00065 // Returns zero on success 00066 int connect() 00067 { 00068 // clean up first 00069 if ( shutdown() != 0 ) return -1; 00070 00071 // then resubscribe 00072 try { 00073 Ice::ObjectPtr objPtr = consumer_; 00074 callbackPrx_ = orcaice::createConsumerInterface<ConsumerPrxType>( context_, 00075 objPtr ); 00076 00077 topicPrx_ = detail::subscribeListener<ProxyType, 00078 ConsumerType, 00079 ConsumerPrxType, 00080 SubscriptionMakerType>( context_, proxyString_, consumer_, callbackPrx_, proxy_ ); 00081 assert( proxy_ != 0 ); 00082 isSubscribed_ = true; 00083 return 0; 00084 } 00085 catch ( ... ) { 00086 return -1; 00087 } 00088 }; 00089 00090 double msSinceReceipt() const { return consumer_->msSinceReceipt(); } 00091 void resetTimer() { consumer_->resetTimer(); } 00092 std::string interfaceName() const { return proxyString_; }; 00093 00094 const ProxyType& proxy() { return proxy_; }; 00095 00096 private: 00097 00098 DataType data_; 00099 detail::BufferedTimedConsumerI<ConsumerType,DataType> *consumer_; 00100 00101 orcaice::Context context_; 00102 std::string proxyString_; 00103 IceStorm::TopicPrx topicPrx_; 00104 00105 void registerWithAdapter() 00106 { 00107 Ice::ObjectPrx obj = context_.adapter()->addWithUUID( consumer_ ); 00108 Ice::ObjectPrx prx = context_.adapter()->createDirectProxy( obj->ice_getIdentity() ); 00109 callbackPrx_ = ConsumerPrxType::uncheckedCast( prx ); 00110 } 00111 00112 // 0=success 00113 int shutdown() 00114 { 00115 // std::cout<<"TRACE(icestormlistener.h): start of shutdown(): isSubscribed_: " << isSubscribed_ << std::endl; 00116 // unsubscribe 00117 if ( isSubscribed_ ) 00118 { 00119 try { 00120 detail::unSubscribeListener<ConsumerType, 00121 ConsumerPrxType, 00122 UnSubscriptionMakerType>( context_, topicPrx_, consumer_, callbackPrx_ ); 00123 00124 // remove consumer from the list of active servants 00125 context_.adapter()->remove( callbackPrx_->ice_getIdentity() ); 00126 00127 isSubscribed_ = false; 00128 // std::cout<<"TRACE(icestormlistener.h): unsubscription successful." << std::endl; 00129 } catch ( ... ) { 00130 return -1; 00131 } 00132 } 00133 return 0; 00134 } 00135 00136 // Proxy to remote object 00137 ProxyType proxy_; 00138 00139 // Smart pointer to consumer_. 00140 // We need to hold onto this to keep it alive. 00141 ConsumerPrxType callbackPrx_; 00142 bool isSubscribed_; 00143 }; 00144 00145 } // namespace 00146 00147 #endif |
Webmaster: Tobias Kaupp (tobasco at users.sourceforge.net)
1.4.5