blob: 72169d83fb7042ef3ca43e90cbcf913952662dd2 [file] [log] [blame]
Zhenkai Zhu97019eb2013-01-08 00:21:43 -08001#ifndef EVENT_SCHEDULER_H
2#define EVENT_SCHEDULER_H
3#include <event2/event.h>
4#include <event2/event_struct.h>
5#include <event2/thread.h>
6
7#include <boost/function.hpp>
8#include <boost/shared_ptr.hpp>
9#include <boost/random/mersenne_twister.hpp>
10#include <boost/random/uniform_real.hpp>
11#include <boost/random/variate_generator.hpp>
12#include <boost/exception/all.hpp>
13#include <math.h>
14#include <multimap>
15
16#define _OVERRIDE
17#ifdef __GNUC__
18#if __GNUC_MAJOR >= 4 && __GNUC_MINOR__ >= 7
19 #undef _OVERRIDE
20 #define _OVERRIDE override
21#endif // __GNUC__ version
22#endif // __GNUC__
23
24using namespace std;
25
26
27class Task
28{
29public:
30 typedef boost::function<void (void *)> Callback;
31 typedef string Tag;
32 typedef boost::function<bool (const Task &task)> TaskMatcher;
33
34 Task(const Callback &callback, void *arg, const Tag &tag);
35 Task(const Task &other);
36 Task &
37 operator=(const Task &other);
38
39 virtual void
40 run() { m_callback(m_arg); }
41
42 Tag
43 tag() { return m_tag; }
44
45
46protected:
47 Callback m_callback;
48 Tag m_tag;
49 void *arg;
50};
51
52struct SchedulerException : virtual boost::exception, virtual exception { };
53
54class IntervalGenerator;
55typedef boost::shared_ptr<IntervalGenerator> IntervalGeneratorPtr;
56class Scheduler
57{
58public:
59 Scheduler();
60 virtual ~Scheduler();
61
62 virtual void
63 start();
64
65 virtual void
66 shutdown();
67
68 virtual void
69 addTask(const Task &task, double delay);
70
71 virtual void
72 addTimeoutTask(const Task &task, double timeout);
73
74 virtual void
75 addPeriodicTask(const Task &task, const IntervalGeneratorPtr &generator);
76
77 virtual void
78 deleteTask(const Tag &tag);
79
80 virtual void
81 deleteTask(const TaskMatcher &matcher);
82
83protected:
84 typedef multimap<Tag, Task> TaskMap;
85 TaskMap m_taskMap;
86};
87
88class IntervalGenerator
89{
90public:
91 virtual double
92 nextInterval() = 0;
93};
94
95class SimpleIntervalGenerator : IntervalGenerator
96{
97public:
98 SimpleIntervalGenerator(double interval) : m_interval(interval) {}
99 ~SimpleIntervalGenerator(){}
100 virtual double
101 nextInterval() _OVERRIDE { return m_interval; }
102private:
103 SimpleIntervalGenerator(const SimpleIntervalGenerator &other){};
104private:
105 double m_interval;
106};
107
108class RandomIntervalGenerator : IntervalGenerator
109{
110 typedef enum
111 {
112 UP,
113 DOWN,
114 EVEN
115 } Direction;
116
117public:
118 RandomIntervalGenerator(double interval, double percent, Direction direction = EVEN);
119 ~RandomIntervalGenerator(){}
120 virtual double
121 nextInterval() _OVERRIDE;
122
123private:
124 RandomIntervalGenerator(const RandomIntervalGenerator &other){};
125 inline double fractional(double x) { double dummy; return abs(modf(x, &dummy)); }
126
127private:
128 typedef boost::mt19937 RNG_TYPE;
129 RNG_TYPE m_rng;
130 boost::uniform_real<> m_dist;
131 boost::rariate_generator<RNG_TYPE &, boost::uniform_real<> > m_random;
132 Direction m_direction;
133 double m_interval;
134 double m_percent;
135
136};
137#endif // EVENT_SCHEDULER_H