summaryrefslogtreecommitdiff
path: root/src/timers.cpp
blob: 64d16cb8bd3bc0da1af6ad3749ca2f113d5e56ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Timer stuff.
 *
 * (C) 2003-2010 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for furhter details.
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church. 
 * 
 * $Id$ 
 *
 */

#include "services.h"

std::vector<Timer *> TimerManager::Timers;

/** Default constructor, initializes the triggering time
 * @param time_from_now The number of seconds from now to trigger the timer
 * @param now The time now
 * @param repeating Repeat this timer every time_from_now if this is true
 */
Timer::Timer(long time_from_now, time_t now, bool repeating)
{
	trigger = now + time_from_now;
	secs = time_from_now;
	repeat = repeating;
	settime = now;

	TimerManager::AddTimer(this);
}

/** Default destructor, removes the timer from the list
 */
Timer::~Timer()
{
	TimerManager::DelTimer(this);
}

/** Set the trigger time to a new value
 * @param t The new time
*/
void Timer::SetTimer(time_t t)
{
	trigger = t;
}

/** Retrieve the triggering time
 * @return The trigger time
 */
const time_t Timer::GetTimer()
{
	return trigger;
}

/** Returns true if the timer is set to repeat
 * @return Returns true if the timer is set to repeat
 */
const bool Timer::GetRepeat()
{
	return repeat;
}

/** Returns the time this timer was created
* @return The time this timer was created
*/
const time_t Timer::GetSetTime()
{
	return settime;
}

/** Returns the interval between ticks
* @return The interval
*/
const long Timer::GetSecs()
{
	return secs;
}

/** Add a timer to the list
 * @param T A Timer derived class to add
 */
void TimerManager::AddTimer(Timer *T)
{
	Timers.push_back(T);
	sort(Timers.begin(), Timers.end(), TimerManager::TimerComparison);
}

/** Deletes a timer
 * @param T A Timer derived class to delete
 */
void TimerManager::DelTimer(Timer *T)
{
	std::vector<Timer *>::iterator i = std::find(Timers.begin(), Timers.end(), T);
		
	if (i != Timers.end())
	{
		Timers.erase(i);
	}
}

/** Tick all pending timers
 * @param ctime The current time
 */
void TimerManager::TickTimers(time_t ctime)
{
	while (Timers.size() && (ctime > Timers.front()->GetTimer()))
	{
		Timer *t = Timers.front();

		t->Tick(ctime);
		
		if (t->GetRepeat())
		{
			t->SetTimer(ctime + t->GetSecs());
			sort(Timers.begin(), Timers.end(), TimerManager::TimerComparison);
		}
		else
			delete t;
	}
}

/** Compares two timers
 */
bool TimerManager::TimerComparison(Timer *one, Timer *two)
{
	return (one->GetTimer() < two->GetTimer());
}