1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Application; |
6
|
|
|
|
7
|
|
|
use unreal4u\MQTT\DataTypes\QoSLevel; |
8
|
|
|
use unreal4u\MQTT\Exceptions\MessageTooBig; |
9
|
|
|
use unreal4u\MQTT\Exceptions\MissingTopicName; |
10
|
|
|
use unreal4u\MQTT\Internals\ProtocolBase; |
11
|
|
|
|
12
|
|
|
final class Message extends ProtocolBase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* This field indicates the level of assurance for delivery of an Application Message. Can be 0, 1 or 2 |
16
|
|
|
* |
17
|
|
|
* 0: At most once delivery (default) |
18
|
|
|
* 1: At least once delivery |
19
|
|
|
* 2: Exactly once delivery |
20
|
|
|
* |
21
|
|
|
* @var QoSLevel |
22
|
|
|
*/ |
23
|
|
|
private $qosLevel; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $payload; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* If the RETAIN flag is set to 1, in a PUBLISH Packet sent by a Client to a Server, the Server MUST store the |
32
|
|
|
* Application Message and its QoS, so that it can be delivered to future subscribers whose subscriptions match its |
33
|
|
|
* topic name |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private $isRetained = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The Topic Name identifies the information channel to which payload data is published |
40
|
|
|
* @var Topic |
41
|
|
|
*/ |
42
|
|
|
private $topic; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Will perform validation on the message before sending it to the MQTT broker |
46
|
|
|
* |
47
|
|
|
* @return Message |
48
|
|
|
* @throws \unreal4u\MQTT\Exceptions\MissingTopicName |
49
|
|
|
* @throws \unreal4u\MQTT\Exceptions\MessageTooBig |
50
|
|
|
*/ |
51
|
1 |
|
public function validateMessage(): Message |
52
|
|
|
{ |
53
|
1 |
|
if ($this->getTopicName() === '') { |
54
|
|
|
$this->logger->error('Topic name is empty, probably not filled in beforehand'); |
55
|
|
|
throw new MissingTopicName('Topic name can\'t be empty, please provide one'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (mb_strlen($this->payload) > 65535) { |
59
|
|
|
$this->logger->error('Message payload exceeds 65535 bytes'); |
60
|
|
|
throw new MessageTooBig('Message payload can not exceed 65535 bytes!'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets the actual payload to be sent to the message broker |
68
|
|
|
* |
69
|
|
|
* @param string $payload |
70
|
|
|
* @return Message |
71
|
|
|
*/ |
72
|
|
|
public function setPayload(string $payload): Message |
73
|
|
|
{ |
74
|
|
|
$this->payload = $payload; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getPayload(): string |
79
|
|
|
{ |
80
|
|
|
return $this->payload; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Sets the QoS level to the indicated value. Must be 0, 1 or 2. |
85
|
|
|
* |
86
|
|
|
* @param QoSLevel $level |
87
|
|
|
* @return Message |
88
|
|
|
* @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel |
89
|
|
|
*/ |
90
|
|
|
public function setQoSLevel(QosLevel $level): Message |
91
|
|
|
{ |
92
|
|
|
$this->qosLevel = $level; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Sets the retain flag to the given value |
98
|
|
|
* |
99
|
|
|
* @param bool $flag Set to true if message should be retained, false otherwise (default) |
100
|
|
|
* @return Message |
101
|
|
|
*/ |
102
|
|
|
public function setRetainFlag(bool $flag): Message |
103
|
|
|
{ |
104
|
|
|
$this->isRetained = $flag; |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Sets the topic name to the given value |
110
|
|
|
* |
111
|
|
|
* @param Topic $topic |
112
|
|
|
* @return Message |
113
|
|
|
*/ |
114
|
|
|
public function setTopic(Topic $topic): Message |
115
|
|
|
{ |
116
|
|
|
$this->topic = $topic; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Gets the topic name |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
* @throws \unreal4u\MQTT\Exceptions\MissingTopicName |
125
|
|
|
*/ |
126
|
1 |
|
public function getTopicName(): string |
127
|
|
|
{ |
128
|
1 |
|
if ($this->topic === null) { |
129
|
1 |
|
throw new MissingTopicName('A topic must be set before calling getTopicName()'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->topic->getTopicName(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Gets the current QoS level |
137
|
|
|
* |
138
|
|
|
* @return int |
139
|
|
|
*/ |
140
|
|
|
public function getQoSLevel(): int |
141
|
|
|
{ |
142
|
|
|
return $this->qosLevel->getQoSLevel(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Gets the set retain flag |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function isRetained(): bool |
151
|
|
|
{ |
152
|
|
|
return $this->isRetained; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|