]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/timer.cpp
c172f2fa5bac2f2c35b38ce6a00c880f8ca79810
[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 using namespace std;
18
19 #include "inspircd_config.h"
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <string>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <poll.h>
29 #include <sstream>
30 #include <iostream>
31 #include <fstream>
32 #include <stdexcept>
33 #include "inspircd.h"
34 #include "inspircd_io.h"
35 #include "inspstring.h"
36 #include "helperfuncs.h"
37
38 extern InspIRCd* ServerInstance;
39 extern ServerConfig* Config;
40 extern time_t TIME;
41
42 typedef std::vector<InspTimer*> timergroup;
43 typedef std::map<time_t, timergroup*> timerlist;
44 timerlist Timers;
45
46 void TickTimers(time_t TIME)
47 {
48         timerlist::iterator found = Timers.find(TIME);
49
50         if (found != Timers.end())
51         {
52                 timergroup* x = found->second;
53                 /*
54                  * There are pending timers to trigger
55                  */
56                 for (timergroup::iterator y = x->begin(); y != x->end(); y++)
57                 {
58                         InspTimer* n = (InspTimer*)*y;
59                         n->Tick(TIME);
60                         delete n;
61                 }
62
63                 Timers.erase(found);
64                 delete x;
65         }
66 }
67
68 void TickMissedTimers(time_t TIME)
69 {
70         for (time_t n = TIME-1; n > TIME-120; n--)
71         {
72                 timerlist::iterator found = Timers.find(n);
73                 if (found != Timers.end())
74                 {
75                         timergroup* x = found->second;
76                         for (timergroup::iterator y = x->begin(); y != x->end(); y++)
77                         {
78                                 InspTimer* z = (InspTimer*)*y;
79                                 z->Tick(TIME);
80                                 delete z;
81                         }
82
83                         Timers.erase(found);
84                         delete x;
85                 }
86         }
87 }
88
89 void AddTimer(InspTimer* T)
90 {
91         timergroup* x = NULL;
92
93         timerlist::iterator found = Timers.find(T->GetTimer());
94         
95         if (found != Timers.end())
96         {
97                 x = found->second;
98         }
99         else
100         {
101                 x = new timergroup;
102                 Timers[T->GetTimer()] = x;
103         }
104
105         x->push_back(T);
106 }