1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Internals; |
6
|
|
|
|
7
|
|
|
use unreal4u\MQTT\Application\EmptyReadableResponse; |
8
|
|
|
use unreal4u\MQTT\Protocol\ConnAck; |
9
|
|
|
use unreal4u\MQTT\Protocol\Connect; |
10
|
|
|
use unreal4u\MQTT\Protocol\Disconnect; |
11
|
|
|
use unreal4u\MQTT\Protocol\PingReq; |
12
|
|
|
use unreal4u\MQTT\Protocol\PingResp; |
13
|
|
|
use unreal4u\MQTT\Protocol\PubAck; |
14
|
|
|
use unreal4u\MQTT\Protocol\PubComp; |
15
|
|
|
use unreal4u\MQTT\Protocol\Publish; |
16
|
|
|
use unreal4u\MQTT\Protocol\PubRec; |
17
|
|
|
use unreal4u\MQTT\Protocol\PubRel; |
18
|
|
|
use unreal4u\MQTT\Protocol\SubAck; |
19
|
|
|
use unreal4u\MQTT\Protocol\Subscribe; |
20
|
|
|
use unreal4u\MQTT\Protocol\UnsubAck; |
21
|
|
|
use unreal4u\MQTT\Protocol\Unsubscribe; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Is able to load in an incoming event and handle it with properly, providing the ability to actively validate |
25
|
|
|
*/ |
26
|
|
|
final class EventManager extends ProtocolBase |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Current object in string format |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $currentObjectType = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* A list of all Readable objects that this class may instantiate at some point |
36
|
|
|
* |
37
|
|
|
* @see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349209 |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private static $readableObjects = [ |
41
|
|
|
2 => ConnAck::class, |
42
|
|
|
3 => Publish::class, |
43
|
|
|
4 => PubAck::class, |
44
|
|
|
5 => PubRec::class, |
45
|
|
|
6 => PubRel::class, |
46
|
|
|
7 => PubComp::class, |
47
|
|
|
9 => SubAck::class, |
48
|
|
|
11 => UnsubAck::class, |
49
|
|
|
13 => PingResp::class, |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Not used in this class but handy to have, will maybe be used in the future? |
54
|
|
|
* |
55
|
|
|
* @see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349209 |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
/* |
59
|
|
|
private static $writableObjects = [ |
60
|
|
|
1 => Connect::class, |
61
|
|
|
3 => Publish::class, |
62
|
|
|
4 => PubAck::class, |
63
|
|
|
5 => PubRec::class, |
64
|
|
|
6 => PubRel::class, |
65
|
|
|
7 => PubComp::class, |
66
|
|
|
8 => Subscribe::class, |
67
|
|
|
10 => Unsubscribe::class, |
68
|
|
|
12 => PingReq::class, |
69
|
|
|
14 => Disconnect::class, |
70
|
|
|
]; |
71
|
|
|
*/ |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Will check within all the Readable objects whether one of those is the correct packet we are looking for |
75
|
|
|
* |
76
|
|
|
* @param string $rawMQTTHeaders Arbitrary size of minimum 1 incoming byte(s) |
77
|
|
|
* @param ClientInterface $client Used if the object itself needs to process some more stuff |
78
|
|
|
* @return ReadableContentInterface |
79
|
|
|
* @throws \DomainException |
80
|
|
|
*/ |
81
|
3 |
|
public function analyzeHeaders(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface |
82
|
|
|
{ |
83
|
3 |
|
if ($rawMQTTHeaders === '') { |
84
|
1 |
|
$this->logger->debug('Empty headers, returning an empty object'); |
85
|
1 |
|
return new EmptyReadableResponse($this->logger); |
86
|
|
|
} |
87
|
2 |
|
$controlPacketType = \ord($rawMQTTHeaders[0]) >> 4; |
88
|
|
|
|
89
|
2 |
|
if (isset(self::$readableObjects[$controlPacketType])) { |
90
|
1 |
|
$this->currentObjectType = self::$readableObjects[$controlPacketType]; |
91
|
1 |
|
$this->logger->info('Found a matching object, instantiating', [ |
92
|
1 |
|
'type' => $this->currentObjectType, |
93
|
1 |
|
'controlPacketNumber' => $controlPacketType, |
94
|
|
|
]); |
95
|
|
|
/** @var ReadableContentInterface $readableContent */ |
96
|
1 |
|
$readableContent = new $this->currentObjectType($this->logger); |
97
|
1 |
|
$readableContent->instantiateObject($rawMQTTHeaders, $client); |
98
|
|
|
} else { |
99
|
1 |
|
$this->logger->error('Invalid control packet type found', [ |
100
|
1 |
|
'controlPacketType' => $controlPacketType, |
101
|
|
|
#'rawMQTTHeaders' => str2bin($rawMQTTHeaders), |
102
|
|
|
]); |
103
|
1 |
|
throw new \DomainException(sprintf('Invalid control packet found (%d)', $controlPacketType)); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return $readableContent; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|