1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 1 | package org.vostokframework.application.monitoring.monitors |
30 | |
{ |
31 | |
import org.as3collections.IList; |
32 | |
import org.as3coreaddendum.errors.IllegalStateError; |
33 | |
import org.as3coreaddendum.errors.ObjectDisposedError; |
34 | |
import org.as3coreaddendum.errors.UnsupportedOperationError; |
35 | |
import org.as3utils.ReflectionUtil; |
36 | |
import org.vostokframework.VostokIdentification; |
37 | |
import org.vostokframework.application.monitoring.ILoadingMonitor; |
38 | |
import org.vostokframework.application.monitoring.LoadingMonitoring; |
39 | |
import org.vostokframework.domain.loading.ILoader; |
40 | |
import org.vostokframework.domain.loading.events.LoaderErrorEvent; |
41 | |
import org.vostokframework.domain.loading.events.LoaderEvent; |
42 | |
|
43 | |
import flash.events.Event; |
44 | |
import flash.events.ProgressEvent; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public class LoadingMonitor implements ILoadingMonitor |
52 | |
{ |
53 | |
|
54 | |
|
55 | |
|
56 | |
private var _dispatcher:LoadingMonitorDispatcher; |
57 | |
private var _disposed:Boolean; |
58 | |
private var _loader:ILoader; |
59 | |
private var _monitoring:LoadingMonitoring; |
60 | |
|
61 | |
public function get loader():ILoader { return _loader; } |
62 | |
|
63 | |
public function get monitoring():LoadingMonitoring { return _monitoring; } |
64 | |
|
65 | |
protected function get dispatcher():LoadingMonitorDispatcher { return _dispatcher; } |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public function LoadingMonitor(loader:ILoader, dispatcher:LoadingMonitorDispatcher) |
72 | 1 | { |
73 | 1 | if (!loader) throw new ArgumentError("Argument <loader> must not be null."); |
74 | |
|
75 | 1 | _loader = loader; |
76 | 1 | _dispatcher = dispatcher; |
77 | |
|
78 | 1 | addLoaderListeners(); |
79 | 1 | } |
80 | |
|
81 | |
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void |
82 | |
{ |
83 | 1 | dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference); |
84 | 1 | } |
85 | |
|
86 | |
public function addChild(child:ILoadingMonitor):void |
87 | |
{ |
88 | 0 | throw new UnsupportedOperationError("Method must be overridden in subclass: " + ReflectionUtil.getClassPath(this)); |
89 | |
} |
90 | |
|
91 | |
public function addChildren(children:IList):void |
92 | |
{ |
93 | 0 | throw new UnsupportedOperationError("Method must be overridden in subclass: " + ReflectionUtil.getClassPath(this)); |
94 | |
} |
95 | |
|
96 | |
public function containsChild(identification:VostokIdentification):Boolean |
97 | |
{ |
98 | 1 | return false; |
99 | |
} |
100 | |
|
101 | |
public function dispatchEvent(event:Event) : Boolean |
102 | |
{ |
103 | 0 | return dispatcher.dispatchEvent(event); |
104 | |
} |
105 | |
|
106 | |
public function dispose():void |
107 | |
{ |
108 | 1 | if (_disposed) return; |
109 | |
|
110 | 1 | removeLoaderListeners(); |
111 | |
|
112 | 1 | _loader = null; |
113 | 1 | _monitoring = null; |
114 | |
|
115 | 1 | doDispose(); |
116 | 1 | _disposed = true; |
117 | 1 | } |
118 | |
|
119 | |
public function getChild(identification:VostokIdentification):ILoadingMonitor |
120 | |
{ |
121 | 0 | throw new UnsupportedOperationError("Method must be overridden in subclass: " + ReflectionUtil.getClassPath(this)); |
122 | |
} |
123 | |
|
124 | |
public function hasEventListener(type:String):Boolean |
125 | |
{ |
126 | 0 | return dispatcher.hasEventListener(type); |
127 | |
} |
128 | |
|
129 | |
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void |
130 | |
{ |
131 | 1 | dispatcher.removeEventListener(type, listener, useCapture); |
132 | 1 | } |
133 | |
|
134 | |
public function removeChild(identification:VostokIdentification):void |
135 | |
{ |
136 | 0 | throw new UnsupportedOperationError("Method must be overridden in subclass: " + ReflectionUtil.getClassPath(this)); |
137 | |
} |
138 | |
|
139 | |
public function willTrigger(type:String):Boolean |
140 | |
{ |
141 | 0 | return dispatcher.willTrigger(type); |
142 | |
} |
143 | |
|
144 | |
protected function createLoadingMonitoring(latency:int):void |
145 | |
{ |
146 | 1 | validateDisposal(); |
147 | 1 | _monitoring = new LoadingMonitoring(latency); |
148 | 1 | } |
149 | |
|
150 | |
protected function doDispose():void |
151 | |
{ |
152 | |
|
153 | 0 | } |
154 | |
|
155 | |
protected function loadingComplete():void |
156 | |
{ |
157 | |
|
158 | 1 | } |
159 | |
|
160 | |
protected function loadingStarted():void |
161 | |
{ |
162 | |
|
163 | 1 | } |
164 | |
|
165 | |
protected function updateMonitoring(bytesTotal:int, bytesLoaded:int):void |
166 | |
{ |
167 | 1 | validateDisposal(); |
168 | 1 | _monitoring.update(bytesTotal, bytesLoaded); |
169 | 1 | } |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
protected function validateDisposal():void |
175 | |
{ |
176 | 1 | if (_disposed) throw new ObjectDisposedError("This object was disposed, therefore no more operations can be performed."); |
177 | 1 | } |
178 | |
|
179 | |
private function addLoaderListeners():void |
180 | |
{ |
181 | 1 | validateDisposal(); |
182 | |
|
183 | 1 | _loader.addEventListener(LoaderEvent.CANCELED, loaderCanceledHandler, false, int.MAX_VALUE, true); |
184 | 1 | _loader.addEventListener(LoaderEvent.COMPLETE, loaderCompleteHandler, false, int.MAX_VALUE, true); |
185 | 1 | _loader.addEventListener(LoaderErrorEvent.FAILED, loaderFailedHandler, false, int.MAX_VALUE, true); |
186 | 1 | _loader.addEventListener(LoaderEvent.HTTP_STATUS, loaderHttpStatusHandler, false, int.MAX_VALUE, true); |
187 | 1 | _loader.addEventListener(LoaderEvent.INIT, loaderInitHandler, false, int.MAX_VALUE, true); |
188 | 1 | _loader.addEventListener(LoaderEvent.OPEN, loaderOpenHandler, false, int.MAX_VALUE, true); |
189 | 1 | _loader.addEventListener(ProgressEvent.PROGRESS, loaderProgressHandler, false, int.MAX_VALUE, true); |
190 | 1 | _loader.addEventListener(LoaderEvent.STOPPED, loaderStoppedHandler, false, int.MAX_VALUE, true); |
191 | 1 | } |
192 | |
|
193 | |
private function removeLoaderListeners():void |
194 | |
{ |
195 | 1 | validateDisposal(); |
196 | |
|
197 | 1 | _loader.removeEventListener(LoaderEvent.CANCELED, loaderCanceledHandler, false); |
198 | 1 | _loader.removeEventListener(LoaderEvent.COMPLETE, loaderCompleteHandler, false); |
199 | 1 | _loader.removeEventListener(LoaderErrorEvent.FAILED, loaderFailedHandler, false); |
200 | 1 | _loader.removeEventListener(LoaderEvent.HTTP_STATUS, loaderHttpStatusHandler, false); |
201 | 1 | _loader.removeEventListener(LoaderEvent.INIT, loaderInitHandler, false); |
202 | 1 | _loader.removeEventListener(LoaderEvent.OPEN, loaderOpenHandler, false); |
203 | 1 | _loader.removeEventListener(ProgressEvent.PROGRESS, loaderProgressHandler, false); |
204 | 1 | _loader.removeEventListener(LoaderEvent.STOPPED, loaderStoppedHandler, false); |
205 | 1 | } |
206 | |
|
207 | |
private function loaderCanceledHandler(event:LoaderEvent):void |
208 | |
{ |
209 | 1 | validateDisposal(); |
210 | 1 | _dispatcher.dispatchCanceledEvent(_monitoring); |
211 | 1 | } |
212 | |
|
213 | |
private function loaderCompleteHandler(event:LoaderEvent):void |
214 | |
{ |
215 | 1 | validateDisposal(); |
216 | |
|
217 | 1 | if (_monitoring) updateMonitoring(_monitoring.bytesTotal, _monitoring.bytesTotal); |
218 | |
|
219 | 1 | loadingComplete(); |
220 | 1 | _dispatcher.dispatchProgressEvent(_monitoring); |
221 | 1 | _dispatcher.dispatchCompleteEvent(_monitoring, event.data); |
222 | 1 | } |
223 | |
|
224 | |
private function loaderFailedHandler(event:LoaderErrorEvent):void |
225 | |
{ |
226 | 1 | validateDisposal(); |
227 | 1 | _dispatcher.dispatchFailedEvent(_monitoring, event.errors); |
228 | 1 | } |
229 | |
|
230 | |
private function loaderHttpStatusHandler(event:LoaderEvent):void |
231 | |
{ |
232 | 1 | validateDisposal(); |
233 | 1 | _dispatcher.dispatchHttpStatusEvent(_monitoring, event.httpStatus); |
234 | 1 | } |
235 | |
|
236 | |
private function loaderInitHandler(event:LoaderEvent):void |
237 | |
{ |
238 | 1 | validateDisposal(); |
239 | 1 | _dispatcher.dispatchInitEvent(_monitoring, event.data); |
240 | 1 | } |
241 | |
|
242 | |
private function loaderOpenHandler(event:LoaderEvent):void |
243 | |
{ |
244 | 1 | validateDisposal(); |
245 | 1 | createLoadingMonitoring(event.latency); |
246 | 1 | _dispatcher.dispatchOpenEvent(_monitoring, event.data); |
247 | 1 | loadingStarted(); |
248 | 1 | } |
249 | |
|
250 | |
private function loaderProgressHandler(event:ProgressEvent):void |
251 | |
{ |
252 | 1 | validateDisposal(); |
253 | |
|
254 | 1 | if (!_monitoring) |
255 | |
{ |
256 | 0 | var errorMessage:String = "Object entered into an illegal state:\n"; |
257 | 0 | errorMessage += "Event: <ProgressEvent.PROGRESS> must be dispatched after event: <" + LoaderEvent.OPEN + ">"; |
258 | |
|
259 | 0 | throw new IllegalStateError(errorMessage); |
260 | |
} |
261 | |
|
262 | 1 | updateMonitoring(event.bytesTotal, event.bytesLoaded); |
263 | 1 | _dispatcher.dispatchProgressEvent(_monitoring); |
264 | 1 | } |
265 | |
|
266 | |
private function loaderStoppedHandler(event:LoaderEvent):void |
267 | |
{ |
268 | 1 | validateDisposal(); |
269 | 1 | _dispatcher.dispatchStoppedEvent(_monitoring); |
270 | 1 | } |
271 | |
|
272 | |
} |
273 | |
|
274 | |
} |