Modify implementation
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 18 Mar 2025 10:03:14 +0000 (19:03 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 18 Mar 2025 10:03:14 +0000 (19:03 +0900)
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/service_context.cc
src/service_context.hh
src/service_info.cc
src/service_info.hh
src/service_loader.cc
src/service_loader.hh

index 18efc87324d1948738eb4e6d9108dfd0032c111d..31ddcdbebdd610adc58da6471c3c87c47fecf89a 100644 (file)
@@ -63,7 +63,8 @@ bool ServiceContext::Init() {
 void ServiceContext::Shutdown() {
   if (!handle_) return;
 
-  auto* shutdown_func = reinterpret_cast<void (*)(void)>(dlsym(handle_, kUnitedServiceShutdown));
+  auto* shutdown_func =
+      reinterpret_cast<void (*)(void)>(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<Service> ServiceContext::GetService() const { return service_; }
 
+std::shared_ptr<ServiceInfo> ServiceContext::GetServiceInfo() const {
+  return info_;
+}
+
 const std::string& ServiceContext::GetName() const { return info_->GetName(); }
 
 bool ServiceContext::Load() {
index 585472f98057fae133c693c76db35c91cb6ae8f6..54939a9ace98195e2561fd0be6540e4bf4f53f00 100644 (file)
@@ -33,6 +33,7 @@ class ServiceContext {
   bool Init();
   void Shutdown();
   std::shared_ptr<Service> GetService() const;
+  std::shared_ptr<ServiceInfo> GetServiceInfo() const;
   const std::string& GetName() const;
 
  private:
index f9556433710c4d7fdd1d95713bc62b98a6448857..9b2d8bc7b3cbd52185560e934f3964ec792135b5 100644 (file)
@@ -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> 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() {}
index 96a48a7cff486cf5716ab6748237ccffbb956bac..3a6baa8b821b385d9731cdf76565376211057ad6 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef SERVICE_INFO_HH_
 #define SERVICE_INFO_HH_
 
+#include <memory>
 #include <string>
 
 #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> dictionary);
   ~ServiceInfo();
 
   const std::string& GetConfName() const;
index 312dce8c91987c8945dd465009580f232036637a..99ffbbea22ddc722eba643f0067dfcb1fe45dc27 100644 (file)
 #include <tizen_core.h>
 #include <tizen_core_channel.h>
 
+#include <filesystem>
 #include <utility>
 
+#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<ServiceInfo>(file.string(), std::move(dictionary));
+        auto context = std::make_shared<ServiceContext>(std::move(info));
+        context->Init();
+        contexts_.push_back(std::move(context));
+      }
+    }
+  }
+}
+
 int ServiceLoader::Run() {
   OnCreate();
   running_ = true;
index 0a4c7a62ee6e2fd55bbbd0e46d0bccdcda127976..a4bc63df5b4103d67a1bc6536f0b09863ea1ed92 100644 (file)
  * limitations under the License.
  */
 
-#include <tizen_core.h>
-
 #include <bundle_cpp.h>
+#include <tizen_core.h>
 
+#include <memory>
 #include <string>
 
+#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<std::shared_ptr<ServiceContext>> contexts_;
 };
 
 } // namespace tizen_base