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 #if TBB_VERSION_MAJOR >= 2021
34 #endif
35 #include "gazebo/util/system.hh"
36 
38 GZ_SINGLETON_DECLARE(GZ_TRANSPORT_VISIBLE, gazebo, transport, ConnectionManager)
39 
40 namespace gazebo
41 {
42  namespace transport
43  {
46 
49  class GZ_TRANSPORT_VISIBLE ConnectionManager :
50  public SingletonT<ConnectionManager>
51  {
53  private: ConnectionManager();
54 
56  private: virtual ~ConnectionManager();
57 
64  public: bool Init(const std::string &_masterHost,
65  unsigned int _masterPort,
66  uint32_t _timeoutIterations = 30);
67 
70  public: bool IsInitialized() const;
71 
74  public: void Run();
75 
78  public: bool IsRunning() const;
79 
81  public: void Fini();
82 
84  public: void Stop();
85 
91  public: void Subscribe(const std::string &_topic,
92  const std::string &_msgType,
93  bool _latching);
94 
97  public: void Unsubscribe(const msgs::Subscribe &_sub);
98 
102  public: void Unsubscribe(const std::string &_topic,
103  const std::string &_msgType);
104 
108  public: void Advertise(const std::string &_topic,
109  const std::string &_msgType);
110 
113  public: void Unadvertise(const std::string &_topic);
114 
117  public: void GetAllPublishers(std::list<msgs::Publish> &_publishers);
118 
121  public: void RemoveConnection(ConnectionPtr &_conn);
122 
125  public: void RegisterTopicNamespace(const std::string &_name);
126 
129  public: void GetTopicNamespaces(std::list<std::string> &_namespaces);
130 
135  private: ConnectionPtr FindConnection(const std::string &_host,
136  unsigned int _port);
137 
142  public: ConnectionPtr ConnectToRemoteHost(const std::string &_host,
143  unsigned int _port);
144 
146  public: void TriggerUpdate();
147 
151  private: void OnMasterRead(const std::string &_data);
152 
155  private: void OnAccept(ConnectionPtr _newConnection);
156 
160  private: void OnRead(ConnectionPtr _newConnection,
161  const std::string &_data);
162 
165  private: void ProcessMessage(const std::string &_packet);
166 
168  private: void RunUpdate();
169 
171  private: boost::condition_variable updateCondition;
172 
174  private: boost::mutex updateMutex;
175 
176  private: ConnectionPtr masterConn;
177  private: ConnectionPtr serverConn;
178 
179  private: std::list<ConnectionPtr> connections;
180  protected: std::vector<event::ConnectionPtr> eventConnections;
181 
182  private: bool initialized;
183  private: bool stop, stopped;
184 
185  private: unsigned int tmpIndex;
186  private: boost::recursive_mutex listMutex;
187 
189  private: boost::mutex namespaceMutex;
190  private: boost::recursive_mutex masterMessagesMutex;
191  private: boost::recursive_mutex connectionMutex;
192 
193  private: std::list<msgs::Publish> publishers;
194  private: std::list<std::string> namespaces;
195  private: std::list<std::string> masterMessages;
196 
198  private: boost::condition_variable namespaceCondition;
199 
200 #if TBB_VERSION_MAJOR >= 2021
202  private: TaskGroup taskGroup;
203 #endif
204 
205  // Singleton implementation
206  private: friend class SingletonT<ConnectionManager>;
207  };
209  }
210 }
211 #endif
transport
Definition: ConnectionManager.hh:38
Singleton template class.
Definition: SingletonT.hh:34
Manager of connections.
Definition: ConnectionManager.hh:51
void Fini()
Finalize the connection manager.
void GetAllPublishers(std::list< msgs::Publish > &_publishers)
Explicitly update the publisher list.
void RemoveConnection(ConnectionPtr &_conn)
Remove a connection from the manager.
void Unsubscribe(const std::string &_topic, const std::string &_msgType)
Unsubscribe from a topic.
void Stop()
Stop the conneciton manager.
void Subscribe(const std::string &_topic, const std::string &_msgType, bool _latching)
Subscribe to a topic.
bool IsInitialized() const
Is the manager initialized?
bool Init(const std::string &_masterHost, unsigned int _masterPort, uint32_t _timeoutIterations=30)
Initialize the connection manager.
void TriggerUpdate()
Inform the connection manager that it needs an update.
void RegisterTopicNamespace(const std::string &_name)
Register a new topic namespace.
void GetTopicNamespaces(std::list< std::string > &_namespaces)
Get all the topic namespaces.
void Run()
Run the connection manager loop.
void Unsubscribe(const msgs::Subscribe &_sub)
Unsubscribe from a topic.
void Unadvertise(const std::string &_topic)
Unadvertise a topic.
std::vector< event::ConnectionPtr > eventConnections
Definition: ConnectionManager.hh:180
bool IsRunning() const
Is the manager running?
ConnectionPtr ConnectToRemoteHost(const std::string &_host, unsigned int _port)
Connect to a remote server.
void Advertise(const std::string &_topic, const std::string &_msgType)
Advertise a topic.
Definition: TaskGroup.hh:30
#define GZ_SINGLETON_DECLARE(visibility, n1, n2, singletonType)
Helper to declare typed SingletonT.
Definition: SingletonT.hh:61
void stop()
Stop the transport component from running.
boost::shared_ptr< Connection > ConnectionPtr
Definition: Connection.hh:67
Forward declarations for the common classes.
Definition: Animation.hh:27