]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/timer.h
9c46b4f819674e92a78fb41e0d018fb77e0e2623
[user/henk/code/inspircd.git] / include / timer.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef INSPIRCD_TIMER_H
15 #define INSPIRCD_TIMER_H
16
17 //class InspIRCd;
18
19 /** Timer class for one-second resolution timers
20  * Timer provides a facility which allows module
21  * developers to create one-shot timers. The timer
22  * can be made to trigger at any time up to a one-second
23  * resolution. To use Timer, inherit a class from
24  * Timer, then insert your inherited class into the
25  * queue using Server::AddTimer(). The Tick() method of
26  * your object (which you should override) will be called
27  * at the given time.
28  */
29 class CoreExport Timer
30 {
31  private:
32         /** The triggering time
33          */
34         time_t trigger;
35         /** Number of seconds between triggers
36          */
37         long secs;
38         /** True if this is a repeating timer
39          */
40         bool repeat;
41  public:
42         /** Default constructor, initializes the triggering time
43          * @param secs_from_now The number of seconds from now to trigger the timer
44          * @param now The time now
45          * @param repeating Repeat this timer every secs_from_now seconds if set to true
46          */
47         Timer(long secs_from_now, time_t now, bool repeating = false)
48         {
49                 trigger = now + secs_from_now;
50                 secs = secs_from_now;
51                 repeat = repeating;
52         }
53
54         /** Default destructor, does nothing.
55          */
56         virtual ~Timer() { }
57
58         /** Retrieve the current triggering time
59          */
60         virtual time_t GetTimer()
61         {
62                 return trigger;
63         }
64
65         /** Sets the trigger timeout to a new value
66          */
67         virtual void SetTimer(time_t t)
68         {
69                 trigger = t;
70         }
71
72         /** Called when the timer ticks.
73          * You should override this method with some useful code to
74          * handle the tick event.
75          */
76         virtual void Tick(time_t TIME) = 0;
77
78         /** Returns true if this timer is set to repeat
79          */
80         bool GetRepeat()
81         {
82                 return repeat;
83         }
84
85         /** Returns the interval (number of seconds between ticks)
86          * of this timer object.
87          */
88         long GetSecs()
89         {
90                 return secs;
91         }
92
93         /** Cancels the repeat state of a repeating timer.
94          * If you call this method, then the next time your
95          * timer ticks, it will be removed immediately after.
96          * You should use this method call to remove a recurring
97          * timer if you wish to do so within the timer's Tick
98          * event, as calling TimerManager::DelTimer() from within
99          * the Timer::Tick() method is dangerous and may
100          * cause a segmentation fault. Calling CancelRepeat()
101          * is safe in this case.
102          */
103         void CancelRepeat()
104         {
105                 repeat = false;
106         }
107 };
108
109
110 /** This class manages sets of Timers, and triggers them at their defined times.
111  * This will ensure timers are not missed, as well as removing timers that have
112  * expired and allowing the addition of new ones.
113  */
114 class CoreExport TimerManager
115 {
116  protected:
117         /** A list of all pending timers
118          */
119         std::vector<Timer *> Timers;
120
121         /** Creating server instance
122          */
123         InspIRCd* ServerInstance;
124  public:
125         /** Constructor
126          */
127         TimerManager(InspIRCd* Instance);
128         ~TimerManager();
129
130         /** Tick all pending Timers
131          * @param TIME the current system time
132          */
133         void TickTimers(time_t TIME);
134
135         /** Add an Timer
136          * @param T an Timer derived class to add
137          * @param secs_from_now You may set this to the number of seconds
138          * from the current time when the timer will tick, or you may just
139          * leave this unset and the values set by the Timers constructor
140          * will be used. This is used internally for re-triggering repeating
141          * timers.
142          */
143         void AddTimer(Timer *T);
144
145         /** Delete an Timer
146          * @param T an Timer derived class to delete
147          */
148         void DelTimer(Timer* T);
149
150         /** Compares two timers
151          */
152         static bool TimerComparison( Timer *one,  Timer*two);
153 };
154
155 #endif
156