All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Node.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Nate Koenig
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef _NODE_HH_
19 #define _NODE_HH_
20 
21 #include <boost/enable_shared_from_this.hpp>
22 #include <map>
23 #include <list>
24 #include <string>
25 #include <vector>
26 
29 
30 namespace gazebo
31 {
32  namespace transport
33  {
36 
40  class Node : public boost::enable_shared_from_this<Node>
41  {
43  public: Node();
44 
46  public: virtual ~Node();
47 
52  public: void Init(const std::string &_space ="");
53 
55  public: void Fini();
56 
59  public: std::string GetTopicNamespace() const;
60 
64  public: std::string DecodeTopicName(const std::string &_topic);
65 
69  public: std::string EncodeTopicName(const std::string &_topic);
70 
73  public: unsigned int GetId() const;
74 
77  public: void ProcessPublishers();
78 
80  public: void ProcessIncoming();
81 
89  template<typename M>
90  transport::PublisherPtr Advertise(const std::string &_topic,
91  unsigned int _queueLimit = 1000,
92  bool _latch = false)
93  {
94  std::string decodedTopic = this->DecodeTopicName(_topic);
95  PublisherPtr publisher =
96  transport::TopicManager::Instance()->Advertise<M>(
97  decodedTopic, _queueLimit, _latch);
98 
99  boost::recursive_mutex::scoped_lock lock(this->publisherMutex);
100  this->publishers.push_back(publisher);
101  this->publishersEnd = this->publishers.end();
102 
103  return publisher;
104  }
105 
113  template<typename M, typename T>
114  SubscriberPtr Subscribe(const std::string &_topic,
115  void(T::*_fp)(const boost::shared_ptr<M const> &), T *_obj,
116  bool _latching = false)
117  {
118  SubscribeOptions ops;
119  std::string decodedTopic = this->DecodeTopicName(_topic);
120  ops.template Init<M>(decodedTopic, shared_from_this(), _latching);
121 
122  boost::recursive_mutex::scoped_lock lock(this->incomingMutex);
123  this->callbacks[decodedTopic].push_back(CallbackHelperPtr(
124  new CallbackHelperT<M>(boost::bind(_fp, _obj, _1))));
125 
126  return transport::TopicManager::Instance()->Subscribe(ops);
127  }
128 
135  template<typename M>
136  SubscriberPtr Subscribe(const std::string &_topic,
137  void(*_fp)(const boost::shared_ptr<M const> &),
138  bool _latching = false)
139  {
140  SubscribeOptions ops;
141  std::string decodedTopic = this->DecodeTopicName(_topic);
142  ops.template Init<M>(decodedTopic, shared_from_this(), _latching);
143 
144  boost::recursive_mutex::scoped_lock lock(this->incomingMutex);
145  this->callbacks[decodedTopic].push_back(
147 
148  return transport::TopicManager::Instance()->Subscribe(ops);
149  }
150 
155  public: bool HandleData(const std::string &_topic,
156  const std::string &_msg);
157 
164  public: void InsertLatchedMsg(const std::string &_topic,
165  const std::string &_msg);
166 
167 
171  public: std::string GetMsgType(const std::string &_topic) const;
172 
173  private: std::string topicNamespace;
174  private: std::vector<PublisherPtr> publishers;
175  private: std::vector<PublisherPtr>::iterator publishersIter;
176  private: std::vector<PublisherPtr>::iterator publishersEnd;
177  private: static unsigned int idCounter;
178  private: unsigned int id;
179 
180  private: typedef std::list<CallbackHelperPtr> Callback_L;
181  private: typedef std::map<std::string, Callback_L> Callback_M;
182  private: Callback_M callbacks;
183  private: std::map<std::string, std::list<std::string> > incomingMsgs;
184  private: boost::recursive_mutex publisherMutex;
185  private: boost::recursive_mutex incomingMutex;
186 
187  private: bool initialized;
188  };
190  }
191 }
192 #endif