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