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 Psr\Log; |
20
|
|
|
|
21
|
|
|
use React; |
22
|
|
|
use React\EventLoop; |
23
|
|
|
use React\ZMQ; |
24
|
|
|
|
25
|
|
|
use IPub; |
26
|
|
|
use IPub\WebSocketsZMQ; |
27
|
|
|
|
28
|
|
|
use IPub\WebSocketsWAMP\Application; |
29
|
|
|
use IPub\WebSocketsWAMP\Entities; |
30
|
|
|
use IPub\WebSocketsWAMP\PushMessages; |
31
|
|
|
use IPub\WebSocketsWAMP\Serializers; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ZeroMQ consumer |
35
|
|
|
* |
36
|
|
|
* @package iPublikuj:WebSocketsZMQ! |
37
|
|
|
* @subpackage Consumer |
38
|
|
|
* |
39
|
|
|
* @author Adam Kadlec <[email protected]> |
40
|
|
|
*/ |
41
|
1 |
|
final class Consumer extends PushMessages\Consumer |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var WebSocketsZMQ\Configuration |
45
|
|
|
*/ |
46
|
|
|
private $configuration; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Serializers\PushMessageSerializer |
50
|
|
|
*/ |
51
|
|
|
private $serializer; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var ZMQ\SocketWrapper |
55
|
|
|
*/ |
56
|
|
|
private $socket; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Log\LoggerInterface |
60
|
|
|
*/ |
61
|
|
|
private $logger; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param WebSocketsZMQ\Configuration $configuration |
65
|
|
|
* @param Serializers\PushMessageSerializer $serializer |
66
|
|
|
* @param Log\LoggerInterface|NULL $logger |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
69
|
|
|
WebSocketsZMQ\Configuration $configuration, |
70
|
|
|
Serializers\PushMessageSerializer $serializer, |
71
|
|
|
Log\LoggerInterface $logger = NULL |
72
|
|
|
) { |
73
|
1 |
|
parent::__construct('zmq'); |
74
|
|
|
|
75
|
1 |
|
$this->configuration = $configuration; |
76
|
1 |
|
$this->serializer = $serializer; |
77
|
1 |
|
$this->logger = $logger === NULL ? new Log\NullLogger : $logger; |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function connect(EventLoop\LoopInterface $loop, Application\IApplication $application) |
84
|
|
|
{ |
85
|
|
|
$context = new ZMQ\Context($loop); |
86
|
|
|
|
87
|
|
|
$this->socket = $context->getSocket(\ZMQ::SOCKET_PULL); |
88
|
|
|
|
89
|
|
|
$this->logger->info(sprintf( |
90
|
|
|
'ZMQ transport listening on %s:%s', |
91
|
|
|
$this->configuration->getHost(), |
92
|
|
|
$this->configuration->getPort() |
93
|
|
|
)); |
94
|
|
|
|
95
|
|
|
$this->socket->bind($this->configuration->getProtocol() . '://' . $this->configuration->getHost() . ':' . $this->configuration->getPort()); |
96
|
|
|
|
97
|
|
|
$this->socket->on('message', function ($data) use ($application) { |
98
|
|
|
try { |
99
|
|
|
/** @var Entities\PushMessages\IMessage $message */ |
100
|
|
|
$message = $this->serializer->deserialize($data); |
101
|
|
|
|
102
|
|
|
$application->handlePush($message, $this->getName()); |
103
|
|
|
|
104
|
|
|
$this->onSuccess($this, $data); |
105
|
|
|
|
106
|
|
|
} catch (\Exception $ex) { |
107
|
|
|
$this->logger->error( |
108
|
|
|
'ZMQ socket failed to ack message', [ |
109
|
|
|
'exception_message' => $ex->getMessage(), |
110
|
|
|
'file' => $ex->getFile(), |
111
|
|
|
'line' => $ex->getLine(), |
112
|
|
|
'message' => $data, |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$this->onFail($this, $data); |
117
|
|
|
} |
118
|
|
|
}); |
119
|
|
|
|
120
|
|
|
$this->socket->on('error', function (\Exception $ex) use ($application) { |
121
|
|
|
$this->logger->error( |
122
|
|
|
'ZMQ socket failed to receive message', [ |
123
|
|
|
'exception_message' => $ex->getMessage(), |
124
|
|
|
'file' => $ex->getFile(), |
125
|
|
|
'line' => $ex->getLine(), |
126
|
|
|
] |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$this->onFail($this); |
130
|
|
|
}); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function close() : void |
137
|
|
|
{ |
138
|
|
|
$this->socket ?: $this->socket->close(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
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.