Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscription meta events #362

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/Role/Broker.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,15 @@ protected function processPublish(Session $session, PublishMessage $msg)
protected function processUnsubscribe(Session $session, UnsubscribeMessage $msg)
{
$subscription = false;
$subscriptionKey = "";
// should probably be more efficient about this - maybe later
/** @var SubscriptionGroup $subscriptionGroup */
foreach ($this->subscriptionGroups as $subscriptionGroup) {
foreach ($this->subscriptionGroups as $key => $subscriptionGroup) {
$result = $subscriptionGroup->processUnsubscribe($session, $msg);

if ($result !== false) {
$subscription = $result;
$subscriptionKey = $key;
}
}

Expand All @@ -217,6 +219,8 @@ protected function processUnsubscribe(Session $session, UnsubscribeMessage $msg)

return;
}

$this->cleanSubscriptionGroup($subscriptionKey);
}

/**
Expand Down Expand Up @@ -264,14 +268,24 @@ public function leave(Session $session)
$subscriptionGroup->removeSubscription($subscription);
}

$subscriptions = $subscriptionGroup->getSubscriptions();
if (empty($subscriptions)) {
unset($this->subscriptionGroups[$key]);
}
$this->cleanSubscriptionGroup($key);
}
}
}

/**
* Called when a session unsubscribe or leave the group.
* Will clean and remove the group if this was the last subscription to it
* @param $key
*/
private function cleanSubscriptionGroup($key)
{
$subscriptions = $this->subscriptionGroups[$key]->getSubscriptions();
if (empty($subscriptions)) {
unset($this->subscriptionGroups[$key]);
}
}

/**
* todo: this may be used by testing
*
Expand Down
64 changes: 64 additions & 0 deletions src/Subscription/SubscriptionGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class SubscriptionGroup
*/
private $id;

/**
* @var \DateTime
*/
private $created;

/**
* @var array
*/
Expand Down Expand Up @@ -69,13 +74,49 @@ public function __construct(MatcherInterface $matcher, $uri, $options)
$this->setUri($uri);
$this->setMatcher($matcher);
$this->lastPublicationId = 0;
$this->id = Utils::getUniqueId();
$this->created = new \DateTime();
}

/**
* Is subscription group already a meta event?
* @return bool
*/
public function isMetaSubscription()
{
return strpos($this->uri, 'wamp.metaevent') === 0;
}

/**
* Publish a meta event ONLY if not already a meta subscription group
* @param string $metaEvent
* @param Subscription $subscription
*/
public function publishMetaEvent($metaEvent, Subscription $subscription)
{
if (!$this->isMetaSubscription()) {
$session = $subscription->getSession();
$realm = $session->getRealm();
$realm->publishMeta(
$metaEvent,
[$session->getMetaInfo(), $this->getMetaInfo()]
);
}
}

/**
* @param Subscription $subscription
*/
public function addSubscription(Subscription $subscription)
{
// We've just been created, this is the first subscription in this URI
if (empty($this->subscriptions)) {
$this->publishMetaEvent('wamp.metaevent.subscription.on_create', $subscription);
}

// Notify of the subscription
$this->publishMetaEvent('wamp.metaevent.subscription.on_subscribe', $subscription);

$this->subscriptions[$subscription->getId()] = $subscription;
}

Expand All @@ -86,6 +127,14 @@ public function removeSubscription(Subscription $subscription)
{
if (isset($this->subscriptions[$subscription->getId()])) {
unset($this->subscriptions[$subscription->getId()]);

// Notify of the unsubscription
$this->publishMetaEvent('wamp.metaevent.subscription.on_unsubscribe', $subscription);

// Deal with a now empty subscription URI
if (empty($this->subscriptions)) {
$this->publishMetaEvent('wamp.metaevent.subscription.on_delete', $subscription);
}
}
}

Expand Down Expand Up @@ -313,4 +362,19 @@ public function getLastPublicationId()
{
return $this->lastPublicationId;
}

/**
* Get meta info
*
* @return array
*/
private function getMetaInfo()
{
return [
'id' => $this->id,
'created' => $this->created->format(\DateTime::ATOM),
'uri' => $this->uri,
'match' => $this->getMatchType(),
];
}
}