blob: 29ac69f4645171c8d6b735d3036ef31e9bdbd06e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
<?php
/**
* Phergie
*
* PHP version 5
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.
* It is also available through the world-wide-web at this URL:
* http://phergie.org/license
*
* @category Phergie
* @package Phergie_Plugin_Ping
* @author Phergie Development Team <team@phergie.org>
* @copyright 2008-2010 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_Ping
*/
/**
* Uses a self CTCP PING to ensure that the client connection has not been
* dropped.
*
* @category Phergie
* @package Phergie_Plugin_Ping
* @author Phergie Development Team <team@phergie.org>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_Ping
*/
class Phergie_Plugin_Ping extends Phergie_Plugin_Abstract
{
/**
* Timestamp for the last instance in which an event was received
*
* @var int
*/
protected $lastEvent;
/**
* Timestamp for the last instance in which a PING was sent
*
* @var int
*/
protected $lastPing;
/**
* Initialize event timestamps upon connecting to the server.
*
* @return void
*/
public function onConnect()
{
$this->lastEvent = time();
$this->lastPing = null;
}
/**
* Updates the timestamp since the last received event when a new event
* arrives.
*
* @return void
*/
public function preEvent()
{
$this->lastEvent = time();
}
/**
* Clears the ping time if a reply is received.
*
* @return void
*/
public function onPingResponse()
{
$this->lastPing = null;
}
/**
* Performs a self ping if the event threshold has been exceeded or
* issues a termination command if the ping threshold has been exceeded.
*
* @return void
*/
public function onTick()
{
$time = time();
if (!empty($this->lastPing)) {
if ($time - $this->lastPing > $this->getConfig('ping.ping', 10)) {
$this->doQuit();
}
} elseif (
$time - $this->lastEvent > $this->getConfig('ping.event', 300)
) {
$this->lastPing = $time;
$this->doPing($this->getConnection()->getNick(), $this->lastPing);
}
}
/**
* Gets the last ping time
* lastPing needs exposing for things such as unit testing
*
* @return int timestamp of last ping
*/
public function getLastPing()
{
return $this->lastPing;
}
/**
* Set the last ping time
* lastPing needs to be exposed for unit testing
*
* @param int|null $ping timestamp of last ping
*
* @return self
*/
public function setLastPing($ping = null)
{
if (null === $ping) {
$ping = time();
}
if (!is_int($ping)) {
throw new InvalidArgumentException('$ping must be an integer or null');
}
$this->lastPing = $ping;
return $this;
}
/**
* Gets the last event time
* lastEvent needs exposing for things such as unit testing
*
* @return int timestamp of last ping
*/
public function getLastEvent()
{
return $this->lastEvent;
}
/**
* Set the last event time
* lastEvent needs to be exposed for unit testing
*
* @param int|null $event timestamp of last ping
*
* @return self
*/
public function setLastEvent($event = null)
{
if (null === $event) {
$event = time();
}
if (!is_int($event)) {
throw new InvalidArgumentException('$ping must be an integer or null');
}
$this->lastEvent = $event;
return $this;
}
}
|