From: Mu-Woong Lee Date: Mon, 12 Jun 2017 04:59:15 +0000 (+0900) Subject: Add job information class serializer X-Git-Tag: accepted/tizen/unified/20170630.083109^2~14 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=263c92bb84d60361eb9110802e79fb0f31e6f62a;p=platform%2Fcore%2Fcontext%2Fjob-scheduler.git Add job information class serializer Change-Id: I941116c863e8d4ac908a77c6657cdef6d9428967 Signed-off-by: Mu-Woong Lee --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 1830936..865ea2a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) PROJECT(context-job-scheduler) INCLUDE(GNUInstallDirs) -SET(DEPS "glib-2.0 gio-2.0 dlog capi-base-common") +SET(DEPS "glib-2.0 gio-2.0 jsoncpp dlog capi-base-common") SET(INCDIR "${CMAKE_INSTALL_INCLUDEDIR}/context-service") INCLUDE_DIRECTORIES( diff --git a/packaging/context-job-scheduler.spec b/packaging/context-job-scheduler.spec index b1a245f..65a2667 100644 --- a/packaging/context-job-scheduler.spec +++ b/packaging/context-job-scheduler.spec @@ -9,6 +9,7 @@ Source0: %{name}-%{version}.tar.gz BuildRequires: cmake BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(gio-2.0) +BuildRequires: pkgconfig(jsoncpp) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(bundle) BuildRequires: pkgconfig(capi-base-common) diff --git a/src/shared/Attribute.cpp b/src/shared/Attribute.cpp index f3f2871..f32f7f5 100644 --- a/src/shared/Attribute.cpp +++ b/src/shared/Attribute.cpp @@ -16,8 +16,15 @@ #include #include +#include #include "Attribute.h" +#define KEY_DATA_TYPE "DataType" +#define KEY_CLOSED_RANGE "ClosedRange" +#define KEY_OPEN_RANGE "OpenRange" +#define KEY_TARGET "Target" +#define KEY_NON_TARGET "NonTarget" + using namespace ctx; Attribute::Attribute(const std::string& name) : @@ -53,36 +60,62 @@ Attribute::Type IntegerAttribute::getType() return Attribute::Type::INTEGER; } -void IntegerAttribute::setClosedLowerBound(int bound) +IntegerAttribute& IntegerAttribute::setClosedLowerBound(int bound) { __closedLowerBound = std::max(__closedLowerBound, bound); + return *this; } -void IntegerAttribute::setClosedUpperBound(int bound) +IntegerAttribute& IntegerAttribute::setClosedUpperBound(int bound) { __closedUpperBound = std::min(__closedUpperBound, bound); + return *this; } -void IntegerAttribute::setOpenLowerBound(int bound) +IntegerAttribute& IntegerAttribute::setOpenLowerBound(int bound) { __openLowerBound = std::max(__openLowerBound, bound); + return *this; } -void IntegerAttribute::setOpenUpperBound(int bound) +IntegerAttribute& IntegerAttribute::setOpenUpperBound(int bound) { __openUpperBound = std::min(__openUpperBound, bound); + return *this; } -void IntegerAttribute::addTarget(int target) +IntegerAttribute& IntegerAttribute::addTarget(int target) { __targets.insert(target); + return *this; } -void IntegerAttribute::addNonTarget(int nonTarget) +IntegerAttribute& IntegerAttribute::addNonTarget(int nonTarget) { __nonTargets.insert(nonTarget); + return *this; +} + +void IntegerAttribute::serialize(Json::Value& jsonNode) +{ + jsonNode[KEY_DATA_TYPE] = static_cast(Type::INTEGER); + + jsonNode[KEY_CLOSED_RANGE].append(__closedLowerBound); + jsonNode[KEY_CLOSED_RANGE].append(__closedUpperBound); + + jsonNode[KEY_OPEN_RANGE].append(__openLowerBound); + jsonNode[KEY_OPEN_RANGE].append(__openUpperBound); + + for (auto& target : __targets) { + jsonNode[KEY_TARGET].append(target); + } + + for (auto& nonTarget : __nonTargets) { + jsonNode[KEY_NON_TARGET].append(nonTarget); + } } + StringAttribute::StringAttribute(const std::string& name) : Attribute(name) { @@ -97,12 +130,27 @@ Attribute::Type StringAttribute::getType() return Attribute::Type::STRING; } -void StringAttribute::addTarget(const std::string& target) +StringAttribute& StringAttribute::addTarget(const std::string& target) { __targets.insert(target); + return *this; } -void StringAttribute::addNonTarget(const std::string& nonTarget) +StringAttribute& StringAttribute::addNonTarget(const std::string& nonTarget) { __nonTargets.insert(nonTarget); + return *this; +} + +void StringAttribute::serialize(Json::Value& jsonNode) +{ + jsonNode[KEY_DATA_TYPE] = static_cast(Type::STRING); + + for (auto& target : __targets) { + jsonNode[KEY_TARGET].append(target); + } + + for (auto& nonTarget : __nonTargets) { + jsonNode[KEY_NON_TARGET].append(nonTarget); + } } diff --git a/src/shared/Attribute.h b/src/shared/Attribute.h index fe7af01..fc45db8 100644 --- a/src/shared/Attribute.h +++ b/src/shared/Attribute.h @@ -20,6 +20,10 @@ #include #include +namespace Json { + class Value; +} + namespace ctx { class Attribute { @@ -32,6 +36,7 @@ namespace ctx { virtual ~Attribute(); virtual Attribute::Type getType() = 0; + virtual void serialize(Json::Value& jsonNode) = 0; const std::string& getName(); @@ -50,12 +55,14 @@ namespace ctx { Attribute::Type getType(); - void setClosedLowerBound(int bound); - void setClosedUpperBound(int bound); - void setOpenLowerBound(int bound); - void setOpenUpperBound(int bound); - void addTarget(int target); - void addNonTarget(int nonTarget); + IntegerAttribute& setClosedLowerBound(int bound); + IntegerAttribute& setClosedUpperBound(int bound); + IntegerAttribute& setOpenLowerBound(int bound); + IntegerAttribute& setOpenUpperBound(int bound); + IntegerAttribute& addTarget(int target); + IntegerAttribute& addNonTarget(int nonTarget); + + void serialize(Json::Value& jsonNode); private: int __closedLowerBound; @@ -74,8 +81,10 @@ namespace ctx { Attribute::Type getType(); - void addTarget(const std::string& target); - void addNonTarget(const std::string& nonTarget); + StringAttribute& addTarget(const std::string& target); + StringAttribute& addNonTarget(const std::string& nonTarget); + + void serialize(Json::Value& jsonNode); private: std::set __targets; diff --git a/src/shared/JobAction.cpp b/src/shared/JobAction.cpp index 8419034..f3118bc 100644 --- a/src/shared/JobAction.cpp +++ b/src/shared/JobAction.cpp @@ -14,8 +14,20 @@ * limitations under the License. */ +#include +#include #include "JobAction.h" +#define KEY_DBUS "DBusCall" +#define KEY_APP_CTRL "AppCtrl" + +#define KEY_BUS_NAME "BusName" +#define KEY_OBJ_PATH "ObjPath" +#define KEY_INTERFACE "Interface" +#define KEY_METHOD "Method" +#define KEY_PARAM "Param" +#define KEY_BUNDLE "Bundle" + using namespace ctx; JobAction::JobAction() @@ -27,7 +39,7 @@ JobAction::~JobAction() } -JobDBusMethod::JobDBusMethod(const std::string& busName, const std::string& objPath, +JobDBusCall::JobDBusCall(const std::string& busName, const std::string& objPath, const std::string& interface, const std::string& methodName, GVariant* param) : __busName(busName), __objectPath(objPath), @@ -37,42 +49,57 @@ JobDBusMethod::JobDBusMethod(const std::string& busName, const std::string& objP { } -JobDBusMethod::~JobDBusMethod() +JobDBusCall::~JobDBusCall() { if (__parameters) g_variant_unref(__parameters); } -JobAction::Type JobDBusMethod::getType() +JobAction::Type JobDBusCall::getType() { return JobAction::Type::DBUS_METHOD; } -const std::string& JobDBusMethod::getBusName() +const std::string& JobDBusCall::getBusName() { return __busName; } -const std::string& JobDBusMethod::getObjectPath() +const std::string& JobDBusCall::getObjectPath() { return __objectPath; } -const std::string& JobDBusMethod::getInterface() +const std::string& JobDBusCall::getInterface() { return __interface; } -const std::string& JobDBusMethod::getMethodName() +const std::string& JobDBusCall::getMethodName() { return __methodName; } -GVariant* JobDBusMethod::getParameters() +GVariant* JobDBusCall::getParameters() { return __parameters; } +void JobDBusCall::serialize(Json::Value& jsonNode) +{ + Json::Value& node = jsonNode[KEY_DBUS]; + + node[KEY_BUS_NAME] = __busName; + node[KEY_OBJ_PATH] = __objectPath; + node[KEY_INTERFACE] = __interface; + node[KEY_METHOD] = __methodName; + + if (__parameters) { + char* param = g_variant_print(__parameters, TRUE); + //TODO: escaping quotes? + node[KEY_PARAM] = param; + } +} JobAppControl::JobAppControl(bundle* appCtrlBundle) : __appCtrlBundle(appCtrlBundle) @@ -94,3 +121,16 @@ bundle* JobAppControl::getAppControl() { return __appCtrlBundle; } + +void JobAppControl::serialize(Json::Value& jsonNode) +{ + bundle_raw* encoded = NULL; + int length = 0; + + bundle_encode(__appCtrlBundle, &encoded, &length); + IF_FAIL_VOID_TAG(encoded, _E, "bundle_encode() failed"); + + jsonNode[KEY_APP_CTRL][KEY_BUNDLE] = reinterpret_cast(encoded); + + bundle_free_encoded_rawdata(&encoded); +} diff --git a/src/shared/JobAction.h b/src/shared/JobAction.h index 25fc6e5..d0a4e3b 100644 --- a/src/shared/JobAction.h +++ b/src/shared/JobAction.h @@ -21,6 +21,10 @@ #include #include +namespace Json { + class Value; +} + namespace ctx { class JobAction { @@ -32,17 +36,18 @@ namespace ctx { virtual ~JobAction(); virtual Type getType() = 0; + virtual void serialize(Json::Value& jsonNode) = 0; protected: JobAction(); }; - class JobDBusMethod : public JobAction { + class JobDBusCall : public JobAction { public: - JobDBusMethod(const std::string& busName, const std::string& objPath, + JobDBusCall(const std::string& busName, const std::string& objPath, const std::string& interface, const std::string& methodName, GVariant* param); - ~JobDBusMethod(); + ~JobDBusCall(); JobAction::Type getType(); @@ -52,6 +57,8 @@ namespace ctx { const std::string& getMethodName(); GVariant* getParameters(); + void serialize(Json::Value& jsonNode); + private: std::string __busName; std::string __objectPath; @@ -70,6 +77,8 @@ namespace ctx { bundle* getAppControl(); + void serialize(Json::Value& jsonNode); + private: bundle* __appCtrlBundle; }; diff --git a/src/shared/JobContext.cpp b/src/shared/JobContext.cpp index e7563f3..cf966b4 100644 --- a/src/shared/JobContext.cpp +++ b/src/shared/JobContext.cpp @@ -14,9 +14,13 @@ * limitations under the License. */ +#include #include "JobContext.h" #include "Attribute.h" +#define KEY_OPTIONAL "Optional" +#define KEY_DISJUNCTION "Disjunction" + using namespace ctx; JobContext::JobContext(const std::string& uri) : @@ -73,6 +77,15 @@ JobContext& JobContext::setDisjunction(bool disjunction) return *this; } +void JobContext::serializeAttributes(Json::Value& jsonNode) +{ + for (auto& attr : __attributes) { + attr->serialize(jsonNode[attr->getName()]); + } + + jsonNode[KEY_DISJUNCTION] = __disjunction; +} + JobTrigger::JobTrigger(const std::string& uri) : JobContext(uri) @@ -83,6 +96,11 @@ JobTrigger::~JobTrigger() { } +void JobTrigger::serialize(Json::Value& jsonNode) +{ + serializeAttributes(jsonNode); +} + JobRequirement::JobRequirement(const std::string& uri, bool optional) : JobContext(uri), @@ -98,3 +116,10 @@ bool JobRequirement::isOptional() { return __optional; } + +void JobRequirement::serialize(Json::Value& jsonNode) +{ + jsonNode[KEY_OPTIONAL] = __optional; + + serializeAttributes(jsonNode); +} diff --git a/src/shared/JobContext.h b/src/shared/JobContext.h index 255bdf3..6a624d7 100644 --- a/src/shared/JobContext.h +++ b/src/shared/JobContext.h @@ -21,6 +21,10 @@ #include #include +namespace Json { + class Value; +} + namespace ctx { class Attribute; @@ -34,12 +38,16 @@ namespace ctx { bool prepareAttributeInt(const std::string& attrName); bool prepareAttributeStr(const std::string& attrName); + virtual void serialize(Json::Value& jsonNode) = 0; + /* Legacy support */ JobContext& setDisjunction(bool disjunction); protected: JobContext(const std::string& uri); + void serializeAttributes(Json::Value& jsonNode); + private: std::string __uri; std::list __attributes; @@ -52,6 +60,8 @@ namespace ctx { public: JobTrigger(const std::string& uri); ~JobTrigger(); + + void serialize(Json::Value& jsonNode); }; @@ -62,6 +72,8 @@ namespace ctx { bool isOptional(); + void serialize(Json::Value& jsonNode); + private: bool __optional; }; diff --git a/src/shared/JobInfo.cpp b/src/shared/JobInfo.cpp index 3e581f4..6018d4d 100644 --- a/src/shared/JobInfo.cpp +++ b/src/shared/JobInfo.cpp @@ -14,10 +14,23 @@ * limitations under the License. */ +#include +#include +#include "JobSchedulerTypesPrivate.h" #include "JobInfo.h" #include "JobContext.h" #include "JobAction.h" +#define KEY_ONE_TIME "OneTime" +#define KEY_PERSISTENT "Persistent" +#define KEY_USER_DATA "UserData" +#define KEY_INTERVAL "Interval" +#define KEY_ANCHOR "Anchor" +#define KEY_TRIGGER "Trigger" +#define KEY_REQUIREMENT "Requirement" +#define KEY_ACTION "Action" +#define KEY_DISJUNCTION "Disjunction" + using namespace ctx; JobInfo::JobInfo() : @@ -25,7 +38,6 @@ JobInfo::JobInfo() : __started(false), __oneTime(false), __persistent(false), - __userBundle(NULL), __action(NULL), __disjunction(false) { @@ -38,9 +50,6 @@ JobInfo::~JobInfo() } delete __action; - - if (__userBundle) - bundle_free(__userBundle); } int JobInfo::getId() @@ -53,9 +62,9 @@ bool JobInfo::isStarted() return __started; } -bundle* JobInfo::getBundle() +const std::string& JobInfo::getUserData() { - return __userBundle; + return __userData; } JobInfo& JobInfo::setOneTime(bool oneTime) @@ -70,14 +79,9 @@ JobInfo& JobInfo::setPersistent(bool persistent) return *this; } -JobInfo& JobInfo::setBundle(bundle*& userBundle) +JobInfo& JobInfo::setUserData(const std::string& userData) { - if (__userBundle) - bundle_free(__userBundle); - - __userBundle = userBundle; - userBundle = NULL; - + __userData = userData; return *this; } @@ -99,6 +103,34 @@ JobInfo& JobInfo::setAction(JobAction* action) return *this; } +const std::string& JobInfo::toString() +{ + if (__serializedStr.empty()) { + Json::Value jsonRoot; + serialize(jsonRoot); + + Json::FastWriter fastWriter; + __serializedStr = fastWriter.write(jsonRoot); + } + return __serializedStr; +} + +void JobInfo::serializeCommon(Json::Value& jsonRoot) +{ + jsonRoot[KEY_ONE_TIME] = __oneTime; + jsonRoot[KEY_PERSISTENT] = __persistent; + jsonRoot[KEY_USER_DATA] = __userData; + + if (__action) + __action->serialize(jsonRoot[KEY_ACTION]); + + for (auto& req : __requirements) { + req->serialize(jsonRoot[KEY_REQUIREMENT][req->getUri()]); + } + + jsonRoot[KEY_DISJUNCTION] = __disjunction; +} + JobInfo& JobInfo::setDisjunction(bool disjunction) { __disjunction = disjunction; @@ -116,6 +148,14 @@ PeriodicJobInfo::~PeriodicJobInfo() { } +void PeriodicJobInfo::serialize(Json::Value& jsonRoot) +{ + serializeCommon(jsonRoot); + + jsonRoot[KEY_INTERVAL] = __intervalMs; + jsonRoot[KEY_ANCHOR] = static_cast(__anchor); +} + OnDemandJobInfo::OnDemandJobInfo() { @@ -132,3 +172,12 @@ OnDemandJobInfo& OnDemandJobInfo::addTrigger(JobTrigger* trigger) return *this; } + +void OnDemandJobInfo::serialize(Json::Value& jsonRoot) +{ + serializeCommon(jsonRoot); + + for (auto& trig : __triggers) { + trig->serialize(jsonRoot[KEY_TRIGGER][trig->getUri()]); + } +} diff --git a/src/shared/JobInfo.h b/src/shared/JobInfo.h index 91ac825..743f4f0 100644 --- a/src/shared/JobInfo.h +++ b/src/shared/JobInfo.h @@ -17,10 +17,15 @@ #ifndef __CONTEXT_JOB_SCHEDULER_JOB_INFO_H__ #define __CONTEXT_JOB_SCHEDULER_JOB_INFO_H__ -#include +#include #include +#include #include +namespace Json { + class Value; +} + namespace ctx { class JobTrigger; @@ -33,29 +38,37 @@ namespace ctx { int getId(); bool isStarted(); - bundle* getBundle(); + const std::string& getUserData(); JobInfo& setOneTime(bool oneTime); JobInfo& setPersistent(bool persistent); - JobInfo& setBundle(bundle*& userBundle); + + // UserData should be a string that can be a string element of Json + JobInfo& setUserData(const std::string& userData); JobInfo& addRequirement(JobRequirement* req); JobInfo& setAction(JobAction* action); + const std::string& toString(); + virtual void serialize(Json::Value& jsonRoot) = 0; + /* Legacy support */ JobInfo& setDisjunction(bool disjunction); protected: JobInfo(); + void serializeCommon(Json::Value& jsonRoot); + private: int __jobId; bool __started; bool __oneTime; bool __persistent; - bundle* __userBundle; + std::string __userData; std::list __requirements; JobAction* __action; + std::string __serializedStr; /* Legacy support */ bool __disjunction; @@ -67,6 +80,8 @@ namespace ctx { PeriodicJobInfo(unsigned int intervalMs, time_t anchor); ~PeriodicJobInfo(); + void serialize(Json::Value& jsonRoot); + private: unsigned int __intervalMs; time_t __anchor; @@ -80,6 +95,8 @@ namespace ctx { OnDemandJobInfo& addTrigger(JobTrigger* trigger); + void serialize(Json::Value& jsonRoot); + private: std::list __triggers; }; diff --git a/src/shared/JobSchedulerTypesPrivate.h b/src/shared/JobSchedulerTypesPrivate.h index 8b8d1b7..fa8a3d9 100644 --- a/src/shared/JobSchedulerTypesPrivate.h +++ b/src/shared/JobSchedulerTypesPrivate.h @@ -27,4 +27,4 @@ " " \ "" -#endif +#endif /* __CONTEXT_JOB_SCHEDULER_TYPES_PRIVATE_H__ */