3 * This file is part of IrBot, irc robot.
4 * Copyright (C) 2007 Bellière Ludovic
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 private $ticks = array();
24 static public $instance = false;
26 public static function GetInstance() {
27 if (!self
::$instance) {
28 self
::$instance = new tick();
30 return self
::$instance;
33 private function __construct() {
34 $this->ircmain
= bot
::GetInstance();
38 * Adding a job to the tick list $tickname
40 * @param string $tickname
41 * @param string $jobname
42 * @param string $function Function to call
43 * @param array $params Parameters send to the $function
44 * @param boolean $temporary Make the job temporary (will be deleted on next execution of the tick)
45 * @param object $object Reference to an object if $function is a method
48 public function addJob($tickname,$jobname,$function,$params,$temporary=0,$object=null) {
49 if (isset($this->ticks
[$tickname])) {
50 if (!is_null($object)) {
51 $this->ticks
[$tickname]['jobs'][] = array(
53 'function' => $function,
55 'temporary' => $temporary,
56 'objectRef' => &$object
59 $this->ticks
[$tickname]['jobs'][] = array(
61 'function' => $function,
63 'temporary' => $temporary
72 * Create a tick if not exist
75 * @param int $timer Number of seconds before the next tick
78 public function setTick($name,$timer) {
79 if (!isset($this->ticks
[$name])) {
80 $this->ticks
[$name] = array(
82 'reference' => time(),
83 'tickOn' => strtotime("+$timer seconds"),
97 public function delTick($name) {
98 if (isset($this->ticks
[$name])) {
99 unset($this->ticks
[$name]);
105 public function doAllTicks() {
108 foreach($this->ticks
as $name => $options) {
110 if ($options['tickOn']<=$curtime) {
111 echo $options['tickOn']," - $curtime\n";
113 foreach ($options['jobs'] as $functions) {
115 if (!$functions['temporary']) {
116 $jlist[] = $functions;
119 if (!$this->ircmain
->connected
) {
123 switch (isset($functions['objectRef'])) {
125 if (!is_object($functions['objectRef'])) {
126 throw new Exception('The objectRef for tick '.$name.' at job '.$functions['jname'].' is not an object.',0);
128 //print_r($functions);break;
129 call_user_func_array(array($functions['objectRef'],$functions['function']),$functions['params']);
133 call_user_func_array($functions['function'],$functions['params']);
139 $options['jobs'] = $jlist;
141 $this->ticks
[$name] = array(
142 'timer' => $options['timer'],
143 'reference' => time(),
144 'tickOn' => strtotime("+{$options['timer']} seconds"),
145 'jobs' => $options['jobs']
148 echo "newTickOn {$this->ticks[$name]['tickOn']}\n";