1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Consumer.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:WebSocketsZMQ! |
9
|
|
|
* @subpackage Consumer |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 28.02.17 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\WebSocketsZMQ\Consumer; |
18
|
|
|
|
19
|
|
|
use Closure; |
20
|
|
|
use ZMQ; |
21
|
|
|
use ZMQSocketException; |
22
|
|
|
use Throwable; |
23
|
|
|
|
24
|
|
|
use Psr\Log; |
25
|
|
|
|
26
|
|
|
use React; |
27
|
|
|
use React\EventLoop; |
28
|
|
|
use React\ZMQ as ReactZMQ; |
29
|
|
|
|
30
|
|
|
use IPub; |
31
|
|
|
use IPub\WebSocketsZMQ; |
32
|
|
|
|
33
|
|
|
use IPub\WebSocketsWAMP\Application; |
34
|
|
|
use IPub\WebSocketsWAMP\Entities; |
35
|
|
|
use IPub\WebSocketsWAMP\PushMessages; |
36
|
|
|
use IPub\WebSocketsWAMP\Serializers; |
37
|
|
|
|
38
|
|
|
use IPub\WebSockets\Exceptions as WebSocketsExceptions; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* ZeroMQ consumer |
42
|
|
|
* |
43
|
|
|
* @package iPublikuj:WebSocketsZMQ! |
44
|
|
|
* @subpackage Consumer |
45
|
|
|
* |
46
|
|
|
* @author Adam Kadlec <[email protected]> |
47
|
|
|
* |
48
|
1 |
|
* @method onSuccess(Consumer $consumer, $data = NULL) |
49
|
|
|
* @method onFail(Consumer $consumer, $data = NULL) |
50
|
|
|
*/ |
51
|
|
|
final class Consumer extends PushMessages\Consumer |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* @var Closure |
55
|
|
|
*/ |
56
|
|
|
public $onSuccess = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Closure |
60
|
|
|
*/ |
61
|
|
|
public $onFail = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var WebSocketsZMQ\Configuration |
65
|
|
|
*/ |
66
|
|
|
private $configuration; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var Serializers\PushMessageSerializer |
70
|
|
|
*/ |
71
|
|
|
private $serializer; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var ReactZMQ\SocketWrapper |
75
|
|
|
*/ |
76
|
|
|
private $socket; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var Log\LoggerInterface |
80
|
|
|
*/ |
81
|
|
|
private $logger; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param WebSocketsZMQ\Configuration $configuration |
85
|
|
|
* @param Serializers\PushMessageSerializer $serializer |
86
|
|
|
* @param Log\LoggerInterface|NULL $logger |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
89
|
|
|
WebSocketsZMQ\Configuration $configuration, |
90
|
1 |
|
Serializers\PushMessageSerializer $serializer, |
91
|
|
|
Log\LoggerInterface $logger = NULL |
92
|
1 |
|
) { |
93
|
1 |
|
parent::__construct('zmq'); |
94
|
1 |
|
|
95
|
1 |
|
$this->configuration = $configuration; |
96
|
|
|
$this->serializer = $serializer; |
97
|
|
|
$this->logger = $logger === NULL ? new Log\NullLogger : $logger; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
* |
103
|
|
|
* @throws ZMQSocketException |
104
|
|
|
*/ |
105
|
|
|
public function connect(EventLoop\LoopInterface $loop, Application\IApplication $application) |
106
|
|
|
{ |
107
|
|
|
$context = new ReactZMQ\Context($loop); |
108
|
|
|
|
109
|
|
|
$this->socket = $context->getSocket(ZMQ::SOCKET_PULL); |
110
|
|
|
|
111
|
|
|
$this->logger->info(sprintf( |
112
|
|
|
'ZMQ transport listening on %s:%s', |
113
|
|
|
$this->configuration->getHost(), |
114
|
|
|
$this->configuration->getPort() |
115
|
|
|
)); |
116
|
|
|
|
117
|
|
|
$this->socket->bind($this->configuration->getProtocol() . '://' . $this->configuration->getHost() . ':' . $this->configuration->getPort()); |
118
|
|
|
|
119
|
|
|
$this->socket->on('message', function ($data) use ($application) { |
120
|
|
|
try { |
121
|
|
|
/** @var Entities\PushMessages\IMessage $message */ |
122
|
|
|
$message = $this->serializer->deserialize($data); |
123
|
|
|
|
124
|
|
|
$application->handlePush($message, $this->getName()); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$this->onSuccess($this, $data); |
127
|
|
|
|
128
|
|
|
} catch (WebSocketsExceptions\TerminateException $ex) { |
|
|
|
|
129
|
|
|
throw $ex; |
130
|
|
|
|
131
|
|
|
} catch (Throwable $ex) { |
132
|
|
|
$this->logger->error( |
133
|
|
|
'ZMQ socket failed to ack message', [ |
134
|
|
|
'exception_message' => $ex->getMessage(), |
135
|
|
|
'file' => $ex->getFile(), |
136
|
|
|
'line' => $ex->getLine(), |
137
|
|
|
'message' => $data, |
138
|
|
|
] |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
$this->onFail($this, $data); |
142
|
|
|
} |
143
|
|
|
}); |
144
|
|
|
|
145
|
|
|
$this->socket->on('error', function (Throwable $ex) use ($application) { |
146
|
|
|
$this->logger->error( |
147
|
|
|
'ZMQ socket failed to receive message', [ |
148
|
|
|
'exception_message' => $ex->getMessage(), |
149
|
|
|
'file' => $ex->getFile(), |
150
|
|
|
'line' => $ex->getLine(), |
151
|
|
|
] |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
$this->onFail($this); |
155
|
|
|
}); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
1 |
|
public function close() : void |
162
|
|
|
{ |
163
|
|
|
$this->socket ?: $this->socket->close(); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.