From 763c46cced9594642ceed6623dd9f8e40f8835a7 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 18 Mar 2025 19:03:14 +0900 Subject: [PATCH] Modify implementation Signed-off-by: Hwankyu Jhun --- src/service_context.cc | 7 ++++++- src/service_context.hh | 1 + src/service_info.cc | 13 +++++++------ src/service_info.hh | 3 ++- src/service_loader.cc | 41 +++++++++++++++++++++++++++++++++++++++-- src/service_loader.hh | 12 +++++++++--- 6 files changed, 64 insertions(+), 13 deletions(-) diff --git a/src/service_context.cc b/src/service_context.cc index 18efc87..31ddcdb 100644 --- a/src/service_context.cc +++ b/src/service_context.cc @@ -63,7 +63,8 @@ bool ServiceContext::Init() { void ServiceContext::Shutdown() { if (!handle_) return; - auto* shutdown_func = reinterpret_cast(dlsym(handle_, kUnitedServiceShutdown)); + auto* shutdown_func = + reinterpret_cast(dlsym(handle_, kUnitedServiceShutdown)); if (shutdown_func == nullptr) { _E("dlsym() is failed. error=%s", dlerror()); } else { @@ -75,6 +76,10 @@ void ServiceContext::Shutdown() { std::shared_ptr ServiceContext::GetService() const { return service_; } +std::shared_ptr ServiceContext::GetServiceInfo() const { + return info_; +} + const std::string& ServiceContext::GetName() const { return info_->GetName(); } bool ServiceContext::Load() { diff --git a/src/service_context.hh b/src/service_context.hh index 585472f..54939a9 100644 --- a/src/service_context.hh +++ b/src/service_context.hh @@ -33,6 +33,7 @@ class ServiceContext { bool Init(); void Shutdown(); std::shared_ptr GetService() const; + std::shared_ptr GetServiceInfo() const; const std::string& GetName() const; private: diff --git a/src/service_info.cc b/src/service_info.cc index f955643..9b2d8bc 100644 --- a/src/service_info.cc +++ b/src/service_info.cc @@ -33,13 +33,14 @@ static const std::string kPath = kTagUnitedService + ":Path"; namespace tizen_base { -ServiceInfo::ServiceInfo(std::string conf_name, const Dictionary& dictionary) +ServiceInfo::ServiceInfo(std::string conf_name, + std::shared_ptr dictionary) : conf_name_(std::move(conf_name)) { - description_ = dictionary.Get(kDescription); - name_ = dictionary.Get(kName); - mode_ = dictionary.Get(kMode); - type_ = dictionary.Get(kType); - path_ = dictionary.Get(kPath); + description_ = dictionary->Get(kDescription); + name_ = dictionary->Get(kName); + mode_ = dictionary->Get(kMode); + type_ = dictionary->Get(kType); + path_ = dictionary->Get(kPath); } ServiceInfo::~ServiceInfo() {} diff --git a/src/service_info.hh b/src/service_info.hh index 96a48a7..3a6baa8 100644 --- a/src/service_info.hh +++ b/src/service_info.hh @@ -17,6 +17,7 @@ #ifndef SERVICE_INFO_HH_ #define SERVICE_INFO_HH_ +#include #include #include "dictionary.hh" @@ -25,7 +26,7 @@ namespace tizen_base { class ServiceInfo { public: - ServiceInfo(std::string conf_name, const Dictionary& dictionary); + ServiceInfo(std::string conf_name, std::shared_ptr dictionary); ~ServiceInfo(); const std::string& GetConfName() const; diff --git a/src/service_loader.cc b/src/service_loader.cc index 312dce8..99ffbbe 100644 --- a/src/service_loader.cc +++ b/src/service_loader.cc @@ -19,11 +19,21 @@ #include #include +#include #include +#include "exception.hh" +#include "iniparser.hh" #include "log_private.hh" +namespace { + +constexpr const char kPathUnitedService[] = "/usr/share/united-service/"; + +} // namespace + namespace tizen_base { +namespace fs = std::filesystem; ServiceLoader::ServiceLoader(int argc, char** argv, std::string name) : argc_(argc), argv_(argv), name_(std::move(name)) { @@ -59,9 +69,19 @@ bool ServiceLoader::Init() { _E("tizen_core_add_channel() is failed"); return false; } - receiver_ = nullptr; - return false; + + try { + LoadServices(); + } catch (const fs::filesystem_error& e) { + _E("Exception occurs. error: %s(%d)", e.what(), e.code().value()); + return false; + } catch (const Exception& e) { + _E("Exception occurs. error: %s", e.what()); + return false; + } + + return true; } void ServiceLoader::Shutdown() { @@ -86,6 +106,23 @@ void ServiceLoader::Shutdown() { } } +void ServiceLoader::LoadServices() { + const fs::path directory(kPathUnitedService + name_); + for (auto const& entry : fs::directory_iterator(directory)) { + if (entry.is_regular_file()) { + auto file(entry.path()); + if (file.extension() == ".service") { + auto dictionary = IniParser::Parse(file.string()); + auto info = + std::make_shared(file.string(), std::move(dictionary)); + auto context = std::make_shared(std::move(info)); + context->Init(); + contexts_.push_back(std::move(context)); + } + } + } +} + int ServiceLoader::Run() { OnCreate(); running_ = true; diff --git a/src/service_loader.hh b/src/service_loader.hh index 0a4c7a6..a4bc63d 100644 --- a/src/service_loader.hh +++ b/src/service_loader.hh @@ -14,12 +14,15 @@ * limitations under the License. */ -#include - #include +#include +#include #include +#include "service_context.hh" +#include "service_info.hh" + namespace tizen_base { class ServiceLoader { @@ -39,10 +42,12 @@ class ServiceLoader { private: bool Init(); void Shutdown(); + void LoadServices(); static void ChannelReceiveCb(tizen_core_channel_object_h object, void* user_data); - private : int argc_; + private: + int argc_; char** argv_; std::string name_; tizen_core_task_h task_ = nullptr; @@ -51,6 +56,7 @@ class ServiceLoader { tizen_core_channel_receiver_h receiver_ = nullptr; tizen_core_source_h source_ = nullptr; bool running_ = false; + std::vector> contexts_; }; } // namespace tizen_base -- 2.34.1