Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 1 | #include "starter_rpc_lib.h" |
| 2 | |
| 3 | #include "aos/events/shm_event_loop.h" |
| 4 | #include "aos/flatbuffer_merge.h" |
| 5 | |
| 6 | namespace aos { |
| 7 | namespace starter { |
| 8 | |
| 9 | const aos::starter::ApplicationStatus *FindApplicationStatus( |
| 10 | const aos::starter::Status &status, std::string_view name) { |
| 11 | if (!status.has_statuses()) { |
| 12 | return nullptr; |
| 13 | } |
| 14 | |
| 15 | auto statuses = status.statuses(); |
| 16 | |
| 17 | auto search = |
| 18 | std::find_if(statuses->begin(), statuses->end(), |
| 19 | [name](const aos::starter::ApplicationStatus *app_status) { |
| 20 | return app_status->has_name() && |
| 21 | app_status->name()->string_view() == name; |
| 22 | }); |
| 23 | if (search == statuses->end()) { |
| 24 | return nullptr; |
| 25 | } |
| 26 | return *search; |
| 27 | } |
| 28 | |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 29 | std::string_view FindApplication(const std::string_view &name, |
| 30 | const aos::Configuration *config) { |
| 31 | std::string_view app_name = name; |
| 32 | for (const auto app : *config->applications()) { |
Milind Upadhyay | 7641ce8 | 2021-04-10 14:15:28 -0700 | [diff] [blame] | 33 | if (app->has_executable_name() && |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 34 | app->executable_name()->string_view() == name) { |
| 35 | app_name = app->name()->string_view(); |
| 36 | break; |
| 37 | } |
| 38 | } |
| 39 | return app_name; |
| 40 | } |
| 41 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 42 | bool SendCommandBlocking(aos::starter::Command command, std::string_view name, |
| 43 | const aos::Configuration *config, |
| 44 | std::chrono::milliseconds timeout) { |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 45 | return SendCommandBlocking( |
| 46 | std::vector<std::pair<aos::starter::Command, std::string_view>>{ |
| 47 | {command, name}}, |
| 48 | config, timeout); |
| 49 | } |
| 50 | |
| 51 | bool SendCommandBlocking( |
| 52 | std::vector<std::pair<aos::starter::Command, std::string_view>> commands, |
| 53 | const aos::Configuration *config, std::chrono::milliseconds timeout) { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 54 | aos::ShmEventLoop event_loop(config); |
| 55 | event_loop.SkipAosLog(); |
| 56 | |
| 57 | ::aos::Sender<aos::starter::StarterRpc> cmd_sender = |
| 58 | event_loop.MakeSender<aos::starter::StarterRpc>("/aos"); |
| 59 | |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 60 | // Wait until event loop starts to send all commands so the watcher is ready |
| 61 | event_loop.OnRun([&cmd_sender, &commands] { |
| 62 | for (const std::pair<aos::starter::Command, std::string_view> |
| 63 | &command_pair : commands) { |
| 64 | const aos::starter::Command command = command_pair.first; |
| 65 | const std::string_view name = command_pair.second; |
| 66 | aos::Sender<aos::starter::StarterRpc>::Builder builder = |
| 67 | cmd_sender.MakeBuilder(); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 68 | |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 69 | auto name_str = builder.fbb()->CreateString(name); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 70 | |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 71 | aos::starter::StarterRpc::Builder cmd_builder = |
| 72 | builder.MakeBuilder<aos::starter::StarterRpc>(); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 73 | |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 74 | cmd_builder.add_name(name_str); |
| 75 | cmd_builder.add_command(command); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 76 | |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 77 | builder.Send(cmd_builder.Finish()); |
| 78 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 79 | }); |
| 80 | |
| 81 | // If still waiting after timeout milliseconds, exit the loop |
| 82 | event_loop.AddTimer([&event_loop] { event_loop.Exit(); }) |
| 83 | ->Setup(event_loop.monotonic_now() + timeout); |
| 84 | |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 85 | // Fetch the last list of statuses. The id field changes every time the |
| 86 | // application restarts. By detecting when the application is running with a |
| 87 | // different ID, we can detect restarts. |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 88 | auto initial_status_fetcher = |
| 89 | event_loop.MakeFetcher<aos::starter::Status>("/aos"); |
| 90 | initial_status_fetcher.Fetch(); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 91 | |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 92 | std::vector<std::optional<uint64_t>> initial_ids; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 93 | |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 94 | for (const std::pair<aos::starter::Command, std::string_view> &command_pair : |
| 95 | commands) { |
| 96 | const std::string_view name = command_pair.second; |
| 97 | auto initial_status = |
| 98 | initial_status_fetcher.get() |
| 99 | ? FindApplicationStatus(*initial_status_fetcher, name) |
| 100 | : nullptr; |
| 101 | |
| 102 | initial_ids.emplace_back( |
| 103 | (initial_status != nullptr && initial_status->has_id()) |
| 104 | ? std::make_optional(initial_status->id()) |
| 105 | : std::nullopt); |
| 106 | } |
| 107 | |
| 108 | std::vector<bool> successes(commands.size(), false); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 109 | bool success = false; |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 110 | event_loop.MakeWatcher("/aos", [&event_loop, &commands, &initial_ids, &success, |
| 111 | &successes]( |
| 112 | const aos::starter::Status &status) { |
| 113 | size_t index = 0; |
| 114 | for (const std::pair<aos::starter::Command, std::string_view> |
| 115 | &command_pair : commands) { |
| 116 | const aos::starter::Command command = command_pair.first; |
| 117 | const std::string_view name = command_pair.second; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 118 | |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 119 | const aos::starter::ApplicationStatus *app_status = |
| 120 | FindApplicationStatus(status, name); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 121 | |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 122 | const std::optional<aos::starter::State> state = |
| 123 | (app_status != nullptr && app_status->has_state()) |
| 124 | ? std::make_optional(app_status->state()) |
| 125 | : std::nullopt; |
| 126 | |
| 127 | switch (command) { |
| 128 | case aos::starter::Command::START: { |
| 129 | if (state == aos::starter::State::RUNNING) { |
| 130 | successes[index] = true; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 131 | } |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 132 | break; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 133 | } |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 134 | case aos::starter::Command::STOP: { |
| 135 | if (state == aos::starter::State::STOPPED) { |
| 136 | successes[index] = true; |
| 137 | } |
| 138 | break; |
| 139 | } |
| 140 | case aos::starter::Command::RESTART: { |
| 141 | if (state == aos::starter::State::RUNNING && app_status->has_id() && |
| 142 | app_status->id() != initial_ids[index]) { |
| 143 | successes[index] = true; |
| 144 | } |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | ++index; |
| 149 | } |
| 150 | |
| 151 | // Wait until all applications are ready. |
| 152 | if (std::count(successes.begin(), successes.end(), true) == |
| 153 | static_cast<ssize_t>(successes.size())) { |
| 154 | event_loop.Exit(); |
| 155 | success = true; |
| 156 | } |
| 157 | }); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 158 | |
| 159 | event_loop.Run(); |
| 160 | |
| 161 | return success; |
| 162 | } |
| 163 | |
| 164 | const FlatbufferDetachedBuffer<aos::starter::ApplicationStatus> GetStatus( |
| 165 | std::string_view name, const Configuration *config) { |
| 166 | ShmEventLoop event_loop(config); |
| 167 | event_loop.SkipAosLog(); |
| 168 | |
| 169 | auto status_fetcher = event_loop.MakeFetcher<aos::starter::Status>("/aos"); |
| 170 | status_fetcher.Fetch(); |
Milind Upadhyay | 49174a7 | 2021-04-10 16:24:57 -0700 | [diff] [blame] | 171 | auto status = status_fetcher.get() |
| 172 | ? FindApplicationStatus(*status_fetcher, name) |
| 173 | : nullptr; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 174 | return status ? aos::CopyFlatBuffer(status) |
| 175 | : FlatbufferDetachedBuffer< |
| 176 | aos::starter::ApplicationStatus>::Empty(); |
| 177 | } |
| 178 | |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 179 | std::optional<const aos::FlatbufferVector<aos::starter::Status>> |
| 180 | GetStarterStatus(const aos::Configuration *config) { |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 181 | ShmEventLoop event_loop(config); |
| 182 | event_loop.SkipAosLog(); |
| 183 | |
| 184 | auto status_fetcher = event_loop.MakeFetcher<aos::starter::Status>("/aos"); |
| 185 | status_fetcher.Fetch(); |
Milind Upadhyay | 49174a7 | 2021-04-10 16:24:57 -0700 | [diff] [blame] | 186 | return (status_fetcher.get() |
| 187 | ? std::make_optional(status_fetcher.CopyFlatBuffer()) |
| 188 | : std::nullopt); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 191 | } // namespace starter |
| 192 | } // namespace aos |