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 (C) 2012-2014 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 #include "gazebo/util/system.hh"
33 
34 namespace gazebo
35 {
36  namespace transport
37  {
40 
44  public SingletonT<ConnectionManager>
45  {
47  private: ConnectionManager();
48 
50  private: virtual ~ConnectionManager();
51 
58  public: bool Init(const std::string &_masterHost,
59  unsigned int _masterPort,
60  uint32_t _timeoutIterations = 30);
61 
64  public: void Run();
65 
68  public: bool IsRunning() const;
69 
71  public: void Fini();
72 
74  public: void Stop();
75 
81  public: void Subscribe(const std::string &_topic,
82  const std::string &_msgType,
83  bool _latching);
84 
87  public: void Unsubscribe(const msgs::Subscribe &_sub);
88 
92  public: void Unsubscribe(const std::string &_topic,
93  const std::string &_msgType);
94 
98  public: void Advertise(const std::string &_topic,
99  const std::string &_msgType);
100 
103  public: void Unadvertise(const std::string &_topic);
104 
107  public: void GetAllPublishers(std::list<msgs::Publish> &_publishers);
108 
111  public: void RemoveConnection(ConnectionPtr &_conn);
112 
115  public: void RegisterTopicNamespace(const std::string &_name);
116 
119  public: void GetTopicNamespaces(std::list<std::string> &_namespaces);
120 
125  private: ConnectionPtr FindConnection(const std::string &_host,
126  unsigned int _port);
127 
132  public: ConnectionPtr ConnectToRemoteHost(const std::string &_host,
133  unsigned int _port);
134 
136  public: void TriggerUpdate();
137 
141  private: void OnMasterRead(const std::string &_data);
142 
145  private: void OnAccept(ConnectionPtr _newConnection);
146 
150  private: void OnRead(ConnectionPtr _newConnection,
151  const std::string &_data);
152 
155  private: void ProcessMessage(const std::string &_packet);
156 
158  private: void RunUpdate();
159 
161  private: boost::condition_variable updateCondition;
162 
164  private: boost::mutex updateMutex;
165 
166  private: ConnectionPtr masterConn;
167  private: Connection *serverConn;
168 
169  private: std::list<ConnectionPtr> connections;
170  protected: std::vector<event::ConnectionPtr> eventConnections;
171 
172  private: bool initialized;
173  private: bool stop, stopped;
174 
175  private: unsigned int tmpIndex;
176  private: boost::recursive_mutex listMutex;
177 
179  private: boost::mutex namespaceMutex;
180  private: boost::recursive_mutex masterMessagesMutex;
181  private: boost::recursive_mutex connectionMutex;
182 
183  private: std::list<msgs::Publish> publishers;
184  private: std::list<std::string> namespaces;
185  private: std::list<std::string> masterMessages;
186 
188  private: boost::condition_variable namespaceCondition;
189 
190  // Singleton implementation
191  private: friend class SingletonT<ConnectionManager>;
192  };
194  }
195 }
196 #endif