3 * This file is part of IrBot, irc robot.
4 * Copyright (C) 2007-2008 Bellière Ludovic
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * @copyright Copyright (c) 2007-2008 Bellière Ludovic
24 * @license http://www.gnu.org/licenses/gpl-3.0.html
29 * Cron event emulation
33 * @copyright Copyright (c) 2007-2008 Bellière Ludovic
34 * @license http://www.gnu.org/licenses/gpl-3.0.html
37 private $ticks = array();
45 static public $instance = false;
47 public static function GetInstance(IRCMain
$ircmain) {
48 if (!self
::$instance) {
49 self
::$instance = new tick($ircmain);
51 return self
::$instance;
54 private function __construct(IRCMain
$ircmain) {
55 $this->ircmain
= $ircmain;
59 * Adding a job to the tick list $tickname
61 * @param string $tickname
62 * @param string $jobname
63 * @param string $function Function to call
64 * @param array $params Parameters send to the $function
65 * @param boolean $temporary Make the job temporary (will be deleted on next execution of the tick)
66 * @param object $object Reference to an object if $function is a method
69 public function addJob($tickname,$jobname,$function,$params,$temporary=0,$object=null) {
70 if (isset($this->ticks
[$tickname])) {
71 if (!is_null($object)) {
72 $this->ticks
[$tickname]['jobs'][] = array(
74 'function' => $function,
76 'temporary' => $temporary,
77 'objectRef' => &$object
80 $this->ticks
[$tickname]['jobs'][] = array(
82 'function' => $function,
84 'temporary' => $temporary
93 * Create a tick if not exist
96 * @param int $timer Number of seconds before the next tick
99 public function setTick($name,$timer) {
100 if (!isset($this->ticks
[$name])) {
101 $this->ticks
[$name] = array(
103 'reference' => time(),
104 'tickOn' => strtotime("+$timer seconds"),
115 * @param string $name
118 public function delTick($name) {
119 if (isset($this->ticks
[$name])) {
120 unset($this->ticks
[$name]);
126 public function doAllTicks() {
129 foreach($this->ticks
as $name => $options) {
131 if ($options['tickOn']<=$curtime) {
132 echo $options['tickOn']," - $curtime\n";
134 foreach ($options['jobs'] as $functions) {
136 if (!$functions['temporary']) {
137 $jlist[] = $functions;
140 if (!$this->ircmain
->connected
) {
144 switch (isset($functions['objectRef'])) {
146 if (!is_object($functions['objectRef'])) {
147 throw new Exception('The objectRef for tick '.$name.' at job '.$functions['jname'].' is not an object.',0);
149 //print_r($functions);break;
150 call_user_func_array(array($functions['objectRef'],$functions['function']),$functions['params']);
154 call_user_func_array($functions['function'],$functions['params']);
160 $options['jobs'] = $jlist;
162 $this->ticks
[$name] = array(
163 'timer' => $options['timer'],
164 'reference' => time(),
165 'tickOn' => strtotime("+{$options['timer']} seconds"),
166 'jobs' => $options['jobs']
169 echo "newTickOn {$this->ticks[$name]['tickOn']}\n";