event-scheduler passes tests
diff --git a/include/event-scheduler.h b/include/event-scheduler.h
index 72169d8..b2aa929 100644
--- a/include/event-scheduler.h
+++ b/include/event-scheduler.h
@@ -1,7 +1,9 @@
 #ifndef EVENT_SCHEDULER_H
 #define EVENT_SCHEDULER_H
+
+// use pthread
+
 #include <event2/event.h>
-#include <event2/event_struct.h>
 #include <event2/thread.h>
 
 #include <boost/function.hpp>
@@ -10,8 +12,11 @@
 #include <boost/random/uniform_real.hpp>
 #include <boost/random/variate_generator.hpp>
 #include <boost/exception/all.hpp>
+#include <boost/thread/shared_mutex.hpp>
+#include <boost/thread/thread.hpp>
 #include <math.h>
-#include <multimap>
+#include <map>
+#include <sys/time.h>
 
 #define _OVERRIDE
 #ifdef __GNUC__
@@ -23,36 +28,72 @@
 
 using namespace std;
 
+static void
+eventCallback(evutil_socket_t fd, short what, void *arg);
+
+class Scheduler;
+typedef boost::shared_ptr<Scheduler> SchedulerPtr;
+class IntervalGenerator;
+typedef boost::shared_ptr<IntervalGenerator> IntervalGeneratorPtr;
+class Task;
+typedef boost::shared_ptr<Task> TaskPtr;
+
+class IntervalGenerator
+{
+public:
+  virtual double
+  nextInterval() = 0;
+  static IntervalGeneratorPtr Null;
+};
 
 class Task
 {
 public:
-  typedef boost::function<void (void *)> Callback;
+  typedef boost::function<void ()> Callback;
   typedef string Tag;
-  typedef boost::function<bool (const Task &task)> TaskMatcher;
+  typedef boost::function<bool (const TaskPtr &task)> TaskMatcher;
 
-  Task(const Callback &callback, void *arg, const Tag &tag);
-  Task(const Task &other);
-  Task &
-  operator=(const Task &other);
+  Task(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, const IntervalGeneratorPtr &generator = IntervalGenerator::Null);
+  ~Task();
 
   virtual void
-  run() { m_callback(m_arg); }
+  run();
 
   Tag
   tag() { return m_tag; }
 
+  event *
+  ev() { return m_event; }
+
+  timeval *
+  tv() { return m_tv; }
+
+  void
+  setTv(double delay);
+
+  bool
+  isPeriodic() { return m_generator != IntervalGenerator::Null; }
+
+  void
+  reset();
+
+protected:
+  void
+  selfClean();
 
 protected:
   Callback m_callback;
   Tag m_tag;
-  void *arg;
+  SchedulerPtr m_scheduler;
+  bool m_invoked;
+  event *m_event;
+  timeval *m_tv;
+  IntervalGeneratorPtr m_generator;
 };
 
+
 struct SchedulerException : virtual boost::exception, virtual exception { };
 
-class IntervalGenerator;
-typedef boost::shared_ptr<IntervalGenerator> IntervalGeneratorPtr;
 class Scheduler
 {
 public:
@@ -65,34 +106,48 @@
   virtual void
   shutdown();
 
-  virtual void
-  addTask(const Task &task, double delay);
+  virtual bool
+  addTask(const TaskPtr &task, double delay);
+
+  virtual bool
+  addTask(const TaskPtr &task);
 
   virtual void
-  addTimeoutTask(const Task &task, double timeout);
+  deleteTask(const Task::Tag &tag);
 
   virtual void
-  addPeriodicTask(const Task &task, const IntervalGeneratorPtr &generator);
+  deleteTask(const Task::TaskMatcher &matcher);
 
   virtual void
-  deleteTask(const Tag &tag);
+  rescheduleTask(const Task::Tag &tag);
 
-  virtual void
-  deleteTask(const TaskMatcher &matcher);
+  void
+  eventLoop();
+
+  event_base *
+  base() { return m_base; }
+
+  // used in test
+  int
+  size();
 
 protected:
-  typedef multimap<Tag, Task> TaskMap;
+  bool
+  addToMap(const TaskPtr &task);
+
+protected:
+  typedef map<Task::Tag, TaskPtr> TaskMap;
+  typedef map<Task::Tag, TaskPtr>::iterator TaskMapIt;
+  typedef boost::shared_mutex Mutex;
+  typedef boost::unique_lock<Mutex> WriteLock;
+  typedef boost::shared_lock<Mutex> ReadLock;
   TaskMap m_taskMap;
+  Mutex m_mutex;
+  event_base *m_base;
+  boost::thread m_thread;
 };
 
-class IntervalGenerator
-{
-public:
-  virtual double
-  nextInterval() = 0;
-};
-
-class SimpleIntervalGenerator : IntervalGenerator
+class SimpleIntervalGenerator : public IntervalGenerator
 {
 public:
   SimpleIntervalGenerator(double interval) : m_interval(interval) {}
@@ -100,18 +155,17 @@
   virtual double
   nextInterval() _OVERRIDE { return m_interval; }
 private:
-  SimpleIntervalGenerator(const SimpleIntervalGenerator &other){};
-private:
   double m_interval;
 };
 
-class RandomIntervalGenerator : IntervalGenerator
+class RandomIntervalGenerator : public IntervalGenerator
 {
+public:
   typedef enum
   {
-    UP,
-    DOWN,
-    EVEN
+    UP = 1,
+    DOWN = 2,
+    EVEN = 3
   } Direction;
 
 public:
@@ -121,14 +175,13 @@
   nextInterval() _OVERRIDE;
 
 private:
-  RandomIntervalGenerator(const RandomIntervalGenerator &other){};
   inline double fractional(double x) { double dummy; return abs(modf(x, &dummy)); }
 
 private:
   typedef boost::mt19937 RNG_TYPE;
   RNG_TYPE m_rng;
   boost::uniform_real<> m_dist;
-  boost::rariate_generator<RNG_TYPE &, boost::uniform_real<> > m_random;
+  boost::variate_generator<RNG_TYPE &, boost::uniform_real<> > m_random;
   Direction m_direction;
   double m_interval;
   double m_percent;