]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/timer.cpp
Route some more stuff through socket.h and hash_map.h. Add #define protection to...
[user/henk/code/inspircd.git] / src / timer.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include <vector>
18 #include <map>
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "typedefs.h"
22 #include "helperfuncs.h"
23 #include "timer.h"
24
25 extern time_t TIME;
26
27 typedef std::vector<InspTimer*> timergroup;
28 typedef std::map<time_t, timergroup*> timerlist;
29 timerlist Timers;
30
31 void TickTimers(time_t TIME)
32 {
33         timerlist::iterator found = Timers.find(TIME);
34
35         if (found != Timers.end())
36         {
37                 timergroup* x = found->second;
38                 /*
39                  * There are pending timers to trigger
40                  */
41                 for (timergroup::iterator y = x->begin(); y != x->end(); y++)
42                 {
43                         InspTimer* n = *y;
44                         n->Tick(TIME);
45                         delete n;
46                 }
47
48                 Timers.erase(found);
49                 delete x;
50         }
51 }
52
53 /*
54  * Because some muppets may do odd things, and their ircd may lock up due
55  * to crappy 3rd party modules, or they may change their system time a bit,
56  * this accounts for shifts of up to 120 secs by looking behind for missed
57  * timers and executing them. This is only executed once every 5 secs.
58  * If you move your clock BACK, and your timers move further ahead as a result,
59  * then tough titty you'll just have to wait.
60  */
61 void TickMissedTimers(time_t TIME)
62 {
63         for (time_t n = TIME-1; n > TIME-120; n--)
64         {
65                 timerlist::iterator found = Timers.find(n);
66                 if (found != Timers.end())
67                 {
68                         timergroup* x = found->second;
69                         for (timergroup::iterator y = x->begin(); y != x->end(); y++)
70                         {
71                                 InspTimer* z = *y;
72                                 z->Tick(TIME);
73                                 delete z;
74                         }
75
76                         Timers.erase(found);
77                         delete x;
78                 }
79         }
80 }
81
82 void AddTimer(InspTimer* T)
83 {
84         timergroup* x = NULL;
85
86         timerlist::iterator found = Timers.find(T->GetTimer());
87         
88         if (found != Timers.end())
89         {
90                 x = found->second;
91         }
92         else
93         {
94                 x = new timergroup;
95                 Timers[T->GetTimer()] = x;
96         }
97
98         x->push_back(T);
99 }