From 503d29fbb5c421710ddced7a97c5245be579b11c Mon Sep 17 00:00:00 2001 From: Soo Hwan Na Date: Fri, 22 Dec 2023 15:56:59 +0900 Subject: [PATCH] libextsupport: Add `waitServiceDefault` which polls --- libextsupport/GetServiceSupport.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/libextsupport/GetServiceSupport.h b/libextsupport/GetServiceSupport.h index 11e0090..fe65246 100644 --- a/libextsupport/GetServiceSupport.h +++ b/libextsupport/GetServiceSupport.h @@ -1,3 +1,6 @@ +#include +#include + #include #include @@ -17,3 +20,24 @@ static inline std::shared_ptr getServiceDefault(void) { return getService(std::string() + T::descriptor + "/default"); } + +template +static std::shared_ptr waitServiceDefault(int retries = 5) +{ + std::shared_ptr kService = nullptr; + const auto kServiceDesc = std::string() + T::descriptor + "/default"; + + // If not declared, just return null + if (AServiceManager_isDeclared(kServiceDesc.c_str())) { + do { + kService = getService(kServiceDesc); + if (kService == nullptr) { + std::this_thread::sleep_for(std::chrono::seconds(5)); + --retries; + } else { + break; + } + } while (retries > 0); + } + return kService; +}