JobManager prevents the same client to register the same job twice 98/138098/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 11 Jul 2017 05:29:57 +0000 (14:29 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 11 Jul 2017 05:29:57 +0000 (14:29 +0900)
If a client tries to register the same job, the existing job's ID is returned.

Change-Id: Iaf223ff12b3ece29c7afb31e1f01d5b9a748a4e0
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/server/JobManager.cpp
src/server/JobManager.h
src/shared/JobInfo.cpp
src/shared/JobInfo.h

index 958447be32ca2afa079b539fed893cfa055b81df..69a5c3bd8f42820cbfce773de61b2f662ab573f0 100644 (file)
@@ -29,7 +29,6 @@ JobManager::JobManager(uid_t uid) :
 {
        _I("Initialize JobManager-%u", static_cast<unsigned>(__uid));
        __restore();
-       __dump();
 }
 
 JobManager::~JobManager()
@@ -50,7 +49,12 @@ int JobManager::addJob(JobInfo* jobInfo, IClient* owner)
 
        int jobId = 0;
 
-       //TODO: duplication test
+       JobInfo* duplicate = __getDuplicate(owner->getName(), jobInfo);
+       if (duplicate) {
+               _W("Duplicate found: Job-%d", duplicate->getId());
+               delete jobInfo;
+               return duplicate->getId();
+       }
 
        if (jobInfo->getType() == JobInfo::Type::PERIODIC) {
                jobId = __addPeriodicJob(static_cast<PeriodicJobInfo*>(jobInfo), owner);
@@ -94,7 +98,7 @@ int JobManager::__addOnDemandJob(OnDemandJobInfo* jobInfo, IClient* owner)
 
 int JobManager::startJob(int jobId, IClient* owner)
 {
-       JobRunner* jobRunner = __findRunner(owner->getName(), jobId);
+       JobRunner* jobRunner = __getRunner(owner->getName(), jobId);
        IF_FAIL_RETURN(jobRunner, E_PARAM);
 
        if (jobRunner->isStarted()) {
@@ -112,7 +116,7 @@ int JobManager::startJob(int jobId, IClient* owner)
 
 int JobManager::stopJob(int jobId, IClient* owner)
 {
-       JobRunner* jobRunner = __findRunner(owner->getName(), jobId);
+       JobRunner* jobRunner = __getRunner(owner->getName(), jobId);
        IF_FAIL_RETURN(jobRunner, E_PARAM);
 
        if (!jobRunner->isStarted()) {
@@ -130,7 +134,7 @@ int JobManager::stopJob(int jobId, IClient* owner)
 
 int JobManager::removeJob(int jobId, IClient* owner)
 {
-       JobRunner* runner = __findRunner(owner->getName(), jobId);
+       JobRunner* runner = __getRunner(owner->getName(), jobId);
        IF_FAIL_RETURN_TAG(runner, E_PARAM, _W, "Not found");
 
        if (runner->isStarted())
@@ -146,7 +150,7 @@ int JobManager::removeJob(int jobId, IClient* owner)
 
 JobInfo* JobManager::getJobInfo(int jobId, IClient* owner)
 {
-       JobRunner* jobRunner = __findRunner(owner->getName(), jobId);
+       JobRunner* jobRunner = __getRunner(owner->getName(), jobId);
        IF_FAIL_RETURN(jobRunner, NULL);
 
        return jobRunner->getJobInfo();
@@ -165,6 +169,21 @@ std::vector<JobInfo*> JobManager::getAllJobInfo(IClient* owner)
        return jobInfos;
 }
 
+JobInfo* JobManager::__getDuplicate(const std::string& ownerId, JobInfo* target)
+{
+       for (auto* runner : __jobRunners) {
+               if (runner->getOwner() != ownerId)
+                       continue;
+
+               if (*(runner->getJobInfo()) != *target)
+                       continue;
+
+               return runner->getJobInfo();
+       }
+
+       return NULL;
+}
+
 bool JobManager::__isPermitted(IClient* client, const std::string& uri)
 {
        //TODO: permission check
@@ -254,6 +273,8 @@ void JobManager::__verifyAction(JobInfo* jobInfo, IClient* owner)
        throw static_cast<int>(E_PARAM);
 }
 
+
+
 int JobManager::__generateJobId()
 {
        if (++__lastJobId < 0)
@@ -275,10 +296,10 @@ bool JobManager::__removeRunner(JobRunner* runner)
        return true;
 }
 
-JobRunner* JobManager::__findRunner(const std::string& ownerId, int jobId)
+JobRunner* JobManager::__getRunner(const std::string& ownerId, int jobId)
 {
        for (auto& runner : __jobRunners) {
-               if (runner->getJobId() == jobId)
+               if (runner->getJobId() == jobId && runner->getOwner() == ownerId)
                        return runner;
        }
 
@@ -318,10 +339,3 @@ void JobManager::__release()
 
        __jobRunners.clear();
 }
-
-void JobManager::__dump()
-{
-       for (auto& runner : __jobRunners) {
-               _D("%s : %d", runner->getOwner().c_str(), runner->getJobId());
-       }
-}
index acf9f38bb2b460ac359a01a88a59aeb8270b73cf..2962995b10746e5eb9232b0e1b3cbde4f825f607 100644 (file)
@@ -46,6 +46,8 @@ namespace ctx {
                int __addPeriodicJob(PeriodicJobInfo* jobInfo, IClient* owner);
                int __addOnDemandJob(OnDemandJobInfo* jobInfo, IClient* owner);
 
+               JobInfo* __getDuplicate(const std::string& onwerId, JobInfo* target);
+
                bool __isPermitted(IClient* client, const std::string& uri);
 
                void __verifyPeriodicJob(PeriodicJobInfo* jobInfo, IClient* owner);
@@ -58,12 +60,11 @@ namespace ctx {
 
                unsigned int __addRunner(JobRunner* runner);
                bool __removeRunner(JobRunner* runner);
-               JobRunner* __findRunner(const std::string& ownerId, int jobId);
-               std::vector<JobRunner*> __findRunners(const std::string& ownerId);
+               JobRunner* __getRunner(const std::string& ownerId, int jobId);
+               std::vector<JobRunner*> __getRunners(const std::string& ownerId);
 
                void __restore();
                void __release();
-               void __dump();
 
                uid_t __uid;
                std::list<JobRunner*> __jobRunners;
index e58ad2ac21c815c6c07d2ca977076a9c83434f10..1896a0026e7245365981dc3739da2ee5da8975f9 100644 (file)
@@ -213,8 +213,24 @@ void JobInfo::toJson(Json::Value& jsonRoot) const
 
 bool JobInfo::operator==(const JobInfo& rhs) const
 {
-       //TODO: compare two JobInfo, but ignore jobId & started flag
-       return false;
+       Json::Value self;
+       Json::Value opponent;
+
+       this->toJson(self);
+       rhs.toJson(opponent);
+
+       self.removeMember(KEY_JOB_ID);
+       self.removeMember(KEY_STARTED);
+
+       opponent.removeMember(KEY_JOB_ID);
+       opponent.removeMember(KEY_STARTED);
+
+       return (self == opponent);
+}
+
+bool JobInfo::operator!=(const JobInfo& rhs) const
+{
+       return !operator==(rhs);
 }
 
 bool JobInfo::isDisjunction() const
index 3663c30e752254d5061ca46f7b10128169b87ad7..dbe661b7cc9fa26a480aa09189fe6cbe891a614b 100644 (file)
@@ -74,6 +74,7 @@ namespace ctx {
                void toJson(Json::Value& jsonRoot) const;
 
                bool operator==(const JobInfo& rhs) const;
+               bool operator!=(const JobInfo& rhs) const;
 
                /* Legacy support */
                bool isDisjunction() const;