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.domain.loading.states.queueloader |
30 | |
{ |
31 | |
import org.as3collections.IIterator; |
32 | |
import org.vostokframework.domain.loading.ILoader; |
33 | |
import org.vostokframework.domain.loading.ILoaderStateTransition; |
34 | |
import org.vostokframework.domain.loading.events.LoaderErrorEvent; |
35 | |
import org.vostokframework.domain.loading.events.LoaderEvent; |
36 | |
import org.vostokframework.domain.loading.states.queueloader.IQueueLoadingPolicy; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class LoadingQueueLoader extends QueueLoaderState |
44 | |
{ |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private var _openEventDispatched:Boolean; |
50 | |
|
51 | |
override public function get isLoading():Boolean { return true; } |
52 | |
|
53 | |
override public function get openedConnections():int |
54 | |
{ |
55 | 1 | validateDisposal(); |
56 | |
|
57 | 1 | var it:IIterator = loadingStatus.allLoaders.iterator(); |
58 | |
var child:ILoader; |
59 | |
var sum:int; |
60 | |
|
61 | 1 | while (it.hasNext()) |
62 | |
{ |
63 | 1 | child = it.next(); |
64 | 1 | sum += child.openedConnections; |
65 | |
} |
66 | |
|
67 | 1 | return sum; |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public function LoadingQueueLoader(loader:ILoaderStateTransition, loadingStatus:QueueLoadingStatus, policy:IQueueLoadingPolicy, maxConcurrentConnections:int) |
83 | |
{ |
84 | 1 | super(loadingStatus, policy, maxConcurrentConnections); |
85 | |
|
86 | 1 | if (loadingStatus.queuedLoaders.isEmpty()) |
87 | |
{ |
88 | 1 | var errorMessage:String = "Argument <loadingStatus> must not have an empty queue (loadingStatus.queuedLoaders)."; |
89 | 1 | errorMessage += " Queue must have at least one element."; |
90 | |
|
91 | 1 | throw new ArgumentError(errorMessage); |
92 | |
} |
93 | |
|
94 | 1 | setLoader(loader); |
95 | 1 | addChildrenListeners(); |
96 | 1 | processLoading(); |
97 | 1 | } |
98 | |
|
99 | |
override public function load():void |
100 | |
{ |
101 | |
|
102 | 1 | } |
103 | |
|
104 | |
override public function stop():void |
105 | |
{ |
106 | 1 | super.stop(); |
107 | 1 | _openEventDispatched = false; |
108 | 1 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
override protected function doDispose():void |
114 | |
{ |
115 | 1 | removeChildrenListeners(); |
116 | 1 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
override protected function childAdded(child:ILoader): void |
122 | |
{ |
123 | 1 | addChildListeners(child); |
124 | 1 | processLoading(); |
125 | 1 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
override protected function childRemoved(child:ILoader): void |
131 | |
{ |
132 | 1 | removeChildListeners(child); |
133 | 1 | processLoading(); |
134 | 1 | } |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
override protected function childResumed(child:ILoader): void |
140 | |
{ |
141 | 1 | child = null; |
142 | 1 | processLoading(); |
143 | 1 | } |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
override protected function childStopped(child:ILoader): void |
149 | |
{ |
150 | 1 | child = null; |
151 | 1 | processLoading(); |
152 | 1 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
override protected function maxConcurrentConnectionsChanged(): void |
158 | |
{ |
159 | 1 | processLoading(); |
160 | 1 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
override protected function priorityChildChanged(child:ILoader): void |
166 | |
{ |
167 | 1 | child = null; |
168 | 1 | processLoading(); |
169 | 1 | } |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
private function addChildrenListeners():void |
175 | |
{ |
176 | 1 | if (loadingStatus.allLoaders.isEmpty()) return; |
177 | |
|
178 | 1 | var it:IIterator = loadingStatus.allLoaders.iterator(); |
179 | |
var child:ILoader; |
180 | |
|
181 | 1 | while (it.hasNext()) |
182 | |
{ |
183 | 1 | child = it.next(); |
184 | 1 | addChildListeners(child); |
185 | |
} |
186 | 1 | } |
187 | |
|
188 | |
private function addChildListeners(child:ILoader):void |
189 | |
{ |
190 | 1 | validateDisposal(); |
191 | |
|
192 | 1 | child.addEventListener(LoaderEvent.COMPLETE, childCompleteHandler, false, 0, true); |
193 | 1 | child.addEventListener(LoaderErrorEvent.FAILED, childFailedHandler, false, 0, true); |
194 | 1 | child.addEventListener(LoaderEvent.OPEN, childOpenHandler, false, 0, true); |
195 | 1 | } |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
private function isLoadingComplete():Boolean |
201 | |
{ |
202 | 1 | validateDisposal(); |
203 | |
|
204 | 1 | return loadingStatus.loadingLoaders.isEmpty() && |
205 | 1 | loadingStatus.queuedLoaders.isEmpty() && |
206 | 1 | loadingStatus.stoppedLoaders.isEmpty(); |
207 | |
} |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
private function loadingComplete(): void |
213 | |
{ |
214 | 1 | removeChildrenListeners(); |
215 | |
|
216 | 1 | loader.setState(new CompleteQueueLoader(loader, loadingStatus, policy, maxConcurrentConnections)); |
217 | 1 | loader.dispatchEvent(new LoaderEvent(LoaderEvent.COMPLETE)); |
218 | 1 | dispose(); |
219 | 1 | } |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
private function processLoading(): void |
225 | |
{ |
226 | 1 | validateDisposal(); |
227 | |
|
228 | 1 | if (isLoadingComplete()) |
229 | |
{ |
230 | 1 | loadingComplete(); |
231 | 1 | return; |
232 | |
} |
233 | |
|
234 | 1 | if (loadingStatus.queuedLoaders.isEmpty()) return; |
235 | |
|
236 | 1 | policy.process(loadingStatus, maxConcurrentConnections); |
237 | 1 | } |
238 | |
|
239 | |
private function removeChildrenListeners():void |
240 | |
{ |
241 | 1 | validateDisposal(); |
242 | |
|
243 | 1 | var it:IIterator = loadingStatus.allLoaders.iterator(); |
244 | |
var child:ILoader; |
245 | |
|
246 | 1 | while (it.hasNext()) |
247 | |
{ |
248 | 1 | child = it.next(); |
249 | 1 | removeChildListeners(child); |
250 | |
} |
251 | 1 | } |
252 | |
|
253 | |
private function removeChildListeners(child:ILoader):void |
254 | |
{ |
255 | 1 | validateDisposal(); |
256 | |
|
257 | 1 | child.removeEventListener(LoaderEvent.COMPLETE, childCompleteHandler, false); |
258 | 1 | child.removeEventListener(LoaderErrorEvent.FAILED, childFailedHandler, false); |
259 | 1 | child.removeEventListener(LoaderEvent.OPEN, childOpenHandler, false); |
260 | 1 | } |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
private function childCompleteHandler(event:LoaderEvent):void |
267 | |
{ |
268 | 1 | loadingStatus.completeLoaders.add(event.target); |
269 | 1 | loadingStatus.loadingLoaders.remove(event.target); |
270 | |
|
271 | 1 | processLoading(); |
272 | 1 | } |
273 | |
|
274 | |
private function childFailedHandler(event:LoaderErrorEvent):void |
275 | |
{ |
276 | 1 | loadingStatus.failedLoaders.add(event.target); |
277 | 1 | loadingStatus.loadingLoaders.remove(event.target); |
278 | |
|
279 | 1 | processLoading(); |
280 | 1 | } |
281 | |
|
282 | |
private function childOpenHandler(event:LoaderEvent):void |
283 | |
{ |
284 | 1 | validateDisposal(); |
285 | |
|
286 | 1 | if (!_openEventDispatched) loader.dispatchEvent(new LoaderEvent(LoaderEvent.OPEN, null, event.latency)); |
287 | 1 | _openEventDispatched = true; |
288 | 1 | } |
289 | |
} |
290 | |
|
291 | |
} |