1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Protocol; |
6
|
|
|
|
7
|
|
|
use unreal4u\MQTT\DataTypes\PacketIdentifier as PacketIdentifierDataType; |
8
|
|
|
use unreal4u\MQTT\Internals\ClientInterface; |
9
|
|
|
use unreal4u\MQTT\Internals\PacketIdentifier; |
10
|
|
|
use unreal4u\MQTT\Internals\ProtocolBase; |
11
|
|
|
use unreal4u\MQTT\Internals\ReadableContent; |
12
|
|
|
use unreal4u\MQTT\Internals\ReadableContentInterface; |
13
|
|
|
use unreal4u\MQTT\Internals\WritableContent; |
14
|
|
|
use unreal4u\MQTT\Internals\WritableContentInterface; |
15
|
|
|
use unreal4u\MQTT\Utilities; |
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
|
|
|
final class PubRec extends ProtocolBase implements ReadableContentInterface, WritableContentInterface |
23
|
|
|
{ |
24
|
|
|
use ReadableContent, WritableContent, PacketIdentifier; |
25
|
|
|
|
26
|
|
|
const CONTROL_PACKET_VALUE = 5; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $rawMQTTHeaders |
30
|
|
|
* @param ClientInterface $client |
31
|
|
|
* @return ReadableContentInterface |
32
|
|
|
* @throws \OutOfRangeException |
33
|
|
|
*/ |
34
|
|
|
public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface |
35
|
|
|
{ |
36
|
|
|
$this->setPacketIdentifier(new PacketIdentifierDataType($this->extractPacketIdentifier($rawMQTTHeaders))); |
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Creates the variable header that each method has |
42
|
|
|
* @return string |
43
|
|
|
* @throws \OutOfRangeException |
44
|
|
|
*/ |
45
|
|
|
public function createVariableHeader(): string |
46
|
|
|
{ |
47
|
|
|
return $this->getPacketIdentifierBinaryRepresentation(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Creates the actual payload to be sent |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function createPayload(): string |
55
|
|
|
{ |
56
|
|
|
return ''; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Some responses won't expect an answer back, others do in some situations |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function shouldExpectAnswer(): bool |
64
|
|
|
{ |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Any class can overwrite the default behaviour |
70
|
|
|
* @param ClientInterface $client |
71
|
|
|
* @param WritableContentInterface $originalRequest |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool |
75
|
|
|
{ |
76
|
|
|
$pubRel = new PubRel($this->logger); |
77
|
|
|
$pubRel->packetIdentifier = $this->packetIdentifier; |
|
|
|
|
78
|
|
|
$pubComp = $client->processObject($pubRel); |
79
|
|
|
$this->logger->debug('Created PubRel as response, got PubComp back', ['PubComp' => $pubComp]); |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
|
|
public function getOriginControlPacket(): int |
87
|
|
|
{ |
88
|
|
|
return Publish::getControlPacketValue(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|