1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Protocol; |
6
|
|
|
|
7
|
|
|
use unreal4u\MQTT\DataTypes\ProtocolVersion; |
8
|
|
|
use unreal4u\MQTT\Exceptions\Connect\NoConnectionParametersDefined; |
9
|
|
|
use unreal4u\MQTT\Exceptions\MustProvideUsername; |
10
|
|
|
use unreal4u\MQTT\Internals\ProtocolBase; |
11
|
|
|
use unreal4u\MQTT\Internals\WritableContent; |
12
|
|
|
use unreal4u\MQTT\Internals\WritableContentInterface; |
13
|
|
|
use unreal4u\MQTT\Protocol\Connect\Parameters; |
14
|
|
|
use unreal4u\MQTT\Utilities; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* After a Network Connection is established by a Client to a Server, the first Packet sent from the Client to the |
18
|
|
|
* Server MUST be a CONNECT Packet |
19
|
|
|
*/ |
20
|
|
|
final class Connect extends ProtocolBase implements WritableContentInterface |
21
|
|
|
{ |
22
|
|
|
use WritableContent; |
23
|
|
|
|
24
|
|
|
const CONTROL_PACKET_VALUE = 1; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Parameters |
28
|
|
|
*/ |
29
|
|
|
private $connectionParameters; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Saves the mandatory connection parameters onto this object |
33
|
|
|
* @param Parameters $connectionParameters |
34
|
|
|
* |
35
|
|
|
* @return Connect |
36
|
|
|
*/ |
37
|
4 |
|
public function setConnectionParameters(Parameters $connectionParameters): Connect |
38
|
|
|
{ |
39
|
4 |
|
$this->connectionParameters = $connectionParameters; |
40
|
4 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the connection parameters from the private object |
45
|
|
|
* |
46
|
|
|
* @return Parameters |
47
|
|
|
* @throws \unreal4u\MQTT\Exceptions\Connect\NoConnectionParametersDefined |
48
|
|
|
*/ |
49
|
|
|
public function getConnectionParameters(): Parameters |
50
|
|
|
{ |
51
|
|
|
if ($this->connectionParameters === null) { |
52
|
|
|
throw new NoConnectionParametersDefined('You must pass on the connection parameters before connecting'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->connectionParameters; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
* @throws \OutOfRangeException |
61
|
|
|
* @throws \unreal4u\MQTT\Exceptions\Connect\UnacceptableProtocolVersion |
62
|
|
|
*/ |
63
|
1 |
|
public function createVariableHeader(): string |
64
|
|
|
{ |
65
|
|
|
// Initialize a default value if it has not been done so already |
66
|
1 |
|
if ($this->protocolVersion === null) { |
67
|
1 |
|
$this->protocolVersion = new ProtocolVersion(); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
$bitString = $this->createUTF8String('MQTT'); // Connect MUST begin with MQTT |
71
|
1 |
|
$bitString .= $this->getProtocolVersion(); // Protocol level |
72
|
1 |
|
$bitString .= \chr($this->connectionParameters->getFlags()); |
73
|
1 |
|
$bitString .= Utilities::convertNumberToBinaryString($this->connectionParameters->keepAlivePeriod); |
74
|
1 |
|
return $bitString; |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
public function createPayload(): string |
78
|
|
|
{ |
79
|
3 |
|
$output = ''; |
80
|
|
|
// The order in a connect string is clientId first |
81
|
3 |
|
if ($this->connectionParameters->getClientId() !== '') { |
82
|
3 |
|
$output .= $this->createUTF8String($this->connectionParameters->getClientId()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Then the willTopic if it is set |
86
|
3 |
|
if ($this->connectionParameters->getWillTopic() !== '') { |
87
|
1 |
|
$output .= $this->createUTF8String($this->connectionParameters->getWillTopic()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// The willMessage will come next |
91
|
3 |
|
if ($this->connectionParameters->getWillMessage() !== '') { |
92
|
1 |
|
$output .= $this->createUTF8String($this->connectionParameters->getWillMessage()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// If the username is set, it will come next |
96
|
3 |
|
if ($this->connectionParameters->getUsername() !== '') { |
97
|
1 |
|
$output .= $this->createUTF8String($this->connectionParameters->getUsername()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// And finally the password as last parameter |
101
|
3 |
|
if ($this->connectionParameters->getPassword() !== '') { |
102
|
2 |
|
if ($this->connectionParameters->getUsername() === '') { |
103
|
1 |
|
throw new MustProvideUsername('A password can not be set without a username! Please set username'); |
104
|
|
|
} |
105
|
1 |
|
$output .= $this->createUTF8String($this->connectionParameters->getPassword()); |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
return $output; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function shouldExpectAnswer(): bool |
112
|
|
|
{ |
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|