1 | <?php |
||
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) |
||
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) |
||
139 | |||
140 | /** |
||
141 | * @return \Tidal\WampWatch\Subscription\Collection |
||
142 | */ |
||
143 | public function getMetaSubscriptionCollection() |
||
149 | |||
150 | protected function setInitialCall($procedure, callable $callback) |
||
151 | { |
||
152 | $this->initialCallProcedure = (string) $procedure; |
||
153 | $this->initialCallCallback = $callback; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return \React\Promise\Promise |
||
158 | */ |
||
159 | protected function callInitialProcedure() |
||
180 | |||
181 | /** |
||
182 | * Checks if all necessary subscriptions and calls have been responded to. |
||
183 | */ |
||
184 | protected function checkStarted() |
||
194 | |||
195 | protected function isMetaSubscribed() |
||
203 | |||
204 | private function getErrorCallback() |
||
212 | } |
||
213 |