-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CONNACK & PUBLISH & PINGRESP Message
* [x] CONNACK * [x] PUBLISH * [x] PINGRESP
- Loading branch information
1 parent
99a85bf
commit 700a6c9
Showing
4 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |