All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TopicManager.hh
Go to the documentation of this file.
1 /*
2  * Copyright 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 <map>
22 #include <list>
23 #include <string>
24 #include <vector>
25 
26 #include "gazebo/common/Assert.hh"
28 #include "gazebo/msgs/msgs.hh"
30 
39 
40 namespace gazebo
41 {
42  namespace transport
43  {
46 
49  class TopicManager : public SingletonT<TopicManager>
50  {
51  private: TopicManager();
52  private: virtual ~TopicManager();
53 
55  public: void Init();
56 
58  public: void Fini();
59 
63  public: PublicationPtr FindPublication(const std::string &_topic);
64 
67  public: void AddNode(NodePtr _node);
68 
71  public: void RemoveNode(unsigned int _id);
72 
76  public: void ProcessNodes(bool _onlyOut = false);
77 
81  public: bool IsAdvertised(const std::string &_topic);
82 
86  public: SubscriberPtr Subscribe(const SubscribeOptions &_options);
87 
92  public: void Unsubscribe(const std::string &_topic, const NodePtr &_sub);
93 
101  public: template<typename M>
102  PublisherPtr Advertise(const std::string &_topic,
103  unsigned int _queueLimit,
104  double _hzRate)
105  {
106  google::protobuf::Message *msg = NULL;
107  M msgtype;
108  msg = dynamic_cast<google::protobuf::Message *>(&msgtype);
109  if (!msg)
110  gzthrow("Advertise requires a google protobuf type");
111 
112  this->UpdatePublications(_topic, msg->GetTypeName());
113 
114  PublisherPtr pub = PublisherPtr(new Publisher(_topic,
115  msg->GetTypeName(), _queueLimit, _hzRate));
116 
117  std::string msgTypename;
118  PublicationPtr publication;
119 
120  // Connect all local subscription to the publisher
121  msgTypename = msg->GetTypeName();
122 
123  publication = this->FindPublication(_topic);
124  GZ_ASSERT(publication != NULL, "FindPublication returned NULL");
125 
126  publication->AddPublisher(pub);
127  if (!publication->GetLocallyAdvertised())
128  {
129  ConnectionManager::Instance()->Advertise(_topic, msgTypename);
130  }
131 
132  publication->SetLocallyAdvertised(true);
133  pub->SetPublication(publication);
134 
135 
136  SubNodeMap::iterator iter2;
137  SubNodeMap::iterator stEnd2 = this->subscribedNodes.end();
138  for (iter2 = this->subscribedNodes.begin();
139  iter2 != stEnd2; ++iter2)
140  {
141  if (iter2->first == _topic)
142  {
143  std::list<NodePtr>::iterator liter;
144  std::list<NodePtr>::iterator lEnd = iter2->second.end();
145  for (liter = iter2->second.begin();
146  liter != lEnd; ++liter)
147  {
148  publication->AddSubscription(*liter);
149  }
150  }
151  }
152 
153  return pub;
154  }
155 
158  public: void Unadvertise(const std::string &_topic);
159 
165  public: void Publish(const std::string &_topic, MessagePtr _message,
166  const boost::function<void()> &_cb = NULL);
167 
171  public: void ConnectPubToSub(const std::string &_topic,
172  const SubscriptionTransportPtr _sublink);
173 
176  public: void ConnectSubToPub(const msgs::Publish &_pub);
177 
182  public: void DisconnectPubFromSub(const std::string &_topic,
183  const std::string &_host,
184  unsigned int _port);
185 
190  public: void DisconnectSubFromPub(const std::string &_topic,
191  const std::string &_host,
192  unsigned int _port);
193 
196  public: void ConnectSubscribers(const std::string &_topic);
197 
203  public: PublicationPtr UpdatePublications(const std::string &_topic,
204  const std::string &_msgType);
205 
208  public: void RegisterTopicNamespace(const std::string &_name);
209 
212  public: void GetTopicNamespaces(std::list<std::string> &_namespaces);
213 
218  public: std::map<std::string, std::list<std::string> >
220 
222  public: void ClearBuffers();
223 
226  public: void PauseIncoming(bool _pause);
227 
229  typedef std::map<std::string, std::list<NodePtr> > SubNodeMap;
230 
231  private: typedef std::map<std::string, PublicationPtr> PublicationPtr_M;
232  private: PublicationPtr_M advertisedTopics;
233  private: PublicationPtr_M::iterator advertisedTopicsEnd;
234  private: SubNodeMap subscribedNodes;
235  private: std::vector<NodePtr> nodes;
236 
237  private: boost::recursive_mutex nodeMutex;
238 
240  private: boost::mutex subscriberMutex;
241 
242  private: bool pauseIncoming;
243 
244  // Singleton implementation
245  private: friend class SingletonT<TopicManager>;
246  };
248  }
249 }
250 #endif