2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
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
36 class CoreExport Timer
38 /** The triggering time
42 /** Number of seconds between triggers
46 /** True if this is a repeating timer
51 /** Default constructor, initializes the triggering time
52 * @param mod The module that created this timer
53 * @param secs_from_now The number of seconds from now to trigger the timer
54 * @param now The time now
55 * @param repeating Repeat this timer every secs_from_now seconds if set to true
57 Timer(unsigned int secs_from_now, time_t now, bool repeating = false)
59 trigger = now + secs_from_now;
64 /** Default destructor, removes the timer from the timer manager
68 /** Retrieve the current triggering time
70 time_t GetTrigger() const
75 /** Sets the trigger timeout to a new value
76 * This does not update the bookkeeping in TimerManager, use SetInterval()
77 * to change the interval between ticks while keeping TimerManager updated
79 void SetTrigger(time_t nexttrigger)
81 trigger = nexttrigger;
84 /** Sets the interval between two ticks.
86 void SetInterval(time_t interval);
88 /** Called when the timer ticks.
89 * You should override this method with some useful code to
90 * handle the tick event.
92 virtual bool Tick(time_t TIME) = 0;
94 /** Returns true if this timer is set to repeat
96 bool GetRepeat() const
101 /** Returns the interval (number of seconds between ticks)
102 * of this timer object.
104 unsigned int GetInterval() const
109 /** Cancels the repeat state of a repeating timer.
110 * If you call this method, then the next time your
111 * timer ticks, it will be removed immediately after.
119 typedef std::multimap<time_t, Timer*> TimerMap;
121 /** This class manages sets of Timers, and triggers them at their defined times.
122 * This will ensure timers are not missed, as well as removing timers that have
123 * expired and allowing the addition of new ones.
125 class CoreExport TimerManager
127 /** A list of all pending timers
132 /** Tick all pending Timers
133 * @param TIME the current system time
135 void TickTimers(time_t TIME);
138 * @param T an Timer derived class to add
140 void AddTimer(Timer *T);
143 * @param T an Timer derived class to remove
145 void DelTimer(Timer* T);