-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
643 additions
and
147 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 |
---|---|---|
|
@@ -8,8 +8,7 @@ | |
}, | ||
"authors": [ | ||
{ | ||
"name": "Evert Jan Hakvoort", | ||
"email": "[email protected]" | ||
"name": "EJTJ3" | ||
} | ||
], | ||
"require": { | ||
|
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EJTJ3\PhpNats\Connection; | ||
|
||
final class Acknowledgement implements NatsResponseInterface | ||
{ | ||
} |
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
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EJTJ3\PhpNats\Connection; | ||
|
||
final class Error implements NatsResponseInterface | ||
{ | ||
} |
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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EJTJ3\PhpNats\Connection; | ||
|
||
use EJTJ3\PhpNats\Util\StringUtil; | ||
use Exception; | ||
use InvalidArgumentException; | ||
|
||
final class HMsg implements MessageInterface | ||
{ | ||
private array $headers; | ||
|
||
public ?string $payload; | ||
|
||
public ?string $protocol; | ||
|
||
public function __construct( | ||
// Subject name this message was received on. | ||
public string $subject, | ||
// The unique alphanumeric subscription ID of the subject. | ||
public string $subscriptionId, | ||
// The subject on which the publisher is listening for responses. | ||
public ?string $replyTo, | ||
// The size of the headers section in bytes including the ␍␊␍␊ delimiter before the payload. | ||
public int $headerBytes, | ||
// The total size of headers and payload sections in bytes. | ||
public int $totalBytes | ||
) { | ||
$this->headers = []; | ||
$this->payload = null; | ||
} | ||
|
||
public function getPayload(): string | ||
{ | ||
return $this->payload ?? ''; | ||
} | ||
|
||
public function getHeader(string $key) | ||
{ | ||
return $this->headers[$key] ?? null; | ||
} | ||
|
||
public function setPayload(string $payload): void | ||
{ | ||
$this->payload = $payload; | ||
} | ||
|
||
public function setHeaders(string $headers): void | ||
{ | ||
// Check if we have an inlined status. | ||
$parts = explode(' ', $headers); | ||
|
||
if (isset($parts[1]) && strlen($parts[1]) === 3) { | ||
$this->headers['status'] = (int) $parts[1]; | ||
} | ||
} | ||
|
||
// HMSG <subject> <sid> [reply-to] <#header bytes> <#total bytes>␍␊[headers]␍␊␍␊[payload]␍␊ | ||
public static function create(string $protocolMessage): HMsg | ||
{ | ||
$parts = StringUtil::explode($protocolMessage, 5); | ||
|
||
try { | ||
return match (count($parts)) { | ||
4 => new self($parts[0], $parts[1], null, (int) $parts[2], (int) $parts[3]), | ||
5 => new self($parts[0], $parts[1], $parts[2], (int) $parts[3], (int) $parts[4]), | ||
default => throw new InvalidArgumentException('Invalid msg') | ||
}; | ||
} catch (Exception $e) { | ||
// Add own exception | ||
throw $e; | ||
} | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EJTJ3\PhpNats\Connection; | ||
|
||
interface MessageInterface extends NatsResponseInterface | ||
{ | ||
public function getPayload(): string; | ||
} |
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EJTJ3\PhpNats\Connection; | ||
|
||
use EJTJ3\PhpNats\Util\StringUtil; | ||
use InvalidArgumentException; | ||
|
||
final class Msg implements MessageInterface | ||
{ | ||
/** | ||
* @var string|null the message payload data | ||
*/ | ||
private ?string $payload; | ||
|
||
private function __construct( | ||
// subject name this message was received on - always | ||
public readonly string $subject, | ||
// The unique alphanumeric subscription ID of the subject. - always | ||
public readonly string $sid, | ||
// The subject on which the publisher is listening for responses. - optional | ||
public readonly ?string $replyTo, | ||
// Size of the payload in bytes. | ||
public readonly int $bytes, | ||
) { | ||
$this->payload = ''; | ||
} | ||
|
||
/** | ||
* @param string $protocolMessage MSG <subject> <sid> [reply-to] <#bytes>␍␊[payload]␍␊ | ||
* | ||
* @see https://docs.nats.io/reference/reference-protocols/nats-protocol#syntax-6 | ||
*/ | ||
public static function create(string $protocolMessage): self | ||
{ | ||
$parts = StringUtil::explode($protocolMessage, 4); | ||
|
||
return match (count($parts)) { | ||
3 => new self($parts[0], $parts[1], null, (int) $parts[2]), | ||
4 => new self($parts[0], $parts[1], $parts[2], (int) $parts[3]), | ||
default => throw new InvalidArgumentException('Invalid format') | ||
}; | ||
} | ||
|
||
public function getPayload(): string | ||
{ | ||
return $this->payload ?? ''; | ||
} | ||
|
||
public function setPayload(string $payload): void | ||
{ | ||
$this->payload = $payload; | ||
} | ||
} |
Oops, something went wrong.