Show
Ignore:
Timestamp:
02/15/2008 08:53:29 AM (11 months ago)
Author:
Shadowhand
Message:

Kobot can haz set_timer()!

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/kobot/libraries/Kobot.php

    r2052 r2055  
    1818        public $log_level = 1; 
    1919 
    20         // Command responses 
     20        // Command responses and timers 
    2121        protected $responses = array(); 
     22        protected $timers = array(); 
    2223 
    2324        // Responses to drop by default 
     
    119120                        // Read the PRIVMSG command 
    120121                        $this->set_response('PRIVMSG', array($this, 'response_privmsg')); 
    121  
    122                         // Reply to VERSION commands 
    123                         $this->add_trigger('^VERSION$', array($this, 'trigger_version')); 
    124122                } 
    125123                else 
     
    202200                while ( ! feof($this->socket)) 
    203201                { 
     202                        // Start a new read loop 
     203                        $loop_time = microtime(TRUE); 
     204 
    204205                        // Read the raw server stream, up to 512 characters 
    205206                        while ($raw = fgets($this->socket, 512)) 
     
    222223                                } 
    223224                        } 
    224                         // One half-second is high enough interactivity 
    225                         usleep(500000); 
     225 
     226                        // Check the timers 
     227                        $this->check_timers(); 
     228 
     229                        // Detemine the amount of time spent in this loop, in microseconds 
     230                        $loop_time = (microtime(TRUE) - $loop_time) * 1000000; 
     231 
     232                        // Every loop should take one-half second 
     233                        ($loop_time < 500000) and usleep(500000 - $loop_time); 
    226234                } 
    227235 
     
    332340        } 
    333341 
    334         public function add_trigger($pattern, $callback) 
     342        protected function check_timers() 
     343        { 
     344                foreach ($this->timers as $key => $data) 
     345                { 
     346                        if (microtime(TRUE) >= $data['timeout']) 
     347                        { 
     348                                // Run the callback, passing the bot as the only parameter 
     349                                call_user_func($data['callback'], $this); 
     350 
     351                                // Restart the timer, if it was not removed 
     352                                isset($this->timers[$key]) and $this->timers[$key]['timeout'] = microtime(TRUE) + $data['interval']; 
     353                        } 
     354                } 
     355        } 
     356 
     357        public function set_timer($interval, $callback) 
     358        { 
     359                if ( ! is_callable($callback)) 
     360                        throw new Kohana_Exception('kobot.invalid_timer'); 
     361 
     362                // Add the timer to the timers, forcing the callback to be unique 
     363                $this->timers[$this->callback_hash($callback)] = array 
     364                ( 
     365                        'callback' => $callback, 
     366                        'interval' => $interval, 
     367                        'timeout'  => microtime(TRUE) + $interval, 
     368                ); 
     369 
     370                return $this; 
     371        } 
     372 
     373        public function remove_timer($callback) 
     374        { 
     375                // Remove the timer 
     376                unset($this->timers[$this->callback_hash($callback)]); 
     377 
     378                return $this; 
     379        } 
     380 
     381        public function set_trigger($pattern, $callback) 
    335382        { 
    336383                // Store the trigger and it's callback 
     
    349396 
    350397                return $this; 
     398        } 
     399 
     400        protected function callback_hash($callback) 
     401        { 
     402                $hash = NULL; 
     403                if (is_array($callback)) 
     404                { 
     405                        if (is_string($callback[0])) 
     406                        { 
     407                                // Static method callback 
     408                                $hash = sha1($callback[0].'::'.$callback[1]); 
     409                        } 
     410                        else 
     411                        { 
     412                                // Object method callback 
     413                                $hash = sha1(get_class($callback[0]).'->'.$callback[1]); 
     414                        } 
     415                } 
     416                else 
     417                { 
     418                        // Hash the name 
     419                        $hash = sha1($callback); 
     420                } 
     421 
     422                return $hash; 
    351423        } 
    352424 
     
    423495                                $this->channels[$data['target']][] = $data['sender']; 
    424496 
     497                                // This prevents the userlist key from growing too large, causing a buffer overflow 
     498                                $this->channels[$data['target']] = array_values($this->channels[$data['target']]); 
     499 
    425500                                // Debug the join 
    426501                                $this->log(2, '> '.$data['sender'].' ('.$data['target'].')');