Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 1 | #include "dbus_environment.hpp" |
| 2 | |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 3 | #include "helpers.hpp" |
| 4 | |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 5 | #include <future> |
| 6 | #include <thread> |
| 7 | |
| 8 | DbusEnvironment::~DbusEnvironment() |
| 9 | { |
Michal Orzel | 95f77e6 | 2024-09-16 11:06:42 +0200 | [diff] [blame] | 10 | if (setUp == true) |
| 11 | { |
| 12 | setUp = false; |
| 13 | |
| 14 | objServer = nullptr; |
| 15 | bus = nullptr; |
| 16 | } |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | void DbusEnvironment::SetUp() |
| 20 | { |
| 21 | if (setUp == false) |
| 22 | { |
| 23 | setUp = true; |
| 24 | |
| 25 | bus = std::make_shared<sdbusplus::asio::connection>(ioc); |
| 26 | bus->request_name(serviceName()); |
| 27 | |
| 28 | objServer = std::make_unique<sdbusplus::asio::object_server>(bus); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void DbusEnvironment::TearDown() |
| 33 | { |
| 34 | ioc.poll(); |
| 35 | |
| 36 | futures.clear(); |
| 37 | } |
| 38 | |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 39 | boost::asio::io_context& DbusEnvironment::getIoc() |
| 40 | { |
| 41 | return ioc; |
| 42 | } |
| 43 | |
| 44 | std::shared_ptr<sdbusplus::asio::connection> DbusEnvironment::getBus() |
| 45 | { |
| 46 | return bus; |
| 47 | } |
| 48 | |
| 49 | std::shared_ptr<sdbusplus::asio::object_server> DbusEnvironment::getObjServer() |
| 50 | { |
| 51 | return objServer; |
| 52 | } |
| 53 | |
| 54 | const char* DbusEnvironment::serviceName() |
| 55 | { |
| 56 | return "telemetry.ut"; |
| 57 | } |
| 58 | |
| 59 | std::function<void()> DbusEnvironment::setPromise(std::string_view name) |
| 60 | { |
| 61 | auto promise = std::make_shared<std::promise<bool>>(); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 62 | futures[std::string(name)].emplace_back(promise->get_future()); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 63 | return [p = std::move(promise)]() { p->set_value(true); }; |
| 64 | } |
| 65 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 66 | bool DbusEnvironment::waitForFuture(std::string_view name, Milliseconds timeout) |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 67 | { |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 68 | return waitForFuture(getFuture(name), timeout); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 71 | bool DbusEnvironment::waitForFutures(std::string_view name, |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 72 | Milliseconds timeout) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 73 | { |
| 74 | auto& data = futures[std::string(name)]; |
Patrick Williams | f535cad | 2024-08-16 15:21:20 -0400 | [diff] [blame] | 75 | auto ret = waitForFutures(std::move(data), true, [](auto sum, auto val) { |
| 76 | return sum && val; |
| 77 | }, timeout); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 78 | data = std::vector<std::future<bool>>{}; |
| 79 | return ret; |
| 80 | } |
| 81 | |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 82 | std::future<bool> DbusEnvironment::getFuture(std::string_view name) |
| 83 | { |
| 84 | auto& data = futures[std::string(name)]; |
| 85 | auto it = data.begin(); |
| 86 | |
| 87 | if (it != data.end()) |
| 88 | { |
| 89 | auto result = std::move(*it); |
| 90 | data.erase(it); |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | return {}; |
| 95 | } |
| 96 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 97 | void DbusEnvironment::sleepFor(Milliseconds timeout) |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 98 | { |
| 99 | auto end = std::chrono::high_resolution_clock::now() + timeout; |
| 100 | |
| 101 | while (std::chrono::high_resolution_clock::now() < end) |
| 102 | { |
| 103 | synchronizeIoc(); |
| 104 | std::this_thread::yield(); |
| 105 | } |
| 106 | |
| 107 | synchronizeIoc(); |
| 108 | } |
| 109 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 110 | Milliseconds DbusEnvironment::measureTime(std::function<void()> fun) |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 111 | { |
| 112 | auto begin = std::chrono::high_resolution_clock::now(); |
| 113 | fun(); |
| 114 | auto end = std::chrono::high_resolution_clock::now(); |
| 115 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 116 | return std::chrono::duration_cast<Milliseconds>(end - begin); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | boost::asio::io_context DbusEnvironment::ioc; |
| 120 | std::shared_ptr<sdbusplus::asio::connection> DbusEnvironment::bus; |
| 121 | std::shared_ptr<sdbusplus::asio::object_server> DbusEnvironment::objServer; |
| 122 | std::map<std::string, std::vector<std::future<bool>>> DbusEnvironment::futures; |
| 123 | bool DbusEnvironment::setUp = false; |