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

SourceForge.net Logo
Project
Download
Mailing lists

 

         

providerWithDescriptionImpl.h

00001 /*
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 ORCA_PROVIDER_WITH_DESCRIPTION_IMPL_H
00012 #define ORCA_PROVIDER_WITH_DESCRIPTION_IMPL_H
00013 
00014 #include <memory>
00015 #include <gbxsickacfr/gbxiceutilacfr/store.h>
00016 #include <orcaice/context.h>
00017 #include <orcaice/topichandler.h>
00018 #include <orcaice/configutils.h>
00019 #include <orcaice/convertutils.h>
00020 #include <orcaice/iceutils.h>
00021 
00022 namespace gbxiceutilacfr { class Thread; }
00023 
00024 namespace orcaifaceimpl {
00025 
00037 template<class ProviderType, class ProviderPrxType, class ConsumerType, class ConsumerPrxType, class DataType, class DescriptionType>
00038 class ProviderWithDescriptionImpl : public IceUtil::Shared
00039 {
00040 friend class ProviderTypeI;
00041 
00042 public:
00046     ProviderWithDescriptionImpl( const DescriptionType& descr, const std::string& interfaceTag, const orcaice::Context& context ) :
00047         descr_(descr),
00048         interfaceName_(orcaice::getProvidedInterface(context,interfaceTag).iface),
00049         context_(context)
00050     {
00051         init();
00052     }
00053 
00055     ProviderWithDescriptionImpl( const DescriptionType& descr, const orcaice::Context& context, const std::string& interfaceName ) :
00056         descr_(descr),
00057         interfaceName_(interfaceName),
00058         context_(context)
00059     {
00060         init();
00061     }           
00062 
00063     ~ProviderWithDescriptionImpl()
00064     {
00065         orcaice::tryRemoveInterface( context_, interfaceName_ );
00066     }
00067 
00068     // local interface:
00070     void initInterface()
00071     {
00072         orcaice::createInterfaceWithString( context_, ptr_, interfaceName_ );
00073     
00074         topicHandler_->connectToTopic();
00075     }
00076 
00079     void initInterface( gbxiceutilacfr::Thread* thread, const std::string& subsysName="", int retryInterval=2 )
00080     {
00081         orcaice::createInterfaceWithString( context_, ptr_, interfaceName_, thread, subsysName, retryInterval );
00082     
00083         topicHandler_->connectToTopic( thread, subsysName, retryInterval );
00084     }
00085 
00087     void localSet( const DataType& data )
00088     {
00089         dataStore_.set( data );
00090     }
00091 
00094     void localSetAndSend( const DataType& data )
00095     {
00096         dataStore_.set( data );
00097     
00098         topicHandler_->publish( data );
00099     }
00100 
00101 private:    
00102     //
00103     // This is the implementation of the slice-defined interface
00104     //
00105     class ProviderTypeI : public ProviderType
00106     {
00107     public:
00108         ProviderTypeI( ProviderWithDescriptionImpl &impl )
00109             : impl_(impl) {}
00110         virtual DescriptionType getDescription(const ::Ice::Current&)
00111             { return impl_.internalGetDescription(); }
00112         virtual DataType getData(const Ice::Current&)
00113             { return impl_.internalGetData(); }
00114         virtual IceStorm::TopicPrx subscribe(const ConsumerPrxType& subscriber, const ::Ice::Current& = ::Ice::Current())
00115             { return impl_.internalSubscribe(subscriber); }
00116     private:
00117         ProviderWithDescriptionImpl &impl_;
00118     };
00120 
00121     DescriptionType descr_;
00122     // Holds the current data
00123     gbxiceutilacfr::Store<DataType> dataStore_;
00124 
00125     typedef orcaice::TopicHandler<ConsumerPrxType,DataType> TopicHandlerType;
00126     std::auto_ptr<TopicHandlerType> topicHandler_;
00127 
00128     Ice::ObjectPtr ptr_;
00129     const std::string interfaceName_;
00130     orcaice::Context context_;
00131 
00132     void init()
00133     {
00134         ptr_ = new ProviderTypeI( *this );
00135 
00136         topicHandler_.reset( new TopicHandlerType( orcaice::toTopicAsString(context_.name(),interfaceName_), context_ ) );    
00137     }
00138 
00139     // remote call implementations, mimic (but do not inherit) the orca interface
00140     DescriptionType internalGetDescription() const
00141     {
00142         return descr_;
00143     }
00144 
00145     DataType internalGetData() const
00146     {
00147         context_.tracer().debug( "ProviderWithDescriptionImpl::internalGetData()", 5 );
00148     
00149         if ( dataStore_.isEmpty() )
00150         {
00151             std::stringstream ss;
00152             ss << "No data available! (interface="<<interfaceName_<<")";
00153             throw orca::DataNotExistException( ss.str() );
00154         }
00155     
00156         DataType data;
00157         dataStore_.get( data );
00158         return data;
00159     }
00160 
00161     IceStorm::TopicPrx internalSubscribe(const ConsumerPrxType& subscriber)
00162     {
00163         if ( !topicHandler_.get() ) 
00164         {
00165             throw orca::SubscriptionFailedException("Component does not have a topic to publish its traces.");
00166         }
00167         
00168         // if we have data, send all the information we have to the new subscriber (and to no one else)
00169         if ( dataStore_.isEmpty() )
00170         {
00171             return topicHandler_->subscribe( subscriber );
00172         }
00173         else
00174         {
00175             DataType data;
00176             dataStore_.get( data );
00177         
00178             return topicHandler_->subscribe( subscriber, data );
00179         }
00180     }
00181 };
00182 
00183 }
00184 
00185 #endif
 

Webmaster: Tobias Kaupp (tobasco at users.sourceforge.net)


Generated for Orca Robotics by  doxygen 1.4.5