Publisher.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2016 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 /* Desc: Handles pushing messages out on a named topic
18  * Author: Nate Koenig
19  */
20 
21 #ifndef _PUBLISHER_HH_
22 #define _PUBLISHER_HH_
23 
24 #include <google/protobuf/message.h>
25 #include <boost/thread.hpp>
26 #include <string>
27 #include <list>
28 #include <map>
29 
30 #include "gazebo/common/Time.hh"
32 #include "gazebo/util/system.hh"
33 
34 namespace gazebo
35 {
36  namespace transport
37  {
40 
43  class GZ_TRANSPORT_VISIBLE Publisher
44  {
51  public: Publisher(const std::string &_topic, const std::string &_msgType,
52  unsigned int _limit, double _hzRate);
53 
55  public: virtual ~Publisher();
56 
59  public: bool HasConnections() const;
60 
63  public: void WaitForConnection() const;
64 
70  public: bool WaitForConnection(const common::Time &_timeout) const;
71 
74  public: void SetPublication(PublicationPtr _publication);
75 
80  public: void Publish(const google::protobuf::Message &_message,
81  bool _block = false)
82  { this->PublishImpl(_message, _block); }
83 
88  public: template< typename M>
89  void Publish(M _message, bool _block = false)
90  { this->PublishImpl(_message, _block); }
91 
94  public: unsigned int GetOutgoingCount() const;
95 
98  public: std::string GetTopic() const;
99 
102  public: std::string GetMsgType() const;
103 
105  public: void SendMessage();
106 
110  public: void SetNode(NodePtr _node);
111 
114  public: std::string GetPrevMsg() const;
115 
118  public: MessagePtr GetPrevMsgPtr() const;
119 
121  public: void Fini();
122 
125  public: uint32_t Id() const;
126 
131  private: void PublishImpl(const google::protobuf::Message &_message,
132  bool _block);
133 
136  private: void OnPublishComplete(uint32_t _id);
137 
139  private: std::string topic;
140 
142  private: std::string msgType;
143 
146  private: unsigned int queueLimit;
147 
150  private: double updatePeriod;
151 
154  private: bool queueLimitWarned;
155 
157  private: std::list<MessagePtr> messages;
158 
160  private: mutable boost::mutex mutex;
161 
164  private: PublicationPtr publication;
165 
167  private: NodePtr node;
168 
169  private: common::Time currentTime;
170  private: common::Time prevPublishTime;
171 
173  private: uint32_t pubId;
174 
176  private: std::map<uint32_t, int> pubIds;
177 
179  private: uint32_t id;
180 
182  private: static uint32_t idCounter;
183  };
185  }
186 }
187 #endif
void Publish(M _message, bool _block=false)
Publish an arbitrary message on the topic.
Definition: Publisher.hh:89
boost::shared_ptr< google::protobuf::Message > MessagePtr
Definition: TransportTypes.hh:45
Forward declarations for transport.
boost::shared_ptr< Node > NodePtr
Definition: TransportTypes.hh:57
void Publish(const google::protobuf::Message &_message, bool _block=false)
Publish a protobuf message on the topic.
Definition: Publisher.hh:80
boost::shared_ptr< Publication > PublicationPtr
Definition: TransportTypes.hh:61
A publisher of messages on a topic.
Definition: Publisher.hh:43
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:44