|
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 Psr\Log\LoggerInterface; |
|
16
|
|
|
use Psr\Log\NullLogger; |
|
17
|
|
|
use React\EventLoop\LoopInterface; |
|
18
|
|
|
use React\Promise\Deferred; |
|
19
|
|
|
use React\Socket\Connection; |
|
20
|
|
|
use React\Socket\Server as Socket; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class Node |
|
24
|
|
|
* @package WyriHaximus\PhuninNode |
|
25
|
|
|
*/ |
|
26
|
|
|
class Node |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Current version of PhuninNode |
|
31
|
|
|
*/ |
|
32
|
|
|
const VERSION = '0.3.0-DEV'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var LoopInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $loop; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Socket |
|
41
|
|
|
*/ |
|
42
|
|
|
private $socket; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Configuration |
|
46
|
|
|
*/ |
|
47
|
|
|
private $configuration; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var LoggerInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
private $logger; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Collection of Plugins in use (PluginInterface) |
|
56
|
|
|
* |
|
57
|
|
|
* @var \SplObjectStorage |
|
58
|
|
|
*/ |
|
59
|
|
|
private $plugins; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Collection of connected clients (ConnectionContext) |
|
63
|
|
|
* |
|
64
|
|
|
* @var \SplObjectStorage |
|
65
|
|
|
*/ |
|
66
|
|
|
private $connections; |
|
67
|
|
|
|
|
68
|
|
|
private $commands; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var array |
|
72
|
|
|
*/ |
|
73
|
|
|
private $defaultConfiguration = [ |
|
74
|
|
|
'hostname' => 'HOSTNAME', |
|
75
|
|
|
'verbose' => false, |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param LoopInterface $loop |
|
80
|
|
|
* @param Socket $socket The socket to bind on |
|
81
|
|
|
* @param Configuration $configuration Node configuration |
|
82
|
|
|
* @param LoggerInterface $logger Logger |
|
83
|
|
|
*/ |
|
84
|
42 |
|
public function __construct( |
|
85
|
|
|
LoopInterface $loop, |
|
86
|
|
|
Socket $socket, |
|
87
|
|
|
CommandsCollection $commands, |
|
88
|
|
|
Configuration $configuration = null, |
|
89
|
|
|
LoggerInterface $logger = null |
|
90
|
|
|
) { |
|
91
|
|
|
|
|
92
|
42 |
|
if (false === strpos(PHP_VERSION, 'hiphop')) { |
|
93
|
42 |
|
gc_enable(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
42 |
|
$this->loop = $loop; |
|
97
|
42 |
|
$this->socket = $socket; |
|
98
|
|
|
|
|
99
|
42 |
|
if ($configuration === null) { |
|
100
|
42 |
|
$configuration = new Configuration(); |
|
101
|
|
|
} |
|
102
|
42 |
|
$configuration->applyDefaults($this->defaultConfiguration); |
|
103
|
42 |
|
$this->configuration = $configuration; |
|
104
|
|
|
|
|
105
|
42 |
|
$this->commands = $commands; |
|
106
|
42 |
|
$this->commands->setNode($this); |
|
107
|
|
|
|
|
108
|
42 |
|
if ($logger === null) { |
|
109
|
42 |
|
$logger = new NullLogger(); |
|
110
|
|
|
} |
|
111
|
42 |
|
$this->logger = $logger; |
|
112
|
|
|
|
|
113
|
42 |
|
$this->plugins = new \SplObjectStorage; |
|
114
|
42 |
|
$this->connections = new \SplObjectStorage; |
|
115
|
|
|
|
|
116
|
42 |
|
$this->socket->on('connection', [$this, 'onConnection']); |
|
117
|
42 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Shutdown Node and the underlying socket |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public function shutdown() |
|
123
|
|
|
{ |
|
124
|
1 |
|
$this->socket->shutdown(); |
|
125
|
1 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param Connection $conn |
|
129
|
|
|
*/ |
|
130
|
3 |
|
public function onConnection(Connection $conn) |
|
131
|
|
|
{ |
|
132
|
3 |
|
$this->connections->attach(new ConnectionContext($conn, $this)); |
|
133
|
3 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Detach connection from connection collection |
|
137
|
|
|
* |
|
138
|
|
|
* @param ConnectionContext $connection |
|
139
|
|
|
*/ |
|
140
|
2 |
|
public function onClose(ConnectionContext $connection) |
|
141
|
|
|
{ |
|
142
|
2 |
|
$this->connections->detach($connection); |
|
143
|
2 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Attach a plugin |
|
147
|
|
|
* |
|
148
|
|
|
* @param PluginInterface $plugin |
|
149
|
|
|
*/ |
|
150
|
3 |
|
public function addPlugin(PluginInterface $plugin) |
|
151
|
|
|
{ |
|
152
|
3 |
|
$plugin->setNode($this); |
|
153
|
|
|
|
|
154
|
3 |
|
$this->plugins->attach($plugin); |
|
155
|
3 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns the plugins collection |
|
159
|
|
|
* |
|
160
|
|
|
* @return \SplObjectStorage |
|
161
|
|
|
*/ |
|
162
|
3 |
|
public function getPlugins(): \SplObjectStorage |
|
163
|
|
|
{ |
|
164
|
3 |
|
return $this->plugins; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Return the connection collection |
|
169
|
|
|
* |
|
170
|
|
|
* @return \SplObjectStorage |
|
171
|
|
|
*/ |
|
172
|
4 |
|
public function getConnections(): \SplObjectStorage |
|
173
|
|
|
{ |
|
174
|
4 |
|
return $this->connections; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* return the react |
|
179
|
|
|
* |
|
180
|
|
|
* @return LoopInterface |
|
181
|
|
|
*/ |
|
182
|
4 |
|
public function getLoop(): LoopInterface |
|
183
|
|
|
{ |
|
184
|
4 |
|
return $this->loop; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @return Configuration |
|
189
|
|
|
*/ |
|
190
|
5 |
|
public function getConfiguration(): Configuration |
|
191
|
|
|
{ |
|
192
|
5 |
|
return $this->configuration; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @return CommandsCollection |
|
197
|
|
|
*/ |
|
198
|
1 |
|
public function getCommands() |
|
199
|
|
|
{ |
|
200
|
1 |
|
return $this->commands; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @return LoggerInterface |
|
205
|
|
|
*/ |
|
206
|
5 |
|
public function getLogger(): LoggerInterface |
|
207
|
|
|
{ |
|
208
|
5 |
|
return $this->logger; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Get a plugin by slug or return false when none can be found |
|
213
|
|
|
* |
|
214
|
|
|
* @param string $slug |
|
215
|
|
|
* @return bool|PluginInterface |
|
216
|
|
|
*/ |
|
217
|
1 |
|
public function getPlugin($slug) |
|
218
|
|
|
{ |
|
219
|
1 |
|
$this->plugins->rewind(); |
|
220
|
1 |
|
while ($this->plugins->valid()) { |
|
221
|
1 |
|
if ($this->plugins->current()->getSlug() == $slug) { |
|
222
|
1 |
|
return $this->plugins->current(); |
|
223
|
|
|
} |
|
224
|
1 |
|
$this->plugins->next(); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
1 |
|
return false; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|