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