|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* This file is part of PhuninNode. |
|
6
|
|
|
* |
|
7
|
|
|
** (c) 2013 - 2016 Cees-Jan Kiewiet |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace WyriHaximus\PhuninNode; |
|
14
|
|
|
|
|
15
|
|
|
use \React\EventLoop\Timer\TimerInterface; |
|
16
|
|
|
use React\EventLoop\LoopInterface; |
|
17
|
|
|
use React\Stream\DuplexStreamInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class ConnectionContext |
|
21
|
|
|
* @package WyriHaximus\PhuninNode |
|
22
|
|
|
*/ |
|
23
|
|
|
class ConnectionContext |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The greeting munin expects |
|
27
|
|
|
*/ |
|
28
|
|
|
const GREETING = '# munin node at %s'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The timeout after which we disconnection for no data transmission |
|
32
|
|
|
*/ |
|
33
|
|
|
const CONNECTION_TIMEOUT = 60; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* New line |
|
37
|
|
|
*/ |
|
38
|
|
|
const NEW_LINE = "\n"; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var DuplexStreamInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $conn; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var Node |
|
47
|
|
|
*/ |
|
48
|
|
|
private $node; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var TimerInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
private $timeoutTimer; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var LoopInterface |
|
57
|
|
|
*/ |
|
58
|
|
|
private $loop; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param DuplexStreamInterface $conn |
|
62
|
|
|
* @param Node $node |
|
63
|
|
|
*/ |
|
64
|
5 |
|
public function __construct(DuplexStreamInterface $conn, Node $node) |
|
65
|
|
|
{ |
|
66
|
5 |
|
$this->conn = $conn; |
|
67
|
5 |
|
$this->node = $node; |
|
68
|
5 |
|
$this->loop = $node->getLoop(); |
|
69
|
|
|
|
|
70
|
5 |
|
$this->conn->on('data', [$this, 'onData']); |
|
71
|
5 |
|
$this->conn->on('close', [$this, 'onClose']); |
|
72
|
|
|
|
|
73
|
5 |
|
$this->write( |
|
74
|
|
|
sprintf( |
|
75
|
5 |
|
self::GREETING, |
|
76
|
5 |
|
$node->getConfiguration() |
|
77
|
5 |
|
->getPair('hostname') |
|
78
|
5 |
|
->getValue() |
|
79
|
|
|
) |
|
80
|
|
|
); |
|
81
|
5 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Write data to the connection |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $data |
|
87
|
|
|
*/ |
|
88
|
5 |
|
protected function write(string $data) |
|
89
|
|
|
{ |
|
90
|
5 |
|
$this->clearTimeout(); |
|
91
|
5 |
|
$this->conn->write($data . static::NEW_LINE); |
|
92
|
5 |
|
$this->log('<-' . $data); |
|
93
|
5 |
|
$this->setTimeout(); |
|
94
|
5 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Clear the timeout, close the connection, and tell node the client disconnected |
|
98
|
|
|
*/ |
|
99
|
1 |
|
protected function close() |
|
100
|
|
|
{ |
|
101
|
1 |
|
$this->clearTimeout(); |
|
102
|
1 |
|
$this->node->onClose($this); |
|
103
|
1 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Set a timeout that disconnects the client when it's idle for to long |
|
107
|
|
|
*/ |
|
108
|
5 |
|
protected function setTimeout() |
|
109
|
|
|
{ |
|
110
|
5 |
|
$this->timeoutTimer = $this->loop->addTimer( |
|
111
|
5 |
|
self::CONNECTION_TIMEOUT, |
|
112
|
|
|
function () { |
|
113
|
|
|
$this->close(); |
|
114
|
5 |
|
} |
|
115
|
|
|
); |
|
116
|
5 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Clear the timeout if it's set |
|
120
|
|
|
*/ |
|
121
|
5 |
|
protected function clearTimeout() |
|
122
|
|
|
{ |
|
123
|
5 |
|
if ($this->timeoutTimer !== null) { |
|
124
|
1 |
|
$this->loop->cancelTimer($this->timeoutTimer); |
|
125
|
1 |
|
$this->timeoutTimer = null; |
|
126
|
|
|
} |
|
127
|
5 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Handle a command call from the clients side |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $data |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function onData(string $data) |
|
135
|
|
|
{ |
|
136
|
1 |
|
$data = trim($data); |
|
137
|
1 |
|
$this->log('->' . $data); |
|
138
|
1 |
|
$chunks = explode(' ', $data); |
|
139
|
1 |
|
$command = $chunks[0]; |
|
140
|
1 |
|
unset($chunks[0]); |
|
141
|
1 |
|
$input = ''; |
|
142
|
1 |
|
if (count($chunks) > 0) { |
|
143
|
1 |
|
$input = implode(' ', $chunks); |
|
144
|
|
|
} |
|
145
|
1 |
|
$input = trim($input); |
|
146
|
1 |
|
if ($this->node->getCommands()->has($command)) { |
|
147
|
|
|
$this |
|
148
|
1 |
|
->node |
|
149
|
1 |
|
->getCommands() |
|
150
|
1 |
|
->get($command) |
|
151
|
1 |
|
->handle($this, $input) |
|
152
|
1 |
|
->then(function (array $lines) { |
|
153
|
1 |
|
foreach ($lines as $line) { |
|
154
|
1 |
|
$this->write($line); |
|
155
|
|
|
} |
|
156
|
1 |
|
}); |
|
157
|
1 |
|
return; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
1 |
|
$list = implode(', ', $this->node->getCommands()->keys()); |
|
161
|
1 |
|
$this->write( |
|
162
|
1 |
|
'# Unknown command. Try ' . substr_replace($list, ' or ', strrpos($list, ', '), 2) |
|
163
|
|
|
); |
|
164
|
1 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Close connection |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function quit() |
|
170
|
|
|
{ |
|
171
|
1 |
|
$this->conn->close(); |
|
172
|
1 |
|
$this->close(); |
|
173
|
1 |
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Close connection |
|
177
|
|
|
*/ |
|
178
|
|
|
public function onClose() |
|
179
|
|
|
{ |
|
180
|
|
|
$this->close(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param string $message |
|
185
|
|
|
*/ |
|
186
|
5 |
|
protected function log(string $message) |
|
187
|
|
|
{ |
|
188
|
5 |
|
$this->node->getLogger()->debug('[' . spl_object_hash($this->conn) . ']' . $message); |
|
189
|
5 |
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|