ConnectionManager.hh
00001 /*
00002  * Copyright 2011 Nate Koenig & Andrew Howard
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 #ifndef CONNECTION_MANAGER_HH
00018 #define CONNECTION_MANAGER_HH
00019 
00020 #include <string>
00021 #include <boost/shared_ptr.hpp>
00022 
00023 #include "common/SingletonT.hh"
00024 #include "msgs/msgs.h"
00025 
00026 #include "transport/Publisher.hh"
00027 #include "transport/Connection.hh"
00028 
00029 namespace gazebo
00030 {
00031   namespace transport
00032   {
00035 
00037     class ConnectionManager : public SingletonT<ConnectionManager>
00038     {
00040       private: ConnectionManager();
00041 
00043       private: virtual ~ConnectionManager();
00044 
00045       public: bool Init(const std::string &master_host, 
00046                         unsigned short master_port);
00047 
00049       public: void Run();
00050 
00052       public: bool IsRunning() const;
00053 
00055       public: void Fini();
00056 
00058       public: void Stop();
00059 
00060       public: void Subscribe( const std::string &_topic, 
00061                               const std::string &_msgType,
00062                               bool _latching);
00063 
00064       public: void Unsubscribe( const msgs::Subscribe &_sub );
00065 
00066       public: void Unsubscribe( const std::string &_topic,
00067                                 const std::string &_msgType );
00068 
00069       public: void Advertise( const std::string &topic, 
00070                               const std::string &msgType);
00071 
00072       public: void Unadvertise( const std::string &topic );
00073 
00075       public: void GetAllPublishers( std::list<msgs::Publish> &publishers );
00076 
00078       public: void RemoveConnection(ConnectionPtr &conn);
00079 
00081       public: void RegisterTopicNamespace(const std::string &_name);
00082 
00084       public: void GetTopicNamespaces(std::list<std::string> &_namespaces);
00085 
00087       private: ConnectionPtr FindConnection(const std::string &host, 
00088                                             unsigned short port);
00089              
00091       public: ConnectionPtr ConnectToRemoteHost( const std::string &host,
00092                                                   unsigned short port);
00093 
00094       private: void OnMasterRead( const std::string &data );
00095 
00096       private: void OnAccept(const ConnectionPtr &new_connection);
00097 
00098       private: void OnRead( const ConnectionPtr &new_connection,
00099                             const std::string &data );
00100 
00101       private: void ProcessMessage(const std::string &_packet);
00102 
00103       private: ConnectionPtr masterConn;
00104       private: Connection *serverConn;
00105 
00106       private: std::list<ConnectionPtr> connections;
00107 
00108       private: bool initialized;
00109       private: bool stop;
00110       private: boost::thread *thread;
00111 
00112       private: unsigned int tmpIndex;
00113       private: boost::recursive_mutex *listMutex;
00114       private: boost::recursive_mutex *masterMessagesMutex;
00115       private: boost::recursive_mutex *connectionMutex;
00116 
00117       private: std::list<msgs::Publish> publishers;
00118       private: std::list<std::string> namespaces;
00119       private: std::list<std::string> masterMessages;
00120 
00121       //Singleton implementation
00122       private: friend class SingletonT<ConnectionManager>;
00123     };
00125   }
00126 }
00127 
00128 #endif