Add an API function to duplicate job context handle 48/142248/4
authorMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 3 Aug 2017 07:03:27 +0000 (16:03 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 3 Aug 2017 07:59:36 +0000 (07:59 +0000)
Change-Id: If538c2fb6746f5811e393ae7fa837e92186f5a28
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/job_scheduler_internal.h
src/client-dummy/job_scheduler.cpp
src/client/job_scheduler.cpp
src/shared/Attribute.cpp
src/shared/Attribute.h
src/shared/JobContext.cpp
src/shared/JobContext.h

index b6b0d783cf5d140bed2e85fd56b8b28f424003c7..9a46bb49dc60e8b26cc3508e235b74d9e8fe0460 100644 (file)
@@ -511,6 +511,8 @@ int ctx_sched_custom_register(ctx_sched_h scheduler, const char* uri);
 int ctx_sched_custom_unregister(ctx_sched_h scheduler, const char* uri);
 int ctx_sched_custom_is_registered(ctx_sched_h scheduler, const char* uri, const char* pkg_id, bool* registered);
 
+// Job context handler duplicator
+ctx_sched_job_context_h ctx_sched_job_context_duplicate(ctx_sched_job_context_h job_context);
 
 /**
  * @}
index cf71d3f59457cc7184369b79210eea83c6799340..f91f80133340deb588daf7eb36731a6feafee947 100644 (file)
@@ -281,3 +281,8 @@ EXPORT_API int ctx_sched_custom_is_registered(ctx_sched_h scheduler, const char*
 {
        return E_SUPPORT;
 }
+
+EXPORT_API ctx_sched_job_context_h ctx_sched_job_context_duplicate(ctx_sched_job_context_h job_context)
+{
+       return NULL;
+}
index fc58284243e03e6744323deede767759eb4678d8..a53e21088658a72433c6ac5d88a24176743d9e33 100644 (file)
@@ -689,3 +689,27 @@ EXPORT_API int ctx_sched_custom_is_registered(ctx_sched_h scheduler, const char*
 
        return E_NONE;
 }
+
+EXPORT_API ctx_sched_job_context_h ctx_sched_job_context_duplicate(ctx_sched_job_context_h job_context)
+{
+       IF_FAIL_RETURN(job_context, NULL);
+
+       ctx_sched_job_context_s* dup = new(std::nothrow) ctx_sched_job_context_s();
+       IF_FAIL_RETURN_TAG(dup, NULL, _E, E_STR_ALLOC);
+
+       if (!job_context->jobContext)
+               return dup;
+
+       if (job_context->jobContext->getType() == JobContext::Type::TRIGGER)
+               dup->jobContext = new(std::nothrow) JobTrigger(*static_cast<JobTrigger*>(job_context->jobContext));
+       else
+               dup->jobContext = new(std::nothrow) JobRequirement(*static_cast<JobRequirement*>(job_context->jobContext));
+
+       if (!dup->jobContext) {
+               _E_ALLOC;
+               delete dup;
+               return NULL;
+       }
+
+       return dup;
+}
index aafc6b6e8cc6daa8b7d1049e8066f4d477e34b4f..b7a36d911687808ee9528513380f2e7edb2be3b3 100644 (file)
@@ -55,6 +55,23 @@ Attribute* Attribute::build(const std::string& name, Json::Value& jsonNode)
        return NULL;
 }
 
+Attribute* Attribute::build(const Attribute& other)
+{
+       Attribute::Type type = other.getType();
+
+       if (type == Attribute::Type::INTEGER) {
+               Attribute* dup = new IntegerAttribute(other.getName());
+               *dup = other;
+               return dup;
+
+       } else if (type == Attribute::Type::STRING) {
+               Attribute* dup = new StringAttribute(other.getName());
+               *dup = other;
+               return dup;
+       }
+
+       return NULL;
+}
 
 IntegerAttribute::IntegerAttribute(const std::string& name) :
        Attribute(name),
index e774b3a09c4f4fca944d5b105fe1cbf3678bec56..9835132b754cd81fa394ed8e4ed22c556a2d880c 100644 (file)
@@ -45,6 +45,7 @@ namespace ctx {
                virtual bool operator<=(const std::string& antecedent) = 0;
 
                static Attribute* build(const std::string& name, Json::Value& jsonNode);
+               static Attribute* build(const Attribute& other);
 
        protected:
                Attribute(const std::string& name);
index 39a26c8823fba1ed7a715c4632f9ef97c0c7e937..6442b1cccbc1ba462bfd71f9e290a7f8a39050a9 100644 (file)
 
 using namespace ctx;
 
+static void __dup_attributes(std::list<Attribute*>& dest, std::list<Attribute*>& source)
+{
+       for (auto& attr : dest) {
+               delete attr;
+       }
+
+       dest.clear();
+
+       for (auto& attr : source) {
+               Attribute* dup = Attribute::build(*attr);
+               if (dup)
+                       dest.push_back(dup);
+       }
+}
+
 JobContext::JobContext(const std::string& uri) :
        __uri(uri),
        __disjunction(false)
@@ -37,12 +52,20 @@ JobContext::JobContext(const std::string& uri, Json::Value& jsonNode) :
        Json::Value::Members members = attrNode.getMemberNames();
 
        for (auto& name : members) {
-               __attributes.push_back(Attribute::build(name, attrNode[name]));
+               Attribute* attr = Attribute::build(name, attrNode[name]);
+               if (attr)
+                       __attributes.push_back(attr);
        }
 
        __disjunction = jsonNode[KEY_DISJUNCTION].asBool();
 }
 
+JobContext::JobContext(JobContext& other) :
+       __uri(other.__uri),
+       __disjunction(other.__disjunction)
+{
+}
+
 JobContext::~JobContext()
 {
        for (auto& attr : __attributes) {
@@ -156,6 +179,12 @@ JobTrigger::JobTrigger(const std::string& uri, Json::Value& jsonNode) :
 {
 }
 
+JobTrigger::JobTrigger(JobTrigger& other) :
+       JobContext(static_cast<JobContext&>(other))
+{
+       __dup_attributes(__attributes, other.__attributes);
+}
+
 JobTrigger::~JobTrigger()
 {
 }
@@ -182,6 +211,13 @@ JobRequirement::JobRequirement(const std::string& uri, Json::Value& jsonNode) :
        __optional = jsonNode[KEY_OPTIONAL].asBool();
 }
 
+JobRequirement::JobRequirement(JobRequirement& other) :
+       JobContext(static_cast<JobContext&>(other)),
+       __optional(other.__optional)
+{
+       __dup_attributes(__attributes, other.__attributes);
+}
+
 JobRequirement::~JobRequirement()
 {
 }
index 94986b91474faca8a2f6176c848ecaaa902c1cca..09dfa61755b796843c1bf2c23c599e2440aa1851 100644 (file)
@@ -57,14 +57,15 @@ namespace ctx {
        protected:
                JobContext(const std::string& uri);
                JobContext(const std::string& uri, Json::Value& jsonNode);
-
-       private:
-               virtual void __toJson(Json::Value& jsonNode) const = 0;
+               JobContext(JobContext& other);
 
                std::string __uri;
                std::list<Attribute*> __attributes;
 
                bool __disjunction;
+
+       private:
+               virtual void __toJson(Json::Value& jsonNode) const = 0;
        };
 
 
@@ -72,6 +73,7 @@ namespace ctx {
        public:
                JobTrigger(const std::string& uri);
                JobTrigger(const std::string& uri, Json::Value& jsonNode);
+               JobTrigger(JobTrigger& other);
                ~JobTrigger();
 
                JobContext::Type getType() const;
@@ -85,6 +87,7 @@ namespace ctx {
        public:
                JobRequirement(const std::string& uri, bool optional);
                JobRequirement(const std::string& uri, Json::Value& jsonNode);
+               JobRequirement(JobRequirement& other);
                ~JobRequirement();
 
                JobContext::Type getType() const;