refactor class Task
diff --git a/src/event-scheduler.cpp b/src/event-scheduler.cpp
index 77b86d2..be67aad 100644
--- a/src/event-scheduler.cpp
+++ b/src/event-scheduler.cpp
@@ -14,14 +14,13 @@
task = NULL;
}
-Task::Task(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, const IntervalGeneratorPtr &generator)
+Task::Task(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler)
: m_callback(callback)
, m_tag(tag)
, m_scheduler(scheduler)
, m_invoked(false)
, m_event(NULL)
, m_tv(NULL)
- , m_generator(generator)
{
m_event = evtimer_new(scheduler->base(), eventCallback, this);
m_tv = new timeval;
@@ -43,42 +42,6 @@
}
void
-Task::run()
-{
- if (!m_invoked)
- {
- m_callback();
- m_invoked = true;
- }
- if (isPeriodic())
- {
- reset();
- m_scheduler->rescheduleTask(m_tag);
- }
- else
- {
- selfClean();
- }
-}
-
-void
-Task::selfClean()
-{
- m_scheduler->deleteTask(m_tag);
-}
-
-void
-Task::reset()
-{
- m_invoked = false;
- if (isPeriodic())
- {
- double interval = m_generator->nextInterval();
- setTv(interval);
- }
-}
-
-void
Task::setTv(double delay)
{
double intPart, fraction;
@@ -87,6 +50,60 @@
m_tv->tv_usec = static_cast<int>((fraction * 1000000));
}
+OneTimeTask::OneTimeTask(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, double delay)
+ : Task(callback, tag, scheduler)
+{
+ setTv(delay);
+}
+
+void
+OneTimeTask::run()
+{
+ if (!m_invoked)
+ {
+ m_callback();
+ m_invoked = true;
+ deregisterSelf();
+ }
+}
+
+void
+OneTimeTask::deregisterSelf()
+{
+ m_scheduler->deleteTask(m_tag);
+}
+
+void
+OneTimeTask::reset()
+{
+ m_invoked = false;
+}
+
+PeriodicTask::PeriodicTask(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, const IntervalGeneratorPtr &generator)
+ : Task(callback, tag, scheduler)
+ , m_generator(generator)
+{
+}
+
+void
+PeriodicTask::run()
+{
+ if (!m_invoked)
+ {
+ m_callback();
+ m_invoked = true;
+ m_scheduler->rescheduleTask(m_tag);
+ }
+}
+
+void
+PeriodicTask::reset()
+{
+ m_invoked = false;
+ double interval = m_generator->nextInterval();
+ setTv(interval);
+}
+
RandomIntervalGenerator::RandomIntervalGenerator(double interval, double percent, Direction direction)
: m_interval(interval)
, m_rng(time(NULL))
@@ -144,26 +161,9 @@
}
bool
-Scheduler::addTask(const TaskPtr &task, double delay)
-{
- TaskPtr newTask = task;
- newTask->setTv(delay);
- if (addToMap(newTask))
- {
- evtimer_add(newTask->ev(), newTask->tv());
- return true;
- }
- return false;
-}
-
-bool
Scheduler::addTask(const TaskPtr &task)
{
TaskPtr newTask = task;
- if (!newTask->isPeriodic())
- {
- return false;
- }
if (addToMap(newTask))
{
@@ -183,10 +183,8 @@
if (it != m_taskMap.end())
{
TaskPtr task = it->second;
- if (task->isPeriodic())
- {
- evtimer_add(task->ev(), task->tv());
- }
+ task->reset();
+ evtimer_add(task->ev(), task->tv());
}
}
diff --git a/src/sync-core.h b/src/sync-core.h
index 97d559d..7e787d3 100644
--- a/src/sync-core.h
+++ b/src/sync-core.h
@@ -24,6 +24,7 @@
#include "sync-log.h"
#include <ccnx-wrapper.h>
+#include <event-scheduler.h>
using namespace std;
using namespace Ccnx;
@@ -31,12 +32,13 @@
class SyncCore
{
public:
- SyncCore(const string &path, const Name &localName, CcnxWrapperPtr handle);
+ SyncCore(const string &path, const Name &localName, CcnxWrapperPtr handle, SchedulerPtr scheduler);
~SyncCore();
protected:
SyncLog m_log;
CcnxWrapperPtr m_handle;
+ SchedulerPtr m_scheduler;
};
#endif // SYNC_CORE_H