1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slince/spike package. |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Spike\Server; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Doctrine\Common\Collections\Collection; |
16
|
|
|
use React\EventLoop\Factory; |
17
|
|
|
use React\EventLoop\LoopInterface; |
18
|
|
|
use React\Socket\ConnectionInterface; |
19
|
|
|
use React\Socket\Server as Socket; |
20
|
|
|
use function Slince\Common\jsonBuffer; |
21
|
|
|
use Slince\EventDispatcher\Dispatcher; |
22
|
|
|
use Slince\EventDispatcher\DispatcherInterface; |
23
|
|
|
use Slince\EventDispatcher\Event; |
24
|
|
|
use Spike\Client\ClientInterface; |
25
|
|
|
use Spike\Common\Logger\Logger; |
26
|
|
|
use Spike\Common\Protocol\Spike; |
27
|
|
|
use Spike\Common\Timer\MemoryWatchTimer; |
28
|
|
|
use Spike\Common\Timer\TimersAware; |
29
|
|
|
use Spike\Server\ChunkServer\ChunkServerCollection; |
30
|
|
|
use Spike\Server\ChunkServer\ChunkServerInterface; |
31
|
|
|
use Spike\Server\Event\ClientTerminateEvent; |
32
|
|
|
use Spike\Server\Event\Events; |
33
|
|
|
use Spike\Server\Event\FilterActionHandlerEvent; |
34
|
|
|
use Symfony\Component\Console\Application; |
35
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
36
|
|
|
use Symfony\Component\Console\Input\InputOption; |
37
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
38
|
|
|
|
39
|
|
|
class Server extends Application implements ServerInterface |
40
|
|
|
{ |
41
|
|
|
use TimersAware; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
const LOGO = <<<EOT |
47
|
|
|
_____ _____ _ _ _ _____ _____ |
48
|
|
|
/ ___/ | _ \ | | | | / / | ____| | _ \ |
49
|
|
|
| |___ | |_| | | | | |/ / | |__ | | | | |
50
|
|
|
\___ \ | ___/ | | | |\ \ | __| | | | | |
51
|
|
|
___| | | | | | | | \ \ | |___ | |_| | |
52
|
|
|
/_____/ |_| |_| |_| \_\ |_____| |_____/ |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
EOT; |
56
|
|
|
|
57
|
|
|
const NAME = 'Spike Server'; |
58
|
|
|
|
59
|
|
|
const VERSION = '0.0.1'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var Configuration |
63
|
|
|
*/ |
64
|
|
|
protected $configuration; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var LoopInterface |
68
|
|
|
*/ |
69
|
|
|
protected $eventLoop; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var ChunkServerInterface[]|ChunkServerCollection |
73
|
|
|
*/ |
74
|
|
|
protected $chunkServers; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var ClientInterface[]|Collection |
78
|
|
|
*/ |
79
|
|
|
protected $clients; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var DispatcherInterface |
83
|
|
|
*/ |
84
|
|
|
protected $eventDispatcher; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var Logger |
88
|
|
|
*/ |
89
|
|
|
protected $logger; |
90
|
|
|
|
91
|
|
|
public function __construct(Configuration $configuration, LoopInterface $eventLoop = null) |
92
|
|
|
{ |
93
|
|
|
$this->configuration = $configuration; |
94
|
|
|
$this->eventLoop = $eventLoop ?: Factory::create(); |
95
|
|
|
$this->eventDispatcher = new Dispatcher(); |
96
|
|
|
$this->chunkServers = new ChunkServerCollection(); |
97
|
|
|
$this->clients = new ArrayCollection(); |
98
|
|
|
$this->initializeEvents(); |
99
|
|
|
parent::__construct(static::NAME, static::VERSION); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function getHelp() |
106
|
|
|
{ |
107
|
|
|
return static::LOGO.parent::getHelp(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function doRun(InputInterface $input, OutputInterface $output) |
114
|
|
|
{ |
115
|
|
|
$this->logger = new Logger( |
116
|
|
|
$this->eventLoop, |
117
|
|
|
$this->getConfiguration()->getLogLevel(), |
118
|
|
|
$this->getConfiguration()->getLogFile(), |
119
|
|
|
$output |
120
|
|
|
); |
121
|
|
|
// Execute command if the command name is exists |
122
|
|
|
if ($this->getCommandName($input) || |
|
|
|
|
123
|
|
|
true === $input->hasParameterOption(array('--help', '-h'), true) |
124
|
|
|
) { |
125
|
|
|
$exitCode = parent::doRun($input, $output); |
126
|
|
|
} else { |
127
|
|
|
$exitCode = $this->start(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $exitCode; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
* |
136
|
|
|
* @codeCoverageIgnore |
137
|
|
|
*/ |
138
|
|
|
public function start() |
139
|
|
|
{ |
140
|
|
|
$server = new Socket($this->configuration->getAddress(), $this->eventLoop); |
141
|
|
|
$this->eventDispatcher->dispatch(Events::SERVER_RUN); |
142
|
|
|
$server->on('connection', [$this, 'handleControlConnection']); |
143
|
|
|
|
144
|
|
|
$this->initializeTimers(); |
145
|
|
|
$this->eventLoop->run(); |
146
|
|
|
|
147
|
|
|
return 0; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function stopClient(ClientInterface $client, $closedBy = null) |
154
|
|
|
{ |
155
|
|
|
$chunkServers = $this->chunkServers->filter(function(ChunkServerInterface $chunkServer) use ($client){ |
156
|
|
|
return $client === $chunkServer->getClient(); |
157
|
|
|
}); |
158
|
|
|
|
159
|
|
|
//Fires the client terminate event |
160
|
|
|
$event = new ClientTerminateEvent($client, $chunkServers, $closedBy); |
161
|
|
|
$this->eventDispatcher->dispatch($event); |
162
|
|
|
|
163
|
|
|
foreach ($chunkServers as $chunkServer) { |
164
|
|
|
//Close the tunnel server and remove it |
165
|
|
|
$chunkServer->stop(); |
166
|
|
|
$this->chunkServers->removeElement($chunkServer); |
167
|
|
|
} |
168
|
|
|
$client->close(); //Close the client |
169
|
|
|
$this->clients->removeElement($client); //Removes the client |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Handle control connection. |
174
|
|
|
* |
175
|
|
|
* @param ConnectionInterface $connection |
176
|
|
|
*/ |
177
|
|
|
public function handleControlConnection(ConnectionInterface $connection) |
178
|
|
|
{ |
179
|
|
|
jsonBuffer($connection, function($messages) use ($connection){ |
180
|
|
|
foreach ($messages as $messageData) { |
181
|
|
|
if (!$messageData) { |
182
|
|
|
continue; |
183
|
|
|
} |
184
|
|
|
$message = Spike::fromArray($messageData); |
185
|
|
|
|
186
|
|
|
//Fires filter action handler event |
187
|
|
|
$event = new FilterActionHandlerEvent($this, $message, $connection); |
188
|
|
|
$this->eventDispatcher->dispatch($event); |
189
|
|
|
if ($actionHandler = $event->getActionHandler()) { |
190
|
|
|
try { |
191
|
|
|
$actionHandler->handle($message); |
192
|
|
|
} catch (\Exception $exception) { |
193
|
|
|
//Ignore bad message |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
}, function($exception) use ($connection){ |
198
|
|
|
$this->eventDispatcher->dispatch(new Event(Events::CONNECTION_ERROR, $this, [ |
199
|
|
|
'connection' => $connection, |
200
|
|
|
'exception' => $exception, |
201
|
|
|
])); |
202
|
|
|
}); |
203
|
|
|
//Disconnect |
204
|
|
|
$connection->on('close', function() use($connection){ |
205
|
|
|
//If client has been registered and then close it. |
206
|
|
|
$client = $this->clients->filter(function(ClientInterface $client) use ($connection){ |
207
|
|
|
return $client->getControlConnection() === $connection; |
208
|
|
|
})->first(); |
209
|
|
|
if ($client) { |
210
|
|
|
$this->stopClient($client, ClientTerminateEvent::CLOSED_BY_REMOTE); |
211
|
|
|
} else { |
212
|
|
|
$connection->end(); |
213
|
|
|
} |
214
|
|
|
}); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public function getChunkServers() |
221
|
|
|
{ |
222
|
|
|
return $this->chunkServers; |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return LoopInterface |
227
|
|
|
*/ |
228
|
|
|
public function getEventLoop() |
229
|
|
|
{ |
230
|
|
|
return $this->eventLoop; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* {@inheritdoc} |
235
|
|
|
*/ |
236
|
|
|
public function getEventDispatcher() |
237
|
|
|
{ |
238
|
|
|
return $this->eventDispatcher; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return Configuration |
243
|
|
|
*/ |
244
|
|
|
public function getConfiguration() |
245
|
|
|
{ |
246
|
|
|
return $this->configuration; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Gets all clients. |
251
|
|
|
* |
252
|
|
|
* @return Collection|ClientInterface[] |
253
|
|
|
*/ |
254
|
|
|
public function getClients() |
255
|
|
|
{ |
256
|
|
|
return $this->clients; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Gets the client by ID. |
261
|
|
|
* |
262
|
|
|
* @param string $id |
263
|
|
|
* |
264
|
|
|
* @return null|ClientInterface |
265
|
|
|
*/ |
266
|
|
|
public function getClientById($id) |
267
|
|
|
{ |
268
|
|
|
return $this->clients->filter(function(Client $client) use ($id){ |
269
|
|
|
return $client->getId() === $id; |
270
|
|
|
})->first(); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return Logger |
275
|
|
|
*/ |
276
|
|
|
public function getLogger() |
277
|
|
|
{ |
278
|
|
|
return $this->logger; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
protected function initializeEvents() |
282
|
|
|
{ |
283
|
|
|
$this->eventDispatcher->addSubscriber(new Listener\ServerListener()); |
284
|
|
|
$this->eventDispatcher->addSubscriber(new Listener\LoggerListener($this)); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Creates default timers. |
289
|
|
|
* |
290
|
|
|
* @codeCoverageIgnore |
291
|
|
|
*/ |
292
|
|
|
protected function initializeTimers() |
293
|
|
|
{ |
294
|
|
|
$this->addTimer(new Timer\ClientScanTimer($this)); |
295
|
|
|
$this->addTimer(new Timer\SummaryWatchTimer($this)); |
296
|
|
|
$this->addTimer(new MemoryWatchTimer($this->getLogger())); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* {@inheritdoc} |
301
|
|
|
*/ |
302
|
|
|
protected function getDefaultCommands() |
303
|
|
|
{ |
304
|
|
|
return array_merge(parent::getDefaultCommands(), [ |
|
|
|
|
305
|
|
|
new Command\InitCommand($this), |
306
|
|
|
]); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* {@inheritdoc} |
311
|
|
|
*/ |
312
|
|
|
protected function getDefaultInputDefinition() |
313
|
|
|
{ |
314
|
|
|
$definition = parent::getDefaultInputDefinition(); |
315
|
|
|
$definition->addOptions([ |
316
|
|
|
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'The configuration file, support json,ini,xml and yaml format'), |
317
|
|
|
new InputOption('address', 'a', InputOption::VALUE_REQUIRED, 'The server address'), |
318
|
|
|
]); |
319
|
|
|
|
320
|
|
|
return $definition; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: