From 700a6c968a17fc6dd25997a31ce567a63885fb88 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Tue, 23 Feb 2021 11:47:48 +0800 Subject: [PATCH] Add CONNACK & PUBLISH & PINGRESP Message * [x] CONNACK * [x] PUBLISH * [x] PINGRESP --- src/Message/AbstractMessage.php | 66 +++++++++++++++++ src/Message/ConnAck.php | 66 +++++++++++++++++ src/Message/PingResp.php | 34 +++++++++ src/Message/Publish.php | 127 ++++++++++++++++++++++++++++++++ 4 files changed, 293 insertions(+) create mode 100644 src/Message/AbstractMessage.php create mode 100644 src/Message/ConnAck.php create mode 100644 src/Message/PingResp.php create mode 100644 src/Message/Publish.php diff --git a/src/Message/AbstractMessage.php b/src/Message/AbstractMessage.php new file mode 100644 index 0000000..f83e45e --- /dev/null +++ b/src/Message/AbstractMessage.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, + * please view the LICENSE file that was distributed with this source code + */ + +declare(strict_types=1); + +namespace Simps\MQTT\Message; + +use Simps\MQTT\Protocol\ProtocolInterface; + +abstract class AbstractMessage +{ + protected $protocolLevel = ProtocolInterface::MQTT_PROTOCOL_LEVEL_3_1_1; + + protected $properties = []; + + public function __construct(array $data = []) + { + foreach ($data as $k => $v) { + $methodName = 'set' . ucfirst($k); + if (method_exists($this, $methodName)) { + $this->{$methodName}($v); + } else { + $this->{$k} = $v; + } + } + } + + public function getProtocolLevel(): int + { + return $this->protocolLevel; + } + + public function setProtocolLevel(int $protocolLevel): self + { + $this->protocolLevel = $protocolLevel; + + return $this; + } + + public function getProperties(): array + { + return $this->properties; + } + + public function setProperties(array $properties): self + { + $this->properties = $properties; + + return $this; + } + + public function isMQTT5() + { + return $this->getProtocolLevel() === ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0; + } + + abstract public function __toString(); +} diff --git a/src/Message/ConnAck.php b/src/Message/ConnAck.php new file mode 100644 index 0000000..0ac7c80 --- /dev/null +++ b/src/Message/ConnAck.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, + * please view the LICENSE file that was distributed with this source code + */ + +declare(strict_types=1); + +namespace Simps\MQTT\Message; + +use Simps\MQTT\Protocol\Types; +use Simps\MQTT\Protocol\V3; +use Simps\MQTT\Protocol\V5; + +class ConnAck extends AbstractMessage +{ + protected $code = 0; + + protected $sessionPresent = 0; + + public function getCode(): int + { + return $this->code; + } + + public function setCode(int $code): self + { + $this->code = $code; + + return $this; + } + + public function getSessionPresent(): int + { + return $this->sessionPresent; + } + + public function setSessionPresent(int $sessionPresent): self + { + $this->sessionPresent = $sessionPresent; + + return $this; + } + + public function __toString() + { + $buffer = [ + 'type' => Types::CONNACK, + 'code' => $this->getCode(), + 'session_present' => $this->getSessionPresent(), + ]; + + if ($this->isMQTT5()) { + $buffer['properties'] = $this->getProperties(); + + return V5::pack($buffer); + } + + return V3::pack($buffer); + } +} diff --git a/src/Message/PingResp.php b/src/Message/PingResp.php new file mode 100644 index 0000000..bc65e3e --- /dev/null +++ b/src/Message/PingResp.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, + * please view the LICENSE file that was distributed with this source code + */ + +declare(strict_types=1); + +namespace Simps\MQTT\Message; + +use Simps\MQTT\Protocol\Types; +use Simps\MQTT\Protocol\V3; +use Simps\MQTT\Protocol\V5; + +class PingResp extends AbstractMessage +{ + public function __toString() + { + $buffer = [ + 'type' => Types::PINGRESP, + ]; + + if ($this->isMQTT5()) { + return V5::pack($buffer); + } + + return V3::pack($buffer); + } +} diff --git a/src/Message/Publish.php b/src/Message/Publish.php new file mode 100644 index 0000000..dc5a499 --- /dev/null +++ b/src/Message/Publish.php @@ -0,0 +1,127 @@ + + * + * For the full copyright and license information, + * please view the LICENSE file that was distributed with this source code + */ + +declare(strict_types=1); + +namespace Simps\MQTT\Message; + +use Simps\MQTT\Protocol\ProtocolInterface; +use Simps\MQTT\Protocol\Types; +use Simps\MQTT\Protocol\V3; +use Simps\MQTT\Protocol\V5; + +class Publish extends AbstractMessage +{ + protected $topic; + + protected $message; + + protected $qos = ProtocolInterface::MQTT_QOS_0; + + protected $dup = 0; + + protected $retain = 0; + + protected $messageId; + + public function getTopic(): string + { + return $this->topic; + } + + public function setTopic(?string $topic): self + { + $this->topic = $topic; + + return $this; + } + + public function getMessage(): string + { + return $this->message; + } + + public function setMessage(?string $message): self + { + $this->message = $message; + + return $this; + } + + public function getQos(): int + { + return $this->qos; + } + + public function setQos(int $qos): self + { + $this->qos = $qos; + + return $this; + } + + public function getDup(): int + { + return $this->dup; + } + + public function setDup(int $dup): self + { + $this->dup = $dup; + + return $this; + } + + public function getRetain(): int + { + return $this->retain; + } + + public function setRetain(int $retain): self + { + $this->retain = $retain; + + return $this; + } + + public function getMessageId(): ?int + { + return $this->messageId; + } + + public function setMessageId(?int $messageId): self + { + $this->messageId = $messageId; + + return $this; + } + + public function __toString() + { + $buffer = [ + 'type' => Types::PUBLISH, + 'topic' => $this->getTopic(), + 'message' => $this->getMessage(), + 'dup' => $this->getDup(), + 'qos' => $this->getQos(), + 'retain' => $this->getRetain(), + 'message_id' => $this->getMessageId(), + ]; + + if ($this->isMQTT5()) { + $buffer['properties'] = $this->getProperties(); + + return V5::pack($buffer); + } + + return V3::pack($buffer); + } +}