Permalink
Browse files

Added timer support

  • Loading branch information...
1 parent 8951559 commit 72cec80e6cbde83827bcb4f6674da9a593a19f7f @Marlinc committed Apr 30, 2012
Showing with 114 additions and 0 deletions.
  1. +25 −0 example/timers.php
  2. +32 −0 src/Entity/Timer.php
  3. +56 −0 src/Handler/Timer.php
  4. +1 −0 src/Module/Main.php
View
25 example/timers.php
@@ -0,0 +1,25 @@
+<?php
+
+require_once __DIR__ . '/../src/Application.php';
+
+$ircbot = Ircbot\Application::getInstance();
+
+function timerOne(\Ircbot\Entity\Timer $timer)
+{
+ echo $timer->getName() . PHP_EOL;
+}
+
+function timerTwo(\Ircbot\Entity\Timer $timer)
+{
+ echo $timer->getName() . PHP_EOL;
+}
+
+
+$timer1 = new \Ircbot\Entity\Timer('timer1', 'timerOne', 2000);
+$timer1->maxIterations = 2;
+$timer2 = new \Ircbot\Entity\Timer('timer2', 'timerTwo', 1000);
+\Ircbot\Handler\Timer::addTimer($timer1);
+\Ircbot\Handler\Timer::addTimer($timer2);
+$ircbot->getLoop()->startLoop();
+
+?>
View
32 src/Entity/Timer.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace Ircbot\Entity;
+
+class Timer
+{
+
+ public $callback;
+ public $interval;
+ public $lastrun;
+ public $itertations;
+ public $maxIterations = 0;
+ private $_name;
+
+ public function __construct($name, $callback, $interval = 100)
+ {
+ if (!is_string($name) || !is_callable($callback) || !is_int($interval)) {
+ throw new \InvalidArgumentException;
+ }
+ $this->_name = $name;
+ $this->callback = $callback;
+ $this->interval = $interval;
+ }
+
+ public function getName()
+ {
+ return $this->_name;
+ }
+
+}
+
+?>
View
56 src/Handler/Timer.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Ircbot\Handler;
+
+class Timer
+{
+
+
+ static protected $_timers = array();
+
+ static public function addTimer(
+ \Ircbot\Entity\Timer &$timer
+ ) {
+ self::$_timers[$timer->getName()] = $timer;
+ }
+
+ static public function delTimer($timerName) {
+ unset(self::$_timers[$timerName]);
+ }
+
+ static public function executeTimers($maxTime = 1000)
+ {
+ $debug = \Ircbot\Application::getInstance()->getDebugger();
+ $start = microtime(true);
+ foreach (self::$_timers as $name => &$timer) {
+ if (microtime(true) * 1000 - $start * 1000 > $maxTime) {
+ $debug->log(
+ 'timers', 'time-execeed',
+ 'Allowed running time exeeded'
+ );
+ break;
+ }
+ if (microtime(true) * 1000 - $timer->lastrun * 1000 > $timer->interval) {
+ $debug->log(
+ 'timers', 'execution',
+ 'Timer \'' . $timer->getName() . '\' started'
+ );
+ call_user_func($timer->callback, $timer);
+ ++$timer->iterations;
+ $timer->lastrun = microtime(true);
+ if ($timer->maxIterations) {
+ if ($timer->iterations == $timer->maxIterations) {
+ $debug->log(
+ 'timers', 'done',
+ 'Timer \'' . $timer->getName()
+ . '\' done. Removing..'
+ );
+ self::delTimer($timer->getName());
+ }
+ }
+ }
+ }
+ }
+
+}
+?>
View
1 src/Module/Main.php
@@ -46,6 +46,7 @@ public function loopIterate()
{
Ircbot::getInstance()->getSignalHandler()->getSignals();
Ircbot::getInstance()->getBotHandler()->handleBots();
+ \Ircbot\Handler\Timer::executeTimers(10);
}
public function onPing(\Ircbot\Command\Ping $ping)

0 comments on commit 72cec80

Please sign in to comment.