Node.hh
00001 /* 00002 * Copyright 2011 Nate Koenig & Andrew Howard 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 * 00016 */ 00017 00018 #ifndef NODE_HH 00019 #define NODE_HH 00020 00021 #include <boost/enable_shared_from_this.hpp> 00022 00023 #include "transport/TransportTypes.hh" 00024 #include "transport/TopicManager.hh" 00025 00026 namespace gazebo 00027 { 00028 namespace transport 00029 { 00032 00035 class Node : public boost::enable_shared_from_this<Node> 00036 { 00038 public: Node(); 00039 00041 public: virtual ~Node(); 00042 00047 public: void Init(const std::string &_space=""); 00048 00049 public: void Fini(); 00050 00053 public: std::string GetTopicNamespace() const; 00054 00056 public: std::string DecodeTopicName(const std::string &topic); 00057 00059 public: std::string EncodeTopicName(const std::string &topic); 00060 00062 public: unsigned int GetId() const; 00063 00066 public: void ProcessPublishers(); 00067 public: void ProcessIncoming(); 00068 00070 template<typename M> 00071 transport::PublisherPtr Advertise(const std::string &topic, 00072 unsigned int _queueLimit=10, 00073 bool _latch = false) 00074 { 00075 std::string decodedTopic = this->DecodeTopicName(topic); 00076 PublisherPtr publisher = 00077 transport::TopicManager::Instance()->Advertise<M>( 00078 decodedTopic,_queueLimit, _latch); 00079 00080 this->publisherMutex->lock(); 00081 this->publishers.push_back(publisher); 00082 this->publishersEnd = this->publishers.end(); 00083 this->publisherMutex->unlock(); 00084 00085 return publisher; 00086 } 00087 00089 template<typename M, typename T> 00090 SubscriberPtr Subscribe(const std::string &topic, 00091 void(T::*fp)(const boost::shared_ptr<M const> &), T *obj, 00092 bool _latching=false) 00093 { 00094 SubscribeOptions ops; 00095 std::string decodedTopic = this->DecodeTopicName(topic); 00096 ops.template Init<M>(decodedTopic, shared_from_this(), _latching); 00097 00098 this->incomingMutex->lock(); 00099 this->callbacks[decodedTopic].push_back(CallbackHelperPtr( 00100 new CallbackHelperT<M>(boost::bind(fp, obj, _1)))); 00101 this->incomingMutex->unlock(); 00102 00103 return transport::TopicManager::Instance()->Subscribe(ops); 00104 } 00105 00107 template<typename M> 00108 SubscriberPtr Subscribe(const std::string &topic, 00109 void(*fp)(const boost::shared_ptr<M const> &), bool _latching=false) 00110 { 00111 SubscribeOptions ops; 00112 std::string decodedTopic = this->DecodeTopicName(topic); 00113 ops.template Init<M>(decodedTopic, shared_from_this(), _latching); 00114 00115 this->incomingMutex->lock(); 00116 this->callbacks[decodedTopic].push_back( 00117 CallbackHelperPtr(new CallbackHelperT<M>(fp))); 00118 this->incomingMutex->unlock(); 00119 00120 return transport::TopicManager::Instance()->Subscribe(ops); 00121 } 00122 00123 public: bool HandleData(const std::string &_topic, 00124 const std::string &_msg); 00125 00127 public: std::string GetMsgType(const std::string &_topic) const; 00128 00129 private: std::string topicNamespace; 00130 private: std::vector<PublisherPtr> publishers; 00131 private: std::vector<PublisherPtr>::iterator publishersIter; 00132 private: std::vector<PublisherPtr>::iterator publishersEnd; 00133 private: static unsigned int idCounter; 00134 private: unsigned int id; 00135 00136 private: typedef std::list<CallbackHelperPtr> Callback_L; 00137 private: typedef std::map<std::string, Callback_L> Callback_M; 00138 private: Callback_M callbacks; 00139 private: std::map<std::string, std::list<std::string> > incomingMsgs; 00140 private: boost::recursive_mutex *publisherMutex; 00141 private: boost::recursive_mutex *incomingMutex; 00142 }; 00144 } 00145 } 00146 #endif

1.7.5.1