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/connectutils.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<ProxyType,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 gbxsickacfr::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 detail::subscribeListener<ProxyType, 00078 ConsumerType, 00079 ConsumerPrxType, 00080 SubscriptionMakerType>( context_, proxyString_, consumer_, callbackPrx_, proxy_ ); 00081 isSubscribed_ = true; 00082 return 0; 00083 } 00084 catch ( ... ) { 00085 return -1; 00086 } 00087 }; 00088 00089 double msSinceReceipt() const { return consumer_->msSinceReceipt(); } 00090 void resetTimer() { consumer_->resetTimer(); } 00091 std::string interfaceName() const { return proxyString_; }; 00092 00093 const ProxyType& proxy() { return proxy_; }; 00094 00095 private: 00096 00097 DataType data_; 00098 detail::BufferedTimedConsumerI<ConsumerType,DataType> *consumer_; 00099 00100 orcaice::Context context_; 00101 std::string proxyString_; 00102 00103 void registerWithAdapter() 00104 { 00105 Ice::ObjectPrx obj = context_.adapter()->addWithUUID( consumer_ ); 00106 Ice::ObjectPrx prx = context_.adapter()->createDirectProxy( obj->ice_getIdentity() ); 00107 callbackPrx_ = ConsumerPrxType::uncheckedCast( prx ); 00108 } 00109 00110 // 0=success 00111 int shutdown() 00112 { 00113 // std::cout<<"TRACE(icestormlistener.h): start of shutdown(): isSubscribed_: " << isSubscribed_ << std::endl; 00114 // unsubscribe 00115 if ( isSubscribed_ ) 00116 { 00117 try { 00118 detail::unSubscribeListener<ProxyType, 00119 ConsumerType, 00120 ConsumerPrxType, 00121 UnSubscriptionMakerType>( context_, proxyString_, consumer_, callbackPrx_, proxy_ ); 00122 00123 // remove consumer from the list of active servants 00124 context_.adapter()->remove( callbackPrx_->ice_getIdentity() ); 00125 00126 isSubscribed_ = false; 00127 // std::cout<<"TRACE(icestormlistener.h): unsubscription successful." << std::endl; 00128 } catch ( ... ) { 00129 return -1; 00130 } 00131 } 00132 return 0; 00133 } 00134 00135 // Proxy to remote object 00136 ProxyType proxy_; 00137 00138 // Smart pointer to consumer_. 00139 // We need to hold onto this to keep it alive. 00140 ConsumerPrxType callbackPrx_; 00141 bool isSubscribed_; 00142 }; 00143 00144 } // namespace 00145 00146 #endif |
Webmaster: Tobias Kaupp (tobasco at users.sourceforge.net)