Adding two small helpers to scheduler
diff --git a/scheduler/scheduler.cc b/scheduler/scheduler.cc
index 2806075..ee6a124 100644
--- a/scheduler/scheduler.cc
+++ b/scheduler/scheduler.cc
@@ -20,9 +20,14 @@
*/
#include "scheduler.h"
+#include "one-time-task.h"
+#include "periodic-task.h"
+
#include <utility>
+#include <boost/make_shared.hpp>
using namespace std;
+using namespace boost;
#define EVLOOP_NO_EXIT_ON_EMPTY 0x04
@@ -91,24 +96,45 @@
m_thread.join();
}
-bool
-Scheduler::addTask(const TaskPtr &task)
+TaskPtr
+Scheduler::scheduleOneTimeTask (SchedulerPtr scheduler, double delay,
+ const Task::Callback &callback, const Task::Tag &tag)
{
- TaskPtr newTask = task;
+ TaskPtr task = make_shared<OneTimeTask> (callback, tag, scheduler, delay);
+ if (scheduler->addTask (task))
+ return task;
+ else
+ return TaskPtr ();
+}
+TaskPtr
+Scheduler::schedulePeriodicTask (SchedulerPtr scheduler, IntervalGeneratorPtr delayGenerator,
+ const Task::Callback &callback, const Task::Tag &tag)
+{
+ TaskPtr task = make_shared<PeriodicTask> (callback, tag, scheduler, delayGenerator);
+
+ if (scheduler->addTask (task))
+ return task;
+ else
+ return TaskPtr ();
+}
+
+bool
+Scheduler::addTask(TaskPtr newTask)
+{
if (addToMap(newTask))
{
newTask->reset();
int res = evtimer_add(newTask->ev(), newTask->tv());
if (res < 0)
{
- cout << "evtimer_add failed for " << task->tag() << endl;
+ cout << "evtimer_add failed for " << newTask->tag() << endl;
}
return true;
}
else
{
- cout << "fail to add task: " << task->tag() << endl;
+ cout << "fail to add task: " << newTask->tag() << endl;
}
return false;
diff --git a/scheduler/scheduler.h b/scheduler/scheduler.h
index 83b9926..e783cf0 100644
--- a/scheduler/scheduler.h
+++ b/scheduler/scheduler.h
@@ -38,6 +38,10 @@
#include <sys/time.h>
#include "task.h"
+#include "interval-generator.h"
+
+class Scheduler;
+typedef boost::shared_ptr<Scheduler> SchedulerPtr;
/**
* @brief Scheduler class
@@ -56,9 +60,18 @@
virtual void
shutdown();
+ // helper method to schedule one-time task
+ static TaskPtr
+ scheduleOneTimeTask (SchedulerPtr scheduler, double delay, const Task::Callback &callback, const Task::Tag &tag);
+
+ // helper method to schedule periodic task
+ static TaskPtr
+ schedulePeriodicTask (SchedulerPtr scheduler, IntervalGeneratorPtr delayGenerator,
+ const Task::Callback &callback, const Task::Tag &tag);
+
// if task with the same tag exists, the task is not added and return false
virtual bool
- addTask(const TaskPtr &task);
+ addTask(TaskPtr task);
// delete task by task->tag, regardless of whether it's invoked or not
virtual void
@@ -117,9 +130,6 @@
boost::thread m_thread;
};
-class Scheduler;
-typedef boost::shared_ptr<Scheduler> SchedulerPtr;
-
struct SchedulerException : virtual boost::exception, virtual std::exception { };
#endif // SCHEDULER_H
diff --git a/src/fetch-manager.cc b/src/fetch-manager.cc
index ba24f97..b0154ea 100644
--- a/src/fetch-manager.cc
+++ b/src/fetch-manager.cc
@@ -32,15 +32,29 @@
: m_ccnx (ccnx)
, m_sync (sync)
{
- m_scheduler.start ();
+ m_scheduler = make_shared<Scheduler> ();
+ m_scheduler->start ();
}
FetchManager::~FetchManager ()
{
- m_scheduler.shutdown ();
+ m_scheduler->shutdown ();
+ m_scheduler.reset ();
}
void
FetchManager::Enqueue (const Ccnx::Name &deviceName, uint32_t minSeqNo, uint32_t maxSeqNo, int priority/*=PRIORITY_NORMAL*/)
{
}
+
+Ccnx::CcnxWrapperPtr
+FetchManager::GetCcnx ()
+{
+ return m_ccnx;
+}
+
+SchedulerPtr
+FetchManager::GetScheduler ()
+{
+ return m_scheduler;
+}
diff --git a/src/fetch-manager.h b/src/fetch-manager.h
index 5efd838..eb38551 100644
--- a/src/fetch-manager.h
+++ b/src/fetch-manager.h
@@ -45,12 +45,19 @@
virtual ~FetchManager ();
void
- Enqueue (const Ccnx::Name &deviceName, uint32_t minSeqNo, uint32_t maxSeqNo, int priority=PRIORITY_NORMAL);
+ Enqueue (const Ccnx::Name &deviceName,
+ uint32_t minSeqNo, uint32_t maxSeqNo, int priority=PRIORITY_NORMAL);
+
+ Ccnx::CcnxWrapperPtr
+ GetCcnx ();
+
+ SchedulerPtr
+ GetScheduler ();
private:
Ccnx::CcnxWrapperPtr m_ccnx;
SyncLogPtr m_sync; // to access forwarding hints
- Scheduler m_scheduler;
+ SchedulerPtr m_scheduler;
};