gazebo / gazebo / Master.hh

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 /* * Copyright 2011 Nate Koenig * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #ifndef _MASTER_HH_ #define _MASTER_HH_ #include #include #include #include #include #include #include "msgs/msgs.hh" #include "transport/Connection.hh" namespace gazebo { /// \class Master Master.hh gazebo_core.hh /// \brief A ROS Master-like manager that directs gztopic connections, enables /// each gazebo network client to locate one another for peer-to-peer /// communication. class Master { /// \brief Constructor public: Master(); /// \brief Destructor public: virtual ~Master(); /// \brief Initialize /// \param[in] _port The master's port public: void Init(uint16_t _port); /// \brief Run the master. public: void Run(); /// \brief Run the master in a new thread public: void RunThread(); /// \brief Run the master one iteration public: void RunOnce(); /// \brief Stop the master public: void Stop(); /// \brief Finalize the master public: void Fini(); /// \brief Process a message /// \param[in] _connectionIndex Index of the connection which generated the /// message /// \param[i] _data The message data private: void ProcessMessage(const unsigned int _connectionIndex, const std::string &_data); /// \brief Connection read callback /// \param[in] _connectionIndex Index of the connection which generated the /// message /// \param[in] _data The message data private: void OnRead(const unsigned int _connectionIndex, const std::string &_data); /// \brief Accept a new connection /// \param[in] _newConnection The new connection private: void OnAccept(const transport::ConnectionPtr &_newConnection); /// \brief Get a publisher for the given topic /// \param[in] _topic Name of the topic /// \return A publish message private: msgs::Publish GetPublisher(const std::string &_topic); /// \brief Find a connection given a host and port /// \param[in] _host Host name /// \param[in] _port Port number /// \return The found connection, or NULL private: transport::ConnectionPtr FindConnection(const std::string &_host, uint16_t _port); /// \brief Remove a connection. /// \param[in] _index Index of the connection to remove. private: void RemoveConnection(unsigned int _index); /// \brief Remove a publisher. /// \param[in] _pub Publish message that contains the info necessary to /// remove a publisher. private: void RemovePublisher(const msgs::Publish _pub); /// \brief Remove a subscriber. /// \param[in] _pub Subscribe message that contains the info necessary to /// remove a subscriber. private: void RemoveSubscriber(const msgs::Subscribe _sub); /// \def Map of unique id's to connections. typedef std::map Connection_M; /// \def Map of publish messages to connections. typedef std::list< std::pair > PubList; /// \def Map of subscribe messages to connections. typedef std::list< std::pair > SubList; /// \brief All the known publishers. private: PubList publishers; /// \brief All the known subscribers. private: SubList subscribers; /// \brief All the known connections. private: Connection_M connections; /// \brief All th worlds. private: std::list worldNames; /// \brief Incoming messages. private: std::list > msgs; /// \brief Our server connection. private: transport::Connection *connection; /// \brief Thread to run the main loop. private: boost::thread *runThread; /// \brief True to stop Master. private: bool stop; /// \brief Mutex to protect connections. private: boost::recursive_mutex *connectionMutex; /// \brief Mutex to protect msg bufferes. private: boost::recursive_mutex *msgsMutex; }; } #endif