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

 

         

providerImpl.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_IMPL_H
00012 #define ORCA_PROVIDER_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 
00036 template<class ProviderType, class ProviderPrxType, class ConsumerType, class ConsumerPrxType, class DataType>
00037 class ProviderImpl : public IceUtil::Shared
00038 {
00039 friend class ProviderTypeI;
00040 
00041 public:
00043     ProviderImpl( const std::string& interfaceTag, const orcaice::Context& context ) :
00044         interfaceName_(orcaice::getProvidedInterface(context,interfaceTag).iface),
00045         context_(context)
00046     {
00047         init();
00048     }
00049 
00051     ProviderImpl( const orcaice::Context& context, const std::string& interfaceName ) :
00052         interfaceName_(interfaceName),
00053         context_(context)
00054     {
00055         init();
00056     }           
00057 
00058     ~ProviderImpl()
00059     {
00060         orcaice::tryRemoveInterface( context_, interfaceName_ );
00061     }
00062 
00063     // local interface:
00065     void initInterface()
00066     {
00067         orcaice::createInterfaceWithString( context_, ptr_, interfaceName_ );
00068     
00069         topicHandler_->connectToTopic();
00070     }
00071 
00074     void initInterface( gbxiceutilacfr::Thread* thread, const std::string& subsysName="", int retryInterval=2 )
00075     {
00076         orcaice::createInterfaceWithString( context_, ptr_, interfaceName_, thread, subsysName, retryInterval );
00077     
00078         topicHandler_->connectToTopic( thread, subsysName, retryInterval );
00079     }
00080 
00082     void localSet( const DataType& data )
00083     {
00084         dataStore_.set( data );
00085     }
00086 
00089     void localSetAndSend( const DataType& data )
00090     {
00091         dataStore_.set( data );
00092     
00093         topicHandler_->publish( data );
00094     }
00095 
00096 private:    
00097     //
00098     // This is the implementation of the slice-defined interface
00099     //
00100     class ProviderTypeI : public ProviderType
00101     {
00102     public:
00103         ProviderTypeI( ProviderImpl &impl )
00104             : impl_(impl) {}
00105         DataType getData(const Ice::Current&)
00106             { return impl_.internalGetData(); }
00107         virtual IceStorm::TopicPrx subscribe(const ConsumerPrxType& subscriber, const ::Ice::Current& = ::Ice::Current())
00108             { return impl_.internalSubscribe(subscriber); }
00109     private:
00110         ProviderImpl &impl_;
00111     };
00113 
00114     // Holds the current data
00115     gbxiceutilacfr::Store<DataType> dataStore_;
00116 
00117     typedef orcaice::TopicHandler<ConsumerPrxType,DataType> TopicHandlerType;
00118     std::auto_ptr<TopicHandlerType> topicHandler_;
00119 
00120     Ice::ObjectPtr ptr_;
00121     const std::string interfaceName_;
00122     orcaice::Context context_;
00123 
00124     void init()
00125     {
00126         ptr_ = new ProviderTypeI( *this );
00127 
00128         topicHandler_.reset( new TopicHandlerType( orcaice::toTopicAsString(context_.name(),interfaceName_), context_ ) );    
00129     }
00130 
00131     // remote call implementations, mimic (but do not inherit) the orca interface
00132     DataType internalGetData() const
00133     {
00134         context_.tracer().debug( "ProviderImpl::internalGetData()", 5 );
00135     
00136         if ( dataStore_.isEmpty() )
00137         {
00138             std::stringstream ss;
00139             ss << "No data available! (interface="<<interfaceName_<<")";
00140             throw orca::DataNotExistException( ss.str() );
00141         }
00142     
00143         DataType data;
00144         dataStore_.get( data );
00145         return data;
00146     }
00147 
00148     IceStorm::TopicPrx internalSubscribe(const ConsumerPrxType& subscriber)
00149     {
00150         if ( !topicHandler_.get() ) 
00151         {
00152             throw orca::SubscriptionFailedException("Component does not have a topic to publish its traces.");
00153         }
00154         
00155         // if we have data, send all the information we have to the new subscriber (and to no one else)
00156         if ( dataStore_.isEmpty() )
00157         {
00158             return topicHandler_->subscribe( subscriber );
00159         }
00160         else
00161         {
00162             DataType data;
00163             dataStore_.get( data );
00164         
00165             return topicHandler_->subscribe( subscriber, data );
00166         }
00167     }
00168 };
00169 
00170 }
00171 
00172 #endif
 

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


Generated for Orca Robotics by  doxygen 1.4.5