Skip to content

Commit

Permalink
feat: add pattern base subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
cydrickn committed Jul 21, 2024
1 parent d8306da commit b1e518e
Show file tree
Hide file tree
Showing 16 changed files with 545 additions and 229 deletions.
51 changes: 25 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,40 +129,39 @@ That will now run the server

**Additional Authentication**

- [ ] Add Feature
- [ ] Add checking of role

### RPC Features

| Feature | Status |
|-------------------------------------------------------------------|---------|
| [Progressive Call Results](#rpc-progressive-call-results) | ✗ |
| [Progressive Call Invocations](#rpc-progressive-call-invocations) | ✗ |
| [Call Timeout](#rpc-call-timeout) | ✗ |
| [Call Canceling](#rpc-call-canceling) | ✗ |
| [Caller Identification](#rpc-call-identification) | ✗ |
| [Call Trustlevels](#rpc-call-trust-levels) | ✗ |
| [Registration Meta API](#rpc-reg-metapi) | ✗ |
| [Pattern-based Registration](#rpc-pattern-reg) | ✗ |
| [Shared Registration](#rpc-shared-registration) | ✗ |
| [Sharded Registration](##rpc-sharded-registration) | ✗ |
| [Registration Revocation](#rpc-registration-revocation) | ✗ |
| [(Interface) Procedure Reflection](#interface-reflection) | ✗ |
| Feature | Status |
|----------------------------------|---------|
| Progressive Call Results | ✗ |
| Progressive Call Invocations | ✗ |
| Call Timeout | ✗ |
| Call Canceling | ✗ |
| Caller Identification | ✗ |
| Call Trustlevels | ✗ |
| Registration Meta API | ✗ |
| Pattern-based Registration | ✗ |
| Shared Registration | ✗ |
| Sharded Registration | ✗ |
| Registration Revocation | ✗ |
| (Interface) Procedure Reflection | ✗ |


### PubSub Features

| Feature | Status |
|-----------------------------------------------------------|---------|
| [Subscriber Blackwhite Listing](#pubsub-bw-listing) | ✓ |
| [Publisher Exclusion](#pubsub-pub-exclusion) | ✓ |
| [Publisher Identification](#pubsub-pub-identification) | ✓ |
| [Publication Trustlevels](#pubsub-pub-trustlevels) | ✗ |
| [Subscription Meta API](#pubsub-sub-metapi) | ✗ |
| [Pattern-based Subscription](#pattern-based-subscription) | ✗ |
| [Sharded Subscription](#pubsub-sharded-subscription) | ✗ |
| [Event History](#pubsub-event-history) | ✗ |
| [(Interface) Topic Reflection](#interface-reflection) | ✗ |
| Feature | Status |
|-------------------------------|---------|
| Subscriber Blackwhite Listing | ✓ |
| Publisher Exclusion | ✓ |
| Publisher Identification | ✓ |
| Publication Trustlevels | ✗ |
| Subscription Meta API | ✗ |
| Pattern-based Subscription | ✓ |
| Sharded Subscription | ✗ |
| Event History | ✗ |
| (Interface) Topic Reflection | ✗ |

### Others

Expand Down
2 changes: 2 additions & 0 deletions src/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public function set(string $key, array $data = []): void;

public function setField(string $key, string $field, mixed $data): void;

public function getField(string $key, string $field): mixed;

public function del(string $key, array $fields = []): void;

public function get(string $key, array $fields = []): ?array;
Expand Down
25 changes: 24 additions & 1 deletion src/Adapter/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use OpenSwoole\Coroutine;
use OpenSwoole\Timer;
use Octamp\Wamp\Pools\Pool;
use Predis\Client;

class RedisAdapter extends \Octamp\Server\Adapter\RedisAdapter implements AdapterInterface
{
Expand Down Expand Up @@ -134,6 +135,19 @@ public function setField(string $key, string $field, mixed $data): void
$this->set($key, [$field => $data]);
}

public function getField(string $key, string $field): mixed
{
$data = $this->runCommand(function (Client $client) use ($key, $field) {
$client->getResource()->hget($key, $field);
});

if (is_array($data)) {
return $this->decodeData($data);
}

return $data;
}

public function countFields(string $key): int
{
$client = $this->clients->pop();
Expand Down Expand Up @@ -230,7 +244,7 @@ protected function decodeData(array $data): array

public function findOne(string $search): ?array
{
$client = $this->clients->pop();;
$client = $this->clients->pop();
$keys = $client->getResource()->keys($search);
$result = null;
if (!empty($keys)) {
Expand All @@ -240,4 +254,13 @@ public function findOne(string $search): ?array

return $result;
}

protected function runCommand(callable $callable): mixed
{
$client = $this->clients->pop();
$data = call_user_func($callable, $client->getResource());
$this->clients->push($client);

return $data;
}
}
19 changes: 19 additions & 0 deletions src/Matcher/ExactMatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Octamp\Wamp\Matcher;

class ExactMatch implements MatchInterface
{

public function getName(): string
{
return 'exact';
}

public function isMatched($uri, $uri2): bool
{
return $uri === $uri2;
}
}
12 changes: 12 additions & 0 deletions src/Matcher/MatchInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Octamp\Wamp\Matcher;

interface MatchInterface
{
public function getName(): string;

public function isMatched(string $uri, string $uri2): bool;
}
44 changes: 44 additions & 0 deletions src/Matcher/Matcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Octamp\Wamp\Matcher;

class Matcher
{
/**
* @var MatchInterface[]
*/
protected array $matches = [];

public function __construct()
{
$this->addMatch(new ExactMatch());
}

public function addMatch(MatchInterface $match): void
{
$this->matches[$match->getName()] = $match;
}

public function isMatch(string $from, string $to, string $name): bool
{
if (!isset($this->matches[$name])) {
return false;
}

return $this->matches[$name]->isMatched($from, $to);
}

/**
* @throws UnExistMatchException
*/
public function getMatch($name): MatchInterface
{
if (!isset($this->matches[$name])) {
throw new UnExistMatchException('Matcher ' . $name . ' does not exists');
}

return $this->matches[$name];
}
}
25 changes: 25 additions & 0 deletions src/Matcher/PrefixMatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Octamp\Wamp\Matcher;

class PrefixMatch implements MatchInterface
{

public function getName(): string
{
return 'prefix';
}

public function isMatched(string $uri, string $uri2): bool
{
if ($uri === $uri2) {
return true;
}

$pattern = '/^(' . $uri . ')(\.|-).*$/';

return preg_match($pattern, $uri2) === 1;
}
}
10 changes: 10 additions & 0 deletions src/Matcher/UnExistMatchException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Octamp\Wamp\Matcher;

class UnExistMatchException extends \Exception
{

}
44 changes: 44 additions & 0 deletions src/Matcher/WildcardMatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Octamp\Wamp\Matcher;

class WildcardMatch implements MatchInterface
{

public function getName(): string
{
return 'wildcard';
}

public function isMatched(string $uri, string $uri2): bool
{
if ($uri === $uri2) {
return true;
}

$len = strlen($uri);
if ($uri[$len - 1] === '.' || $uri[$len - 1] === '-') {
$uri = substr_replace($uri, ')(.*)(\.|-)', $len - 1, 1);
} else {
$uri .= ')';
}

if ($uri[0] === '.' || $uri[0] === '-') {
$uri = substr_replace($uri, '(.*)(\.|-)(', 0, 1);
} else {
$uri = '(' . $uri;
}


$uri = str_replace('..', ')(\.|-)(.*)(\.|-)(', $uri);
$uri = str_replace('.-', ')(\.|-)(.*)(\.|-)(', $uri);
$uri = str_replace('-.', ')(\.|-)(.*)(\.|-)(', $uri);
$uri = str_replace('--', ')(\.|-)(.*)(\.|-)(', $uri);

$patterns = '/^' . $uri . '$/';

return preg_match($patterns, $uri2) === 1;
}
}
17 changes: 0 additions & 17 deletions src/Realm/Realm.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,9 @@ public function onAuthenticateMessage(Session $session, AuthenticateMessage $mes

public function onLeaveRealmEvent(Session $session, LeaveRealmEvent $event): void
{
if ($session->isAuthenticated()) {
$this->publishMeta('wamp.session.on_leave', [$session->getMetaInfo()]);
}

$this->sessionStorage->removeSession($session);
}

public function publishMeta(string $topicName, array $arguments, ?object $argumentsKw = null, ?object $options = null): void
{
$session = $this->getMetaSession();
$id = $session->incrementWampId();
$this->handle($this->getMetaSession(), new PublishMessage(
$id,
$options,
$topicName,
$arguments,
$argumentsKw
));
}

public function getMetaSession(): Session
{
if ($this->metaSession === null) {
Expand Down
Loading

0 comments on commit b1e518e

Please sign in to comment.