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 2011 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 
39  class Publisher
40  {
44  public: Publisher(const std::string &topic, const std::string &msg_type,
45  unsigned int _limit, bool _latch);
46 
48  public: virtual ~Publisher();
49 
50  public: bool HasConnections() const;
51 
54  public: void WaitForConnection() const;
55 
56  public: void SetPublication(PublicationPtr &_publication, int _i);
57 
59  public: void Publish(const google::protobuf::Message &_message,
60  bool _block = false)
61  { this->PublishImpl(_message, _block); }
62  public: template< typename M>
63  void Publish(M _message, bool _block = false)
64  { this->PublishImpl(_message, _block); }
65  public: unsigned int GetOutgoingCount() const;
66 
67  private: void PublishImpl(const google::protobuf::Message &_message,
68  bool _block);
69 
71  public: std::string GetTopic() const;
72 
74  public: std::string GetMsgType() const;
75 
77  public: void SendMessage();
78  public: bool GetLatching() const;
79 
80  public: std::string GetPrevMsg() const;
81 
83  private: void OnPublishComplete();
84 
85  private: std::string topic;
86  private: std::string msgType;
87  private: unsigned int queueLimit;
88  private: std::list<google::protobuf::Message *> messages;
89  private: boost::recursive_mutex *mutex;
90  private: PublicationPtr publications[2];
91 
92  private: bool latch;
93  private: google::protobuf::Message *prevMsg;
94  };
96  }
97 }
98 
99 #endif
100 
101