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 2011 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 "msgs/msgs.hh"
27 #include "common/SingletonT.hh"
28 
29 #include "transport/Publisher.hh"
30 #include "transport/Connection.hh"
31 
32 namespace gazebo
33 {
34  namespace transport
35  {
38 
40  class ConnectionManager : public SingletonT<ConnectionManager>
41  {
43  private: ConnectionManager();
44 
46  private: virtual ~ConnectionManager();
47 
48  public: bool Init(const std::string &master_host,
49  unsigned int master_port);
50 
52  public: void Run();
53 
55  public: bool IsRunning() const;
56 
58  public: void Fini();
59 
61  public: void Stop();
62 
63  public: void Subscribe(const std::string &_topic,
64  const std::string &_msgType,
65  bool _latching);
66 
67  public: void Unsubscribe(const msgs::Subscribe &_sub);
68 
69  public: void Unsubscribe(const std::string &_topic,
70  const std::string &_msgType);
71 
72  public: void Advertise(const std::string &topic,
73  const std::string &msgType);
74 
75  public: void Unadvertise(const std::string &topic);
76 
78  public: void GetAllPublishers(std::list<msgs::Publish> &publishers);
79 
81  public: void RemoveConnection(ConnectionPtr &conn);
82 
84  public: void RegisterTopicNamespace(const std::string &_name);
85 
87  public: void GetTopicNamespaces(std::list<std::string> &_namespaces);
88 
90  private: ConnectionPtr FindConnection(const std::string &host,
91  unsigned int port);
92 
94  public: ConnectionPtr ConnectToRemoteHost(const std::string &host,
95  unsigned int port);
96 
97  private: void OnMasterRead(const std::string &data);
98 
99  private: void OnAccept(const ConnectionPtr &new_connection);
100 
101  private: void OnRead(const ConnectionPtr &new_connection,
102  const std::string &data);
103 
104  private: void ProcessMessage(const std::string &_packet);
105 
106  public: void RunUpdate();
107 
108  private: ConnectionPtr masterConn;
109  private: Connection *serverConn;
110 
111  private: std::list<ConnectionPtr> connections;
112  protected: std::vector<event::ConnectionPtr> eventConnections;
113 
114  private: bool initialized;
115  private: bool stop, stopped;
116  private: boost::thread *thread;
117 
118  private: unsigned int tmpIndex;
119  private: boost::recursive_mutex *listMutex;
120  private: boost::recursive_mutex *masterMessagesMutex;
121  private: boost::recursive_mutex *connectionMutex;
122 
123  private: std::list<msgs::Publish> publishers;
124  private: std::list<std::string> namespaces;
125  private: std::list<std::string> masterMessages;
126 
127  // Singleton implementation
128  private: friend class SingletonT<ConnectionManager>;
129  };
131  }
132 }
133 
134 #endif
135 
136