#include <utility>
+#include "exception.hh"
#include "log_private.hh"
namespace {
static const std::string kMode = kTagUnitedService + ":mode";
static const std::string kType = kTagUnitedService + ":type";
static const std::string kPath = kTagUnitedService + ":path";
+static const std::string kPriority = kTagUnitedService + ":priority";
} // namespace
_D("Type=%s", type_.c_str());
path_ = dictionary->Get(kPath);
_D("Path=%s", path_.c_str());
+
+ try {
+ priority_ = stoul(dictionary->Get(kPriority));
+ _D("Priority=%d", priority_);
+
+ if (priority_ < 1 || priority_ > 99)
+ priority_ = 0;
+ } catch (const Exception& e) {
+ _E("Failed to get priority : %s", e.what());
+ priority_ = 0;
+ }
}
ServiceInfo::~ServiceInfo() {}
const std::string& ServiceInfo::GetPath() const { return path_; }
+const unsigned int ServiceInfo::GetPriority() const { return priority_; }
+
} // namespace tizen_base
#include <tizen_core.h>
#include <tizen_core_channel.h>
+#include <algorithm>
#include <filesystem>
#include <utility>
constexpr const char kPathUnitedService[] = "/usr/share/united-service/";
constexpr const char kConf[] = "/conf/";
+bool compare_context(const std::shared_ptr<tizen_base::ServiceContext>& context1,
+ const std::shared_ptr<tizen_base::ServiceContext>& context2) {
+ return context1->GetPriority() > context2->GetPriority();
+}
+
+void PrintState(const std::shared_ptr<tizen_base::ServiceContext>& context) {
+ switch(context->GetService()->GetState()) {
+ case tizen_base::Service::State::Initialized:
+ _W("%s : state Initialized", context->GetService()->GetName().c_str());
+ break;
+ case tizen_base::Service::State::Created:
+ _W("%s : state Created", context->GetService()->GetName().c_str());
+ break;
+ case tizen_base::Service::State::Running:
+ _W("%s : state Running", context->GetService()->GetName().c_str());
+ break;
+ case tizen_base::Service::State::Destroyed:
+ _W("%s : state Destroyed", context->GetService()->GetName().c_str());
+ break;
+ default:
+ _W("invalid state");
+ break;
+ }
+}
+
} // namespace
namespace tizen_base {
auto info =
std::make_shared<ServiceInfo>(file.string(), std::move(dictionary));
auto context = std::make_shared<ServiceContext>(std::move(info));
- context->Init();
+ // context->Init();
contexts_.push_back(std::move(context));
}
}
}
+
+ if (!contexts_.empty())
+ std::sort(contexts_.begin(), contexts_.end(), compare_context);
+}
+
+void ServiceLoader::RunServices() {
+ unsigned int previous_priority = 0;
+ std::shared_ptr<ServiceContext> previous_service = nullptr;
+
+ for (auto& context : contexts_) {
+ if (context->GetServiceInfo()->GetMode().compare("on-demand") == 0) {
+ _W("%s is on-demand mode. it's skipped to run", context->GetName().c_str());
+ continue;
+ }
+
+ if (previous_priority == 0)
+ previous_priority = context->GetPriority();
+
+ if (previous_priority == context->GetPriority()) {
+ _E("@@ Priority : %d, pre-priority : %d, name : %s", context->GetPriority(), previous_priority, context->GetName().c_str());
+ context->Run();
+
+ PrintState(context);
+
+ previous_service = context;
+ } else {
+ _E("@@ Priority : %d, pre-priority : %d, name : %s", context->GetPriority(), previous_priority, context->GetName().c_str());
+ previous_service->GetService()->WaitRun();
+
+ _E("@@ Previous state");
+ PrintState(previous_service);
+
+ context->Run();
+
+ _E("@@ Current state");
+ PrintState(context);
+
+ previous_service = context;
+ previous_priority = context->GetPriority();
+ }
+ }
+
+ // throw new std::runtime_error("Failed to Run service");
}
int ServiceLoader::Run() {
void ServiceLoader::OnCreate() {
try {
LoadServices();
+
+ RunServices();
} catch (const fs::filesystem_error& e) {
_E("Exception occurs. error: %s(%d)", e.what(), e.code().value());
} catch (const Exception& e) {