2 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "anbox/rpc/connection_creator.h"
19 #include "anbox/logger.h"
20 #include "anbox/network/local_socket_messenger.h"
21 #include "anbox/rpc/message_processor.h"
25 namespace ba = boost::asio;
29 ConnectionCreator::ConnectionCreator(const std::shared_ptr<Runtime>& rt,
30 const MessageProcessorFactory& factory)
32 next_connection_id_(0),
34 std::make_shared<network::Connections<network::SocketConnection>>()),
35 message_processor_factory_(factory) {}
37 ConnectionCreator::~ConnectionCreator() noexcept {}
39 void ConnectionCreator::create_connection_for(
40 std::shared_ptr<boost::asio::local::stream_protocol::socket> const&
42 if (connections_->size() >= 1) {
45 "A second client tried to connect. Denied request as we already have "
46 "one and only allow a single client");
50 auto const messenger =
51 std::make_shared<network::LocalSocketMessenger>(socket);
52 auto const processor = message_processor_factory_(messenger);
54 auto const& connection = std::make_shared<network::SocketConnection>(
55 messenger, messenger, next_id(), connections_, processor);
56 connection->set_name("rpc");
57 connections_->add(connection);
58 connection->read_next_message();
61 int ConnectionCreator::next_id() { return next_connection_id_.fetch_add(1); }