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 
34 namespace gazebo
35 {
36  namespace transport
37  {
40 
43  class GZ_TRANSPORT_VISIBLE ConnectionManager :
44  public SingletonT<ConnectionManager>
45  {
47  private: ConnectionManager();
48 
50  private: virtual ~ConnectionManager();
51 
58  public: bool Init(const std::string &_masterHost,
59  unsigned int _masterPort,
60  uint32_t _timeoutIterations = 30);
61 
64  public: bool IsInitialized() const;
65 
68  public: void Run();
69 
72  public: bool IsRunning() const;
73 
75  public: void Fini();
76 
78  public: void Stop();
79 
85  public: void Subscribe(const std::string &_topic,
86  const std::string &_msgType,
87  bool _latching);
88 
91  public: void Unsubscribe(const msgs::Subscribe &_sub);
92 
96  public: void Unsubscribe(const std::string &_topic,
97  const std::string &_msgType);
98 
102  public: void Advertise(const std::string &_topic,
103  const std::string &_msgType);
104 
107  public: void Unadvertise(const std::string &_topic);
108 
111  public: void GetAllPublishers(std::list<msgs::Publish> &_publishers);
112 
115  public: void RemoveConnection(ConnectionPtr &_conn);
116 
119  public: void RegisterTopicNamespace(const std::string &_name);
120 
123  public: void GetTopicNamespaces(std::list<std::string> &_namespaces);
124 
129  private: ConnectionPtr FindConnection(const std::string &_host,
130  unsigned int _port);
131 
136  public: ConnectionPtr ConnectToRemoteHost(const std::string &_host,
137  unsigned int _port);
138 
140  public: void TriggerUpdate();
141 
145  private: void OnMasterRead(const std::string &_data);
146 
149  private: void OnAccept(ConnectionPtr _newConnection);
150 
154  private: void OnRead(ConnectionPtr _newConnection,
155  const std::string &_data);
156 
159  private: void ProcessMessage(const std::string &_packet);
160 
162  private: void RunUpdate();
163 
165  private: boost::condition_variable updateCondition;
166 
168  private: boost::mutex updateMutex;
169 
170  private: ConnectionPtr masterConn;
171  private: ConnectionPtr serverConn;
172 
173  private: std::list<ConnectionPtr> connections;
174  protected: std::vector<event::ConnectionPtr> eventConnections;
175 
176  private: bool initialized;
177  private: bool stop, stopped;
178 
179  private: unsigned int tmpIndex;
180  private: boost::recursive_mutex listMutex;
181 
183  private: boost::mutex namespaceMutex;
184  private: boost::recursive_mutex masterMessagesMutex;
185  private: boost::recursive_mutex connectionMutex;
186 
187  private: std::list<msgs::Publish> publishers;
188  private: std::list<std::string> namespaces;
189  private: std::list<std::string> masterMessages;
190 
192  private: boost::condition_variable namespaceCondition;
193 
194  // Singleton implementation
195  private: friend class SingletonT<ConnectionManager>;
196  };
198  }
199 }
200 #endif
Forward declarations for the common classes.
Definition: Animation.hh:26
Singleton template class.
Definition: SingletonT.hh:33
std::vector< event::ConnectionPtr > eventConnections
Definition: ConnectionManager.hh:174
void stop()
Stop the transport component from running.
boost::shared_ptr< Connection > ConnectionPtr
Definition: Connection.hh:51
Manager of connections.
Definition: ConnectionManager.hh:43
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.