|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Protocol; |
|
6
|
|
|
|
|
7
|
|
|
use LogicException; |
|
8
|
|
|
use OutOfRangeException; |
|
9
|
|
|
use unreal4u\MQTT\Internals\ClientInterface; |
|
10
|
|
|
use unreal4u\MQTT\Internals\PacketIdentifierFunctionality; |
|
11
|
|
|
use unreal4u\MQTT\Internals\ProtocolBase; |
|
12
|
|
|
use unreal4u\MQTT\Internals\ReadableContent; |
|
13
|
|
|
use unreal4u\MQTT\Internals\ReadableContentInterface; |
|
14
|
|
|
use unreal4u\MQTT\Internals\WritableContent; |
|
15
|
|
|
use unreal4u\MQTT\Internals\WritableContentInterface; |
|
16
|
|
|
|
|
17
|
|
|
use function strlen; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* A PUBREL Packet is the response to a PUBREC Packet. |
|
21
|
|
|
* |
|
22
|
|
|
* It is the third packet of the QoS 2 protocol exchange. |
|
23
|
|
|
* |
|
24
|
|
|
* QoS lvl2: |
|
25
|
|
|
* First packet: PUBLISH |
|
26
|
|
|
* Second packet: PUBREC |
|
27
|
|
|
* Third packet: PUBREL |
|
28
|
|
|
* Fourth packet: PUBCOMP |
|
29
|
|
|
* |
|
30
|
|
|
* @see https://go.gliffy.com/go/publish/12498076 |
|
31
|
|
|
*/ |
|
32
|
|
|
final class PubRel extends ProtocolBase implements ReadableContentInterface, WritableContentInterface |
|
33
|
|
|
{ |
|
34
|
1 |
|
use ReadableContent; |
|
35
|
|
|
use /** @noinspection TraitsPropertiesConflictsInspection */ |
|
36
|
1 |
|
WritableContent; |
|
37
|
1 |
|
use PacketIdentifierFunctionality; |
|
38
|
|
|
|
|
39
|
|
|
private const CONTROL_PACKET_VALUE = 6; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $rawMQTTHeaders |
|
43
|
|
|
* @param ClientInterface $client |
|
44
|
|
|
* @return ReadableContentInterface |
|
45
|
|
|
* @throws OutOfRangeException |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface |
|
48
|
|
|
{ |
|
49
|
3 |
|
$rawHeadersSize = strlen($rawMQTTHeaders); |
|
50
|
|
|
// A PubRel message is always 4 bytes in size |
|
51
|
3 |
|
if ($rawHeadersSize !== 4) { |
|
52
|
2 |
|
$this->logger->debug('Headers are smaller than 4 bytes, retrieving the rest', [ |
|
53
|
2 |
|
'currentSize' => $rawHeadersSize |
|
54
|
|
|
]); |
|
55
|
2 |
|
$rawMQTTHeaders .= $client->readBrokerData(4 - $rawHeadersSize); |
|
56
|
|
|
} |
|
57
|
3 |
|
$this->setPacketIdentifierFromRawHeaders($rawMQTTHeaders); |
|
58
|
3 |
|
$this->logger->debug('Determined packet identifier', [ |
|
59
|
3 |
|
'PI' => $this->packetIdentifier->getPacketIdentifierValue() |
|
60
|
|
|
]); |
|
61
|
|
|
|
|
62
|
3 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Creates the variable header that each method has |
|
67
|
|
|
* @return string |
|
68
|
|
|
* @throws OutOfRangeException |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function createVariableHeader(): string |
|
71
|
|
|
{ |
|
72
|
1 |
|
$this->specialFlags |= 2; |
|
73
|
1 |
|
return $this->getPacketIdentifierBinaryRepresentation(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Creates the actual payload to be sent |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function createPayload(): string |
|
81
|
|
|
{ |
|
82
|
1 |
|
return ''; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* PUBREL should ALWAYS expect an answer back (in the form of a PUBCOMP) |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function shouldExpectAnswer(): bool |
|
90
|
|
|
{ |
|
91
|
1 |
|
return true; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param ClientInterface $client |
|
96
|
|
|
* @param WritableContentInterface $originalRequest |
|
97
|
|
|
* @return bool |
|
98
|
|
|
* @throws LogicException |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool |
|
101
|
|
|
{ |
|
102
|
2 |
|
if ($originalRequest instanceof PubRec) { |
|
103
|
1 |
|
$this->controlPacketIdentifiers($originalRequest); |
|
104
|
1 |
|
$pubComp = new PubComp($this->logger); |
|
105
|
1 |
|
$pubComp->setPacketIdentifier($this->packetIdentifier); |
|
106
|
1 |
|
$client->processObject($pubComp); |
|
107
|
|
|
|
|
108
|
1 |
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
$this->logger->warning('Original request NOT a PubRec, ignoring object entirely'); |
|
112
|
1 |
|
return false; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @inheritdoc |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function getOriginControlPacket(): int |
|
119
|
|
|
{ |
|
120
|
1 |
|
return PubRec::getControlPacketValue(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|