Split the implementations of JobRunner & JobState child classes 66/140266/2
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 24 Jul 2017 08:50:16 +0000 (17:50 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 24 Jul 2017 09:29:47 +0000 (18:29 +0900)
Change-Id: I5363406ecf1d00f0b2e185c257895028e548d994
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
19 files changed:
src/server/ActionState.cpp [new file with mode: 0644]
src/server/ActionState.h [new file with mode: 0644]
src/server/ClosingState.cpp [new file with mode: 0644]
src/server/ClosingState.h [new file with mode: 0644]
src/server/JobManager.cpp
src/server/JobRunner.cpp
src/server/JobRunner.h
src/server/JobState.cpp
src/server/JobState.h
src/server/OnDemandJobRunner.cpp [new file with mode: 0644]
src/server/OnDemandJobRunner.h [new file with mode: 0644]
src/server/PeriodicJobRunner.cpp [new file with mode: 0644]
src/server/PeriodicJobRunner.h [new file with mode: 0644]
src/server/ReqVerificationState.cpp [new file with mode: 0644]
src/server/ReqVerificationState.h [new file with mode: 0644]
src/server/TimerStandbyState.cpp [new file with mode: 0644]
src/server/TimerStandbyState.h [new file with mode: 0644]
src/server/TriggerStandbyState.cpp [new file with mode: 0644]
src/server/TriggerStandbyState.h [new file with mode: 0644]

diff --git a/src/server/ActionState.cpp b/src/server/ActionState.cpp
new file mode 100644 (file)
index 0000000..11667e6
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * 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 "ActionState.h"
+
+using namespace ctx;
+
+ActionState::ActionState(JobState* prevState) :
+       JobState(prevState)
+{
+       _ENTER;
+       //TODO: Check whether the timeout expired.
+}
+
+ActionState::~ActionState()
+{
+       _EXIT;
+}
+
+bool ActionState::execute()
+{
+       //TODO: Set a timeout for waiting the client finishes the job.
+       // If the timeout expires, let the client know that the device is about to suspend.
+       // And goto ClosingState.
+       return true;
+}
+
+void ActionState::setClient(IClient* client)
+{
+       //TODO: Notify the client which job has been expired (once).
+       // Notify the client if the timeout already expired (once).
+}
+
+void ActionState::jobFinished()
+{
+       //TODO: cancel the timeout and goto ClosingState.
+}
diff --git a/src/server/ActionState.h b/src/server/ActionState.h
new file mode 100644 (file)
index 0000000..d53ec2d
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * 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_ACTION_STATE_H__
+#define __CONTEXT_JOB_SCHEDULER_ACTION_STATE_H__
+
+#include <WakeLock.h>
+#include "JobState.h"
+
+namespace ctx {
+
+       class ActionState : public JobState {
+       public:
+               ActionState(JobState* prevState);
+               ~ActionState();
+
+               bool execute();
+
+               void setClient(IClient* client);
+               void jobFinished();
+
+       private:
+               WakeLock __wakeLock;
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_ACTION_STATE_H__ */
diff --git a/src/server/ClosingState.cpp b/src/server/ClosingState.cpp
new file mode 100644 (file)
index 0000000..5b5514c
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * 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 "ClosingState.h"
+
+using namespace ctx;
+
+ClosingState::ClosingState(JobRunner* runner) :
+       JobState(runner)
+{
+       _ENTER;
+}
+
+ClosingState::ClosingState(JobState* prevState) :
+       JobState(prevState)
+{
+       _ENTER;
+}
+
+ClosingState::~ClosingState()
+{
+       _EXIT;
+}
+
+bool ClosingState::execute()
+{
+       //TODO: restart or terminate the job.
+       return false;
+}
diff --git a/src/server/ClosingState.h b/src/server/ClosingState.h
new file mode 100644 (file)
index 0000000..ba717d5
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * 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_CLOSING_STATE_H__
+#define __CONTEXT_JOB_SCHEDULER_CLOSING_STATE_H__
+
+#include <WakeLock.h>
+#include "JobState.h"
+
+namespace ctx {
+
+       class ClosingState : public JobState {
+       public:
+               ClosingState(JobRunner* runner);
+               ClosingState(JobState* prevState);
+               ~ClosingState();
+
+               bool execute();
+
+       private:
+               WakeLock __wakeLock;
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_CLOSING_STATE_H__ */
index 1a96d9dfa5984d1983113c995ca5aec0b6a20b77..3b82c056e2beff25589ce8a30d5b8fc56db71a39 100644 (file)
@@ -20,7 +20,8 @@
 #include <JobAction.h>
 #include "ContextManager.h"
 #include "SchedTimer.h"
-#include "JobRunner.h"
+#include "OnDemandJobRunner.h"
+#include "PeriodicJobRunner.h"
 #include "JobManager.h"
 
 using namespace ctx;
index 4feafa94d1ab2bf74405187c92907b9600873638..806424823038583a6ee1b2efaff548af5578d456 100644 (file)
  */
 
 #include <stdexcept>
-#include "SchedTimer.h"
 #include "JobManager.h"
 #include "JobState.h"
 #include "JobRunner.h"
 
 using namespace ctx;
 
-unsigned int PeriodicJobRunner::__activePeriodicJobCnt = 0;
-
 JobRunner::JobRunner(JobManager* mgr, const std::string& owner, JobInfo* info) :
        __jobManager(mgr),
        __jobInfo(info),
@@ -101,94 +98,3 @@ void JobRunner::jobFinished()
        if (__state)
                __state->jobFinished();
 }
-
-
-PeriodicJobRunner::PeriodicJobRunner(JobManager* mgr, const std::string& owner, JobInfo* info) :
-       JobRunner(mgr, owner, info),
-       __lastScheduledTime(0)
-{
-}
-
-PeriodicJobRunner::~PeriodicJobRunner()
-{
-}
-
-void PeriodicJobRunner::start()
-{
-       __jobInfo->setStarted(true);
-       setState(new TimerStandbyState(this));
-
-       __activePeriodicJobCnt++;
-
-       if (__activePeriodicJobCnt == TimerStandbyState::getCount())
-               SchedTimer::reset();
-}
-
-void PeriodicJobRunner::stop()
-{
-       __jobInfo->setStarted(false);
-       setState(NULL);
-
-       __activePeriodicJobCnt--;
-
-       if (__activePeriodicJobCnt == TimerStandbyState::getCount())
-               SchedTimer::reset();
-}
-
-void PeriodicJobRunner::restart()
-{
-       setState(new TimerStandbyState(this));
-
-       if (__activePeriodicJobCnt == TimerStandbyState::getCount())
-               SchedTimer::reset();
-}
-
-void PeriodicJobRunner::terminate()
-{
-       setState(NULL);
-       __jobManager->removeRunner(this);
-
-       __activePeriodicJobCnt--;
-
-       if (__activePeriodicJobCnt == TimerStandbyState::getCount())
-               SchedTimer::reset();
-}
-
-time_t PeriodicJobRunner::getLastTime()
-{
-       return __lastScheduledTime;
-}
-
-void PeriodicJobRunner::setLastTime(time_t t)
-{
-       __lastScheduledTime = t;
-}
-
-
-OnDemandJobRunner::OnDemandJobRunner(JobManager* mgr, const std::string& owner, JobInfo* info) :
-       JobRunner(mgr, owner, info)
-{
-}
-
-void OnDemandJobRunner::start()
-{
-       __jobInfo->setStarted(true);
-       setState(new TriggerStandbyState(this));
-}
-
-void OnDemandJobRunner::stop()
-{
-       __jobInfo->setStarted(false);
-       setState(NULL);
-}
-
-void OnDemandJobRunner::restart()
-{
-       setState(new TriggerStandbyState(this));
-}
-
-void OnDemandJobRunner::terminate()
-{
-       setState(NULL);
-       __jobManager->removeRunner(this);
-}
index 0a3c6fc014d83753e6ea4834dac3a84af8f0b5e5..da0655c729344c8b4bd9be5529c1d98164a8c371 100644 (file)
@@ -58,36 +58,6 @@ namespace ctx {
                JobState* __state;
        };
 
-
-       class PeriodicJobRunner : public JobRunner {
-       public:
-               PeriodicJobRunner(JobManager* mgr, const std::string& owner, JobInfo* info);
-               ~PeriodicJobRunner();
-
-               void start();
-               void stop();
-               void restart();
-               void terminate();
-
-               time_t getLastTime();
-               void setLastTime(time_t t);
-
-       private:
-               time_t __lastScheduledTime;
-               static unsigned int __activePeriodicJobCnt;
-       };
-
-
-       class OnDemandJobRunner : public JobRunner {
-       public:
-               OnDemandJobRunner(JobManager* mgr, const std::string& owner, JobInfo* info);
-
-               void start();
-               void stop();
-               void restart();
-               void terminate();
-       };
-
 }
 
 #endif /* __CONTEXT_JOB_SCHEDULER_JOB_RUNNER_H__ */
index c3b199a39c4050ac15b658b00f2dd7d2ec508ea5..eab13b28da91fce1a68afee9d5492024777579cf 100644 (file)
  * limitations under the License.
  */
 
-#include <Conf.h>
 #include "JobRunner.h"
 #include "JobState.h"
 
-#define ENTER  _I("ENTER: Job-%d of '%s'", getJobId(), getOwner().c_str())
-#define EXIT   _I("EXIT : Job-%d of '%s'", getJobId(), getOwner().c_str())
-
-#define SEC_PER_MIN    60
-
 using namespace ctx;
 
-unsigned int TimerStandbyState::__timerStandbyStateCnt = 0;
-
 JobState::JobState(JobRunner* runner) :
        __jobRunner(runner)
 {
@@ -78,156 +70,3 @@ void JobState::transit(JobState* nextState)
 {
        __jobRunner->setState(nextState);
 }
-
-
-TimerStandbyState::TimerStandbyState(JobRunner* runner) :
-       JobState(runner)
-{
-       ENTER;
-       ++__timerStandbyStateCnt;
-}
-
-TimerStandbyState::~TimerStandbyState()
-{
-       EXIT;
-       --__timerStandbyStateCnt;
-       SchedTimer::remove(this);
-}
-
-bool TimerStandbyState::execute()
-{
-       return __setTimer();
-}
-
-void TimerStandbyState::onSchedTimerExpired()
-{
-       transit(new ReqVerificationState(this));
-}
-
-unsigned int TimerStandbyState::getCount()
-{
-       return __timerStandbyStateCnt;
-}
-
-bool TimerStandbyState::__setTimer()
-{
-       PeriodicJobRunner* runner = static_cast<PeriodicJobRunner*>(getJobRunner());
-       time_t interval = static_cast<time_t>(static_cast<PeriodicJobInfo*>(getJobInfo())->getInterval()) * SEC_PER_MIN;
-
-       time_t lastTime = [&]()->time_t {
-               if (runner->getLastTime() != 0)
-                       return runner->getLastTime();
-
-               time_t now = time(NULL);
-               time_t anchor = static_cast<PeriodicJobInfo*>(getJobInfo())->getAnchor();
-
-               anchor = anchor % interval;
-               return static_cast<time_t>((now - anchor) / interval) * interval + anchor;
-       }();
-
-       time_t nextTime = lastTime + interval;
-       unsigned int margin = static_cast<double>(interval) * CTX_PERIOD_MARGIN;
-
-       runner->setLastTime(nextTime);
-
-       _I("Set a timer at %ld with %us margin", static_cast<long>(nextTime), margin);
-       return SchedTimer::set(this, nextTime, margin);
-}
-
-
-TriggerStandbyState::TriggerStandbyState(JobRunner* runner) :
-       JobState(runner)
-{
-       ENTER;
-}
-
-TriggerStandbyState::~TriggerStandbyState()
-{
-       EXIT;
-}
-
-bool TriggerStandbyState::execute()
-{
-       //TODO: Subscribe the trigger contexts.
-       // If a context expires, verify its attributes, and transit to ReqVerificationState.
-       return true;
-}
-
-
-ReqVerificationState::ReqVerificationState(JobState* prevState) :
-       JobState(prevState)
-{
-       ENTER;
-}
-
-ReqVerificationState::~ReqVerificationState()
-{
-       EXIT;
-}
-
-bool ReqVerificationState::execute()
-{
-       //TODO: Read the requirement contexts.
-       // If all satisfied, goto ActionState.
-       // If the RequirementTimeout == 0, goto restart the machine.
-       // Set the timeout.
-       // If the timeout expires, read the requirements again.
-       // If all mandatory satisfied, goto ActionState, restart otherwise.
-       return true;
-}
-
-
-ActionState::ActionState(JobState* prevState) :
-       JobState(prevState)
-{
-       ENTER;
-       //TODO: Check whether the timeout expired.
-}
-
-ActionState::~ActionState()
-{
-       EXIT;
-}
-
-bool ActionState::execute()
-{
-       //TODO: Set a timeout for waiting the client finishes the job.
-       // If the timeout expires, let the client know that the device is about to suspend.
-       // And goto ClosingState.
-       return true;
-}
-
-void ActionState::setClient(IClient* client)
-{
-       //TODO: Notify the client which job has been expired (once).
-       // Notify the client if the timeout already expired (once).
-}
-
-void ActionState::jobFinished()
-{
-       //TODO: cancel the timeout and goto ClosingState.
-}
-
-
-ClosingState::ClosingState(JobRunner* runner) :
-       JobState(runner)
-{
-       ENTER;
-}
-
-ClosingState::ClosingState(JobState* prevState) :
-       JobState(prevState)
-{
-       ENTER;
-}
-
-ClosingState::~ClosingState()
-{
-       EXIT;
-}
-
-bool ClosingState::execute()
-{
-       //TODO: restart or terminate the job.
-       return false;
-}
index 95872dd3bb6f81ee8c1d863629c550a1f9b9686f..c1998c8d33865e2d9aeb34156c214230b21370a6 100644 (file)
 #define __CONTEXT_JOB_SCHEDULER_JOB_STATE_H__
 
 #include <string>
-#include <WakeLock.h>
 #include <IClient.h>
 #include <JobInfo.h>
-#include "SchedTimer.h"
+
+#define _ENTER _I("ENTER: Job-%d of '%s'", getJobId(), getOwner().c_str())
+#define _EXIT  _I("EXIT : Job-%d of '%s'", getJobId(), getOwner().c_str())
 
 namespace ctx {
 
        class JobRunner;
 
-
        class JobState {
        public:
                virtual ~JobState();
@@ -53,74 +53,6 @@ namespace ctx {
                JobRunner* __jobRunner;
        };
 
-
-       class TimerStandbyState : public JobState, public ISchedTimerListener {
-       public:
-               TimerStandbyState(JobRunner* runner);
-               ~TimerStandbyState();
-
-               bool execute();
-
-               void onSchedTimerExpired();
-
-               static unsigned int getCount();
-
-       private:
-               bool __setTimer();
-
-               time_t __lastExpiredTime;
-               static unsigned int __timerStandbyStateCnt;
-       };
-
-
-       class TriggerStandbyState : public JobState {
-       public:
-               TriggerStandbyState(JobRunner* runner);
-               ~TriggerStandbyState();
-
-               bool execute();
-       };
-
-
-       class ReqVerificationState : public JobState {
-       public:
-               ReqVerificationState(JobState* prevState);
-               ~ReqVerificationState();
-
-               bool execute();
-
-       private:
-               WakeLock __wakeLock;
-       };
-
-
-       class ActionState : public JobState {
-       public:
-               ActionState(JobState* prevState);
-               ~ActionState();
-
-               bool execute();
-
-               void setClient(IClient* client);
-               void jobFinished();
-
-       private:
-               WakeLock __wakeLock;
-       };
-
-
-       class ClosingState : public JobState {
-       public:
-               ClosingState(JobRunner* runner);
-               ClosingState(JobState* prevState);
-               ~ClosingState();
-
-               bool execute();
-
-       private:
-               WakeLock __wakeLock;
-       };
-
 }
 
 #endif /* __CONTEXT_JOB_SCHEDULER_JOB_STATE_H__ */
diff --git a/src/server/OnDemandJobRunner.cpp b/src/server/OnDemandJobRunner.cpp
new file mode 100644 (file)
index 0000000..9d266e4
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * 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 "JobManager.h"
+#include "TriggerStandbyState.h"
+#include "OnDemandJobRunner.h"
+
+using namespace ctx;
+
+OnDemandJobRunner::OnDemandJobRunner(JobManager* mgr, const std::string& owner, JobInfo* info) :
+       JobRunner(mgr, owner, info)
+{
+}
+
+void OnDemandJobRunner::start()
+{
+       __jobInfo->setStarted(true);
+       setState(new TriggerStandbyState(this));
+}
+
+void OnDemandJobRunner::stop()
+{
+       __jobInfo->setStarted(false);
+       setState(NULL);
+}
+
+void OnDemandJobRunner::restart()
+{
+       setState(new TriggerStandbyState(this));
+}
+
+void OnDemandJobRunner::terminate()
+{
+       setState(NULL);
+       __jobManager->removeRunner(this);
+}
diff --git a/src/server/OnDemandJobRunner.h b/src/server/OnDemandJobRunner.h
new file mode 100644 (file)
index 0000000..5ec01f3
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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_ON_DEMAND_JOB_RUNNER_H__
+#define __CONTEXT_JOB_SCHEDULER_ON_DEMAND_JOB_RUNNER_H__
+
+#include "JobRunner.h"
+
+namespace ctx {
+
+       class OnDemandJobRunner : public JobRunner {
+       public:
+               OnDemandJobRunner(JobManager* mgr, const std::string& owner, JobInfo* info);
+
+               void start();
+               void stop();
+               void restart();
+               void terminate();
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_ON_DEMAND_JOB_RUNNER_H__ */
diff --git a/src/server/PeriodicJobRunner.cpp b/src/server/PeriodicJobRunner.cpp
new file mode 100644 (file)
index 0000000..bc6a77e
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * 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 "SchedTimer.h"
+#include "JobManager.h"
+#include "TimerStandbyState.h"
+#include "PeriodicJobRunner.h"
+
+using namespace ctx;
+
+unsigned int PeriodicJobRunner::__activePeriodicJobCnt = 0;
+
+PeriodicJobRunner::PeriodicJobRunner(JobManager* mgr, const std::string& owner, JobInfo* info) :
+       JobRunner(mgr, owner, info),
+       __lastScheduledTime(0)
+{
+}
+
+PeriodicJobRunner::~PeriodicJobRunner()
+{
+}
+
+void PeriodicJobRunner::start()
+{
+       __jobInfo->setStarted(true);
+       setState(new TimerStandbyState(this));
+
+       __activePeriodicJobCnt++;
+
+       if (__activePeriodicJobCnt == TimerStandbyState::getCount())
+               SchedTimer::reset();
+}
+
+void PeriodicJobRunner::stop()
+{
+       __jobInfo->setStarted(false);
+       setState(NULL);
+
+       __activePeriodicJobCnt--;
+
+       if (__activePeriodicJobCnt == TimerStandbyState::getCount())
+               SchedTimer::reset();
+}
+
+void PeriodicJobRunner::restart()
+{
+       setState(new TimerStandbyState(this));
+
+       if (__activePeriodicJobCnt == TimerStandbyState::getCount())
+               SchedTimer::reset();
+}
+
+void PeriodicJobRunner::terminate()
+{
+       setState(NULL);
+       __jobManager->removeRunner(this);
+
+       __activePeriodicJobCnt--;
+
+       if (__activePeriodicJobCnt == TimerStandbyState::getCount())
+               SchedTimer::reset();
+}
+
+time_t PeriodicJobRunner::getLastTime()
+{
+       return __lastScheduledTime;
+}
+
+void PeriodicJobRunner::setLastTime(time_t t)
+{
+       __lastScheduledTime = t;
+}
diff --git a/src/server/PeriodicJobRunner.h b/src/server/PeriodicJobRunner.h
new file mode 100644 (file)
index 0000000..39d21ac
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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_PERIODIC_JOB_RUNNER_H__
+#define __CONTEXT_JOB_SCHEDULER_PERIODIC_JOB_RUNNER_H__
+
+#include "JobRunner.h"
+
+namespace ctx {
+
+       class PeriodicJobRunner : public JobRunner {
+       public:
+               PeriodicJobRunner(JobManager* mgr, const std::string& owner, JobInfo* info);
+               ~PeriodicJobRunner();
+
+               void start();
+               void stop();
+               void restart();
+               void terminate();
+
+               time_t getLastTime();
+               void setLastTime(time_t t);
+
+       private:
+               time_t __lastScheduledTime;
+               static unsigned int __activePeriodicJobCnt;
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_PERIODIC_JOB_RUNNER_H__ */
diff --git a/src/server/ReqVerificationState.cpp b/src/server/ReqVerificationState.cpp
new file mode 100644 (file)
index 0000000..fccee1e
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * 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 "ReqVerificationState.h"
+
+using namespace ctx;
+
+ReqVerificationState::ReqVerificationState(JobState* prevState) :
+       JobState(prevState)
+{
+       _ENTER;
+}
+
+ReqVerificationState::~ReqVerificationState()
+{
+       _EXIT;
+}
+
+bool ReqVerificationState::execute()
+{
+       //TODO: Read the requirement contexts.
+       // If all satisfied, goto ActionState.
+       // If the RequirementTimeout == 0, goto restart the machine.
+       // Set the timeout.
+       // If the timeout expires, read the requirements again.
+       // If all mandatory satisfied, goto ActionState, restart otherwise.
+       return true;
+}
diff --git a/src/server/ReqVerificationState.h b/src/server/ReqVerificationState.h
new file mode 100644 (file)
index 0000000..4919866
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * 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_REQ_VERIFICATION_STATE_H__
+#define __CONTEXT_JOB_SCHEDULER_REQ_VERIFICATION_STATE_H__
+
+#include <WakeLock.h>
+#include "JobState.h"
+
+namespace ctx {
+
+       class ReqVerificationState : public JobState {
+       public:
+               ReqVerificationState(JobState* prevState);
+               ~ReqVerificationState();
+
+               bool execute();
+
+       private:
+               WakeLock __wakeLock;
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_REQ_VERIFICATION_STATE_H__ */
diff --git a/src/server/TimerStandbyState.cpp b/src/server/TimerStandbyState.cpp
new file mode 100644 (file)
index 0000000..ae3df63
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * 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 <Conf.h>
+#include "PeriodicJobRunner.h"
+#include "ReqVerificationState.h"
+#include "TimerStandbyState.h"
+
+#define SEC_PER_MIN    60
+
+using namespace ctx;
+
+unsigned int TimerStandbyState::__timerStandbyStateCnt = 0;
+
+TimerStandbyState::TimerStandbyState(JobRunner* runner) :
+       JobState(runner)
+{
+       _ENTER;
+       ++__timerStandbyStateCnt;
+}
+
+TimerStandbyState::~TimerStandbyState()
+{
+       _EXIT;
+       --__timerStandbyStateCnt;
+       SchedTimer::remove(this);
+}
+
+bool TimerStandbyState::execute()
+{
+       return __setTimer();
+}
+
+void TimerStandbyState::onSchedTimerExpired()
+{
+       transit(new ReqVerificationState(this));
+}
+
+unsigned int TimerStandbyState::getCount()
+{
+       return __timerStandbyStateCnt;
+}
+
+bool TimerStandbyState::__setTimer()
+{
+       PeriodicJobRunner* runner = static_cast<PeriodicJobRunner*>(getJobRunner());
+       time_t interval = static_cast<time_t>(static_cast<PeriodicJobInfo*>(getJobInfo())->getInterval()) * SEC_PER_MIN;
+
+       time_t lastTime = [&]()->time_t {
+               if (runner->getLastTime() != 0)
+                       return runner->getLastTime();
+
+               time_t now = time(NULL);
+               time_t anchor = static_cast<PeriodicJobInfo*>(getJobInfo())->getAnchor();
+
+               anchor = anchor % interval;
+               return static_cast<time_t>((now - anchor) / interval) * interval + anchor;
+       }();
+
+       time_t nextTime = lastTime + interval;
+       unsigned int margin = static_cast<double>(interval) * CTX_PERIOD_MARGIN;
+
+       runner->setLastTime(nextTime);
+
+       _I("Set a timer at %ld with %us margin", static_cast<long>(nextTime), margin);
+       return SchedTimer::set(this, nextTime, margin);
+}
diff --git a/src/server/TimerStandbyState.h b/src/server/TimerStandbyState.h
new file mode 100644 (file)
index 0000000..0835213
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * 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_TIMER_STANDBY_STATE_H__
+#define __CONTEXT_JOB_SCHEDULER_TIMER_STANDBY_STATE_H__
+
+#include <WakeLock.h>
+#include "SchedTimer.h"
+#include "JobState.h"
+
+namespace ctx {
+
+       class TimerStandbyState : public JobState, public ISchedTimerListener {
+       public:
+               TimerStandbyState(JobRunner* runner);
+               ~TimerStandbyState();
+
+               bool execute();
+
+               void onSchedTimerExpired();
+
+               static unsigned int getCount();
+
+       private:
+               bool __setTimer();
+
+               time_t __lastExpiredTime;
+               static unsigned int __timerStandbyStateCnt;
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_TIMER_STANDBY_STATE_H__ */
diff --git a/src/server/TriggerStandbyState.cpp b/src/server/TriggerStandbyState.cpp
new file mode 100644 (file)
index 0000000..ff681cb
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * 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 "TriggerStandbyState.h"
+
+using namespace ctx;
+
+TriggerStandbyState::TriggerStandbyState(JobRunner* runner) :
+       JobState(runner)
+{
+       _ENTER;
+}
+
+TriggerStandbyState::~TriggerStandbyState()
+{
+       _EXIT;
+}
+
+bool TriggerStandbyState::execute()
+{
+       //TODO: Subscribe the trigger contexts.
+       // If a context expires, verify its attributes, and transit to ReqVerificationState.
+       return true;
+}
diff --git a/src/server/TriggerStandbyState.h b/src/server/TriggerStandbyState.h
new file mode 100644 (file)
index 0000000..3d0e42f
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * 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_TRIGGER_STANDBY_STATE_H__
+#define __CONTEXT_JOB_SCHEDULER_TRIGGER_STANDBY_STATE_H__
+
+#include <WakeLock.h>
+#include "JobState.h"
+
+namespace ctx {
+
+       class TriggerStandbyState : public JobState {
+       public:
+               TriggerStandbyState(JobRunner* runner);
+               ~TriggerStandbyState();
+
+               bool execute();
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_TRIGGER_STANDBY_STATE_H__ */