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