]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/timer.cpp
First revision of new timer code, not in the makefile yet (so it wont build till...
[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 "timer.h"
34 #include "inspircd.h"
35 #include "inspircd_io.h"
36 #include "inspstring.h"
37 #include "helperfuncs.h"
38
39 extern InspIRCd* ServerInstance;
40 extern ServerConfig* Config;
41 extern time_t TIME;
42
43 typedef std::vector<InspTimer*> timergroup;
44 typedef std::map<time_t, timergroup*> timerlist;
45 timerlist Timers;
46
47 void TickTimers(time_t TIME)
48 {
49         timerlist::iterator found = Timers.find(TIME);
50
51         if (found != Timers.end())
52         {
53                 log("timer.cpp: There are timers to trigger");
54                 timerlist* x = found->second;
55                 /*
56                  * There are pending timers to trigger
57                  */
58                 for (timerlist::iterator y = x.begin(); y != x.end(); y++)
59                 {
60                         log("timer.cpp: Triggering a timer");
61                         InspTimer* n = (InspTimer*)*y;
62                         n->Tick(TIME);
63                         log("timer.cpp: TICK!");
64                         delete n;
65                 }
66
67                 log("timer.cpp: Done triggering timers, tidying up");
68                 Timers.erase(found);
69                 delete x;
70         }
71 }
72
73 void AddTimer(InspTimer* T)
74 {
75         log("timer.cpp: Adding timer");
76         
77         timergroup* x = NULL;
78
79         timerlist::iterator found = Timers.find(T->GetTimer());
80         
81         if (found != Timers.end())
82         {
83                 log("timer.cpp: Add timer to existing group");
84                 x = found->second;
85         }
86         else
87         {
88                 log("timer.cpp: Add timer to new group");
89                 x = new timergroup;
90                 Timers[T->GetTimer()] = x;
91         }
92
93         x->push_back(T);
94 }