TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / cmds / wait_ready.cpp
1 /*
2  * Copyright (C) 2016 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  *
18  */
19
20 #include "anbox/cmds/wait_ready.h"
21 #include "anbox/dbus/stub/application_manager.h"
22
23 namespace {
24 constexpr const unsigned int max_wait_attempts{30};
25 }
26
27 anbox::cmds::WaitReady::WaitReady()
28     : CommandWithFlagsAndAction{
29           cli::Name{"wait-ready"}, cli::Usage{"wait-ready"},
30           cli::Description{"Wait until the Android system has successfully booted"}} {
31
32   flag(cli::make_flag(cli::Name{"use-system-dbus"},
33                       cli::Description{"Use system instead of session DBus"},
34                       use_system_dbus_));
35
36   action([this](const cli::Command::Context&) {
37     auto bus_type = anbox::dbus::Bus::Type::Session;
38     if (use_system_dbus_)
39       bus_type = anbox::dbus::Bus::Type::System;
40     auto bus = std::make_shared<anbox::dbus::Bus>(bus_type);
41
42     auto stub = dbus::stub::ApplicationManager::create_for_bus(bus);
43
44     unsigned int n = 0;
45     while (n < max_wait_attempts) {
46       stub->update_properties();
47       if (stub->ready().get())
48         return EXIT_SUCCESS;
49
50       std::this_thread::sleep_for(std::chrono::seconds{1});
51       n++;
52     }
53
54     return EXIT_FAILURE;
55   });
56 }