RSB  0.17.0
BusServerImpl.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSB project
4  *
5  * Copyright (C) 2011, 2012, 2015 Jan Moringen <jmoringe@techfak.uni-bielefeld.de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "BusServerImpl.h"
28 
29 #include <list>
30 
31 #include <boost/bind.hpp>
32 
33 #include <boost/thread/thread_time.hpp>
34 
35 #include "../../MetaData.h"
36 #include "Factory.h"
37 
38 using namespace std;
39 
40 using namespace boost::asio;
41 using boost::asio::ip::tcp;
42 
43 using namespace rsc::logging;
44 
45 namespace rsb {
46 namespace transport {
47 namespace socket {
48 
49 BusServerImpl::BusServerImpl(AsioServiceContextPtr asioService,
50  boost::uint16_t port,
51  bool tcpnodelay)
52  : Bus(asioService, tcpnodelay), BusServer(asioService, tcpnodelay),
53  logger(Logger::getLogger("rsb.transport.socket.BusServerImpl")),
54  acceptor(*this->getService()->getService(), tcp::endpoint(tcp::v4(), port)),
55  active(false), shutdown(false) {
56 }
57 
58 
60  RSCDEBUG(logger, "Destructing");
61  if (this->active) {
62  deactivate();
63  }
64 }
65 
67  RSCDEBUG(logger, "Activating");
68  acceptOne(shared_from_this());
69 
70  this->active = true;
71 }
72 
74  RSCDEBUG(logger, "Deactivating");
75  // Initiate shutdown sequence, cancel acceptor and wait until the
76  // asynchronous callbacks signal completion of the shutdown
77  // sequence (see handleAccept()).
78  this->shutdown = true;
79  this->acceptor.cancel();
80  while (this->shutdown);
81 
82  this->active = false;
83 }
84 
85 void BusServerImpl::acceptOne(boost::shared_ptr<BusServerImpl> ref) {
86  SocketPtr socket(new tcp::socket(*this->getService()->getService()));
87 
88  RSCINFO(logger, "Listening on " << this->acceptor.local_endpoint());
89  acceptor.async_accept(*socket,
90  boost::bind(&BusServerImpl::handleAccept, this, ref, socket,
91  boost::asio::placeholders::error));
92 }
93 
94 void BusServerImpl::handleAccept(boost::shared_ptr<BusServerImpl> ref,
95  SocketPtr socket,
96  const boost::system::error_code& error) {
97  if (!error) {
98  RSCINFO(logger, "Got connection from " << socket->remote_endpoint());
99 
100  BusConnectionPtr connection(new BusConnection(ref, socket, false, isTcpnodelay()));
101  addConnection(connection);
102  connection->startReceiving();
103  } else if (!this->shutdown){
104  RSCWARN(logger, "Accept failure, trying to continue");
105  }
106 
107  // Maybe continue accepting connections. If not, a shutdown has
108  // been requested from another thread. In that case, we reset
109  // this->shutdown to false to indicate that the shutdown sequence
110  // is complete. The other thread can just busy-wait until
111  // this->shutdown == false.
112  if (!this->shutdown) {
113  acceptOne(ref);
114  } else {
115  this->shutdown = false;
116  }
117 }
118 
120  BusConnectionPtr connection) {
121  Bus::handleIncoming(event, connection);
122 
123  RSCDEBUG(logger, "Delivering received event to connections " << event);
124  {
125  boost::recursive_mutex::scoped_lock lock(getConnectionLock());
126 
128  list<BusConnectionPtr> failing;
129  for (ConnectionList::iterator it = connections.begin();
130  it != connections.end(); ++it) {
131  if (*it != connection) {
132  RSCDEBUG(logger, "Delivering to connection " << *it);
133  try {
134  (*it)->sendEvent(event, event->getMetaData().getUserInfo("rsb.wire-schema"));
135  } catch (const std::exception& e) {
136  RSCWARN(logger, "Send failure (" << e.what() << "); will close connection later");
137  // We record failing connections instead of
138  // closing them immediately to avoid invalidating
139  // the iterator.
140  failing.push_back(*it);
141  }
142  }
143  }
144 
145  // This should remove all references to the connection
146  // objects.
147  for (list<BusConnectionPtr>::const_iterator it = failing.begin();
148  it != failing.end(); ++it) {
149  removeConnection(*it);
150  }
151  }
152 }
153 
154 const std::string BusServerImpl::getTransportURL() const {
155  return boost::str(boost::format("socket://%1%:%2%")
156  % this->acceptor.local_endpoint().address().to_string()
157  % this->acceptor.local_endpoint().port());
158 }
159 
160 
161 }
162 }
163 }
ConnectionList connections
Definition: Bus.h:131
void handleIncoming(EventPtr event, BusConnectionPtr connection)
boost::shared_ptr< boost::asio::ip::tcp::socket > SocketPtr
Definition: BusServerImpl.h:83
ConnectionList getConnections() const
Definition: Bus.cpp:88
Instances of this class provide access to a socket-based bus.
Definition: Bus.h:75
virtual void removeConnection(BusConnectionPtr connection)
Removes connection from the list of connections of this bus.
Definition: Bus.cpp:142
virtual void handleIncoming(EventPtr event, BusConnectionPtr connection)
Definition: Bus.cpp:150
STL namespace.
virtual const std::string getTransportURL() const
boost::shared_ptr< AsioServiceContext > AsioServiceContextPtr
boost::shared_ptr< BusConnection > BusConnectionPtr
rsc::logging::LoggerPtr logger
Definition: BusServerImpl.h:86
Instances of this class provide access to a socket-based bus for local and remote bus clients...
Definition: BusServer.h:54
friend class BusConnection
Definition: Bus.h:76
virtual bool isTcpnodelay() const
Definition: Bus.cpp:84
virtual void addConnection(BusConnectionPtr connection)
Adds connection to the list of connections of the bus.
Definition: Bus.cpp:134
void handleAccept(boost::shared_ptr< BusServerImpl > ref, SocketPtr socket, const boost::system::error_code &error)
boost::recursive_mutex & getConnectionLock()
Definition: Bus.cpp:92
boost::asio::ip::tcp::acceptor acceptor
Definition: BusServerImpl.h:88
void activate()
Activate the object.
void acceptOne(boost::shared_ptr< BusServerImpl > ref)
virtual AsioServiceContextPtr getService() const
Definition: Bus.cpp:80
boost::shared_ptr< Event > EventPtr
Definition: Event.h:264
std::list< BusConnectionPtr > ConnectionList
Definition: Bus.h:113