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
|
orcaice/connectutils.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 ORCAICE_CONNECT_UTILITIES_H 00012 #define ORCAICE_CONNECT_UTILITIES_H 00013 00014 // include defnition of Ice runtime 00015 #include <Ice/Ice.h> 00016 #include <IceStorm/IceStorm.h> 00017 00018 #include <orcaice/context.h> 00019 #include <orcaice/exceptions.h> 00020 #include <gbxsickacfr/gbxiceutilacfr/safethread.h> 00021 #include <orcaice/configutils.h> 00022 #include <hydroutil/sysutils.h> 00023 #include <orcaice/printutils.h> 00024 #include <orcaice/stringutils.h> 00025 00026 namespace orcaice 00027 { 00032 00037 bool isRegistryReachable( const Context& context ); 00038 00049 bool isInterfaceReachable( const Context& context, 00050 const std::string& proxyString, 00051 std::string& diagnostic ); 00052 00065 void createInterfaceWithString( const Context & context, 00066 Ice::ObjectPtr & object, 00067 const std::string & name ); 00068 00080 void createInterfaceWithTag( const Context & context, 00081 Ice::ObjectPtr & object, 00082 const std::string & interfaceTag ); 00083 00098 template<class ConsumerProxyType> 00099 ConsumerProxyType 00100 createConsumerInterface( const Context & context, 00101 Ice::ObjectPtr & consumer ) 00102 { 00103 // create servant and tell adapter about it (let it make up a globally unique name) 00104 // NOTE: addWithUUID() does not throw exceptions. 00105 Ice::ObjectPrx obj = context.adapter()->addWithUUID( consumer ); 00106 00107 // make a direct proxy 00108 Ice::ObjectPrx prx = context.adapter()->createDirectProxy( obj->ice_getIdentity() ); 00109 return ConsumerProxyType::uncheckedCast( prx ); 00110 } 00111 00112 // 00113 // Implementation Note: 00114 // the two 'connectToInterface' functions have to be templated, unfortunately, 00115 // to ensure we're connecting to the right type. 00130 template<class ProxyType> 00131 void 00132 connectToInterfaceWithString( const Context & context, 00133 ProxyType & proxy, 00134 const std::string & proxyString) 00135 { 00136 00137 // for indirect proxies only: if platform is set to 'local', replace it by hostname 00138 std::string resolvedProxyString = resolveLocalPlatform( context, proxyString ); 00139 00140 Ice::ObjectPrx base = context.communicator()->stringToProxy( resolvedProxyString ); 00141 00142 // check with the server that the one we found is of the right type 00143 // the check operation is remote and may fail (see sec.6.11.2) 00144 try { 00145 proxy = ProxyType::checkedCast( base ); 00146 // got some answer, check that it's the right type 00147 if ( !proxy ) { 00148 std::string errString = "Required interface '" + resolvedProxyString + "' is of wrong type."; 00149 initTracerWarning( context, errString, 2 ); 00150 throw orcaice::TypeMismatchException( ERROR_INFO, errString ); 00151 } 00152 } 00153 // typically we get ConnectionRefusedException, but it may be ObjectNotExistException 00154 catch ( Ice::LocalException &e ) 00155 { 00156 // Give some feedback as to why shit isn't working 00157 std::stringstream ss; 00158 ss << "Failed to connect to '" << resolvedProxyString << "': "<<e; 00159 initTracerWarning( context, ss.str(), 2 ); 00160 throw orcaice::NetworkException( ERROR_INFO, ss.str() ); 00161 } 00162 00164 } 00165 00176 template<class ProxyType> 00177 void 00178 connectToInterfaceWithTag( const Context & context, 00179 ProxyType & proxy, 00180 const std::string & interfaceTag ) 00181 { 00182 // this may throw ConfigFileException, we don't catch it, let the user catch it at the component level 00183 std::string proxyString = orcaice::getRequiredInterfaceAsString( context, interfaceTag ); 00184 00185 // now that we have the stingified proxy, use the function above. 00186 connectToInterfaceWithString( context, proxy, proxyString ); 00187 } 00188 00191 std::string getInterfaceIdWithString( const Context& context, const std::string& proxyString ); 00192 00196 std::string getInterfaceIdWithTag( const Context& context, const std::string& interfaceTag ); 00197 00198 // FUNCTIONS WITHOUT DOXYGEN TAGS ARE UTILITY FUNCTIONS 00199 // THEY ARE PUBLICLY AVAILABLE BUT ARE NOT ADVERTIZED THROUGH DOXYGEN 00200 00201 /* 00202 * Behaves the same as the one above but connects to the topic manager 00203 * specified in the current properties. 00204 */ 00205 IceStorm::TopicPrx connectToIceStormTopicPrx( const Context &, 00206 const std::string & topicName, 00207 bool createIfMissing=false ); 00208 00209 /* 00210 * Behaves the same as the one above but connects to the topic manager 00211 * specified in the communicator's properties. 00212 */ 00213 IceStorm::TopicPrx connectToIceStormTopicPrx( const Ice::CommunicatorPtr & communicator, 00214 const std::string & topicName, 00215 bool createIfMissing=false ); 00216 00217 /* 00218 * Given the stingified proxy to topic manager and topic name, connect to the topic proxy. 00219 * If topic manager does not exist, throws Exception exception. 00220 */ 00221 IceStorm::TopicPrx connectToIceStormTopicPrxWithManager( const Ice::CommunicatorPtr & communicator, 00222 const std::string & topicName, 00223 const std::string & topicManagerString, 00224 bool createIfMissing=false ); 00225 00226 /* 00227 * Publisher is used from the provider end. It is the consumer of information. 00228 * So you can push data into it. 00229 */ 00230 Ice::ObjectPrx connectToIceStormTopicPublisherPrx( const Ice::CommunicatorPtr & communicator, 00231 const std::string & topicName ); 00232 00233 /* 00234 * Behaves like the one above. 00235 */ 00236 Ice::ObjectPrx connectToIceStormTopicPublisherPrx( const IceStorm::TopicPrx & topic ); 00237 00238 00247 template<class ConsumerProxyType> 00248 IceStorm::TopicPrx 00249 connectToTopicWithTag( const Context & context, 00250 ConsumerProxyType & publisher, 00251 const std::string & interfaceTag, 00252 const std::string & subtopic="*" ) 00253 { 00254 context.tracer().debug( "orcaice::connectToTopicWithTag() tag="+interfaceTag, 10 ); 00255 00256 // lookup the name of the interface in the config file and generate topic name. 00257 // this generates a standard topic name based on fully-qualified interface name. 00258 std::string topicName = orcaice::toString( 00259 orcaice::getProvidedTopicWithTag( context, interfaceTag, subtopic ) ); 00260 00261 // do the conversion to string by hand, to cut dependency on libOrcaObj 00262 // see <orcaobj/stringutils.cpp> 00263 // orca::FQTopicName name = orcaice::getProvidedTopicWithTag( context, interfaceTag, subtopic ); 00264 // std::string topicName = name.iface + "/" + name.topic + "@" + name.platform + "/" + name.component; 00265 00266 return connectToTopicWithString( context, publisher, topicName ); 00267 } 00268 00280 template<class ConsumerProxyType> 00281 IceStorm::TopicPrx 00282 connectToTopicWithString( const Context & context, 00283 ConsumerProxyType & publisher, 00284 const std::string & topicName ) 00285 { 00286 IceStorm::TopicPrx topicPrx; 00287 00288 try { 00289 const bool createIfMissing = true; 00290 // 00291 // set the proxy to the topic 00292 // 00293 topicPrx = connectToIceStormTopicPrx( context.communicator(), topicName, createIfMissing ); 00294 00295 Ice::ObjectPrx obj = connectToIceStormTopicPublisherPrx( topicPrx ); 00296 // 00297 // set the proxy to the publisher 00298 // 00299 publisher = ConsumerProxyType::uncheckedCast(obj); 00300 } 00301 //catch ( const gbxsickacfr::gbxutilacfr::Exception & e ) { 00302 // we'll catch it here if the topic manager does not exist 00303 //} 00304 catch ( Ice::ConnectionRefusedException &e ) 00305 { 00306 // Give some feedback as to why this isn't working 00307 std::stringstream ss; ss<<"Error while connecting to IceStorm topic publisher '"<<topicName<<"': "<<e; 00308 initTracerError( context, ss.str(), 2 ); 00309 initTracerInfo( context, "hint: Is IceStorm running?", 10 ); 00310 throw orcaice::NetworkException( ERROR_INFO, ss.str() ); 00311 } 00312 catch( const Ice::LocalException &e ) 00313 { 00314 std::stringstream ss; 00315 ss<<"Error while connecting to IceStorm topic publisher '"<<topicName<<"': "<<e; 00316 initTracerError( context, ss.str(), 2 ); 00317 throw gbxsickacfr::gbxutilacfr::Exception( ERROR_INFO, ss.str() ); 00318 } 00319 catch ( Ice::Exception &e ) 00320 { 00321 // Give some feedback as to why this isn't working 00322 std::stringstream ss; ss<<"Error while connecting to IceStorm topic publisher '"<<topicName<<"': "<<e; 00323 initTracerError( context, ss.str(), 2 ); 00324 throw orcaice::NetworkException( ERROR_INFO, ss.str() ); 00325 } 00326 00327 return topicPrx; 00328 } 00329 00331 00332 } // namespace 00333 00334 #endif |
Webmaster: Tobias Kaupp (tobasco at users.sourceforge.net)