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  {
48  public: Publisher(const std::string &_topic, const std::string &_msgType,
49  unsigned int _limit, double _hzRate);
50 
52  public: virtual ~Publisher();
53 
56  public: bool HasConnections() const;
57 
60  public: void WaitForConnection() const;
61 
67  public: bool WaitForConnection(const common::Time &_timeout) const;
68 
71  public: void SetPublication(PublicationPtr _publication);
72 
77  public: void Publish(const google::protobuf::Message &_message,
78  bool _block = false)
79  { this->PublishImpl(_message, _block); }
80 
85  public: template< typename M>
86  void Publish(M _message, bool _block = false)
87  { this->PublishImpl(_message, _block); }
88 
91  public: unsigned int GetOutgoingCount() const;
92 
93  private: void PublishImpl(const google::protobuf::Message &_message,
94  bool _block);
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 
122  private: void OnPublishComplete(uint32_t _id);
123 
125  private: std::string topic;
126 
128  private: std::string msgType;
129 
132  private: unsigned int queueLimit;
133 
136  private: double updatePeriod;
137 
140  private: bool queueLimitWarned;
141 
143  private: std::list<MessagePtr> messages;
144 
146  private: mutable boost::mutex mutex;
147 
150  private: PublicationPtr publication;
151 
153  private: MessagePtr prevMsg;
154 
156  private: NodePtr node;
157 
158  private: common::Time currentTime;
159  private: common::Time prevPublishTime;
160 
162  private: bool waiting;
163 
165  private: uint32_t pubId;
166  private: std::list<uint32_t> pubIds;
167  };
169  }
170 }
171 #endif