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 "android/service/host_connector.h"
19 #include "android/service/local_socket_connection.h"
20 #include "android/service/message_processor.h"
21 #include "android/service/android_api_skeleton.h"
22 #include "android/service/platform_api_stub.h"
24 #include "anbox/rpc/channel.h"
30 HostConnector::HostConnector() :
31 socket_(std::make_shared<LocalSocketConnection>("/dev/anbox_bridge")),
32 pending_calls_(std::make_shared<rpc::PendingCallCache>()),
33 android_api_skeleton_(std::make_shared<AndroidApiSkeleton>()),
34 message_processor_(std::make_shared<MessageProcessor>(socket_, pending_calls_, android_api_skeleton_)),
35 rpc_channel_(std::make_shared<rpc::Channel>(pending_calls_, socket_)),
36 platform_api_stub_(std::make_shared<PlatformApiStub>(rpc_channel_)),
40 HostConnector::~HostConnector() {
43 void HostConnector::start() {
47 running_.exchange(true);
48 thread_ = std::thread(std::bind(&HostConnector::main_loop, this));
51 void HostConnector::stop() {
52 if (!running_.exchange(false))
58 void HostConnector::main_loop() {
60 std::array<std::uint8_t, 8192> buffer;
61 const auto bytes_read = socket_->read_all(buffer.data(), buffer.size());
65 // MessageProcessor wants an vector so give it what it wants until
67 std::vector<std::uint8_t> data;
68 for (unsigned n = 0; n < bytes_read; n++)
69 data.push_back(buffer[n]);
71 if (!message_processor_->process_data(data))
76 std::shared_ptr<anbox::PlatformApiStub> HostConnector::platform_api_stub() const {
77 return platform_api_stub_;