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