TopicManager.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 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: SubscriberPtr Subscribe(const SubscribeOptions &_options);
85 
90  public: void Unsubscribe(const std::string &_topic, const NodePtr &_sub);
91 
99  public: template<typename M>
100  PublisherPtr Advertise(const std::string &_topic,
101  unsigned int _queueLimit,
102  double _hzRate)
103  {
104  google::protobuf::Message *msg = NULL;
105  M msgtype;
106  msg = dynamic_cast<google::protobuf::Message *>(&msgtype);
107  if (!msg)
108  gzthrow("Advertise requires a google protobuf type");
109 
110  this->UpdatePublications(_topic, msg->GetTypeName());
111 
112  PublisherPtr pub = PublisherPtr(new Publisher(_topic,
113  msg->GetTypeName(), _queueLimit, _hzRate));
114 
115  std::string msgTypename;
116  PublicationPtr publication;
117 
118  // Connect all local subscription to the publisher
119  msgTypename = msg->GetTypeName();
120 
121  publication = this->FindPublication(_topic);
122  GZ_ASSERT(publication != NULL, "FindPublication returned NULL");
123 
124  publication->AddPublisher(pub);
125  if (!publication->GetLocallyAdvertised())
126  {
127  ConnectionManager::Instance()->Advertise(_topic, msgTypename);
128  }
129 
130  publication->SetLocallyAdvertised(true);
131  pub->SetPublication(publication);
132 
133 
134  SubNodeMap::iterator iter2;
135  SubNodeMap::iterator stEnd2 = this->subscribedNodes.end();
136  for (iter2 = this->subscribedNodes.begin();
137  iter2 != stEnd2; ++iter2)
138  {
139  if (iter2->first == _topic)
140  {
141  std::list<NodePtr>::iterator liter;
142  std::list<NodePtr>::iterator lEnd = iter2->second.end();
143  for (liter = iter2->second.begin();
144  liter != lEnd; ++liter)
145  {
146  publication->AddSubscription(*liter);
147  }
148  }
149  }
150 
151  return pub;
152  }
153 
156  public: void Unadvertise(const std::string &_topic);
157 
160  public: void Unadvertise(PublisherPtr _pub);
161 
166  public: void Unadvertise(const std::string &_topic, const uint32_t _id);
167 
174  public: void Publish(const std::string &_topic, MessagePtr _message,
175  boost::function<void(uint32_t)> _cb, uint32_t _id);
176 
180  public: void ConnectPubToSub(const std::string &_topic,
181  const SubscriptionTransportPtr _sublink);
182 
185  public: void ConnectSubToPub(const msgs::Publish &_pub);
186 
191  public: void DisconnectPubFromSub(const std::string &_topic,
192  const std::string &_host,
193  unsigned int _port);
194 
199  public: void DisconnectSubFromPub(const std::string &_topic,
200  const std::string &_host,
201  unsigned int _port);
202 
205  public: void ConnectSubscribers(const std::string &_topic);
206 
212  public: PublicationPtr UpdatePublications(const std::string &_topic,
213  const std::string &_msgType);
214 
217  public: void RegisterTopicNamespace(const std::string &_name);
218 
221  public: void GetTopicNamespaces(std::list<std::string> &_namespaces);
222 
224  public: void ClearBuffers();
225 
228  public: void PauseIncoming(bool _pause);
229 
232  public: void AddNodeToProcess(NodePtr _ptr);
233 
235  typedef std::map<std::string, std::list<NodePtr> > SubNodeMap;
236 
237  private: typedef std::map<std::string, PublicationPtr> PublicationPtr_M;
238  private: PublicationPtr_M advertisedTopics;
239  private: PublicationPtr_M::iterator advertisedTopicsEnd;
240  private: SubNodeMap subscribedNodes;
241  private: std::vector<NodePtr> nodes;
242 
244  private: boost::unordered_set<NodePtr> nodesToProcess;
245 
246  private: boost::recursive_mutex nodeMutex;
247 
249  private: boost::mutex subscriberMutex;
250 
252  private: boost::mutex processNodesMutex;
253 
254  private: bool pauseIncoming;
255 
256  // Singleton implementation
257  private: friend class SingletonT<TopicManager>;
258  };
260  }
261 }
262 #endif
Options for a subscription.
Definition: SubscribeOptions.hh:35
#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< google::protobuf::Message > MessagePtr
Definition: TransportTypes.hh:45
#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.
boost::shared_ptr< Publisher > PublisherPtr
Definition: TransportTypes.hh:49
boost::shared_ptr< Subscriber > SubscriberPtr
Definition: TransportTypes.hh:53
Singleton template class.
Definition: SingletonT.hh:33
boost::shared_ptr< Node > NodePtr
Definition: TransportTypes.hh:57
boost::shared_ptr< Publication > PublicationPtr
Definition: TransportTypes.hh:61
boost::shared_ptr< SubscriptionTransport > SubscriptionTransportPtr
Definition: TransportTypes.hh:69
Manages topics and their subscriptions.
Definition: TopicManager.hh:52
A publisher of messages on a topic.
Definition: Publisher.hh:43
#define NULL
Definition: CommonTypes.hh:31
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
PublisherPtr Advertise(const std::string &_topic, unsigned int _queueLimit, double _hzRate)
Advertise on a topic.
Definition: TopicManager.hh:100
std::map< std::string, std::list< NodePtr > > SubNodeMap
A map of string->list of Node pointers.
Definition: TopicManager.hh:235
static ConnectionManager * Instance()
Get an instance of the singleton.
Definition: SingletonT.hh:36