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 Evenement\EventEmitterTrait; |
15
|
|
|
use React\Promise\Promise; |
16
|
|
|
use Tidal\WampWatch\ClientSessionInterface as ClientSession; |
17
|
|
|
use Tidal\WampWatch\Subscription\Collection as SubscriptionCollection; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Description of MonitorTrait. |
21
|
|
|
* |
22
|
|
|
* @author Timo |
23
|
|
|
*/ |
24
|
|
|
trait MonitorTrait |
25
|
|
|
{ |
26
|
|
|
use EventEmitterTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The monitor's WAMP client session. |
30
|
|
|
* |
31
|
|
|
* @var ClientSession |
32
|
|
|
*/ |
33
|
|
|
protected $session; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* if the monitor is running. |
37
|
|
|
* |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected $isRunning = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var SubscriptionCollection collection for meta subscriptions |
44
|
|
|
*/ |
45
|
|
|
protected $metaSubscriptionCollection; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $initialCallProcedure; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var callable |
54
|
|
|
*/ |
55
|
|
|
protected $initialCallCallback; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
protected $initialCallDone = false; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param ClientSession $session |
64
|
|
|
*/ |
65
|
|
|
protected function setClientSession(ClientSession $session) |
66
|
|
|
{ |
67
|
|
|
$this->session = $session; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Start the monitor. |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function start() |
76
|
|
|
{ |
77
|
|
|
$this->getMetaSubscriptionCollection()->subscribe()->done(function () { |
78
|
|
|
$this->checkStarted(); |
79
|
|
|
}); |
80
|
|
|
$this->callInitialProcedure()->done(function () { |
81
|
|
|
$this->checkStarted(); |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Stop the monitor. |
89
|
|
|
* Returns boolean if the monitor could be started. |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function stop() |
94
|
|
|
{ |
95
|
|
|
if (!$this->isRunning()) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->getMetaSubscriptionCollection()->unsubscribe(); |
100
|
|
|
|
101
|
|
|
$this->isRunning = false; |
102
|
|
|
$this->emit('stop', [$this]); |
103
|
|
|
|
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function getList() |
108
|
|
|
{ |
109
|
|
|
return []; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the monitor's WAMP client session. |
114
|
|
|
* |
115
|
|
|
* @return ClientSession |
116
|
|
|
*/ |
117
|
|
|
public function getServerSession() |
118
|
|
|
{ |
119
|
|
|
return $this->session; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the monitor's running state. |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function isRunning() |
128
|
|
|
{ |
129
|
|
|
return $this->isRunning; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param \Tidal\WampWatch\Subscription\Collection $collection |
134
|
|
|
*/ |
135
|
|
|
public function setMetaSubscriptionCollection(SubscriptionCollection $collection) |
136
|
|
|
{ |
137
|
|
|
$this->metaSubscriptionCollection = $collection; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return \Tidal\WampWatch\Subscription\Collection |
142
|
|
|
*/ |
143
|
|
|
public function getMetaSubscriptionCollection() |
144
|
|
|
{ |
145
|
|
|
return isset($this->metaSubscriptionCollection) |
146
|
|
|
? $this->metaSubscriptionCollection |
147
|
|
|
: $this->metaSubscriptionCollection = new SubscriptionCollection($this->session); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
protected function setInitialCall($pocedure, callable $callback) |
151
|
|
|
{ |
152
|
|
|
$this->initialCallProcedure = (string) $pocedure; |
153
|
|
|
$this->initialCallCallback = $callback; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return \React\Promise\Promise |
158
|
|
|
*/ |
159
|
|
|
protected function callInitialProcedure() |
160
|
|
|
{ |
161
|
|
|
if (!isset($this->initialCallProcedure) || !isset($this->initialCallCallback)) { |
162
|
|
|
$this->initialCallDone = true; |
163
|
|
|
$resolver = function (callable $resolve) { |
164
|
|
|
$resolve(); |
165
|
|
|
}; |
166
|
|
|
|
167
|
|
|
return new Promise($resolver); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this->session->call($this->initialCallProcedure, [])->then(function ($res) { |
171
|
|
|
$this->initialCallDone = true; |
172
|
|
|
$cb = $this->initialCallCallback; |
173
|
|
|
$cb($res); |
174
|
|
|
|
175
|
|
|
return $res; |
176
|
|
|
}); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Checks if all necessary subscriptions and calls have been responded to. |
181
|
|
|
*/ |
182
|
|
|
protected function checkStarted() |
183
|
|
|
{ |
184
|
|
|
if ($this->isMetaSubscribed() && |
185
|
|
|
$this->initialCallDone && |
186
|
|
|
!$this->isRunning() |
187
|
|
|
) { |
188
|
|
|
$this->isRunning = true; |
189
|
|
|
$this->emit('start', [$this->getList()]); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
protected function isMetaSubscribed() |
194
|
|
|
{ |
195
|
|
|
if (!$this->getMetaSubscriptionCollection()->hasSubscription()) { |
196
|
|
|
return true; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $this->getMetaSubscriptionCollection()->isSubscribed(); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|