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 Nate Koenig
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 <string>
23 #include <list>
24 #include <vector>
25 
26 #include "gazebo/msgs/msgs.hh"
28 
31 
32 namespace gazebo
33 {
34  namespace transport
35  {
38 
41  class ConnectionManager : public SingletonT<ConnectionManager>
42  {
44  private: ConnectionManager();
45 
47  private: virtual ~ConnectionManager();
48 
53  public: bool Init(const std::string &_masterHost,
54  unsigned int _masterPort);
55 
58  public: void Run();
59 
62  public: bool IsRunning() const;
63 
65  public: void Fini();
66 
68  public: void Stop();
69 
75  public: void Subscribe(const std::string &_topic,
76  const std::string &_msgType,
77  bool _latching);
78 
81  public: void Unsubscribe(const msgs::Subscribe &_sub);
82 
86  public: void Unsubscribe(const std::string &_topic,
87  const std::string &_msgType);
88 
92  public: void Advertise(const std::string &_topic,
93  const std::string &_msgType);
94 
97  public: void Unadvertise(const std::string &_topic);
98 
101  public: void GetAllPublishers(std::list<msgs::Publish> &_publishers);
102 
105  public: void RemoveConnection(ConnectionPtr &_conn);
106 
109  public: void RegisterTopicNamespace(const std::string &_name);
110 
113  public: void GetTopicNamespaces(std::list<std::string> &_namespaces);
114 
119  private: ConnectionPtr FindConnection(const std::string &_host,
120  unsigned int _port);
121 
126  public: ConnectionPtr ConnectToRemoteHost(const std::string &_host,
127  unsigned int _port);
128 
132  private: void OnMasterRead(const std::string &_data);
133 
136  private: void OnAccept(const ConnectionPtr &_newConnection);
137 
141  private: void OnRead(const ConnectionPtr &_newConnection,
142  const std::string &_data);
143 
146  private: void ProcessMessage(const std::string &_packet);
147 
149  public: void RunUpdate();
150 
151  private: ConnectionPtr masterConn;
152  private: Connection *serverConn;
153 
154  private: std::list<ConnectionPtr> connections;
155  protected: std::vector<event::ConnectionPtr> eventConnections;
156 
157  private: bool initialized;
158  private: bool stop, stopped;
159  private: boost::thread *thread;
160 
161  private: unsigned int tmpIndex;
162  private: boost::recursive_mutex *listMutex;
163  private: boost::recursive_mutex *masterMessagesMutex;
164  private: boost::recursive_mutex *connectionMutex;
165 
166  private: std::list<msgs::Publish> publishers;
167  private: std::list<std::string> namespaces;
168  private: std::list<std::string> masterMessages;
169 
170  // Singleton implementation
171  private: friend class SingletonT<ConnectionManager>;
172  };
174  }
175 }
176 #endif