1 | <?php |
||
22 | class SessionMonitor implements MonitorInterface, EventEmitterInterface |
||
23 | { |
||
24 | use MonitorTrait { |
||
25 | start as doStart; |
||
26 | } |
||
27 | |||
28 | const SESSION_JOIN_TOPIC = 'wamp.session.on_join'; |
||
29 | const SESSION_LEAVE_TOPIC = 'wamp.session.on_leave'; |
||
30 | const SESSION_COUNT_TOPIC = 'wamp.session.count'; |
||
31 | const SESSION_LIST_TOPIC = 'wamp.session.list'; |
||
32 | const SESSION_INFO_TOPIC = 'wamp.session.get'; |
||
33 | |||
34 | /** |
||
35 | * @var array monitored session ids |
||
36 | */ |
||
37 | protected $sessionIds = []; |
||
38 | |||
39 | /** |
||
40 | * @var int subscription id for on_join |
||
41 | */ |
||
42 | protected $joinSubscriptionId = 0; |
||
43 | |||
44 | /** |
||
45 | * @var int subscription id for on_leave |
||
46 | */ |
||
47 | protected $leaveSubscriptionId = 0; |
||
48 | |||
49 | /** |
||
50 | * @var bool flag if list call has returned |
||
51 | */ |
||
52 | protected $calledList = false; |
||
53 | |||
54 | /** |
||
55 | * Constructor. |
||
56 | * |
||
57 | * @param ClientSession $session |
||
58 | */ |
||
59 | public function __construct(ClientSession $session) |
||
60 | { |
||
61 | $this->setClientSession($session); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Start the monitor. |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | public function start() |
||
70 | { |
||
71 | $this->initSetupCalls(); |
||
72 | $this->getSubscriptionCollection()->subscribe()->done(function () { |
||
73 | $this->checkStarted(); |
||
74 | }); |
||
75 | $this->retrieveSessionIds(); |
||
76 | |||
77 | return true; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Checks if all necessary subscriptions and calls have been responded to. |
||
82 | */ |
||
83 | protected function checkStarted() |
||
84 | { |
||
85 | if ($this->getSubscriptionCollection()->isSubscribed() && |
||
86 | $this->calledList && |
||
87 | !$this->isRunning() |
||
88 | ) { |
||
89 | $this->doStart(); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Retrieves the session-info for given sessionId |
||
95 | * and populates it in via given callback. |
||
96 | * |
||
97 | * @param $sessionId |
||
98 | * @param callable $callback |
||
99 | * |
||
100 | * @return mixed |
||
101 | */ |
||
102 | public function getSessionInfo($sessionId, callable $callback = null) |
||
103 | { |
||
104 | return $this->session->call(self::SESSION_INFO_TOPIC, [$sessionId])->then( |
||
105 | function ($res) use ($callback) { |
||
106 | $this->emit('info', [$res[0]]); |
||
107 | if ($callback !== null) { |
||
108 | $callback($res[0]); |
||
109 | } |
||
110 | }, |
||
111 | function ($error) { |
||
112 | $this->emit('error', [$error]); |
||
113 | } |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Retrieves the Ids of the sessions currently |
||
119 | * registered on the wamp-router in the monitor's realm |
||
120 | * and populates the data via given callback,. |
||
121 | * |
||
122 | * @param callable $callback |
||
123 | * |
||
124 | * @return mixed |
||
125 | */ |
||
126 | public function getSessionIds(callable $callback) |
||
136 | |||
137 | /** |
||
138 | * Checks if a session id is known. |
||
139 | * |
||
140 | * @param $sessionId |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | public function hasSessionId($sessionId) |
||
148 | |||
149 | /** |
||
150 | * Removes a session id. |
||
151 | * |
||
152 | * @param int $sessionId |
||
153 | */ |
||
154 | protected function removeSessionId($sessionId) |
||
164 | |||
165 | /** |
||
166 | * Checks if a session is known by extracting its id. |
||
167 | * |
||
168 | * @param $sessionInfo |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | protected function hasSession($sessionInfo) |
||
176 | |||
177 | /** |
||
178 | * Adds and publishes a joined session. |
||
179 | * |
||
180 | * @param $sessionInfo |
||
181 | */ |
||
182 | protected function addSession($sessionInfo) |
||
187 | |||
188 | /** |
||
189 | * Validates the sessionInfo sent from the router. |
||
190 | * |
||
191 | * @param string $sessionInfo |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | protected function validateSessionInfo($sessionInfo) |
||
199 | |||
200 | /** |
||
201 | * Initializes the subscription to the meta-events. |
||
202 | */ |
||
203 | protected function initSetupCalls() |
||
226 | |||
227 | /** |
||
228 | * Retrieves the list of current sessionIds on the router. |
||
229 | * |
||
230 | * @param callable|null $callback |
||
231 | */ |
||
232 | protected function retrieveSessionIds(callable $callback = null) |
||
243 | |||
244 | protected function getSessionIdRetrievalCallback() |
||
257 | |||
258 | protected function setList($list) |
||
262 | |||
263 | protected function getList() |
||
267 | |||
268 | /** |
||
269 | * remove the sessionID of the Monitor from the list. |
||
270 | * |
||
271 | * @param array $sessionsIds |
||
272 | * |
||
273 | * @return mixed |
||
274 | */ |
||
275 | protected function removeOwnSessionId(array $sessionsIds) |
||
285 | } |
||
286 |