Skip to content

Commit

Permalink
Add CONNACK & PUBLISH & PINGRESP Message
Browse files Browse the repository at this point in the history
* [x] CONNACK
* [x] PUBLISH
* [x] PINGRESP
  • Loading branch information
sy-records committed Feb 24, 2021
1 parent 99a85bf commit 700a6c9
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/Message/AbstractMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* This file is part of Simps
*
* @link https://github.com/simps/mqtt
* @contact Lu Fei <lufei@simps.io>
*
* 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();
}
66 changes: 66 additions & 0 deletions src/Message/ConnAck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* This file is part of Simps
*
* @link https://github.com/simps/mqtt
* @contact Lu Fei <lufei@simps.io>
*
* 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);
}
}
34 changes: 34 additions & 0 deletions src/Message/PingResp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* This file is part of Simps
*
* @link https://github.com/simps/mqtt
* @contact Lu Fei <lufei@simps.io>
*
* 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);
}
}
127 changes: 127 additions & 0 deletions src/Message/Publish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* This file is part of Simps
*
* @link https://github.com/simps/mqtt
* @contact Lu Fei <lufei@simps.io>
*
* 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);
}
}

0 comments on commit 700a6c9

Please sign in to comment.