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 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, PacketIdentifierFunctionality; |
23
|
|
|
|
24
|
|
|
const CONTROL_PACKET_VALUE = 5; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $rawMQTTHeaders |
28
|
|
|
* @param ClientInterface $client |
29
|
|
|
* @return ReadableContentInterface |
30
|
|
|
* @throws \OutOfRangeException |
31
|
|
|
*/ |
32
|
1 |
|
public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface |
33
|
|
|
{ |
34
|
1 |
|
$this->setPacketIdentifierFromRawHeaders($rawMQTTHeaders); |
35
|
1 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates the variable header that each method has |
40
|
|
|
* @return string |
41
|
|
|
* @throws \OutOfRangeException |
42
|
|
|
*/ |
43
|
|
|
public function createVariableHeader(): string |
44
|
|
|
{ |
45
|
|
|
return $this->getPacketIdentifierBinaryRepresentation(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Creates the actual payload to be sent |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function createPayload(): string |
53
|
|
|
{ |
54
|
|
|
return ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Some responses won't expect an answer back, others do in some situations |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function shouldExpectAnswer(): bool |
62
|
|
|
{ |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Any class can overwrite the default behaviour |
68
|
|
|
* @param ClientInterface $client |
69
|
|
|
* @param WritableContentInterface $originalRequest |
70
|
|
|
* @return bool |
71
|
|
|
* @throws \LogicException |
72
|
|
|
*/ |
73
|
|
|
public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool |
74
|
|
|
{ |
75
|
|
|
$this->controlPacketIdentifiers($originalRequest); |
76
|
|
|
$pubRel = new PubRel($this->logger); |
77
|
|
|
$pubRel->setPacketIdentifier($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
|
|
|
|