##

TopicManager.hh
Go to the documentation of this file.
/*
* Copyright (C) 2012 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef GAZEBO_TRANSPORT_TOPICMANAGER_HH_
#define GAZEBO_TRANSPORT_TOPICMANAGER_HH_
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <map>
#include <list>
#include <string>
#include <vector>
#include <boost/unordered/unordered_set.hpp>
#include "gazebo/common/Assert.hh"
#include "gazebo/common/Exception.hh"
#include "gazebo/msgs/msgs.hh"
#include "gazebo/common/SingletonT.hh"
#include "gazebo/transport/TransportTypes.hh"
#include "gazebo/transport/SubscribeOptions.hh"
#include "gazebo/transport/SubscriptionTransport.hh"
#include "gazebo/transport/PublicationTransport.hh"
#include "gazebo/transport/ConnectionManager.hh"
#include "gazebo/transport/Publisher.hh"
#include "gazebo/transport/Publication.hh"
#include "gazebo/transport/Subscriber.hh"
#include "gazebo/util/system.hh"
GZ_SINGLETON_DECLARE(GZ_TRANSPORT_VISIBLE, gazebo, transport, TopicManager)
namespace gazebo
{
namespace transport
{
class GZ_TRANSPORT_VISIBLE TopicManager : public SingletonT<TopicManager>
{
private: TopicManager();
private: virtual ~TopicManager();
public: void Init();
public: void Fini();
public: PublicationPtr FindPublication(const std::string &_topic);
public: void AddNode(NodePtr _node);
public: void RemoveNode(unsigned int _id);
public: void ProcessNodes(bool _onlyOut = false);
public: SubscriberPtr Subscribe(const SubscribeOptions &_options);
public: void Unsubscribe(const std::string &_topic, const NodePtr &_sub);
public: template<typename M>
PublisherPtr Advertise(const std::string &_topic,
unsigned int _queueLimit,
double _hzRate)
{
google::protobuf::Message *msg = NULL;
M msgtype;
msg = dynamic_cast<google::protobuf::Message *>(&msgtype);
if (!msg)
gzthrow("Advertise requires a google protobuf type");
this->UpdatePublications(_topic, msg->GetTypeName());
PublisherPtr pub = PublisherPtr(new Publisher(_topic,
msg->GetTypeName(), _queueLimit, _hzRate));
std::string msgTypename;
PublicationPtr publication;
// Connect all local subscription to the publisher
msgTypename = msg->GetTypeName();
publication = this->FindPublication(_topic);
GZ_ASSERT(publication != NULL, "FindPublication returned NULL");
publication->AddPublisher(pub);
if (!publication->GetLocallyAdvertised())
{
ConnectionManager::Instance()->Advertise(_topic, msgTypename);
}
publication->SetLocallyAdvertised(true);
pub->SetPublication(publication);
SubNodeMap::iterator iter2;
SubNodeMap::iterator stEnd2 = this->subscribedNodes.end();
for (iter2 = this->subscribedNodes.begin();
iter2 != stEnd2; ++iter2)
{
if (iter2->first == _topic)
{
std::list<NodePtr>::iterator liter;
std::list<NodePtr>::iterator lEnd = iter2->second.end();
for (liter = iter2->second.begin();
liter != lEnd; ++liter)
{
publication->AddSubscription(*liter);
}
}
}
return pub;
}
public: void Unadvertise(const std::string &_topic);
public: void Unadvertise(PublisherPtr _pub);
public: void Unadvertise(const std::string &_topic, const uint32_t _id);
public: void Publish(const std::string &_topic, MessagePtr _message,
boost::function<void(uint32_t)> _cb, uint32_t _id);
public: void ConnectPubToSub(const std::string &_topic,
const SubscriptionTransportPtr _sublink);
public: void ConnectSubToPub(const msgs::Publish &_pub);
public: void DisconnectPubFromSub(const std::string &_topic,
const std::string &_host,
unsigned int _port);
public: void DisconnectSubFromPub(const std::string &_topic,
const std::string &_host,
unsigned int _port);
public: void ConnectSubscribers(const std::string &_topic);
public: PublicationPtr UpdatePublications(const std::string &_topic,
const std::string &_msgType);
public: void RegisterTopicNamespace(const std::string &_name);
public: void GetTopicNamespaces(std::list<std::string> &_namespaces);
public: void ClearBuffers();
public: void PauseIncoming(bool _pause);
public: void AddNodeToProcess(NodePtr _ptr);
typedef std::map<std::string, std::list<NodePtr> > SubNodeMap;
private: typedef std::map<std::string, PublicationPtr> PublicationPtr_M;
private: PublicationPtr_M advertisedTopics;
private: PublicationPtr_M::iterator advertisedTopicsEnd;
private: SubNodeMap subscribedNodes;
private: std::vector<NodePtr> nodes;
private: boost::unordered_set<NodePtr> nodesToProcess;
private: boost::recursive_mutex nodeMutex;
private: boost::mutex subscriberMutex;
private: boost::mutex processNodesMutex;
private: bool pauseIncoming;
// Singleton implementation
private: friend class SingletonT<TopicManager>;
};
}
}
#endif
ConnectionManager.hh
gazebo::transport::SubscribeOptions
Options for a subscription.
Definition: SubscribeOptions.hh:35
gazebo
Forward declarations for the common classes.
Definition: Animation.hh:26
Exception.hh
gazebo::transport::MessagePtr
boost::shared_ptr< google::protobuf::Message > MessagePtr
Definition: TransportTypes.hh:45
NULL
#define NULL
Definition: CommonTypes.hh:31
SingletonT
Singleton template class.
Definition: SingletonT.hh:33
gzthrow
#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
gazebo::transport::PublisherPtr
boost::shared_ptr< Publisher > PublisherPtr
Definition: TransportTypes.hh:49
TransportTypes.hh
Forward declarations for transport.
GZ_ASSERT
#define GZ_ASSERT(_expr, _msg)
This macro define the standard way of launching an exception inside gazebo.
Definition: Assert.hh:24
system.hh
msgs.hh
gazebo::transport::SubscriberPtr
boost::shared_ptr< Subscriber > SubscriberPtr
Definition: TransportTypes.hh:53
gazebo::transport::PublicationPtr
boost::shared_ptr< Publication > PublicationPtr
Definition: TransportTypes.hh:61
gazebo::transport::NodePtr
boost::shared_ptr< Node > NodePtr
Definition: TransportTypes.hh:57
gazebo::transport::SubscriptionTransportPtr
boost::shared_ptr< SubscriptionTransport > SubscriptionTransportPtr
Definition: TransportTypes.hh:69
PublicationTransport.hh
transport
transport
Definition: TopicManager.hh:44
gazebo::transport::TopicManager
Manages topics and their subscriptions.
Definition: TopicManager.hh:55
gazebo::transport::Publisher
A publisher of messages on a topic.
Definition: Publisher.hh:43
gazebo
gazebo
Definition: TopicManager.hh:44
gazebo::msgs::Init
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
SingletonT< ConnectionManager >::Instance
static ConnectionManager * Instance()
Get an instance of the singleton.
Definition: SingletonT.hh:36
Assert.hh
Publication.hh
SubscriptionTransport.hh
Publisher.hh
SubscribeOptions.hh
gazebo::transport::TopicManager::Advertise
PublisherPtr Advertise(const std::string &_topic, unsigned int _queueLimit, double _hzRate)
Advertise on a topic.
Definition: TopicManager.hh:103
GZ_SINGLETON_DECLARE
#define GZ_SINGLETON_DECLARE(visibility, n1, n2, singletonType)
Helper to declare typed SingletonT.
Definition: SingletonT.hh:61
Subscriber.hh
SingletonT.hh
gazebo::transport::TopicManager::SubNodeMap
std::map< std::string, std::list< NodePtr > > SubNodeMap
A map of string->list of Node pointers.
Definition: TopicManager.hh:238