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\Listener; |
13
|
|
|
|
14
|
|
|
use Slince\EventDispatcher\Event; |
15
|
|
|
use Slince\EventDispatcher\SubscriberInterface; |
16
|
|
|
use Spike\Common\Logger\Logger; |
17
|
|
|
use Spike\Server\Event\ClientTerminateEvent; |
18
|
|
|
use Spike\Server\Event\Events; |
19
|
|
|
use Spike\Server\Event\FilterActionHandlerEvent; |
20
|
|
|
use Spike\Server\Server; |
21
|
|
|
|
22
|
|
|
class LoggerListener implements SubscriberInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Server |
26
|
|
|
*/ |
27
|
|
|
protected $server; |
28
|
|
|
|
29
|
|
|
public function __construct(Server $server) |
30
|
|
|
{ |
31
|
|
|
$this->server = $server; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return Logger |
36
|
|
|
*/ |
37
|
|
|
protected function getLogger() |
38
|
|
|
{ |
39
|
|
|
return $this->server->getLogger(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function getSubscribedEvents() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
Events::SERVER_RUN => 'onServerRun', |
46
|
|
|
Events::ACCEPT_CONNECTION => 'onAcceptConnection', |
47
|
|
|
Events::SERVER_ACTION => 'onReceiveMessage', |
48
|
|
|
Events::SOCKET_ERROR => 'onSocketError', |
49
|
|
|
Events::CONNECTION_ERROR => 'onConnectionError', |
50
|
|
|
Events::CLIENT_CLOSE => 'onClientClose' |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function onServerRun(Event $event) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->getLogger()->info('The server is running ...'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function onAcceptConnection(Event $event) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$this->getLogger()->info('Accepted a connection.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function onReceiveMessage(FilterActionHandlerEvent $event) |
65
|
|
|
{ |
66
|
|
|
$this->getLogger()->info("Received a message:\r\n".$event->getMessage()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function onSocketError(Event $event) |
70
|
|
|
{ |
71
|
|
|
$this->getLogger()->warning('Received a error: ' |
72
|
|
|
.$event->getArgument('exception')->getMessage()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function onConnectionError(Event $event) |
76
|
|
|
{ |
77
|
|
|
$this->getLogger()->warning(sprintf('Got a bad protocol message: "%s" from "%s"', |
78
|
|
|
$event->getArgument('exception')->getMessage(), |
79
|
|
|
$event->getArgument('connection')->getRemoteAddress() |
80
|
|
|
)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function onClientClose(ClientTerminateEvent $event) |
84
|
|
|
{ |
85
|
|
|
$client = $event->getClient(); |
86
|
|
|
$message = sprintf('The client "%s[%s]" is closed by %s', |
87
|
|
|
$client->getId(), |
88
|
|
|
$client->getControlConnection()->getRemoteAddress(), |
89
|
|
|
$event->getClosedBy() |
90
|
|
|
); |
91
|
|
|
$this->getLogger()->warning($message); |
92
|
|
|
} |
93
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.