Anope IRC Services  Version 2.0
timers.cpp
Go to the documentation of this file.
1 /* Timer stuff.
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  */
9 
10 #include "services.h"
11 #include "timers.h"
12 
13 std::multimap<time_t, Timer *> TimerManager::Timers;
14 
15 Timer::Timer(long time_from_now, time_t now, bool repeating)
16 {
17  owner = NULL;
18  trigger = now + time_from_now;
19  secs = time_from_now;
20  repeat = repeating;
21  settime = now;
22 
24 }
25 
26 Timer::Timer(Module *creator, long time_from_now, time_t now, bool repeating)
27 {
28  owner = creator;
29  trigger = now + time_from_now;
30  secs = time_from_now;
31  repeat = repeating;
32  settime = now;
33 
35 }
36 
38 {
40 }
41 
42 void Timer::SetTimer(time_t t)
43 {
45  trigger = t;
47 }
48 
49 time_t Timer::GetTimer() const
50 {
51  return trigger;
52 }
53 
54 bool Timer::GetRepeat() const
55 {
56  return repeat;
57 }
58 
59 time_t Timer::GetSetTime() const
60 {
61  return settime;
62 }
63 
64 void Timer::SetSecs(time_t t)
65 {
67  secs = t;
68  trigger = Anope::CurTime + t;
70 }
71 
72 long Timer::GetSecs() const
73 {
74  return secs;
75 }
76 
78 {
79  return owner;
80 }
81 
83 {
84  Timers.insert(std::make_pair(t->GetTimer(), t));
85 }
86 
88 {
89  std::pair<std::multimap<time_t, Timer *>::iterator, std::multimap<time_t, Timer *>::iterator> itpair = Timers.equal_range(t->GetTimer());
90  for (std::multimap<time_t, Timer *>::iterator i = itpair.first; i != itpair.second; ++i)
91  {
92  if (i->second == t)
93  {
94  Timers.erase(i);
95  break;
96  }
97  }
98 }
99 
100 void TimerManager::TickTimers(time_t ctime)
101 {
102  while (!Timers.empty())
103  {
104  std::multimap<time_t, Timer *>::iterator it = Timers.begin();
105  Timer *t = it->second;
106 
107  if (t->GetTimer() > ctime)
108  break;
109 
110  t->Tick(ctime);
111 
112  if (t->GetRepeat())
113  t->SetTimer(ctime + t->GetSecs());
114  else
115  delete t;
116  }
117 }
118 
120 {
121  for (std::multimap<time_t, Timer *>::iterator it = Timers.begin(), it_next = it; it != Timers.end(); it = it_next)
122  {
123  ++it_next;
124  if (it->second->GetOwner() == m)
125  delete it->second;
126  }
127 }
128 
Definition: timers.h:18
static void DelTimer(Timer *t)
Definition: timers.cpp:87
long secs
Definition: timers.h:35
CoreExport time_t CurTime
Definition: main.cpp:41
void SetTimer(time_t t)
Definition: timers.cpp:42
bool repeat
Definition: timers.h:39
bool GetRepeat() const
Definition: timers.cpp:54
virtual void Tick(time_t ctime)=0
time_t settime
Definition: timers.h:27
time_t GetSetTime() const
Definition: timers.cpp:59
Timer(long time_from_now, time_t now=Anope::CurTime, bool repeating=false)
Definition: timers.cpp:15
Module * GetOwner() const
Definition: timers.cpp:77
void SetSecs(time_t t)
Definition: timers.cpp:64
long GetSecs() const
Definition: timers.cpp:72
static void TickTimers(time_t ctime=Anope::CurTime)
Definition: timers.cpp:100
time_t GetTimer() const
Definition: timers.cpp:49
static std::multimap< time_t, Timer * > Timers
Definition: timers.h:110
time_t trigger
Definition: timers.h:31
virtual ~Timer()
Definition: timers.cpp:37
Module * owner
Definition: timers.h:23
static void DeleteTimersFor(Module *m)
Definition: timers.cpp:119
static void AddTimer(Timer *t)
Definition: timers.cpp:82