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
|
|
|
/** |
18
|
|
|
* A PUBREC Packet is the response to a PUBLISH Packet with QoS 2. |
19
|
|
|
* |
20
|
|
|
* It is the second packet of the QoS 2 protocol exchange. |
21
|
|
|
* |
22
|
|
|
* First packet: PUBLISH (QoS lvl2) |
23
|
|
|
* Second packet: PUBREC |
24
|
|
|
* Third packet: PUBREL |
25
|
|
|
* Fourth packet: PUBCOMP |
26
|
|
|
* |
27
|
|
|
* @see https://go.gliffy.com/go/publish/12498076 |
28
|
|
|
*/ |
29
|
|
|
final class PubRec extends ProtocolBase implements ReadableContentInterface, WritableContentInterface |
30
|
|
|
{ |
31
|
|
|
use ReadableContent; |
32
|
|
|
use /** @noinspection TraitsPropertiesConflictsInspection */ |
33
|
|
|
WritableContent; |
34
|
|
|
use PacketIdentifierFunctionality; |
35
|
|
|
|
36
|
|
|
private const CONTROL_PACKET_VALUE = 5; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $rawMQTTHeaders |
40
|
|
|
* @param ClientInterface $client |
41
|
|
|
* @return ReadableContentInterface |
42
|
|
|
* @throws OutOfRangeException |
43
|
|
|
*/ |
44
|
|
|
public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface |
45
|
|
|
{ |
46
|
|
|
$this->setPacketIdentifierFromRawHeaders($rawMQTTHeaders); |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Creates the variable header that each method has |
52
|
|
|
* @return string |
53
|
|
|
* @throws OutOfRangeException |
54
|
|
|
*/ |
55
|
1 |
|
public function createVariableHeader(): string |
56
|
|
|
{ |
57
|
1 |
|
return $this->getPacketIdentifierBinaryRepresentation(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Creates the actual payload to be sent |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
1 |
|
public function createPayload(): string |
65
|
|
|
{ |
66
|
1 |
|
return ''; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Some responses won't expect an answer back, others do in some situations |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
1 |
|
public function shouldExpectAnswer(): bool |
74
|
|
|
{ |
75
|
1 |
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Any class can overwrite the default behaviour |
80
|
|
|
* @param ClientInterface $client |
81
|
|
|
* @param WritableContentInterface $originalRequest |
82
|
|
|
* @return bool |
83
|
|
|
* @throws LogicException |
84
|
|
|
*/ |
85
|
|
|
public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool |
86
|
|
|
{ |
87
|
|
|
$this->controlPacketIdentifiers($originalRequest); |
88
|
|
|
$pubRel = new PubRel($this->logger); |
89
|
|
|
$pubRel->setPacketIdentifier($this->packetIdentifier); |
90
|
|
|
$pubComp = $client->processObject($pubRel); |
91
|
|
|
$this->logger->debug('Created PubRel as response, got PubComp back', ['PubComp' => $pubComp]); |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritdoc |
97
|
|
|
*/ |
98
|
|
|
public function getOriginControlPacket(): int |
99
|
|
|
{ |
100
|
|
|
return Publish::getControlPacketValue(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|