RSB  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BusServer.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 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 "BusServer.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 BusServer::BusServer(boost::uint16_t port,
50  bool tcpnodelay,
51  io_service& service)
52  : Bus(service, tcpnodelay),
53  logger(Logger::getLogger("rsb.transport.socket.BusServer")),
54  acceptor(service, tcp::endpoint(tcp::v4(), port)),
55  service(service),
56  active(false), shutdown(false) {
57 }
58 
59 
61  if (this->active) {
62  deactivate();
63  }
64 }
65 
67  acceptOne(boost::dynamic_pointer_cast<BusServer>(shared_from_this()));
68 
69  this->active = true;
70 }
71 
73  // Initiate shutdown squence, cancel acceptor and wait until the
74  // asynchronous callbacks signal completion of the shutdown
75  // sequence (see handleAccept()).
76  this->shutdown = true;
77  this->acceptor.cancel();
78  while (this->shutdown);
79 
80  this->active = false;
81 }
82 
84  SocketPtr socket(new tcp::socket(this->service));
85 
86  RSCINFO(logger, "Listening on " << this->acceptor.local_endpoint());
87  acceptor.async_accept(*socket,
88  boost::bind(&BusServer::handleAccept, this, ref, socket,
89  boost::asio::placeholders::error));
90 }
91 
93  SocketPtr socket,
94  const boost::system::error_code& error) {
95  if (!error) {
96  RSCINFO(logger, "Got connection from " << socket->remote_endpoint());
97 
98  BusConnectionPtr connection(new BusConnection(ref, socket, false, isTcpnodelay()));
99  addConnection(connection);
100  connection->startReceiving();
101  } else if (!this->shutdown){
102  RSCWARN(logger, "Accept failure, trying to continue");
103  }
104 
105  // Maybe continue accepting connections. If not, a shutdown has
106  // been requested from another thread. In that case, we reset
107  // this->shutdown to false to indicate that the shutdown sequence
108  // is complete. The other thread can just busy-wait until
109  // this->shutdown == false.
110  if (!this->shutdown) {
111  acceptOne(ref);
112  } else {
113  this->shutdown = false;
114  }
115 }
116 
118  BusConnectionPtr connection) {
119  Bus::handleIncoming(event, connection);
120 
121  RSCDEBUG(logger, "Delivering received event to connections " << event);
122  {
123  boost::recursive_mutex::scoped_lock lock(getConnectionLock());
124 
126  list<BusConnectionPtr> failing;
127  for (ConnectionList::iterator it = connections.begin();
128  it != connections.end(); ++it) {
129  if (*it != connection) {
130  RSCDEBUG(logger, "Delivering to connection " << *it);
131  try {
132  (*it)->sendEvent(event, event->getMetaData().getUserInfo("rsb.wire-schema"));
133  } catch (const std::exception& e) {
134  RSCWARN(logger, "Send failure (" << e.what() << "); will close connection later");
135  // We record failing connections instead of
136  // closing them immediately to avoid invalidating
137  // the iterator.
138  failing.push_back(*it);
139  }
140  }
141  }
142 
143  // This should remove all references to the connection
144  // objects.
145  for (list<BusConnectionPtr>::const_iterator it = failing.begin();
146  it != failing.end(); ++it) {
147  removeConnection(*it);
148  }
149  }
150 }
151 
153  Factory::getInstance().removeBusServer(shared_from_this());
154 }
155 
156 }
157 }
158 }
ConnectionList connections
Definition: Bus.h:124
ConnectionList getConnections() const
Definition: Bus.cpp:79
Instances of this class provide access to a socket-based bus.
Definition: Bus.h:71
void removeConnection(BusConnectionPtr connection)
Removes connection from the list of connections of this bus.
Definition: Bus.cpp:153
virtual void handleIncoming(EventPtr event, BusConnectionPtr connection)
Definition: Bus.cpp:161
rsc::logging::LoggerPtr logger
Definition: BusServer.h:83
void handleAccept(boost::shared_ptr< BusServer > ref, SocketPtr socket, const boost::system::error_code &error)
Definition: BusServer.cpp:92
boost::shared_ptr< BusConnection > BusConnectionPtr
boost::asio::ip::tcp::acceptor acceptor
Definition: BusServer.h:85
boost::shared_ptr< BusServer > BusServerPtr
Definition: BusServer.h:105
friend class BusConnection
Definition: Bus.h:73
bool isTcpnodelay() const
Definition: Bus.cpp:75
void addConnection(BusConnectionPtr connection)
Adds connection to the list of connections of the bus.
Definition: Bus.cpp:145
boost::shared_ptr< boost::asio::ip::tcp::socket > SocketPtr
Definition: BusServer.h:81
void handleIncoming(EventPtr event, BusConnectionPtr connection)
Definition: BusServer.cpp:117
boost::recursive_mutex & getConnectionLock()
Definition: Bus.cpp:83
void acceptOne(boost::shared_ptr< BusServer > ref)
Definition: BusServer.cpp:83
boost::shared_ptr< Event > EventPtr
Definition: Event.h:251
boost::asio::io_service & service
Definition: BusServer.h:86
std::list< BusConnectionPtr > ConnectionList
Definition: Bus.h:105
void activate()
Activate the object.
Definition: BusServer.cpp:66