RSB  0.7.0
 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  //
97  RSCINFO(logger, "Got connection from " << socket->remote_endpoint());
98 
99  BusConnectionPtr connection(new BusConnection(ref, socket, false, isTcpnodelay()));
100  addConnection(connection);
101  connection->startReceiving();
102  } else if (!this->shutdown){
103  RSCWARN(logger, "Accept failure, trying to continue");
104  }
105 
106  // Maybe continue accepting connections. If not, a shutdown has
107  // been requested from another thread. In that case, we reset
108  // this->shutdown to false to indicate that the shutdown sequence
109  // is complete. The other thread can just busy-wait until
110  // this->shutdown == false.
111  if (!this->shutdown) {
112  acceptOne(ref);
113  } else {
114  this->shutdown = false;
115  }
116 }
117 
119  BusConnectionPtr connection) {
120  Bus::handleIncoming(event, connection);
121 
122  RSCDEBUG(logger, "Delivering received event to connections " << event);
123  {
124  boost::recursive_mutex::scoped_lock lock(getConnectionLock());
125 
127  list<BusConnectionPtr> failing;
128  for (ConnectionList::iterator it = connections.begin();
129  it != connections.end(); ++it) {
130  if (*it != connection) {
131  RSCDEBUG(logger, "Delivering to connection " << *it);
132  try {
133  (*it)->sendEvent(event, event->getMetaData().getUserInfo("rsb.wire-schema"));
134  } catch (const std::exception& e) {
135  RSCWARN(logger, "Send failure (" << e.what() << "); will close connection later");
136  // We record failing connections instead of
137  // closing them immediately to avoid invalidating
138  // the iterator.
139  failing.push_back(*it);
140  }
141  }
142  }
143 
144  // This should remove all references to the connection
145  // objects.
146  for (list<BusConnectionPtr>::const_iterator it = failing.begin();
147  it != failing.end(); ++it) {
148  removeConnection(*it);
149  }
150  }
151 }
152 
154  Factory::getInstance().removeBusServer(shared_from_this());
155 }
156 
157 }
158 }
159 }