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\ProtocolBase; |
9
|
|
|
use unreal4u\MQTT\Internals\ReadableContent; |
10
|
|
|
use unreal4u\MQTT\Internals\ReadableContentInterface; |
11
|
|
|
use unreal4u\MQTT\Internals\WritableContent; |
12
|
|
|
use unreal4u\MQTT\Internals\WritableContentInterface; |
13
|
|
|
use unreal4u\MQTT\Utilities; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A PUBREC Packet is the response to a PUBLISH Packet with QoS 2. |
17
|
|
|
* |
18
|
|
|
* It is the second packet of the QoS 2 protocol exchange. |
19
|
|
|
*/ |
20
|
|
|
final class PubRec extends ProtocolBase implements ReadableContentInterface, WritableContentInterface |
21
|
|
|
{ |
22
|
|
|
use ReadableContent, WritableContent; |
23
|
|
|
|
24
|
|
|
public $packetIdentifier = 0; |
25
|
|
|
|
26
|
|
|
const CONTROL_PACKET_VALUE = 5; |
27
|
|
|
|
28
|
|
|
public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface |
29
|
|
|
{ |
30
|
|
|
$this->packetIdentifier = $this->extractPacketIdentifier($rawMQTTHeaders); |
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Creates the variable header that each method has |
36
|
|
|
* @return string |
37
|
|
|
* @throws \OutOfRangeException |
38
|
|
|
*/ |
39
|
|
|
public function createVariableHeader(): string |
40
|
|
|
{ |
41
|
|
|
return Utilities::convertNumberToBinaryString($this->packetIdentifier); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Creates the actual payload to be sent |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function createPayload(): string |
49
|
|
|
{ |
50
|
|
|
return ''; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Some responses won't expect an answer back, others do in some situations |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function shouldExpectAnswer(): bool |
58
|
|
|
{ |
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Any class can overwrite the default behaviour |
64
|
|
|
* @param ClientInterface $client |
65
|
|
|
* @param WritableContentInterface $originalRequest |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool |
69
|
|
|
{ |
70
|
|
|
$pubRel = new PubRel($this->logger); |
71
|
|
|
$pubRel->packetIdentifier = $this->packetIdentifier; |
72
|
|
|
$pubComp = $client->sendData($pubRel); |
73
|
|
|
$this->logger->debug('Created PubRel as response, got PubComp back', ['PubComp' => $pubComp]); |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
|
|
public function originPacketIdentifier(): int |
81
|
|
|
{ |
82
|
|
|
return Publish::getControlPacketValue(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|