All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConnectionManager.hh
Go to the documentation of this file.
1 /*
2  * Copyright 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 
33 namespace gazebo
34 {
35  namespace transport
36  {
39 
42  class ConnectionManager : public SingletonT<ConnectionManager>
43  {
45  private: ConnectionManager();
46 
48  private: virtual ~ConnectionManager();
49 
54  public: bool Init(const std::string &_masterHost,
55  unsigned int _masterPort);
56 
59  public: void Run();
60 
63  public: bool IsRunning() const;
64 
66  public: void Fini();
67 
69  public: void Stop();
70 
76  public: void Subscribe(const std::string &_topic,
77  const std::string &_msgType,
78  bool _latching);
79 
82  public: void Unsubscribe(const msgs::Subscribe &_sub);
83 
87  public: void Unsubscribe(const std::string &_topic,
88  const std::string &_msgType);
89 
93  public: void Advertise(const std::string &_topic,
94  const std::string &_msgType);
95 
98  public: void Unadvertise(const std::string &_topic);
99 
102  public: void GetAllPublishers(std::list<msgs::Publish> &_publishers);
103 
106  public: void RemoveConnection(ConnectionPtr &_conn);
107 
110  public: void RegisterTopicNamespace(const std::string &_name);
111 
114  public: void GetTopicNamespaces(std::list<std::string> &_namespaces);
115 
120  private: ConnectionPtr FindConnection(const std::string &_host,
121  unsigned int _port);
122 
127  public: ConnectionPtr ConnectToRemoteHost(const std::string &_host,
128  unsigned int _port);
129 
131  public: void TriggerUpdate();
132 
136  private: void OnMasterRead(const std::string &_data);
137 
140  private: void OnAccept(ConnectionPtr _newConnection);
141 
145  private: void OnRead(ConnectionPtr _newConnection,
146  const std::string &_data);
147 
150  private: void ProcessMessage(const std::string &_packet);
151 
153  private: void RunUpdate();
154 
156  private: boost::condition_variable updateCondition;
157 
159  private: boost::mutex updateMutex;
160 
161  private: ConnectionPtr masterConn;
162  private: Connection *serverConn;
163 
164  private: std::list<ConnectionPtr> connections;
165  protected: std::vector<event::ConnectionPtr> eventConnections;
166 
167  private: bool initialized;
168  private: bool stop, stopped;
169 
170  private: unsigned int tmpIndex;
171  private: boost::recursive_mutex listMutex;
172 
174  private: boost::mutex namespaceMutex;
175  private: boost::recursive_mutex masterMessagesMutex;
176  private: boost::recursive_mutex connectionMutex;
177 
178  private: std::list<msgs::Publish> publishers;
179  private: std::list<std::string> namespaces;
180  private: std::list<std::string> masterMessages;
181 
183  private: boost::condition_variable namespaceCondition;
184 
185  // Singleton implementation
186  private: friend class SingletonT<ConnectionManager>;
187  };
189  }
190 }
191 #endif