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 (C) 2012-2014 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 
29 #include "gazebo/common/Time.hh"
31 
32 namespace gazebo
33 {
34  namespace transport
35  {
38 
41  class Publisher
42  {
49  public: Publisher(const std::string &_topic, const std::string &_msgType,
50  unsigned int _limit, double _hzRate);
51 
53  public: virtual ~Publisher();
54 
57  public: bool HasConnections() const;
58 
61  public: void WaitForConnection() const;
62 
68  public: bool WaitForConnection(const common::Time &_timeout) const;
69 
72  public: void SetPublication(PublicationPtr _publication);
73 
78  public: void Publish(const google::protobuf::Message &_message,
79  bool _block = false)
80  { this->PublishImpl(_message, _block); }
81 
86  public: template< typename M>
87  void Publish(M _message, bool _block = false)
88  { this->PublishImpl(_message, _block); }
89 
92  public: unsigned int GetOutgoingCount() const;
93 
94  private: void PublishImpl(const google::protobuf::Message &_message,
95  bool _block);
96 
99  public: std::string GetTopic() const;
100 
103  public: std::string GetMsgType() const;
104 
106  public: void SendMessage();
107 
111  public: void SetNode(NodePtr _node);
112 
115  public: std::string GetPrevMsg() const;
116 
119  public: MessagePtr GetPrevMsgPtr() const;
120 
123  private: void OnPublishComplete(uint32_t _id);
124 
126  private: std::string topic;
127 
129  private: std::string msgType;
130 
133  private: unsigned int queueLimit;
134 
137  private: double updatePeriod;
138 
141  private: bool queueLimitWarned;
142 
144  private: std::list<MessagePtr> messages;
145 
147  private: mutable boost::mutex mutex;
148 
151  private: PublicationPtr publication;
152 
154  private: MessagePtr prevMsg;
155 
157  private: NodePtr node;
158 
159  private: common::Time currentTime;
160  private: common::Time prevPublishTime;
161 
163  private: bool waiting;
164 
166  private: uint32_t pubId;
167  private: std::list<uint32_t> pubIds;
168  };
170  }
171 }
172 #endif