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 
68  public: void SetPublication(PublicationPtr &_publication, int _i)
69  GAZEBO_DEPRECATED(1.5);
70 
73  public: void SetPublication(PublicationPtr _publication);
74 
79  public: void Publish(const google::protobuf::Message &_message,
80  bool _block = false)
81  { this->PublishImpl(_message, _block); }
82 
87  public: template< typename M>
88  void Publish(M _message, bool _block = false)
89  { this->PublishImpl(_message, _block); }
90 
93  public: unsigned int GetOutgoingCount() const;
94 
95  private: void PublishImpl(const google::protobuf::Message &_message,
96  bool _block);
97 
100  public: std::string GetTopic() const;
101 
104  public: std::string GetMsgType() const;
105 
107  public: void SendMessage();
108 
110  public: bool GetLatching() const GAZEBO_DEPRECATED(1.5);
111 
114  public: std::string GetPrevMsg() const;
115 
118  public: MessagePtr GetPrevMsgPtr() const;
119 
121  private: void OnPublishComplete();
122 
124  private: std::string topic;
125 
127  private: std::string msgType;
128 
131  private: unsigned int queueLimit;
132 
135  private: double updatePeriod;
136 
139  private: bool queueLimitWarned;
140 
142  private: std::list<MessagePtr> messages;
143 
145  private: mutable boost::recursive_mutex mutex;
146 
149  private: PublicationPtr publication;
150 
152  private: MessagePtr prevMsg;
153 
154  private: common::Time currentTime;
155  private: common::Time prevPublishTime;
156  };
158  }
159 }
160 #endif