00001 /* 00002 * Copyright (C) 2009, Willow Garage, Inc. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are met: 00006 * * Redistributions of source code must retain the above copyright notice, 00007 * this list of conditions and the following disclaimer. 00008 * * Redistributions in binary form must reproduce the above copyright 00009 * notice, this list of conditions and the following disclaimer in the 00010 * documentation and/or other materials provided with the distribution. 00011 * * Neither the names of Willow Garage, Inc. nor the names of its 00012 * contributors may be used to endorse or promote products derived from 00013 * this software without specific prior written permission. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00016 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00017 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00019 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00020 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00021 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00023 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00024 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00025 * POSSIBILITY OF SUCH DAMAGE. 00026 */ 00027 00028 #include "forwards.h" 00029 #include "connection.h" 00030 00031 #include <boost/thread/mutex.hpp> 00032 #include <boost/signals/connection.hpp> 00033 00034 namespace ros 00035 { 00036 00037 class PollManager; 00038 typedef boost::shared_ptr<PollManager> PollManagerPtr; 00039 00040 class ConnectionManager; 00041 typedef boost::shared_ptr<ConnectionManager> ConnectionManagerPtr; 00042 00043 class ConnectionManager 00044 { 00045 public: 00046 static const ConnectionManagerPtr& instance(); 00047 00048 ConnectionManager(); 00049 ~ConnectionManager(); 00050 00053 uint32_t getNewConnectionID(); 00054 00059 void addConnection(const ConnectionPtr& connection); 00060 00061 void clear(Connection::DropReason reason); 00062 00063 uint32_t getTCPPort(); 00064 uint32_t getUDPPort(); 00065 00066 const TransportTCPPtr& getTCPServerTransport() { return tcpserver_transport_; } 00067 const TransportUDPPtr& getUDPServerTransport() { return udpserver_transport_; } 00068 00069 void udprosIncomingConnection(const TransportUDPPtr& transport, Header& header); 00070 00071 void start(); 00072 void shutdown(); 00073 00074 private: 00075 void onConnectionDropped(const ConnectionPtr& conn); 00076 // Remove any dropped connections from our list, causing them to be destroyed 00077 // They can't just be removed immediately when they're dropped because the ros 00078 // thread may still be using them (or more likely their transport) 00079 void removeDroppedConnections(); 00080 00081 bool onConnectionHeaderReceived(const ConnectionPtr& conn, const Header& header); 00082 void tcprosAcceptConnection(const TransportTCPPtr& transport); 00083 00084 PollManagerPtr poll_manager_; 00085 00086 S_Connection connections_; 00087 V_Connection dropped_connections_; 00088 boost::mutex connections_mutex_; 00089 boost::mutex dropped_connections_mutex_; 00090 00091 // The connection ID counter, used to assign unique ID to each inbound or 00092 // outbound connection. Access via getNewConnectionID() 00093 uint32_t connection_id_counter_; 00094 boost::mutex connection_id_counter_mutex_; 00095 00096 boost::signals::connection poll_conn_; 00097 00098 TransportTCPPtr tcpserver_transport_; 00099 TransportUDPPtr udpserver_transport_; 00100 00101 const static int MAX_TCPROS_CONN_QUEUE = 100; // magic 00102 }; 00103 00104 } 00105