TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / rpc / connection_creator.cpp
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
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.
7  *
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.
12  *
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/>.
15  *
16  */
17
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"
22
23 #include <string>
24
25 namespace ba = boost::asio;
26
27 namespace anbox {
28 namespace rpc {
29 ConnectionCreator::ConnectionCreator(const std::shared_ptr<Runtime>& rt,
30                                      const MessageProcessorFactory& factory)
31     : runtime_(rt),
32       next_connection_id_(0),
33       connections_(
34           std::make_shared<network::Connections<network::SocketConnection>>()),
35       message_processor_factory_(factory) {}
36
37 ConnectionCreator::~ConnectionCreator() noexcept {}
38
39 void ConnectionCreator::create_connection_for(
40     std::shared_ptr<boost::asio::local::stream_protocol::socket> const&
41         socket) {
42   if (connections_->size() >= 1) {
43     socket->close();
44     WARNING(
45         "A second client tried to connect. Denied request as we already have "
46         "one and only allow a single client");
47     return;
48   }
49
50   auto const messenger =
51       std::make_shared<network::LocalSocketMessenger>(socket);
52   auto const processor = message_processor_factory_(messenger);
53
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();
59 }
60
61 int ConnectionCreator::next_id() { return next_connection_id_.fetch_add(1); }
62
63 }  // namespace rpc
64 }  // namespace anbox