All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Publisher.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 /* 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 
30 
31 namespace gazebo
32 {
33  namespace transport
34  {
37 
40  class Publisher
41  {
43  public: Publisher(const std::string &_topic, const std::string &_msgType,
44  unsigned int _limit, bool _latch) GAZEBO_DEPRECATED(1.5);
45 
52  public: Publisher(const std::string &_topic, const std::string &_msgType,
53  unsigned int _limit, double _hzRate);
54 
56  public: virtual ~Publisher();
57 
60  public: bool HasConnections() const;
61 
64  public: void WaitForConnection() const;
65 
71  public: bool WaitForConnection(const common::Time &_timeout) const;
72 
75  public: void SetPublication(PublicationPtr &_publication, int _i)
76  GAZEBO_DEPRECATED(1.5);
77 
80  public: void SetPublication(PublicationPtr _publication);
81 
86  public: void Publish(const google::protobuf::Message &_message,
87  bool _block = false)
88  { this->PublishImpl(_message, _block); }
89 
94  public: template< typename M>
95  void Publish(M _message, bool _block = false)
96  { this->PublishImpl(_message, _block); }
97 
100  public: unsigned int GetOutgoingCount() const;
101 
102  private: void PublishImpl(const google::protobuf::Message &_message,
103  bool _block);
104 
107  public: std::string GetTopic() const;
108 
111  public: std::string GetMsgType() const;
112 
114  public: void SendMessage();
115 
119  public: void SetNode(NodePtr _node);
120 
122  public: bool GetLatching() const GAZEBO_DEPRECATED(1.5);
123 
126  public: std::string GetPrevMsg() const;
127 
130  public: MessagePtr GetPrevMsgPtr() const;
131 
134  private: void OnPublishComplete(uint32_t _id);
135 
137  private: std::string topic;
138 
140  private: std::string msgType;
141 
144  private: unsigned int queueLimit;
145 
148  private: double updatePeriod;
149 
152  private: bool queueLimitWarned;
153 
155  private: std::list<MessagePtr> messages;
156 
158  private: mutable boost::mutex mutex;
159 
162  private: PublicationPtr publication;
163 
165  private: MessagePtr prevMsg;
166 
168  private: NodePtr node;
169 
170  private: common::Time currentTime;
171  private: common::Time prevPublishTime;
172 
174  private: bool waiting;
175 
177  private: uint32_t pubId;
178  private: std::list<uint32_t> pubIds;
179  };
181  }
182 }
183 #endif