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 2011 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 
39  class Node : public boost::enable_shared_from_this<Node>
40  {
42  public: Node();
43 
45  public: virtual ~Node();
46 
51  public: void Init(const std::string &_space ="");
52 
53  public: void Fini();
54 
57  public: std::string GetTopicNamespace() const;
58 
60  public: std::string DecodeTopicName(const std::string &topic);
61 
63  public: std::string EncodeTopicName(const std::string &topic);
64 
66  public: unsigned int GetId() const;
67 
70  public: void ProcessPublishers();
71  public: void ProcessIncoming();
72 
74  template<typename M>
75  transport::PublisherPtr Advertise(const std::string &topic,
76  unsigned int _queueLimit = 1000,
77  bool _latch = false)
78  {
79  std::string decodedTopic = this->DecodeTopicName(topic);
80  PublisherPtr publisher =
81  transport::TopicManager::Instance()->Advertise<M>(
82  decodedTopic, _queueLimit, _latch);
83 
84  boost::recursive_mutex::scoped_lock lock(this->publisherMutex);
85  this->publishers.push_back(publisher);
86  this->publishersEnd = this->publishers.end();
87 
88  return publisher;
89  }
90 
92  template<typename M, typename T>
93  SubscriberPtr Subscribe(const std::string &topic,
94  void(T::*fp)(const boost::shared_ptr<M const> &), T *obj,
95  bool _latching = false)
96  {
97  SubscribeOptions ops;
98  std::string decodedTopic = this->DecodeTopicName(topic);
99  ops.template Init<M>(decodedTopic, shared_from_this(), _latching);
100 
101  boost::recursive_mutex::scoped_lock lock(this->incomingMutex);
102  this->callbacks[decodedTopic].push_back(CallbackHelperPtr(
103  new CallbackHelperT<M>(boost::bind(fp, obj, _1))));
104 
105  return transport::TopicManager::Instance()->Subscribe(ops);
106  }
107 
109  template<typename M>
110  SubscriberPtr Subscribe(const std::string &topic,
111  void(*fp)(const boost::shared_ptr<M const> &), bool _latching = false)
112  {
113  SubscribeOptions ops;
114  std::string decodedTopic = this->DecodeTopicName(topic);
115  ops.template Init<M>(decodedTopic, shared_from_this(), _latching);
116 
117  boost::recursive_mutex::scoped_lock lock(this->incomingMutex);
118  this->callbacks[decodedTopic].push_back(
120 
121  return transport::TopicManager::Instance()->Subscribe(ops);
122  }
123 
124  public: bool HandleData(const std::string &_topic,
125  const std::string &_msg);
126 
133  public: void InsertLatchedMsg(const std::string &_topic,
134  const std::string &_msg);
135 
136 
138  public: std::string GetMsgType(const std::string &_topic) const;
139 
140  private: std::string topicNamespace;
141  private: std::vector<PublisherPtr> publishers;
142  private: std::vector<PublisherPtr>::iterator publishersIter;
143  private: std::vector<PublisherPtr>::iterator publishersEnd;
144  private: static unsigned int idCounter;
145  private: unsigned int id;
146 
147  private: typedef std::list<CallbackHelperPtr> Callback_L;
148  private: typedef std::map<std::string, Callback_L> Callback_M;
149  private: Callback_M callbacks;
150  private: std::map<std::string, std::list<std::string> > incomingMsgs;
151  private: boost::recursive_mutex publisherMutex;
152  private: boost::recursive_mutex incomingMutex;
153 
154  private: bool initialized;
155  };
157  }
158 }
159 #endif