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/thread.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 
133  private: void OnMasterRead(const std::string &_data);
134 
137  private: void OnAccept(const ConnectionPtr &_newConnection);
138 
142  private: void OnRead(const ConnectionPtr &_newConnection,
143  const std::string &_data);
144 
147  private: void ProcessMessage(const std::string &_packet);
148 
150  public: void RunUpdate();
151 
152  private: ConnectionPtr masterConn;
153  private: Connection *serverConn;
154 
155  private: std::list<ConnectionPtr> connections;
156  protected: std::vector<event::ConnectionPtr> eventConnections;
157 
158  private: bool initialized;
159  private: bool stop, stopped;
160  private: boost::thread *thread;
161 
162  private: unsigned int tmpIndex;
163  private: boost::recursive_mutex *listMutex;
164 
166  private: boost::mutex namespaceMutex;
167  private: boost::recursive_mutex *masterMessagesMutex;
168  private: boost::recursive_mutex *connectionMutex;
169 
170  private: std::list<msgs::Publish> publishers;
171  private: std::list<std::string> namespaces;
172  private: std::list<std::string> masterMessages;
173 
175  private: boost::condition_variable namespaceCondition;
176 
177  // Singleton implementation
178  private: friend class SingletonT<ConnectionManager>;
179  };
181  }
182 }
183 #endif