]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/timer.h
Merge insp20
[user/henk/code/inspircd.git] / include / timer.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
5  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2006-2007 Craig Edwards <craigedwards@brainbox.cc>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #pragma once
23
24 class Module;
25
26 /** Timer class for one-second resolution timers
27  * Timer provides a facility which allows module
28  * developers to create one-shot timers. The timer
29  * can be made to trigger at any time up to a one-second
30  * resolution. To use Timer, inherit a class from
31  * Timer, then insert your inherited class into the
32  * queue using Server::AddTimer(). The Tick() method of
33  * your object (which you have to override) will be called
34  * at the given time.
35  */
36 class CoreExport Timer
37 {
38         /** The triggering time
39          */
40         time_t trigger;
41
42         /** Number of seconds between triggers
43          */
44         unsigned int secs;
45
46         /** True if this is a repeating timer
47          */
48         bool repeat;
49
50  public:
51         /** Default constructor, initializes the triggering time
52          * @param secs_from_now The number of seconds from now to trigger the timer
53          * @param now The time now
54          * @param repeating Repeat this timer every secs_from_now seconds if set to true
55          */
56         Timer(unsigned int secs_from_now, time_t now, bool repeating = false)
57         {
58                 trigger = now + secs_from_now;
59                 secs = secs_from_now;
60                 repeat = repeating;
61         }
62
63         /** Default destructor, removes the timer from the timer manager
64          */
65         virtual ~Timer();
66
67         /** Retrieve the current triggering time
68          */
69         time_t GetTrigger() const
70         {
71                 return trigger;
72         }
73
74         /** Sets the trigger timeout to a new value
75          * This does not update the bookkeeping in TimerManager, use SetInterval()
76          * to change the interval between ticks while keeping TimerManager updated
77          */
78         void SetTrigger(time_t nexttrigger)
79         {
80                 trigger = nexttrigger;
81         }
82
83         /** Sets the interval between two ticks.
84          */
85         void SetInterval(time_t interval);
86
87         /** Called when the timer ticks.
88          * You should override this method with some useful code to
89          * handle the tick event.
90          * @param TIME The current time.
91          * @return True if the Timer object is still valid, false if it was destructed.
92          */
93         virtual bool Tick(time_t TIME) = 0;
94
95         /** Returns true if this timer is set to repeat
96          */
97         bool GetRepeat() const
98         {
99                 return repeat;
100         }
101
102         /** Returns the interval (number of seconds between ticks)
103          * of this timer object.
104          */
105         unsigned int GetInterval() const
106         {
107                 return secs;
108         }
109
110         /** Cancels the repeat state of a repeating timer.
111          * If you call this method, then the next time your
112          * timer ticks, it will be removed immediately after.
113          */
114         void CancelRepeat()
115         {
116                 repeat = false;
117         }
118 };
119
120 typedef std::multimap<time_t, Timer*> TimerMap;
121
122 /** This class manages sets of Timers, and triggers them at their defined times.
123  * This will ensure timers are not missed, as well as removing timers that have
124  * expired and allowing the addition of new ones.
125  */
126 class CoreExport TimerManager
127 {
128         /** A list of all pending timers
129          */
130         TimerMap Timers;
131
132  public:
133         /** Tick all pending Timers
134          * @param TIME the current system time
135          */
136         void TickTimers(time_t TIME);
137
138         /** Add an Timer
139          * @param T an Timer derived class to add
140          */
141         void AddTimer(Timer *T);
142
143         /** Remove a Timer
144          * @param T an Timer derived class to remove
145          */
146         void DelTimer(Timer* T);
147 };