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 GAZEBO_TRANSPORT_TOPICMANAGER_HH_
18 #define GAZEBO_TRANSPORT_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 
44 GZ_SINGLETON_DECLARE(GZ_TRANSPORT_VISIBLE, gazebo, transport, TopicManager)
45 
46 namespace gazebo
47 {
48  namespace transport
49  {
52 
55  class GZ_TRANSPORT_VISIBLE TopicManager : public SingletonT<TopicManager>
56  {
57  private: TopicManager();
58  private: virtual ~TopicManager();
59 
61  public: void Init();
62 
64  public: void Fini();
65 
69  public: PublicationPtr FindPublication(const std::string &_topic);
70 
73  public: void AddNode(NodePtr _node);
74 
77  public: void RemoveNode(unsigned int _id);
78 
82  public: void ProcessNodes(bool _onlyOut = false);
83 
87  public: SubscriberPtr Subscribe(const SubscribeOptions &_options);
88 
93  public: void Unsubscribe(const std::string &_topic, const NodePtr &_sub);
94 
102  public: template<typename M>
103  PublisherPtr Advertise(const std::string &_topic,
104  unsigned int _queueLimit,
105  double _hzRate)
106  {
107  google::protobuf::Message *msg = NULL;
108  M msgtype;
109  msg = dynamic_cast<google::protobuf::Message *>(&msgtype);
110  if (!msg)
111  gzthrow("Advertise requires a google protobuf type");
112 
113  this->UpdatePublications(_topic, msg->GetTypeName());
114 
115  PublisherPtr pub = PublisherPtr(new Publisher(_topic,
116  msg->GetTypeName(), _queueLimit, _hzRate));
117 
118  std::string msgTypename;
119  PublicationPtr publication;
120 
121  // Connect all local subscription to the publisher
122  msgTypename = msg->GetTypeName();
123 
124  publication = this->FindPublication(_topic);
125  GZ_ASSERT(publication != NULL, "FindPublication returned NULL");
126 
127  publication->AddPublisher(pub);
128  if (!publication->GetLocallyAdvertised())
129  {
130  ConnectionManager::Instance()->Advertise(_topic, msgTypename);
131  }
132 
133  publication->SetLocallyAdvertised(true);
134  pub->SetPublication(publication);
135 
136 
137  SubNodeMap::iterator iter2;
138  SubNodeMap::iterator stEnd2 = this->subscribedNodes.end();
139  for (iter2 = this->subscribedNodes.begin();
140  iter2 != stEnd2; ++iter2)
141  {
142  if (iter2->first == _topic)
143  {
144  std::list<NodePtr>::iterator liter;
145  std::list<NodePtr>::iterator lEnd = iter2->second.end();
146  for (liter = iter2->second.begin();
147  liter != lEnd; ++liter)
148  {
149  publication->AddSubscription(*liter);
150  }
151  }
152  }
153 
154  return pub;
155  }
156 
159  public: void Unadvertise(const std::string &_topic);
160 
163  public: void Unadvertise(PublisherPtr _pub);
164 
169  public: void Unadvertise(const std::string &_topic, const uint32_t _id);
170 
177  public: void Publish(const std::string &_topic, MessagePtr _message,
178  boost::function<void(uint32_t)> _cb, uint32_t _id);
179 
183  public: void ConnectPubToSub(const std::string &_topic,
184  const SubscriptionTransportPtr _sublink);
185 
188  public: void ConnectSubToPub(const msgs::Publish &_pub);
189 
194  public: void DisconnectPubFromSub(const std::string &_topic,
195  const std::string &_host,
196  unsigned int _port);
197 
202  public: void DisconnectSubFromPub(const std::string &_topic,
203  const std::string &_host,
204  unsigned int _port);
205 
208  public: void ConnectSubscribers(const std::string &_topic);
209 
215  public: PublicationPtr UpdatePublications(const std::string &_topic,
216  const std::string &_msgType);
217 
220  public: void RegisterTopicNamespace(const std::string &_name);
221 
224  public: void GetTopicNamespaces(std::list<std::string> &_namespaces);
225 
227  public: void ClearBuffers();
228 
231  public: void PauseIncoming(bool _pause);
232 
235  public: void AddNodeToProcess(NodePtr _ptr);
236 
238  typedef std::map<std::string, std::list<NodePtr> > SubNodeMap;
239 
240  private: typedef std::map<std::string, PublicationPtr> PublicationPtr_M;
241  private: PublicationPtr_M advertisedTopics;
242  private: PublicationPtr_M::iterator advertisedTopicsEnd;
243  private: SubNodeMap subscribedNodes;
244  private: std::vector<NodePtr> nodes;
245 
247  private: boost::unordered_set<NodePtr> nodesToProcess;
248 
249  private: boost::recursive_mutex nodeMutex;
250 
252  private: boost::mutex subscriberMutex;
253 
255  private: boost::mutex processNodesMutex;
256 
257  private: bool pauseIncoming;
258 
259  // Singleton implementation
260  private: friend class SingletonT<TopicManager>;
261  };
263  }
264 }
265 #endif
Options for a subscription.
Definition: SubscribeOptions.hh:35
Forward declarations for the common classes.
Definition: Animation.hh:26
boost::shared_ptr< google::protobuf::Message > MessagePtr
Definition: TransportTypes.hh:45
#define NULL
Definition: CommonTypes.hh:31
Singleton template class.
Definition: SingletonT.hh:33
#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
boost::shared_ptr< Publisher > PublisherPtr
Definition: TransportTypes.hh:49
Forward declarations for transport.
#define GZ_ASSERT(_expr, _msg)
This macro define the standard way of launching an exception inside gazebo.
Definition: Assert.hh:24
boost::shared_ptr< Subscriber > SubscriberPtr
Definition: TransportTypes.hh:53
boost::shared_ptr< Publication > PublicationPtr
Definition: TransportTypes.hh:61
boost::shared_ptr< Node > NodePtr
Definition: TransportTypes.hh:57
boost::shared_ptr< SubscriptionTransport > SubscriptionTransportPtr
Definition: TransportTypes.hh:69
transport
Definition: TopicManager.hh:44
Manages topics and their subscriptions.
Definition: TopicManager.hh:55
A publisher of messages on a topic.
Definition: Publisher.hh:43
gazebo
Definition: TopicManager.hh:44
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
static ConnectionManager * Instance()
Get an instance of the singleton.
Definition: SingletonT.hh:36
PublisherPtr Advertise(const std::string &_topic, unsigned int _queueLimit, double _hzRate)
Advertise on a topic.
Definition: TopicManager.hh:103
#define GZ_SINGLETON_DECLARE(visibility, n1, n2, singletonType)
Helper to declare typed SingletonT.
Definition: SingletonT.hh:61
std::map< std::string, std::list< NodePtr > > SubNodeMap
A map of string->list of Node pointers.
Definition: TopicManager.hh:238