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 |
44
|
|
|
*/ |
45
|
|
|
protected $subscriptionCollection; |
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->isRunning = true; |
78
|
|
|
$this->emit('start', [$this->getList()]); |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Stop the monitor. |
85
|
|
|
* Returns boolean if the monitor could be started. |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function stop() |
90
|
|
|
{ |
91
|
|
|
if (!$this->isRunning()) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->getSubscriptionCollection()->unsubscribe(); |
96
|
|
|
|
97
|
|
|
$this->isRunning = false; |
98
|
|
|
$this->emit('stop', [$this]); |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function getList() |
104
|
|
|
{ |
105
|
|
|
return []; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the monitor's WAMP client session. |
110
|
|
|
* |
111
|
|
|
* @return ClientSession |
112
|
|
|
*/ |
113
|
|
|
public function getServerSession() |
114
|
|
|
{ |
115
|
|
|
return $this->session; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the monitor's running state. |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function isRunning() |
124
|
|
|
{ |
125
|
|
|
return $this->isRunning; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return \Tidal\WampWatch\Subscription\Collection |
130
|
|
|
*/ |
131
|
|
|
protected function getSubscriptionCollection() |
132
|
|
|
{ |
133
|
|
|
return isset($this->subscriptionCollection) |
134
|
|
|
? $this->subscriptionCollection |
135
|
|
|
: $this->subscriptionCollection = new SubscriptionCollection($this->session); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function setInitialCall($pocedure, callable $callback) |
139
|
|
|
{ |
140
|
|
|
$this->initialCallProcedure = (string) $pocedure; |
141
|
|
|
$this->initialCallCallback = $callback; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return \React\Promise\PromiseInterface |
146
|
|
|
*/ |
147
|
|
|
protected function callInitialProcedure() |
148
|
|
|
{ |
149
|
|
|
if (!isset($this->initialCallProcedure) || !isset($this->initialCallCallback)) { |
150
|
|
|
$resolver = function (callable $resolve) { |
151
|
|
|
$resolve(); |
152
|
|
|
}; |
153
|
|
|
|
154
|
|
|
return new Promise($resolver); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this->session->call($this->initialCallProcedure)->then(function ($res) { |
158
|
|
|
$this->initialCallDone = true; |
159
|
|
|
|
160
|
|
|
return $res; |
161
|
|
|
}); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Checks if all necessary subscriptions and calls have been responded to. |
166
|
|
|
*/ |
167
|
|
|
protected function checkStarted() |
168
|
|
|
{ |
169
|
|
|
if ($this->getSubscriptionCollection()->isSubscribed() && |
170
|
|
|
$this->initialCallDone && |
171
|
|
|
!$this->isRunning() |
172
|
|
|
) { |
173
|
|
|
$this->doStart(); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.