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 Nate Koenig
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  {
47  public: Publisher(const std::string &_topic, const std::string &_msgType,
48  unsigned int _limit, bool _latch);
49 
51  public: virtual ~Publisher();
52 
55  public: bool HasConnections() const;
56 
59  public: void WaitForConnection() const;
60 
64  public: void SetPublication(PublicationPtr &_publication, int _i);
65 
70  public: void Publish(const google::protobuf::Message &_message,
71  bool _block = false)
72  { this->PublishImpl(_message, _block); }
73 
78  public: template< typename M>
79  void Publish(M _message, bool _block = false)
80  { this->PublishImpl(_message, _block); }
81 
84  public: unsigned int GetOutgoingCount() const;
85 
86  private: void PublishImpl(const google::protobuf::Message &_message,
87  bool _block);
88 
91  public: std::string GetTopic() const;
92 
95  public: std::string GetMsgType() const;
96 
98  public: void SendMessage();
99 
102  public: bool GetLatching() const;
103 
106  public: std::string GetPrevMsg() const;
107 
109  private: void OnPublishComplete();
110 
111  private: std::string topic;
112  private: std::string msgType;
113  private: unsigned int queueLimit;
114  private: std::list<google::protobuf::Message *> messages;
115  private: boost::recursive_mutex *mutex;
116  private: PublicationPtr publications[2];
117 
118  private: bool latch;
119  private: google::protobuf::Message *prevMsg;
120  };
122  }
123 }
124 
125 #endif
126 
127