Publisher.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 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 
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 
127  private: void PublishImpl(const google::protobuf::Message &_message,
128  bool _block);
129 
132  private: void OnPublishComplete(uint32_t _id);
133 
135  private: std::string topic;
136 
138  private: std::string msgType;
139 
142  private: unsigned int queueLimit;
143 
146  private: double updatePeriod;
147 
150  private: bool queueLimitWarned;
151 
153  private: std::list<MessagePtr> messages;
154 
156  private: mutable boost::mutex mutex;
157 
160  private: PublicationPtr publication;
161 
163  private: NodePtr node;
164 
165  private: common::Time currentTime;
166  private: common::Time prevPublishTime;
167 
169  private: uint32_t pubId;
170 
172  private: std::map<uint32_t, int> pubIds;
173 
175  private: uint32_t id;
176 
178  private: static uint32_t idCounter;
179  };
181  }
182 }
183 #endif
void Publish(M _message, bool _block=false)
Publish an arbitrary message on the topic.
Definition: Publisher.hh:89
Forward declarations for transport.
A publisher of messages on a topic.
Definition: Publisher.hh:43
boost::shared_ptr< Publication > PublicationPtr
Definition: TransportTypes.hh:61
boost::shared_ptr< google::protobuf::Message > MessagePtr
Definition: TransportTypes.hh:45
void Publish(const google::protobuf::Message &_message, bool _block=false)
Publish a protobuf message on the topic.
Definition: Publisher.hh:80
boost::shared_ptr< Node > NodePtr
Definition: TransportTypes.hh:57
#define GZ_TRANSPORT_VISIBLE
Definition: system.hh:166
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:39