Publisher.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 /* Desc: Handles pushing messages out on a named topic
18  * Author: Nate Koenig
19  */
20 
21 #ifndef GAZEBO_TRANSPORT_PUBLISHER_HH_
22 #define GAZEBO_TRANSPORT_PUBLISHER_HH_
23 
24 #include <google/protobuf/message.h>
25 #include <boost/thread.hpp>
26 #include <boost/shared_ptr.hpp>
27 #include <string>
28 #include <list>
29 #include <map>
30 
31 #include "gazebo/common/Time.hh"
33 #include "gazebo/util/system.hh"
34 
35 namespace gazebo
36 {
37  namespace transport
38  {
41 
44  class GZ_TRANSPORT_VISIBLE Publisher :
45  public boost::enable_shared_from_this<Publisher>
46  {
53  public: Publisher(const std::string &_topic, const std::string &_msgType,
54  unsigned int _limit, double _hzRate);
55 
57  public: virtual ~Publisher();
58 
61  public: bool HasConnections() const;
62 
65  public: void WaitForConnection() const;
66 
72  public: bool WaitForConnection(const common::Time &_timeout) const;
73 
76  public: void SetPublication(PublicationPtr _publication);
77 
80  public: unsigned int GetRemoteSubscriptionCount();
81 
89  public: void Publish(const google::protobuf::Message &_message,
90  bool _block = false)
91  { this->PublishImpl(_message, _block); }
92 
100  public: template< typename M>
101  void Publish(M _message, bool _block = false)
102  { this->PublishImpl(_message, _block); }
103 
106  public: unsigned int GetOutgoingCount() const;
107 
110  public: std::string GetTopic() const;
111 
114  public: std::string GetMsgType() const;
115 
121  public: void SendMessage();
122 
126  public: void SetNode(NodePtr _node);
127 
130  public: std::string GetPrevMsg() const;
131 
134  public: MessagePtr GetPrevMsgPtr() const;
135 
137  public: void Fini();
138 
141  public: uint32_t Id() const;
142 
147  private: void PublishImpl(const google::protobuf::Message &_message,
148  bool _block);
149 
152  private: void OnPublishComplete(uint32_t _id);
153 
155  private: std::string topic;
156 
158  private: std::string msgType;
159 
162  private: unsigned int queueLimit;
163 
166  private: double updatePeriod;
167 
170  private: bool queueLimitWarned;
171 
173  private: std::list<MessagePtr> messages;
174 
176  private: mutable boost::mutex mutex;
177 
180  private: PublicationPtr publication;
181 
183  private: NodePtr node;
184 
185  private: common::Time currentTime;
186  private: common::Time prevPublishTime;
187 
189  private: uint32_t pubId;
190 
192  private: std::map<uint32_t, int> pubIds;
193 
195  private: uint32_t id;
196 
198  private: static uint32_t idCounter;
199  };
201  }
202 }
203 #endif
void Publish(M _message, bool _block=false)
Publish an arbitrary message on the topic.
Definition: Publisher.hh:101
Forward declarations for the common classes.
Definition: Animation.hh:26
transport
Definition: ConnectionManager.hh:35
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:89
boost::shared_ptr< Publication > PublicationPtr
Definition: TransportTypes.hh:61
A publisher of messages on a topic.
Definition: Publisher.hh:44
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:47