1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013-2014 Attila Molnar <attilamolnar@hush.com>
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) 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 #include "inspircd.h"
28 
SetInterval(unsigned int newinterval)29 void Timer::SetInterval(unsigned int newinterval)
30 {
31 	ServerInstance->Timers.DelTimer(this);
32 	secs = newinterval;
33 	SetTrigger(ServerInstance->Time() + newinterval);
34 	ServerInstance->Timers.AddTimer(this);
35 }
36 
Timer(unsigned int secs_from_now,bool repeating)37 Timer::Timer(unsigned int secs_from_now, bool repeating)
38 	: trigger(ServerInstance->Time() + secs_from_now)
39 	, secs(secs_from_now)
40 	, repeat(repeating)
41 {
42 }
43 
~Timer()44 Timer::~Timer()
45 {
46 	ServerInstance->Timers.DelTimer(this);
47 }
48 
TickTimers(time_t TIME)49 void TimerManager::TickTimers(time_t TIME)
50 {
51 	for (TimerMap::iterator i = Timers.begin(); i != Timers.end(); )
52 	{
53 		Timer* t = i->second;
54 		if (t->GetTrigger() > TIME)
55 			break;
56 
57 		Timers.erase(i++);
58 
59 		if (!t->Tick(TIME))
60 			continue;
61 
62 		if (t->GetRepeat())
63 		{
64 			t->SetTrigger(TIME + t->GetInterval());
65 			AddTimer(t);
66 		}
67 	}
68 }
69 
DelTimer(Timer * t)70 void TimerManager::DelTimer(Timer* t)
71 {
72 	std::pair<TimerMap::iterator, TimerMap::iterator> itpair = Timers.equal_range(t->GetTrigger());
73 
74 	for (TimerMap::iterator i = itpair.first; i != itpair.second; ++i)
75 	{
76 		if (i->second == t)
77 		{
78 			Timers.erase(i);
79 			break;
80 		}
81 	}
82 }
83 
AddTimer(Timer * t)84 void TimerManager::AddTimer(Timer* t)
85 {
86 	Timers.insert(std::make_pair(t->GetTrigger(), t));
87 }
88