4ff8da059db093ddc2571dec4db97e8fc5898126
[iec.git] / src / type3_AndroidCloud / anbox-master / android / service / host_connector.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 "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"
23
24 #include "anbox/rpc/channel.h"
25
26 #include <functional>
27 #include <array>
28
29 namespace anbox {
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_)),
37     running_(false) {
38 }
39
40 HostConnector::~HostConnector() {
41 }
42
43 void HostConnector::start() {
44     if (running_)
45         return;
46
47     running_.exchange(true);
48     thread_ = std::thread(std::bind(&HostConnector::main_loop, this));
49 }
50
51 void HostConnector::stop() {
52     if (!running_.exchange(false))
53         return;
54
55     thread_.join();
56 }
57
58 void HostConnector::main_loop() {
59     while (running_) {
60         std::array<std::uint8_t, 8192> buffer;
61         const auto bytes_read = socket_->read_all(buffer.data(), buffer.size());
62         if (bytes_read == 0)
63             break;
64
65         // MessageProcessor wants an vector so give it what it wants until
66         // we refactor this.
67         std::vector<std::uint8_t> data;
68         for (unsigned n = 0; n < bytes_read; n++)
69             data.push_back(buffer[n]);
70
71         if (!message_processor_->process_data(data))
72             break;
73     }
74 }
75
76 std::shared_ptr<anbox::PlatformApiStub> HostConnector::platform_api_stub() const {
77     return platform_api_stub_;
78 }
79 } // namespace anbox