1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Internals; |
6
|
|
|
|
7
|
|
|
use unreal4u\MQTT\Exceptions\NonAllowedObject; |
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\Publish; |
15
|
|
|
use unreal4u\MQTT\Protocol\PubRec; |
16
|
|
|
use unreal4u\MQTT\Protocol\SubAck; |
17
|
|
|
use unreal4u\MQTT\Protocol\Subscribe; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Is able to load in an incoming event and handle it with properly, providing the ability to actively validate |
21
|
|
|
*/ |
22
|
|
|
final class EventManager extends ProtocolBase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ReadableContentInterface |
26
|
|
|
*/ |
27
|
|
|
private $currentObject; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ReadableContentInterface[] |
31
|
|
|
*/ |
32
|
|
|
private $restrictionObjects = []; |
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, TODO Implement PubRel |
46
|
|
|
#7 => PubComp::class, TODO Implement PubComp |
47
|
|
|
9 => SubAck::class, |
48
|
|
|
#11 => UnsubAck::class, TODO Implement UbsubAck |
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
|
|
|
private static $writableObjects = [ |
|
|
|
|
59
|
|
|
1 => Connect::class, |
60
|
|
|
3 => Publish::class, |
61
|
|
|
4 => PubAck::class, |
62
|
|
|
5 => PubRec::class, |
63
|
|
|
#6 => PubRel::class, |
64
|
|
|
#7 => PubComp::class, |
65
|
|
|
8 => Subscribe::class, |
66
|
|
|
#10 => Unsubscribe::class, TODO Implement Unsubscribe |
67
|
|
|
12 => PingReq::class, |
68
|
|
|
14 => Disconnect::class, |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Will check within all the Readable objects whether one of those is the correct packet we are looking for |
73
|
|
|
* |
74
|
|
|
* @param string $rawMQTTHeaders |
75
|
|
|
* @param ClientInterface $client |
76
|
|
|
* @return EventManager |
77
|
|
|
* @throws \DomainException |
78
|
|
|
*/ |
79
|
|
|
public function analyzeHeaders(string $rawMQTTHeaders, ClientInterface $client): EventManager |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$controlPacketType = \ord($rawMQTTHeaders[0]) >> 4; |
82
|
|
|
|
83
|
|
|
if (array_key_exists($controlPacketType, self::$readableObjects)) { |
84
|
|
|
$this->currentObject = new self::$readableObjects[$controlPacketType]($this->logger); |
85
|
|
|
$this->currentObject->instantiateObject($rawMQTTHeaders); |
86
|
|
|
} else { |
87
|
|
|
$this->logger->error('Invalid control packet type found', ['controlPacketType' => $controlPacketType]); |
88
|
|
|
throw new \DomainException(sprintf('Invalid control packet found (%d)', $controlPacketType)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/* |
92
|
|
|
case 3: |
93
|
|
|
$this->currentObject = new Publish($this->logger); |
94
|
|
|
$this->updateCommunication($client); |
95
|
|
|
$publishPacketControlField = $client->readSocketData(1); |
96
|
|
|
if ((\ord($publishPacketControlField) & 0xf0) > 0) { |
97
|
|
|
$restOfBytes = $client->readSocketData(1); |
98
|
|
|
$payload = $client->readSocketData(\ord($restOfBytes)); |
99
|
|
|
|
100
|
|
|
$this->currentObject->setPayloadType($payloadType); |
101
|
|
|
|
102
|
|
|
$rawMQTTHeaders = $publishPacketControlField . $restOfBytes . $payload; |
103
|
|
|
} |
104
|
|
|
*/ |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function addRestriction(ReadableContentInterface ...$restrictionObject): EventManager |
111
|
|
|
{ |
112
|
|
|
$this->restrictionObjects = $restrictionObject; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getObject(): ReadableContentInterface |
118
|
|
|
{ |
119
|
|
|
foreach ($this->restrictionObjects as $restrictionObject) { |
120
|
|
|
$this->logger->debug('Checking whether currentObject is the correct instance', [ |
121
|
|
|
'currentObject' => \get_class($this->currentObject), |
122
|
|
|
'objectCheck' => \get_class($restrictionObject), |
123
|
|
|
]); |
124
|
|
|
if ($this->currentObject instanceof $restrictionObject) { |
125
|
|
|
return $this->currentObject; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
throw new NonAllowedObject('An non allowed object has been found'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function updateCommunication(ClientInterface $client): bool |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$this->logger->debug('Checking ping'); |
135
|
|
|
if ($client->isItPingTime()) { |
136
|
|
|
$this->logger->notice('Sending ping'); |
137
|
|
|
$client->setBlocking(true); |
138
|
|
|
$client->sendData(new PingReq($this->logger)); |
139
|
|
|
$client->setBlocking(false); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|