1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tidal/WampWatch package. |
5
|
|
|
* (c) 2016 Timo Michna <timomichna/yahoo.de> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tidal\WampWatch; |
13
|
|
|
|
14
|
|
|
use React\Promise\Promise; |
15
|
|
|
use Tidal\WampWatch\ClientSessionInterface as ClientSession; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Description of SessionMonitor. |
19
|
|
|
* |
20
|
|
|
* @author Timo |
21
|
|
|
*/ |
22
|
|
|
class SubscriptionMonitor implements MonitorInterface |
23
|
|
|
{ |
24
|
|
|
use MonitorTrait; |
25
|
|
|
|
26
|
|
|
const SUBSCRIPTION_CREATE_TOPIC = 'wamp.subscription.on_create'; |
27
|
|
|
const SUBSCRIPTION_SUB_TOPIC = 'wamp.subscription.on_subscribe'; |
28
|
|
|
const SUBSCRIPTION_UNSUB_TOPIC = 'wamp.subscription.on_unsubscribe'; |
29
|
|
|
const SUBSCRIPTION_DELETE_TOPIC = 'wamp.subscription.on_delete'; |
30
|
|
|
const SUBSCRIPTION_LIST_TOPIC = 'wamp.subscription.list'; |
31
|
|
|
const SUBSCRIPTION_LOOKUP_TOPIC = 'wamp.subscription.lookup'; |
32
|
|
|
const SUBSCRIPTION_MATCH_TOPIC = 'wamp.subscription.match'; |
33
|
|
|
const SUBSCRIPTION_GET_TOPIC = 'wamp.subscription.get'; |
34
|
|
|
const SUBSCRIPTION_SUBLIST_TOPIC = 'wamp.subscription.list_subscribers'; |
35
|
|
|
const SUBSCRIPTION_SUBCOUNT_TOPIC = 'wamp.subscription.count_subscribers'; |
36
|
|
|
|
37
|
|
|
const LOOKUP_MATCH_WILDCARD = 'wildcard'; |
38
|
|
|
const LOOKUP_MATCH_PREFIX = 'prefix'; |
39
|
|
|
|
40
|
|
|
protected $sessionIds = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \stdClass Objects withs lists of subscriptions (exact, prefix, wildcard) |
44
|
|
|
*/ |
45
|
|
|
protected $subscriptionIds = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Constructor. |
49
|
|
|
* |
50
|
|
|
* @param ClientSession $session |
51
|
|
|
*/ |
52
|
|
|
public function __construct(ClientSession $session) |
53
|
|
|
{ |
54
|
|
|
$this->setClientSession($session); |
55
|
|
|
$this->initSetupCalls(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $topic |
60
|
|
|
* |
61
|
|
|
* @return Promise |
62
|
|
|
*/ |
63
|
|
|
public function getSubscriptionInfo($topic) |
64
|
|
|
{ |
65
|
|
|
return $this->session->call(self::SUBSCRIPTION_GET_TOPIC, $topic)->then( |
66
|
|
|
function ($res) { |
67
|
|
|
$this->emit('info', [$res]); |
68
|
|
|
|
69
|
|
|
return $res; |
70
|
|
|
}, |
71
|
|
|
$this->getErrorCallback() |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getSubscriptionIds() |
76
|
|
|
{ |
77
|
|
|
if (!isset($this->subscriptionIds)) { |
78
|
|
|
return $this->retrieveSubscriptionIds(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return new Promise(function (callable $resolve) { |
82
|
|
|
$resolve($this->subscriptionIds); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Initializes the subscription to the meta-events. |
88
|
|
|
*/ |
89
|
|
|
protected function initSetupCalls() |
90
|
|
|
{ |
91
|
|
|
// @var \Tidal\WampWatch\Subscription\Collection |
92
|
|
|
$collection = $this->getMetaSubscriptionCollection(); |
93
|
|
|
|
94
|
|
|
$collection->addSubscription(self::SUBSCRIPTION_CREATE_TOPIC, $this->getCreateHandler()); |
95
|
|
|
$collection->addSubscription(self::SUBSCRIPTION_DELETE_TOPIC, $this->getSubscriptionHandler('delete')); |
96
|
|
|
$collection->addSubscription(self::SUBSCRIPTION_SUB_TOPIC, $this->getSubscriptionHandler('subscribe')); |
97
|
|
|
$collection->addSubscription(self::SUBSCRIPTION_UNSUB_TOPIC, $this->getSubscriptionHandler('unsubscribe')); |
98
|
|
|
|
99
|
|
|
$this->setInitialCall(self::SUBSCRIPTION_LIST_TOPIC, $this->getSubscriptionIdRetrievalCallback()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function getCreateHandler() |
103
|
|
|
{ |
104
|
|
|
return function ($res) { |
105
|
|
|
$sessionId = $res[0]; |
106
|
|
|
$subscriptionInfo = $res[1]; |
107
|
|
|
$this->emit('create', [$sessionId, $subscriptionInfo]); |
108
|
|
|
}; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function getSubscriptionHandler($event) |
112
|
|
|
{ |
113
|
|
|
return function ($res) use ($event) { |
114
|
|
|
$sessionId = $res[0]; |
115
|
|
|
$subscriptionId = $res[1]; |
116
|
|
|
$this->emit($event, [$sessionId, $subscriptionId]); |
117
|
|
|
}; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function retrieveSubscriptionIds() |
121
|
|
|
{ |
122
|
|
|
return $this->session->call(self::SUBSCRIPTION_LIST_TOPIC, []) |
123
|
|
|
->then( |
124
|
|
|
$this->getSubscriptionIdRetrievalCallback(), |
125
|
|
|
$this->getErrorCallback() |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function setList($list) |
130
|
|
|
{ |
131
|
|
|
$this->subscriptionIds = $list; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function getList() |
135
|
|
|
{ |
136
|
|
|
return $this->subscriptionIds; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function getSubscriptionIdRetrievalCallback() |
140
|
|
|
{ |
141
|
|
|
return function ($res) { |
142
|
|
|
$this->setList($res); |
143
|
|
|
$this->emit('list', [ |
144
|
|
|
$this->subscriptionIds->exact, |
145
|
|
|
$this->subscriptionIds->prefix, |
146
|
|
|
$this->subscriptionIds->wildcard, |
147
|
|
|
]); |
148
|
|
|
$this->checkStarted(); |
149
|
|
|
|
150
|
|
|
return $res; |
151
|
|
|
}; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|