ConnectionManager.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 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 #ifndef _CONNECTION_MANAGER_HH_
18 #define _CONNECTION_MANAGER_HH_
19 
20 
21 #include <boost/shared_ptr.hpp>
22 #include <boost/interprocess/sync/interprocess_semaphore.hpp>
23 #include <string>
24 #include <list>
25 #include <vector>
26 
27 #include "gazebo/msgs/msgs.hh"
29 
32 #include "gazebo/util/system.hh"
33 
35 GZ_SINGLETON_DECLARE(GZ_TRANSPORT_VISIBLE, gazebo, transport, ConnectionManager)
36 
37 namespace gazebo
38 {
39  namespace transport
40  {
43 
46  class GZ_TRANSPORT_VISIBLE ConnectionManager :
47  public SingletonT<ConnectionManager>
48  {
50  private: ConnectionManager();
51 
53  private: virtual ~ConnectionManager();
54 
61  public: bool Init(const std::string &_masterHost,
62  unsigned int _masterPort,
63  uint32_t _timeoutIterations = 30);
64 
67  public: bool IsInitialized() const;
68 
71  public: void Run();
72 
75  public: bool IsRunning() const;
76 
78  public: void Fini();
79 
81  public: void Stop();
82 
88  public: void Subscribe(const std::string &_topic,
89  const std::string &_msgType,
90  bool _latching);
91 
94  public: void Unsubscribe(const msgs::Subscribe &_sub);
95 
99  public: void Unsubscribe(const std::string &_topic,
100  const std::string &_msgType);
101 
105  public: void Advertise(const std::string &_topic,
106  const std::string &_msgType);
107 
110  public: void Unadvertise(const std::string &_topic);
111 
114  public: void GetAllPublishers(std::list<msgs::Publish> &_publishers);
115 
118  public: void RemoveConnection(ConnectionPtr &_conn);
119 
122  public: void RegisterTopicNamespace(const std::string &_name);
123 
126  public: void GetTopicNamespaces(std::list<std::string> &_namespaces);
127 
132  private: ConnectionPtr FindConnection(const std::string &_host,
133  unsigned int _port);
134 
139  public: ConnectionPtr ConnectToRemoteHost(const std::string &_host,
140  unsigned int _port);
141 
143  public: void TriggerUpdate();
144 
148  private: void OnMasterRead(const std::string &_data);
149 
152  private: void OnAccept(ConnectionPtr _newConnection);
153 
157  private: void OnRead(ConnectionPtr _newConnection,
158  const std::string &_data);
159 
162  private: void ProcessMessage(const std::string &_packet);
163 
165  private: void RunUpdate();
166 
168  private: boost::condition_variable updateCondition;
169 
171  private: boost::mutex updateMutex;
172 
173  private: ConnectionPtr masterConn;
174  private: ConnectionPtr serverConn;
175 
176  private: std::list<ConnectionPtr> connections;
177  protected: std::vector<event::ConnectionPtr> eventConnections;
178 
179  private: bool initialized;
180  private: bool stop, stopped;
181 
182  private: unsigned int tmpIndex;
183  private: boost::recursive_mutex listMutex;
184 
186  private: boost::mutex namespaceMutex;
187  private: boost::recursive_mutex masterMessagesMutex;
188  private: boost::recursive_mutex connectionMutex;
189 
190  private: std::list<msgs::Publish> publishers;
191  private: std::list<std::string> namespaces;
192  private: std::list<std::string> masterMessages;
193 
195  private: boost::condition_variable namespaceCondition;
196 
197  // Singleton implementation
198  private: friend class SingletonT<ConnectionManager>;
199  };
201  }
202 }
203 #endif
Forward declarations for the common classes.
Definition: Animation.hh:26
transport
Definition: ConnectionManager.hh:35
Singleton template class.
Definition: SingletonT.hh:33
std::vector< event::ConnectionPtr > eventConnections
Definition: ConnectionManager.hh:177
void stop()
Stop the transport component from running.
boost::shared_ptr< Connection > ConnectionPtr
Definition: Connection.hh:51
Manager of connections.
Definition: ConnectionManager.hh:46
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
#define GZ_SINGLETON_DECLARE(visibility, n1, n2, singletonType)
Helper to declare typed SingletonT.
Definition: SingletonT.hh:61