TopicManager.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 Open Source Robotics Foundation
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 #ifndef _TOPICMANAGER_HH_
18 #define _TOPICMANAGER_HH_
19 
20 #include <boost/bind.hpp>
21 #include <boost/function.hpp>
22 #include <map>
23 #include <list>
24 #include <string>
25 #include <vector>
26 #include <boost/unordered/unordered_set.hpp>
27 
28 #include "gazebo/common/Assert.hh"
30 #include "gazebo/msgs/msgs.hh"
32 
41 #include "gazebo/util/system.hh"
42 
43 namespace gazebo
44 {
45  namespace transport
46  {
49 
52  class GZ_TRANSPORT_VISIBLE TopicManager : public SingletonT<TopicManager>
53  {
54  private: TopicManager();
55  private: virtual ~TopicManager();
56 
58  public: void Init();
59 
61  public: void Fini();
62 
66  public: PublicationPtr FindPublication(const std::string &_topic);
67 
70  public: void AddNode(NodePtr _node);
71 
74  public: void RemoveNode(unsigned int _id);
75 
79  public: void ProcessNodes(bool _onlyOut = false);
80 
84  public: bool IsAdvertised(const std::string &_topic);
85 
89  public: SubscriberPtr Subscribe(const SubscribeOptions &_options);
90 
95  public: void Unsubscribe(const std::string &_topic, const NodePtr &_sub);
96 
104  public: template<typename M>
105  PublisherPtr Advertise(const std::string &_topic,
106  unsigned int _queueLimit,
107  double _hzRate)
108  {
109  google::protobuf::Message *msg = NULL;
110  M msgtype;
111  msg = dynamic_cast<google::protobuf::Message *>(&msgtype);
112  if (!msg)
113  gzthrow("Advertise requires a google protobuf type");
114 
115  this->UpdatePublications(_topic, msg->GetTypeName());
116 
117  PublisherPtr pub = PublisherPtr(new Publisher(_topic,
118  msg->GetTypeName(), _queueLimit, _hzRate));
119 
120  std::string msgTypename;
121  PublicationPtr publication;
122 
123  // Connect all local subscription to the publisher
124  msgTypename = msg->GetTypeName();
125 
126  publication = this->FindPublication(_topic);
127  GZ_ASSERT(publication != NULL, "FindPublication returned NULL");
128 
129  publication->AddPublisher(pub);
130  if (!publication->GetLocallyAdvertised())
131  {
132  ConnectionManager::Instance()->Advertise(_topic, msgTypename);
133  }
134 
135  publication->SetLocallyAdvertised(true);
136  pub->SetPublication(publication);
137 
138 
139  SubNodeMap::iterator iter2;
140  SubNodeMap::iterator stEnd2 = this->subscribedNodes.end();
141  for (iter2 = this->subscribedNodes.begin();
142  iter2 != stEnd2; ++iter2)
143  {
144  if (iter2->first == _topic)
145  {
146  std::list<NodePtr>::iterator liter;
147  std::list<NodePtr>::iterator lEnd = iter2->second.end();
148  for (liter = iter2->second.begin();
149  liter != lEnd; ++liter)
150  {
151  publication->AddSubscription(*liter);
152  }
153  }
154  }
155 
156  return pub;
157  }
158 
161  public: void Unadvertise(const std::string &_topic);
162 
165  public: void Unadvertise(PublisherPtr _pub);
166 
173  public: void Publish(const std::string &_topic, MessagePtr _message,
174  boost::function<void(uint32_t)> _cb, uint32_t _id);
175 
179  public: void ConnectPubToSub(const std::string &_topic,
180  const SubscriptionTransportPtr _sublink);
181 
184  public: void ConnectSubToPub(const msgs::Publish &_pub);
185 
190  public: void DisconnectPubFromSub(const std::string &_topic,
191  const std::string &_host,
192  unsigned int _port);
193 
198  public: void DisconnectSubFromPub(const std::string &_topic,
199  const std::string &_host,
200  unsigned int _port);
201 
204  public: void ConnectSubscribers(const std::string &_topic);
205 
211  public: PublicationPtr UpdatePublications(const std::string &_topic,
212  const std::string &_msgType);
213 
216  public: void RegisterTopicNamespace(const std::string &_name);
217 
220  public: void GetTopicNamespaces(std::list<std::string> &_namespaces);
221 
223  public: void ClearBuffers();
224 
227  public: void PauseIncoming(bool _pause);
228 
231  public: void AddNodeToProcess(NodePtr _ptr);
232 
234  typedef std::map<std::string, std::list<NodePtr> > SubNodeMap;
235 
236  private: typedef std::map<std::string, PublicationPtr> PublicationPtr_M;
237  private: PublicationPtr_M advertisedTopics;
238  private: PublicationPtr_M::iterator advertisedTopicsEnd;
239  private: SubNodeMap subscribedNodes;
240  private: std::vector<NodePtr> nodes;
241 
243  private: boost::unordered_set<NodePtr> nodesToProcess;
244 
245  private: boost::recursive_mutex nodeMutex;
246 
248  private: boost::mutex subscriberMutex;
249 
251  private: boost::mutex processNodesMutex;
252 
253  private: bool pauseIncoming;
254 
255  // Singleton implementation
256  private: friend class SingletonT<TopicManager>;
257  };
259  }
260 }
261 #endif
boost::shared_ptr< SubscriptionTransport > SubscriptionTransportPtr
Definition: TransportTypes.hh:69
Options for a subscription.
Definition: SubscribeOptions.hh:35
static ConnectionManager * Instance()
Get an instance of the singleton.
Definition: SingletonT.hh:36
#define GZ_ASSERT(_expr, _msg)
This macro define the standard way of launching an exception inside gazebo.
Definition: Assert.hh:24
Forward declarations for the common classes.
Definition: Animation.hh:33
boost::shared_ptr< Subscriber > SubscriberPtr
Definition: TransportTypes.hh:53
#define gzthrow(msg)
This macro logs an error to the throw stream and throws an exception that contains the file name and ...
Definition: Exception.hh:39
Forward declarations for transport.
Singleton template class.
Definition: SingletonT.hh:33
PublisherPtr Advertise(const std::string &_topic, unsigned int _queueLimit, double _hzRate)
Advertise on a topic.
Definition: TopicManager.hh:105
Manages topics and their subscriptions.
Definition: TopicManager.hh:52
A publisher of messages on a topic.
Definition: Publisher.hh:43
boost::shared_ptr< Publication > PublicationPtr
Definition: TransportTypes.hh:61
std::map< std::string, std::list< NodePtr > > SubNodeMap
A map of string->list of Node pointers.
Definition: TopicManager.hh:234
boost::shared_ptr< google::protobuf::Message > MessagePtr
Definition: TransportTypes.hh:45
#define NULL
Definition: CommonTypes.hh:30
boost::shared_ptr< Node > NodePtr
Definition: TransportTypes.hh:57
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
#define GZ_TRANSPORT_VISIBLE
Definition: system.hh:166
boost::shared_ptr< Publisher > PublisherPtr
Definition: TransportTypes.hh:49