Job information classes (w/o serialization functions) 99/132899/3
authorMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 8 Jun 2017 08:06:53 +0000 (17:06 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 8 Jun 2017 10:22:00 +0000 (19:22 +0900)
Change-Id: I037b781839800d24e5ea2687330a48252eb90389
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/job_scheduler_internal.h
src/shared/Attribute.cpp [new file with mode: 0644]
src/shared/Attribute.h [new file with mode: 0644]
src/shared/JobAction.cpp [new file with mode: 0644]
src/shared/JobAction.h [new file with mode: 0644]
src/shared/JobContext.cpp [new file with mode: 0644]
src/shared/JobContext.h [new file with mode: 0644]
src/shared/JobInfo.cpp [new file with mode: 0644]
src/shared/JobInfo.h [new file with mode: 0644]

index 11c835765f2324a692d023db12866317876f4185..4f21df55754c2df8c6744ce02c186c2f2ff07b19 100644 (file)
@@ -357,7 +357,15 @@ int ctx_sched_job_requirement_create(const char* uri, bool optional, ctx_sched_j
  * @param[in]  attribute       Name of attribute
  * @return     @c 0 on success, otherwise a negative error value
  */
-int ctx_sched_job_context_prepare_attribute(ctx_sched_job_context_h job_context, const char* attribute);
+int ctx_sched_job_context_prepare_attribute_int(ctx_sched_job_context_h job_context, const char* attribute);
+
+/**
+ * @brief      Prepares an attribute of a context.
+ * @param[in]  job_context     Context handle
+ * @param[in]  attribute       Name of attribute
+ * @return     @c 0 on success, otherwise a negative error value
+ */
+int ctx_sched_job_context_prepare_attribute_str(ctx_sched_job_context_h job_context, const char* attribute);
 
 /**
  * @brief      Adds an equal-to condition of a context attribute.
@@ -450,11 +458,8 @@ int ctx_sched_job_context_destroy(ctx_sched_job_context_h job_context);
 // Logical-disjunction modifiers to support legacy Context-Trigger API.
 // NOT recommended to use.
 int ctx_sched_job_set_disjunction(ctx_sched_job_h job, bool disjunction);
-
 int ctx_sched_job_context_set_disjunction(ctx_sched_job_context_h job_context, bool disjunction);
 
-int ctx_sched_job_context_attribute_set_disjunction(ctx_sched_job_context_h job_context, const char* attribute, bool disjunction);
-
 
 /**
  * @}
diff --git a/src/shared/Attribute.cpp b/src/shared/Attribute.cpp
new file mode 100644 (file)
index 0000000..f3f2871
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <climits>
+#include <algorithm>
+#include "Attribute.h"
+
+using namespace ctx;
+
+Attribute::Attribute(const std::string& name) :
+       __name(name)
+{
+}
+
+Attribute::~Attribute()
+{
+}
+
+const std::string& Attribute::getName()
+{
+       return __name;
+}
+
+
+IntegerAttribute::IntegerAttribute(const std::string& name) :
+       Attribute(name),
+       __closedLowerBound(INT_MIN),
+       __closedUpperBound(INT_MAX),
+       __openLowerBound(INT_MIN),
+       __openUpperBound(INT_MAX)
+{
+}
+
+IntegerAttribute::~IntegerAttribute()
+{
+}
+
+Attribute::Type IntegerAttribute::getType()
+{
+       return Attribute::Type::INTEGER;
+}
+
+void IntegerAttribute::setClosedLowerBound(int bound)
+{
+       __closedLowerBound = std::max(__closedLowerBound, bound);
+}
+
+void IntegerAttribute::setClosedUpperBound(int bound)
+{
+       __closedUpperBound = std::min(__closedUpperBound, bound);
+}
+
+void IntegerAttribute::setOpenLowerBound(int bound)
+{
+       __openLowerBound = std::max(__openLowerBound, bound);
+}
+
+void IntegerAttribute::setOpenUpperBound(int bound)
+{
+       __openUpperBound = std::min(__openUpperBound, bound);
+}
+
+void IntegerAttribute::addTarget(int target)
+{
+       __targets.insert(target);
+}
+
+void IntegerAttribute::addNonTarget(int nonTarget)
+{
+       __nonTargets.insert(nonTarget);
+}
+
+StringAttribute::StringAttribute(const std::string& name) :
+       Attribute(name)
+{
+}
+
+StringAttribute::~StringAttribute()
+{
+}
+
+Attribute::Type StringAttribute::getType()
+{
+       return Attribute::Type::STRING;
+}
+
+void StringAttribute::addTarget(const std::string& target)
+{
+       __targets.insert(target);
+}
+
+void StringAttribute::addNonTarget(const std::string& nonTarget)
+{
+       __nonTargets.insert(nonTarget);
+}
diff --git a/src/shared/Attribute.h b/src/shared/Attribute.h
new file mode 100644 (file)
index 0000000..fe7af01
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_JOB_SCHEDULER_ATTRIBUTE_H__
+#define __CONTEXT_JOB_SCHEDULER_ATTRIBUTE_H__
+
+#include <string>
+#include <set>
+
+namespace ctx {
+
+       class Attribute {
+       public:
+               enum class Type {
+                       INTEGER = 1,
+                       STRING = 3
+               };
+
+               virtual ~Attribute();
+
+               virtual Attribute::Type getType() = 0;
+
+               const std::string& getName();
+
+       protected:
+               Attribute(const std::string& name);
+
+       private:
+               std::string __name;
+       };
+
+
+       class IntegerAttribute : public Attribute {
+       public:
+               IntegerAttribute(const std::string& name);
+               ~IntegerAttribute();
+
+               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);
+
+       private:
+               int __closedLowerBound;
+               int __closedUpperBound;
+               int __openLowerBound;
+               int __openUpperBound;
+               std::set<int> __targets;
+               std::set<int> __nonTargets;
+       };
+
+
+       class StringAttribute : public Attribute {
+       public:
+               StringAttribute(const std::string& name);
+               ~StringAttribute();
+
+               Attribute::Type getType();
+
+               void addTarget(const std::string& target);
+               void addNonTarget(const std::string& nonTarget);
+
+       private:
+               std::set<std::string> __targets;
+               std::set<std::string> __nonTargets;
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_ATTRIBUTE_H__ */
diff --git a/src/shared/JobAction.cpp b/src/shared/JobAction.cpp
new file mode 100644 (file)
index 0000000..8419034
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "JobAction.h"
+
+using namespace ctx;
+
+JobAction::JobAction()
+{
+}
+
+JobAction::~JobAction()
+{
+}
+
+
+JobDBusMethod::JobDBusMethod(const std::string& busName, const std::string& objPath,
+               const std::string& interface, const std::string& methodName, GVariant* param) :
+       __busName(busName),
+       __objectPath(objPath),
+       __interface(interface),
+       __methodName(methodName),
+       __parameters(param)
+{
+}
+
+JobDBusMethod::~JobDBusMethod()
+{
+       if (__parameters)
+               g_variant_unref(__parameters);
+}
+
+JobAction::Type JobDBusMethod::getType()
+{
+       return JobAction::Type::DBUS_METHOD;
+}
+
+const std::string& JobDBusMethod::getBusName()
+{
+       return __busName;
+}
+
+const std::string& JobDBusMethod::getObjectPath()
+{
+       return __objectPath;
+}
+
+const std::string& JobDBusMethod::getInterface()
+{
+       return __interface;
+}
+
+const std::string& JobDBusMethod::getMethodName()
+{
+       return __methodName;
+}
+
+GVariant* JobDBusMethod::getParameters()
+{
+       return __parameters;
+}
+
+
+JobAppControl::JobAppControl(bundle* appCtrlBundle) :
+       __appCtrlBundle(appCtrlBundle)
+{
+}
+
+JobAppControl::~JobAppControl()
+{
+       if (__appCtrlBundle)
+               bundle_free(__appCtrlBundle);
+}
+
+JobAction::Type JobAppControl::getType()
+{
+       return JobAction::Type::APP_CONTROL;
+}
+
+bundle* JobAppControl::getAppControl()
+{
+       return __appCtrlBundle;
+}
diff --git a/src/shared/JobAction.h b/src/shared/JobAction.h
new file mode 100644 (file)
index 0000000..25fc6e5
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_JOB_SCHEDULER_JOB_ACTION_H__
+#define __CONTEXT_JOB_SCHEDULER_JOB_ACTION_H__
+
+#include <string>
+#include <bundle.h>
+#include <ContextTypes.h>
+
+namespace ctx {
+
+       class JobAction {
+       public:
+               enum class Type {
+                       DBUS_METHOD = 1,
+                       APP_CONTROL = 2
+               };
+
+               virtual ~JobAction();
+               virtual Type getType() = 0;
+
+       protected:
+               JobAction();
+       };
+
+
+       class JobDBusMethod : public JobAction {
+       public:
+               JobDBusMethod(const std::string& busName, const std::string& objPath,
+                               const std::string& interface, const std::string& methodName, GVariant* param);
+               ~JobDBusMethod();
+
+               JobAction::Type getType();
+
+               const std::string& getBusName();
+               const std::string& getObjectPath();
+               const std::string& getInterface();
+               const std::string& getMethodName();
+               GVariant* getParameters();
+
+       private:
+               std::string __busName;
+               std::string __objectPath;
+               std::string __interface;
+               std::string __methodName;
+               GVariant* __parameters;
+       };
+
+
+       class JobAppControl : public JobAction {
+       public:
+               JobAppControl(bundle* appCtrlBundle);
+               ~JobAppControl();
+
+               JobAction::Type getType();
+
+               bundle* getAppControl();
+
+       private:
+               bundle* __appCtrlBundle;
+       };
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_JOB_ACTION_H__ */
diff --git a/src/shared/JobContext.cpp b/src/shared/JobContext.cpp
new file mode 100644 (file)
index 0000000..e7563f3
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "JobContext.h"
+#include "Attribute.h"
+
+using namespace ctx;
+
+JobContext::JobContext(const std::string& uri) :
+       __uri(uri)
+{
+}
+
+JobContext::~JobContext()
+{
+       for (auto& attr : __attributes) {
+               delete attr;
+       }
+}
+
+const std::string& JobContext::getUri()
+{
+       return __uri;
+}
+
+Attribute* JobContext::getAttribute(const std::string& attrName)
+{
+       for (auto& attr : __attributes) {
+               if (attr->getName() == attrName)
+                       return attr;
+       }
+       return NULL;
+}
+
+bool JobContext::prepareAttributeInt(const std::string& attrName)
+{
+       IF_FAIL_RETURN(getAttribute(attrName) == NULL, false);
+
+       Attribute* attr = new(std::nothrow) IntegerAttribute(attrName);
+       IF_FAIL_RETURN_TAG(attr, false, _E, E_STR_ALLOC);
+
+       __attributes.push_back(attr);
+       return true;
+}
+
+bool JobContext::prepareAttributeStr(const std::string& attrName)
+{
+       IF_FAIL_RETURN(getAttribute(attrName) == NULL, false);
+
+       Attribute* attr = new(std::nothrow) StringAttribute(attrName);
+       IF_FAIL_RETURN_TAG(attr, false, _E, E_STR_ALLOC);
+
+       __attributes.push_back(attr);
+       return true;
+}
+
+JobContext& JobContext::setDisjunction(bool disjunction)
+{
+       __disjunction = disjunction;
+       return *this;
+}
+
+
+JobTrigger::JobTrigger(const std::string& uri) :
+       JobContext(uri)
+{
+}
+
+JobTrigger::~JobTrigger()
+{
+}
+
+
+JobRequirement::JobRequirement(const std::string& uri, bool optional) :
+       JobContext(uri),
+       __optional(optional)
+{
+}
+
+JobRequirement::~JobRequirement()
+{
+}
+
+bool JobRequirement::isOptional()
+{
+       return __optional;
+}
diff --git a/src/shared/JobContext.h b/src/shared/JobContext.h
new file mode 100644 (file)
index 0000000..255bdf3
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_JOB_SCHEDULER_JOB_CONTEXT_H__
+#define __CONTEXT_JOB_SCHEDULER_JOB_CONTEXT_H__
+
+#include <ContextTypes.h>
+#include <string>
+#include <list>
+
+namespace ctx {
+
+       class Attribute;
+
+       class JobContext {
+       public:
+               virtual ~JobContext();
+
+               const std::string& getUri();
+               Attribute* getAttribute(const std::string& attrName);
+               bool prepareAttributeInt(const std::string& attrName);
+               bool prepareAttributeStr(const std::string& attrName);
+
+               /* Legacy support */
+               JobContext& setDisjunction(bool disjunction);
+
+       protected:
+               JobContext(const std::string& uri);
+
+       private:
+               std::string __uri;
+               std::list<Attribute*> __attributes;
+
+               bool __disjunction;
+       };
+
+
+       class JobTrigger : public JobContext {
+       public:
+               JobTrigger(const std::string& uri);
+               ~JobTrigger();
+       };
+
+
+       class JobRequirement : public JobContext {
+       public:
+               JobRequirement(const std::string& uri, bool optional);
+               ~JobRequirement();
+
+               bool isOptional();
+
+       private:
+               bool __optional;
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_JOB_CONTEXT_H__ */
diff --git a/src/shared/JobInfo.cpp b/src/shared/JobInfo.cpp
new file mode 100644 (file)
index 0000000..3e581f4
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "JobInfo.h"
+#include "JobContext.h"
+#include "JobAction.h"
+
+using namespace ctx;
+
+JobInfo::JobInfo() :
+       __jobId(-1),
+       __started(false),
+       __oneTime(false),
+       __persistent(false),
+       __userBundle(NULL),
+       __action(NULL),
+       __disjunction(false)
+{
+}
+
+JobInfo::~JobInfo()
+{
+       for (auto& req : __requirements) {
+               delete req;
+       }
+
+       delete __action;
+
+       if (__userBundle)
+               bundle_free(__userBundle);
+}
+
+int JobInfo::getId()
+{
+       return __jobId;
+}
+
+bool JobInfo::isStarted()
+{
+       return __started;
+}
+
+bundle* JobInfo::getBundle()
+{
+       return __userBundle;
+}
+
+JobInfo& JobInfo::setOneTime(bool oneTime)
+{
+       __oneTime = oneTime;
+       return *this;
+}
+
+JobInfo& JobInfo::setPersistent(bool persistent)
+{
+       __persistent = persistent;
+       return *this;
+}
+
+JobInfo& JobInfo::setBundle(bundle*& userBundle)
+{
+       if (__userBundle)
+               bundle_free(__userBundle);
+
+       __userBundle = userBundle;
+       userBundle = NULL;
+
+       return *this;
+}
+
+JobInfo& JobInfo::addRequirement(JobRequirement* req)
+{
+       if (req)
+               __requirements.push_back(req);
+
+       return *this;
+}
+
+JobInfo& JobInfo::setAction(JobAction* action)
+{
+       if (__action)
+               delete __action;
+
+       __action = action;
+
+       return *this;
+}
+
+JobInfo& JobInfo::setDisjunction(bool disjunction)
+{
+       __disjunction = disjunction;
+       return *this;
+}
+
+
+PeriodicJobInfo::PeriodicJobInfo(unsigned int intervalMs, time_t anchor) :
+       __intervalMs(intervalMs),
+       __anchor(anchor)
+{
+}
+
+PeriodicJobInfo::~PeriodicJobInfo()
+{
+}
+
+
+OnDemandJobInfo::OnDemandJobInfo()
+{
+}
+
+OnDemandJobInfo::~OnDemandJobInfo()
+{
+}
+
+OnDemandJobInfo& OnDemandJobInfo::addTrigger(JobTrigger* trigger)
+{
+       if (trigger)
+               __triggers.push_back(trigger);
+
+       return *this;
+}
diff --git a/src/shared/JobInfo.h b/src/shared/JobInfo.h
new file mode 100644 (file)
index 0000000..91ac825
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_JOB_SCHEDULER_JOB_INFO_H__
+#define __CONTEXT_JOB_SCHEDULER_JOB_INFO_H__
+
+#include <ContextTypes.h>
+#include <list>
+#include <bundle.h>
+
+namespace ctx {
+
+       class JobTrigger;
+       class JobRequirement;
+       class JobAction;
+
+       class JobInfo {
+       public:
+               virtual ~JobInfo();
+
+               int getId();
+               bool isStarted();
+               bundle* getBundle();
+
+               JobInfo& setOneTime(bool oneTime);
+               JobInfo& setPersistent(bool persistent);
+               JobInfo& setBundle(bundle*& userBundle);
+
+               JobInfo& addRequirement(JobRequirement* req);
+               JobInfo& setAction(JobAction* action);
+
+               /* Legacy support */
+               JobInfo& setDisjunction(bool disjunction);
+
+       protected:
+               JobInfo();
+
+       private:
+               int __jobId;
+               bool __started;
+               bool __oneTime;
+               bool __persistent;
+               bundle* __userBundle;
+               std::list<JobRequirement*> __requirements;
+               JobAction* __action;
+
+               /* Legacy support */
+               bool __disjunction;
+       };
+
+
+       class PeriodicJobInfo : public JobInfo {
+       public:
+               PeriodicJobInfo(unsigned int intervalMs, time_t anchor);
+               ~PeriodicJobInfo();
+
+       private:
+               unsigned int __intervalMs;
+               time_t __anchor;
+       };
+
+
+       class OnDemandJobInfo : public JobInfo {
+       public:
+               OnDemandJobInfo();
+               ~OnDemandJobInfo();
+
+               OnDemandJobInfo& addTrigger(JobTrigger* trigger);
+
+       private:
+               std::list<JobTrigger*> __triggers;
+       };
+
+}      /* namespace ctx */
+
+#endif /* __CONTEXT_JOB_SCHEDULER_JOB_INFO_H__ */