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(
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)
#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) :
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)
{
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);
+ }
}
#include <string>
#include <set>
+namespace Json {
+ class Value;
+}
+
namespace ctx {
class Attribute {
virtual ~Attribute();
virtual Attribute::Type getType() = 0;
+ virtual void serialize(Json::Value& jsonNode) = 0;
const std::string& getName();
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;
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;
* 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()
}
-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),
{
}
-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)
{
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);
+}
#include <bundle.h>
#include <ContextTypes.h>
+namespace Json {
+ class Value;
+}
+
namespace ctx {
class JobAction {
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();
const std::string& getMethodName();
GVariant* getParameters();
+ void serialize(Json::Value& jsonNode);
+
private:
std::string __busName;
std::string __objectPath;
bundle* getAppControl();
+ void serialize(Json::Value& jsonNode);
+
private:
bundle* __appCtrlBundle;
};
* 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) :
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)
{
}
+void JobTrigger::serialize(Json::Value& jsonNode)
+{
+ serializeAttributes(jsonNode);
+}
+
JobRequirement::JobRequirement(const std::string& uri, bool optional) :
JobContext(uri),
{
return __optional;
}
+
+void JobRequirement::serialize(Json::Value& jsonNode)
+{
+ jsonNode[KEY_OPTIONAL] = __optional;
+
+ serializeAttributes(jsonNode);
+}
#include <string>
#include <list>
+namespace Json {
+ class Value;
+}
+
namespace ctx {
class Attribute;
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;
public:
JobTrigger(const std::string& uri);
~JobTrigger();
+
+ void serialize(Json::Value& jsonNode);
};
bool isOptional();
+ void serialize(Json::Value& jsonNode);
+
private:
bool __optional;
};
* 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() :
__started(false),
__oneTime(false),
__persistent(false),
- __userBundle(NULL),
__action(NULL),
__disjunction(false)
{
}
delete __action;
-
- if (__userBundle)
- bundle_free(__userBundle);
}
int JobInfo::getId()
return __started;
}
-bundle* JobInfo::getBundle()
+const std::string& JobInfo::getUserData()
{
- return __userBundle;
+ return __userData;
}
JobInfo& JobInfo::setOneTime(bool oneTime)
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;
}
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;
{
}
+void PeriodicJobInfo::serialize(Json::Value& jsonRoot)
+{
+ serializeCommon(jsonRoot);
+
+ jsonRoot[KEY_INTERVAL] = __intervalMs;
+ jsonRoot[KEY_ANCHOR] = static_cast<int64_t>(__anchor);
+}
+
OnDemandJobInfo::OnDemandJobInfo()
{
return *this;
}
+
+void OnDemandJobInfo::serialize(Json::Value& jsonRoot)
+{
+ serializeCommon(jsonRoot);
+
+ for (auto& trig : __triggers) {
+ trig->serialize(jsonRoot[KEY_TRIGGER][trig->getUri()]);
+ }
+}
#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;
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;
PeriodicJobInfo(unsigned int intervalMs, time_t anchor);
~PeriodicJobInfo();
+ void serialize(Json::Value& jsonRoot);
+
private:
unsigned int __intervalMs;
time_t __anchor;
OnDemandJobInfo& addTrigger(JobTrigger* trigger);
+ void serialize(Json::Value& jsonRoot);
+
private:
std::list<JobTrigger*> __triggers;
};
" <arg type='i' name='return' direction='out'/>" \
"</method>"
-#endif
+#endif /* __CONTEXT_JOB_SCHEDULER_TYPES_PRIVATE_H__ */