summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/timeout.h0
-rw-r--r--include/timers.h115
-rw-r--r--src/timeout.c0
-rw-r--r--src/timers.cpp134
4 files changed, 249 insertions, 0 deletions
diff --git a/include/timeout.h b/include/timeout.h
deleted file mode 100644
index e69de29bb..000000000
--- a/include/timeout.h
+++ /dev/null
diff --git a/include/timers.h b/include/timers.h
new file mode 100644
index 000000000..fd4f30f50
--- /dev/null
+++ b/include/timers.h
@@ -0,0 +1,115 @@
+/* Timer include stuff.
+ *
+ * (C) 2003-2009 Anope Team
+ * Contact us at team@anope.org
+ *
+ * Please read COPYING and README for furhter details.
+ * Based on the original code of Epona by Lara.
+ * Based on the original code of Services by Andy Church.
+ *
+ * $Id$
+ *
+ */
+
+#ifndef TIMERS_H
+#define TIMERS_H
+
+#include "services.h"
+#include <time.h>
+#include <algorithm>
+#include <stdio.h>
+#include <stdlib.h>
+
+class CoreExport Timer : public Extensible
+{
+ private:
+ /** The time this was created
+ */
+ time_t settime;
+
+ /** The triggering time
+ */
+ time_t trigger;
+
+ /** Numer of seconds between triggers
+ */
+ long secs;
+
+ /** True if this is a repeating timer
+ */
+ bool repeat;
+
+ public:
+ /** Default constructor, initializes the triggering time
+ * @param time_from_now The number of seconds from now to trigger the timer
+ * @param now The time now
+ * @param repeating Repeat this timer every time_from_now if this is true
+ */
+ Timer(long time_from_now, time_t now = time(NULL), bool repeating = false);
+
+ /** Default destructor, does nothing
+ */
+ virtual ~Timer();
+
+ /** Set the trigger time to a new value
+ * @param t The new time
+ */
+ void SetTimer(time_t t);
+
+ /** Retrieve the triggering time
+ * @return The trigger time
+ */
+ const time_t GetTimer();
+
+ /** Returns true if the timer is set to repeat
+ * @return Returns true if the timer is set to repeat
+ */
+ const bool GetRepeat();
+
+ /** Returns the interval between ticks
+ * @return The interval
+ */
+ const long GetSecs();
+
+ /** Returns the time this timer was created
+ * @return The time this timer was created
+ */
+ const time_t GetSetTime();
+
+ /** Called when the timer ticks
+ * This should be overridden with something useful
+ */
+ virtual void Tick(time_t ctime) = 0;
+};
+
+/** This class manages sets of Timers, and triggers them at their defined times.
+ * This will ensure timers are not missed, as well as removing timers that have
+ * expired and allowing the addition of new ones.
+ */
+class CoreExport TimerManager : public Extensible
+{
+ protected:
+ /** A list of timers
+ */
+ static std::vector<Timer *> Timers;
+ public:
+ /** Add a timer to the list
+ * @param T A Timer derived class to add
+ */
+ static void AddTimer(Timer *T);
+
+ /** Deletes a timer
+ * @param T A Timer derived class to delete */
+ static void DelTimer(Timer *T);
+
+ /** Tick all pending timers
+ * @param time The current time
+ */
+ static void TickTimers(time_t ctime = time(NULL));
+
+ /** Compares two timers
+ */
+ static bool TimerComparison(Timer *one, Timer *two);
+};
+
+#endif
diff --git a/src/timeout.c b/src/timeout.c
deleted file mode 100644
index e69de29bb..000000000
--- a/src/timeout.c
+++ /dev/null
diff --git a/src/timers.cpp b/src/timers.cpp
new file mode 100644
index 000000000..dada034ec
--- /dev/null
+++ b/src/timers.cpp
@@ -0,0 +1,134 @@
+/* Timer stuff.
+ *
+ * (C) 2003-2009 Anope Team
+ * Contact us at team@anope.org
+ *
+ * Please read COPYING and README for furhter details.
+ * Based on the original code of Epona by Lara.
+ * Based on the original code of Services by Andy Church.
+ *
+ * $Id$
+ *
+ */
+
+#include "timers.h"
+
+std::vector<Timer *> TimerManager::Timers;
+
+/** Default constructor, initializes the triggering time
+ * @param time_from_now The number of seconds from now to trigger the timer
+ * @param now The time now
+ * @param repeating Repeat this timer every time_from_now if this is true
+ */
+Timer::Timer(long time_from_now, time_t now, bool repeating)
+{
+ trigger = now + time_from_now;
+ secs = time_from_now;
+ repeat = repeating;
+
+ TimerManager::AddTimer(this);
+}
+
+/** Default destructor, does nothing
+ */
+Timer::~Timer()
+{
+}
+
+/** Set the trigger time to a new value
+ * @param t The new time
+*/
+void Timer::SetTimer(time_t t)
+{
+ trigger = t;
+}
+
+/** Retrieve the triggering time
+ * @return The trigger time
+ */
+const time_t Timer::GetTimer()
+{
+ return trigger;
+}
+
+/** Returns true if the timer is set to repeat
+ * @return Returns true if the timer is set to repeat
+ */
+const bool Timer::GetRepeat()
+{
+ return repeat;
+}
+
+/** Returns the time this timer was created
+* @return The time this timer was created
+*/
+const time_t Timer::GetSetTime()
+{
+ return settime;
+}
+
+/** Returns the interval between ticks
+* @return The interval
+*/
+const long Timer::GetSecs()
+{
+ return secs;
+}
+
+/** Add a timer to the list
+ * @param T A Timer derived class to add
+ */
+void TimerManager::AddTimer(Timer *T)
+{
+ Timers.push_back(T);
+ sort(Timers.begin(), Timers.end(), TimerManager::TimerComparison);
+}
+
+/** Deletes a timer
+ * @param T A Timer derived class to delete
+ */
+void TimerManager::DelTimer(Timer *T)
+{
+ std::vector<Timer *>::iterator i = std::find(Timers.begin(), Timers.end(), T);
+
+ if (i != Timers.end())
+ {
+ delete (*i);
+ Timers.erase(i);
+ }
+}
+
+/** Tick all pending timers
+ * @param time The current time
+ */
+void TimerManager::TickTimers(time_t ctime)
+{
+ std::vector<Timer *>::iterator i;
+ Timer *t;
+
+ while ((Timers.size()) && (ctime > (*Timers.begin())->GetTimer()))
+ {
+ i = Timers.begin();
+
+ t = *i;
+
+ t->Tick(ctime);
+
+ Timers.erase(i);
+
+ if (t->GetRepeat())
+ {
+ t->SetTimer(ctime + t->GetSecs());
+ AddTimer(t);
+ }
+ else
+ delete t;
+ }
+}
+
+/** Compares two timers
+ */
+bool TimerManager::TimerComparison(Timer *one, Timer *two)
+{
+ return (one->GetTimer() < two->GetTimer());
+}