]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Ping.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Ping.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie
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
20  */
21
22 /**
23  * Uses a self CTCP PING to ensure that the client connection has not been
24  * dropped.
25  *
26  * @category Phergie
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
31  */
32 class Phergie_Plugin_Ping extends Phergie_Plugin_Abstract
33 {
34     /**
35      * Timestamp for the last instance in which an event was received
36      *
37      * @var int
38      */
39     protected $lastEvent;
40
41     /**
42      * Timestamp for the last instance in which a PING was sent
43      *
44      * @var int
45      */
46     protected $lastPing;
47
48     /**
49      * Initialize event timestamps upon connecting to the server.
50      *
51      * @return void
52      */
53     public function onConnect()
54     {
55         $this->lastEvent = time();
56         $this->lastPing = null;
57     }
58
59     /**
60      * Updates the timestamp since the last received event when a new event
61      * arrives.
62      *
63      * @return void
64      */
65     public function preEvent()
66     {
67         $this->lastEvent = time();
68     }
69
70     /**
71      * Clears the ping time if a reply is received.
72      *
73      * @return void
74      */
75     public function onPingResponse()
76     {
77         $this->lastPing = null;
78     }
79
80     /**
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.
83      *
84      * @return void
85      */
86     public function onTick()
87     {
88         $time = time();
89         if (!empty($this->lastPing)) {
90             if ($time - $this->lastPing > $this->getConfig('ping.ping', 20)) {
91                 $this->doQuit();
92             }
93         } elseif (
94             $time - $this->lastEvent > $this->getConfig('ping.event', 300)
95         ) {
96             $this->lastPing = $time;
97             $this->doPing($this->getConnection()->getNick(), $this->lastPing);
98         }
99     }
100
101     /**
102      * Gets the last ping time
103      * lastPing needs exposing for things such as unit testing
104      *
105      * @return int timestamp of last ping
106      */
107     public function getLastPing()
108     {
109         return $this->lastPing;
110     }
111
112     /**
113      * Set the last ping time
114      * lastPing needs to be exposed for unit testing
115      *
116      * @param int|null $ping timestamp of last ping
117      *
118      * @return self
119      */
120     public function setLastPing($ping = null)
121     {
122         if (null === $ping) {
123             $ping = time();
124         }
125         if (!is_int($ping)) {
126             throw new InvalidArgumentException('$ping must be an integer or null');
127         }
128         $this->lastPing = $ping;
129         return $this;
130     }
131
132     /**
133      * Gets the last event time
134      * lastEvent needs exposing for things such as unit testing
135      *
136      * @return int timestamp of last ping
137      */
138     public function getLastEvent()
139     {
140         return $this->lastEvent;
141     }
142
143     /**
144      * Set the last event time
145      * lastEvent needs to be exposed for unit testing
146      *
147      * @param int|null $event timestamp of last ping
148      *
149      * @return self
150      */
151     public function setLastEvent($event = null)
152     {
153         if (null === $event) {
154             $event = time();
155         }
156         if (!is_int($event)) {
157             throw new InvalidArgumentException('$ping must be an integer or null');
158         }
159         $this->lastEvent = $event;
160         return $this;
161     }
162 }