Add job information class serializer 95/133395/3
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 12 Jun 2017 04:59:15 +0000 (13:59 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 12 Jun 2017 06:42:33 +0000 (15:42 +0900)
Change-Id: I941116c863e8d4ac908a77c6657cdef6d9428967
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
CMakeLists.txt
packaging/context-job-scheduler.spec
src/shared/Attribute.cpp
src/shared/Attribute.h
src/shared/JobAction.cpp
src/shared/JobAction.h
src/shared/JobContext.cpp
src/shared/JobContext.h
src/shared/JobInfo.cpp
src/shared/JobInfo.h
src/shared/JobSchedulerTypesPrivate.h

index 1830936..865ea2a 100644 (file)
@@ -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(
index b1a245f..65a2667 100644 (file)
@@ -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)
index f3f2871..f32f7f5 100644 (file)
 
 #include <climits>
 #include <algorithm>
+#include <json/json.h>
 #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<int>(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<int>(Type::STRING);
+
+       for (auto& target : __targets) {
+               jsonNode[KEY_TARGET].append(target);
+       }
+
+       for (auto& nonTarget : __nonTargets) {
+               jsonNode[KEY_NON_TARGET].append(nonTarget);
+       }
 }
index fe7af01..fc45db8 100644 (file)
 #include <string>
 #include <set>
 
+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<std::string> __targets;
index 8419034..f3118bc 100644 (file)
  * limitations under the License.
  */
 
+#include <json/json.h>
+#include <bundle_internal.h>
 #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<const char*>(encoded);
+
+       bundle_free_encoded_rawdata(&encoded);
+}
index 25fc6e5..d0a4e3b 100644 (file)
 #include <bundle.h>
 #include <ContextTypes.h>
 
+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;
        };
index e7563f3..cf966b4 100644 (file)
  * limitations under the License.
  */
 
+#include <json/json.h>
 #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);
+}
index 255bdf3..6a624d7 100644 (file)
 #include <string>
 #include <list>
 
+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<Attribute*> __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;
        };
index 3e581f4..6018d4d 100644 (file)
  * limitations under the License.
  */
 
+#include <json/json.h>
+#include <json/writer.h>
+#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<int64_t>(__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()]);
+       }
+}
index 91ac825..743f4f0 100644 (file)
 #ifndef __CONTEXT_JOB_SCHEDULER_JOB_INFO_H__
 #define __CONTEXT_JOB_SCHEDULER_JOB_INFO_H__
 
-#include <ContextTypes.h>
+#include <string>
 #include <list>
+#include <ContextTypes.h>
 #include <bundle.h>
 
+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<JobRequirement*> __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<JobTrigger*> __triggers;
        };
index 8b8d1b7..fa8a3d9 100644 (file)
@@ -27,4 +27,4 @@
        "       <arg type='i' name='return' direction='out'/>" \
        "</method>"
 
-#endif
+#endif /* __CONTEXT_JOB_SCHEDULER_TYPES_PRIVATE_H__ */