1 | <?php |
||
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 | 6 | public function __construct(DuplexStreamInterface $conn, Node $node) |
|
65 | { |
||
66 | 6 | $this->conn = $conn; |
|
67 | 6 | $this->node = $node; |
|
68 | 6 | $this->loop = $node->getLoop(); |
|
69 | |||
70 | 6 | $this->conn->on('data', [$this, 'onData']); |
|
71 | 6 | $this->conn->on('close', [$this, 'onClose']); |
|
72 | |||
73 | 6 | $this->write( |
|
74 | sprintf( |
||
75 | 6 | self::GREETING, |
|
76 | 6 | $node->getConfiguration() |
|
77 | 6 | ->getPair('hostname') |
|
78 | 6 | ->getValue() |
|
79 | ) |
||
80 | ); |
||
81 | 6 | } |
|
82 | |||
83 | /** |
||
84 | * Write data to the connection |
||
85 | * |
||
86 | * @param string $data |
||
87 | */ |
||
88 | 6 | protected function write(string $data) |
|
95 | |||
96 | /** |
||
97 | * Clear the timeout, close the connection, and tell node the client disconnected |
||
98 | */ |
||
99 | 2 | protected function close() |
|
104 | |||
105 | /** |
||
106 | * Set a timeout that disconnects the client when it's idle for to long |
||
107 | */ |
||
108 | 6 | protected function setTimeout() |
|
117 | |||
118 | /** |
||
119 | * Clear the timeout if it's set |
||
120 | */ |
||
121 | 6 | protected function clearTimeout() |
|
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() |
|
174 | |||
175 | /** |
||
176 | * Close connection |
||
177 | */ |
||
178 | 1 | public function onClose() |
|
179 | { |
||
180 | 1 | $this->close(); |
|
181 | 1 | } |
|
182 | |||
183 | /** |
||
184 | * @param string $message |
||
185 | */ |
||
186 | 6 | protected function log(string $message) |
|
190 | } |
||
191 |