forked from cawka/ndn.cxx
diff --git a/scheduler/task.cc b/scheduler/task.cc
new file mode 100644
index 0000000..fa73490
--- /dev/null
+++ b/scheduler/task.cc
@@ -0,0 +1,74 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ * Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "task.h"
+#include "scheduler.h"
+
+static void
+eventCallback(evutil_socket_t fd, short what, void *arg)
+{
+ Task *task = static_cast<Task *>(arg);
+ task->execute();
+ task = NULL;
+}
+
+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_event = evtimer_new(scheduler->base(), eventCallback, this);
+ m_tv = new timeval;
+}
+
+Task::~Task()
+{
+ if (m_event != NULL)
+ {
+ event_free(m_event);
+ m_event = NULL;
+ }
+
+ if (m_tv != NULL)
+ {
+ delete m_tv;
+ m_tv = NULL;
+ }
+}
+
+void
+Task::setTv(double delay)
+{
+ // Alex: when using abs function, i would recommend use it with std:: prefix, otherwise
+ // the standard one may be used, which converts everything to INT, making a lot of problems
+ double intPart, fraction;
+ fraction = modf(std::abs(delay), &intPart);
+
+ m_tv->tv_sec = static_cast<int>(intPart);
+ m_tv->tv_usec = static_cast<int>((fraction * 1000000));
+}
+
+void
+Task::execute()
+{
+ // m_scheduler->execute(boost::bind(&Task::run, this));
+
+ // using a shared_ptr of this to ensure that when invoked from executor
+ // the task object still exists
+ // otherwise, it could be the case that the run() is to be executed, but before it
+ // could finish, the TaskPtr gets deleted from scheduler and the task object
+ // gets destroyed, causing crash
+ m_scheduler->execute(boost::bind(&Task::run, shared_from_this()));
+}