9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
15 * @package Phergie_Plugin_Ping
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie_Plugin_Ping
23 * Uses a self CTCP PING to ensure that the client connection has not been
27 * @package Phergie_Plugin_Ping
28 * @author Phergie Development Team <team@phergie.org>
29 * @license http://phergie.org/license New BSD License
30 * @link http://pear.phergie.org/package/Phergie_Plugin_Ping
32 class Phergie_Plugin_Ping extends Phergie_Plugin_Abstract
35 * Timestamp for the last instance in which an event was received
42 * Timestamp for the last instance in which a PING was sent
49 * Initialize event timestamps upon connecting to the server.
53 public function onConnect()
55 $this->lastEvent = time();
56 $this->lastPing = null;
60 * Updates the timestamp since the last received event when a new event
65 public function preEvent()
67 $this->lastEvent = time();
71 * Clears the ping time if a reply is received.
75 public function onPingResponse()
77 $this->lastPing = null;
81 * Performs a self ping if the event threshold has been exceeded or
82 * issues a termination command if the ping threshold has been exceeded.
86 public function onTick()
89 if (!empty($this->lastPing)) {
90 if ($time - $this->lastPing > $this->getConfig('ping.ping', 10)) {
94 $time - $this->lastEvent > $this->getConfig('ping.event', 300)
96 $this->lastPing = $time;
97 $this->doPing($this->getConnection()->getNick(), $this->lastPing);
102 * Gets the last ping time
103 * lastPing needs exposing for things such as unit testing
105 * @return int timestamp of last ping
107 public function getLastPing()
109 return $this->lastPing;
113 * Set the last ping time
114 * lastPing needs to be exposed for unit testing
116 * @param int|null $ping timestamp of last ping
120 public function setLastPing($ping = null)
122 if (null === $ping) {
125 if (!is_int($ping)) {
126 throw new InvalidArgumentException('$ping must be an integer or null');
128 $this->lastPing = $ping;
133 * Gets the last event time
134 * lastEvent needs exposing for things such as unit testing
136 * @return int timestamp of last ping
138 public function getLastEvent()
140 return $this->lastEvent;
144 * Set the last event time
145 * lastEvent needs to be exposed for unit testing
147 * @param int|null $event timestamp of last ping
151 public function setLastEvent($event = null)
153 if (null === $event) {
156 if (!is_int($event)) {
157 throw new InvalidArgumentException('$ping must be an integer or null');
159 $this->lastEvent = $event;