connection_manager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "ros/connection_manager.h"
29 #include "ros/poll_manager.h"
30 #include "ros/connection.h"
35 #include "ros/file_log.h"
36 #include "ros/network.h"
37 
38 #include <ros/assert.h>
39 
40 namespace ros
41 {
42 
44 {
45  static ConnectionManagerPtr connection_manager = boost::make_shared<ConnectionManager>();
46  return connection_manager;
47 }
48 
50 : connection_id_counter_(0)
51 {
52 }
53 
55 {
56  shutdown();
57 }
58 
60 {
62  poll_conn_ = poll_manager_->addPollThreadListener(boost::bind(&ConnectionManager::removeDroppedConnections,
63  this));
64 
65  // Bring up the TCP listener socket
66  tcpserver_transport_ = boost::make_shared<TransportTCP>(&poll_manager_->getPollSet());
69  boost::bind(&ConnectionManager::tcprosAcceptConnection, this, boost::placeholders::_1)))
70  {
71  ROS_FATAL("Listen on port [%d] failed", network::getTCPROSPort());
72  ROS_BREAK();
73  }
74 
75  // Bring up the UDP listener socket
76  udpserver_transport_ = boost::make_shared<TransportUDP>(&poll_manager_->getPollSet());
77  if (!udpserver_transport_->createIncoming(0, true))
78  {
79  ROS_FATAL("Listen failed");
80  ROS_BREAK();
81  }
82 }
83 
85 {
87  {
88  udpserver_transport_->close();
89  udpserver_transport_.reset();
90  }
91 
93  {
94  tcpserver_transport_->close();
95  tcpserver_transport_.reset();
96  }
97 
98  poll_manager_->removePollThreadListener(poll_conn_);
99 
101 }
102 
104 {
105  S_Connection local_connections;
106  {
107  boost::mutex::scoped_lock conn_lock(connections_mutex_);
108  local_connections.swap(connections_);
109  }
110 
111  for(S_Connection::iterator itr = local_connections.begin();
112  itr != local_connections.end();
113  itr++)
114  {
115  const ConnectionPtr& conn = *itr;
116  conn->drop(reason);
117  }
118 
119  boost::mutex::scoped_lock dropped_lock(dropped_connections_mutex_);
120  dropped_connections_.clear();
121 }
122 
124 {
125  return tcpserver_transport_->getServerPort();
126 }
127 
129 {
130  return udpserver_transport_->getServerPort();
131 }
132 
134 {
135  boost::mutex::scoped_lock lock(connection_id_counter_mutex_);
136  uint32_t ret = connection_id_counter_++;
137  return ret;
138 }
139 
141 {
142  boost::mutex::scoped_lock lock(connections_mutex_);
143 
144  connections_.insert(conn);
145  conn->addDropListener(boost::bind(&ConnectionManager::onConnectionDropped, this, boost::placeholders::_1));
146 }
147 
149 {
150  boost::mutex::scoped_lock lock(dropped_connections_mutex_);
151  dropped_connections_.push_back(conn);
152 }
153 
155 {
156  V_Connection local_dropped;
157  {
158  boost::mutex::scoped_lock dropped_lock(dropped_connections_mutex_);
159  dropped_connections_.swap(local_dropped);
160  }
161 
162  boost::mutex::scoped_lock conn_lock(connections_mutex_);
163 
164  V_Connection::iterator conn_it = local_dropped.begin();
165  V_Connection::iterator conn_end = local_dropped.end();
166  for (;conn_it != conn_end; ++conn_it)
167  {
168  const ConnectionPtr& conn = *conn_it;
169  connections_.erase(conn);
170  }
171 }
172 
174 {
175  std::string client_uri = ""; // TODO: transport->getClientURI();
176  ROSCPP_LOG_DEBUG("UDPROS received a connection from [%s]", client_uri.c_str());
177 
178  ConnectionPtr conn(boost::make_shared<Connection>());
179  addConnection(conn);
180 
181  conn->initialize(transport, true, NULL);
183 }
184 
186 {
187  std::string client_uri = transport->getClientURI();
188  ROSCPP_LOG_DEBUG("TCPROS received a connection from [%s]", client_uri.c_str());
189 
190  ConnectionPtr conn(boost::make_shared<Connection>());
191  addConnection(conn);
192 
193  conn->initialize(transport, true, boost::bind(&ConnectionManager::onConnectionHeaderReceived, this, boost::placeholders::_1, boost::placeholders::_2));
194 }
195 
197 {
198  bool ret = false;
199  std::string val;
200  if (header.getValue("topic", val))
201  {
202  ROSCPP_CONN_LOG_DEBUG("Connection: Creating TransportSubscriberLink for topic [%s] connected to [%s]",
203  val.c_str(), conn->getRemoteString().c_str());
204 
205  TransportSubscriberLinkPtr sub_link(boost::make_shared<TransportSubscriberLink>());
206  sub_link->initialize(conn);
207  ret = sub_link->handleHeader(header);
208  }
209  else if (header.getValue("service", val))
210  {
211  ROSCPP_LOG_DEBUG("Connection: Creating ServiceClientLink for service [%s] connected to [%s]",
212  val.c_str(), conn->getRemoteString().c_str());
213 
214  ServiceClientLinkPtr link(boost::make_shared<ServiceClientLink>());
215  link->initialize(conn);
216  ret = link->handleHeader(header);
217  }
218  else
219  {
220  ROSCPP_LOG_DEBUG("Got a connection for a type other than 'topic' or 'service' from [%s]. Fail.",
221  conn->getRemoteString().c_str());
222  return false;
223  }
224 
225  return ret;
226 }
227 
228 }
ros::ConnectionManager::onConnectionHeaderReceived
bool onConnectionHeaderReceived(const ConnectionPtr &conn, const Header &header)
Definition: connection_manager.cpp:196
connection_manager.h
ros::ConnectionManager::onConnectionDropped
void onConnectionDropped(const ConnectionPtr &conn)
Definition: connection_manager.cpp:148
ROS_BREAK
#define ROS_BREAK()
ROSCPP_CONN_LOG_DEBUG
#define ROSCPP_CONN_LOG_DEBUG(...)
Definition: file_log.h:36
boost::shared_ptr< ConnectionManager >
ros::ConnectionManager::connection_id_counter_
uint32_t connection_id_counter_
Definition: connection_manager.h:94
ros
ros::ConnectionManager::shutdown
void shutdown()
Definition: connection_manager.cpp:84
ros::ConnectionManager::removeDroppedConnections
void removeDroppedConnections()
Definition: connection_manager.cpp:154
ros::Header
ros::ConnectionManager::~ConnectionManager
~ConnectionManager()
Definition: connection_manager.cpp:54
ros::ConnectionManager::dropped_connections_mutex_
boost::mutex dropped_connections_mutex_
Definition: connection_manager.h:90
ros::ConnectionManager::clear
void clear(Connection::DropReason reason)
Definition: connection_manager.cpp:103
ros::ConnectionManager::addConnection
void addConnection(const ConnectionPtr &connection)
Add a connection to be tracked by the node. Will automatically remove them if they've been dropped,...
Definition: connection_manager.cpp:140
ros::ConnectionManager::tcpserver_transport_
TransportTCPPtr tcpserver_transport_
Definition: connection_manager.h:99
ros::ConnectionManager::ConnectionManager
ConnectionManager()
Definition: connection_manager.cpp:49
ros::ConnectionManager::getUDPPort
uint32_t getUDPPort()
Definition: connection_manager.cpp:128
ros::network::getTCPROSPort
ROSCPP_DECL uint16_t getTCPROSPort()
Definition: network.cpp:76
ros::S_Connection
std::set< ConnectionPtr > S_Connection
Definition: forwards.h:64
ros::ConnectionManager::udpserver_transport_
TransportUDPPtr udpserver_transport_
Definition: connection_manager.h:100
transport_udp.h
ros::V_Connection
std::vector< ConnectionPtr > V_Connection
Definition: forwards.h:65
transport_tcp.h
ros::ConnectionManager::poll_conn_
boost::signals2::connection poll_conn_
Definition: connection_manager.h:97
ros::PollManager::instance
static const PollManagerPtr & instance()
Definition: poll_manager.cpp:36
ROS_FATAL
#define ROS_FATAL(...)
ros::Connection::DropReason
DropReason
Definition: connection.h:73
ros::ConnectionManager::connection_id_counter_mutex_
boost::mutex connection_id_counter_mutex_
Definition: connection_manager.h:95
ros::Connection::Destructing
@ Destructing
Definition: connection.h:77
ros::ConnectionManager::getNewConnectionID
uint32_t getNewConnectionID()
Get a new connection ID.
Definition: connection_manager.cpp:133
ros::ConnectionManager::dropped_connections_
V_Connection dropped_connections_
Definition: connection_manager.h:88
connection.h
ros::ConnectionManager::poll_manager_
PollManagerPtr poll_manager_
Definition: connection_manager.h:85
ros::ConnectionManager::tcprosAcceptConnection
void tcprosAcceptConnection(const TransportTCPPtr &transport)
Definition: connection_manager.cpp:185
ros::ConnectionManager::udprosIncomingConnection
void udprosIncomingConnection(const TransportUDPPtr &transport, Header &header)
Definition: connection_manager.cpp:173
poll_manager.h
network.h
ros::ConnectionManager::instance
static const ConnectionManagerPtr & instance()
Definition: connection_manager.cpp:43
ros::ConnectionManager::start
void start()
Definition: connection_manager.cpp:59
header
const std::string header
assert.h
ros::ConnectionManager::MAX_TCPROS_CONN_QUEUE
const static int MAX_TCPROS_CONN_QUEUE
Definition: connection_manager.h:102
ros::ConnectionManager::connections_
S_Connection connections_
Definition: connection_manager.h:87
ros::ConnectionManager::getTCPPort
uint32_t getTCPPort()
Definition: connection_manager.cpp:123
ROSCPP_LOG_DEBUG
#define ROSCPP_LOG_DEBUG(...)
Definition: file_log.h:35
ros::ConnectionManager::connections_mutex_
boost::mutex connections_mutex_
Definition: connection_manager.h:89
file_log.h


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:35