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.as3collections.IList; |
33 | |
import org.as3coreaddendum.errors.ObjectDisposedError; |
34 | |
import org.as3coreaddendum.events.PriorityEvent; |
35 | |
import org.as3utils.ReflectionUtil; |
36 | |
import org.vostokframework.VostokIdentification; |
37 | |
import org.vostokframework.domain.loading.ILoader; |
38 | |
import org.vostokframework.domain.loading.ILoaderState; |
39 | |
import org.vostokframework.domain.loading.ILoaderStateTransition; |
40 | |
import org.vostokframework.domain.loading.errors.DuplicateLoaderError; |
41 | |
import org.vostokframework.domain.loading.errors.LoaderNotFoundError; |
42 | |
import org.vostokframework.domain.loading.events.LoaderEvent; |
43 | |
|
44 | |
import flash.errors.IllegalOperationError; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public class QueueLoaderState implements ILoaderState |
52 | |
{ |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
private var _disposed:Boolean; |
58 | |
private var _loader:ILoaderStateTransition; |
59 | |
private var _loadingStatus:QueueLoadingStatus; |
60 | |
private var _maxConcurrentConnections:int; |
61 | |
private var _policy:IQueueLoadingPolicy; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
protected function get loader():ILoaderStateTransition { return _loader; } |
67 | |
|
68 | |
protected function get loadingStatus():QueueLoadingStatus { return _loadingStatus; } |
69 | |
|
70 | |
protected function get policy():IQueueLoadingPolicy { return _policy; } |
71 | |
|
72 | |
public function get isLoading():Boolean { return false; } |
73 | |
|
74 | |
public function get isQueued():Boolean { return false; } |
75 | |
|
76 | |
public function get isStopped():Boolean { return false; } |
77 | |
|
78 | |
public function get maxConcurrentConnections():int { return _maxConcurrentConnections; } |
79 | |
|
80 | |
public function get openedConnections():int { return 0; } |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public function QueueLoaderState(loadingStatus:QueueLoadingStatus, policy:IQueueLoadingPolicy, maxConcurrentConnections:int) |
89 | 1 | { |
90 | 1 | if (ReflectionUtil.classPathEquals(this, QueueLoaderState)) throw new IllegalOperationError(ReflectionUtil.getClassName(this) + " is an abstract class and shouldn't be directly instantiated."); |
91 | 1 | if (!loadingStatus) throw new ArgumentError("Argument <loadingStatus> must not be null."); |
92 | 1 | if (!policy) throw new ArgumentError("Argument <policy> must not be null."); |
93 | 1 | if (maxConcurrentConnections < 1) throw new ArgumentError("Argument <maxConcurrentConnections> must be greater than zero. Received: <" + maxConcurrentConnections + ">"); |
94 | |
|
95 | 1 | _loadingStatus = loadingStatus; |
96 | 1 | _policy = policy; |
97 | 1 | _maxConcurrentConnections = maxConcurrentConnections; |
98 | |
|
99 | 1 | addPriorityListenerToLoaders(); |
100 | 1 | } |
101 | |
|
102 | |
public function addChild(child:ILoader): void |
103 | |
{ |
104 | 1 | validateDisposal(); |
105 | 1 | if (!child) throw new ArgumentError("Argument <loader> must not be null."); |
106 | |
|
107 | 1 | if (containsChild(child.identification)) |
108 | |
{ |
109 | 1 | var errorMessage:String = "There is already an ILoader object stored with identification:\n"; |
110 | 1 | errorMessage += "<" + child.identification + ">"; |
111 | |
|
112 | 1 | throw new DuplicateLoaderError(child.identification, errorMessage); |
113 | |
} |
114 | |
|
115 | 1 | child.index = loadingStatus.allLoaders.size(); |
116 | |
|
117 | |
|
118 | 1 | loadingStatus.allLoaders.put(child.identification.toString(), child); |
119 | 1 | loadingStatus.queuedLoaders.add(child); |
120 | |
|
121 | 1 | addPriorityListenerToLoader(child); |
122 | 1 | childAdded(child); |
123 | 1 | } |
124 | |
|
125 | |
public function addChildren(children:IList): void |
126 | |
{ |
127 | 1 | validateDisposal(); |
128 | |
|
129 | 1 | if (!children || children.isEmpty()) throw new ArgumentError("Argument <loaders> must not be null nor empty."); |
130 | |
|
131 | 1 | var it:IIterator = children.iterator(); |
132 | |
var child:ILoader; |
133 | |
|
134 | 1 | while (it.hasNext()) |
135 | |
{ |
136 | 1 | child = it.next(); |
137 | 1 | addChild(child); |
138 | |
} |
139 | 1 | } |
140 | |
|
141 | |
public function cancel():void |
142 | |
{ |
143 | 1 | validateDisposal(); |
144 | |
|
145 | 1 | var it:IIterator = loadingStatus.allLoaders.iterator(); |
146 | |
var child:ILoader; |
147 | |
|
148 | 1 | while (it.hasNext()) |
149 | |
{ |
150 | 1 | child = it.next(); |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | 1 | loadingStatus.allLoaders.remove(child.identification.toString()); |
159 | 1 | loadingStatus.completeLoaders.remove(child); |
160 | 1 | loadingStatus.failedLoaders.remove(child); |
161 | 1 | loadingStatus.loadingLoaders.remove(child); |
162 | 1 | loadingStatus.queuedLoaders.remove(child); |
163 | 1 | loadingStatus.stoppedLoaders.remove(child); |
164 | |
|
165 | 1 | child.cancel(); |
166 | 1 | child.dispose(); |
167 | |
} |
168 | |
|
169 | 1 | loader.setState(new CanceledQueueLoader(loader, loadingStatus, policy, _maxConcurrentConnections)); |
170 | 1 | loader.dispatchEvent(new LoaderEvent(LoaderEvent.CANCELED)); |
171 | 1 | dispose(); |
172 | 1 | } |
173 | |
|
174 | |
public function cancelChild(identification:VostokIdentification): void |
175 | |
{ |
176 | 1 | validateDisposal(); |
177 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
178 | |
|
179 | |
var child:ILoader; |
180 | |
|
181 | 1 | if (loadingStatus.allLoaders.containsKey(identification.toString())) |
182 | |
{ |
183 | 1 | child = loadingStatus.allLoaders.getValue(identification.toString()); |
184 | |
|
185 | 1 | loadingStatus.completeLoaders.remove(child); |
186 | 1 | loadingStatus.loadingLoaders.remove(child); |
187 | 1 | loadingStatus.queuedLoaders.remove(child); |
188 | 1 | loadingStatus.stoppedLoaders.remove(child); |
189 | |
|
190 | 1 | child.cancel(); |
191 | 1 | childCanceled(child); |
192 | |
|
193 | 1 | removeChild(child.identification); |
194 | 1 | child.dispose(); |
195 | |
} |
196 | |
else |
197 | |
{ |
198 | 1 | var it:IIterator = loadingStatus.allLoaders.iterator(); |
199 | |
|
200 | 1 | while (it.hasNext()) |
201 | |
{ |
202 | 1 | child = it.next(); |
203 | 1 | if (child.containsChild(identification)) |
204 | |
{ |
205 | 1 | child.cancelChild(identification); |
206 | 1 | return; |
207 | |
} |
208 | |
} |
209 | |
|
210 | 1 | var message:String = "There is no ILoader object stored with identification:\n"; |
211 | 1 | message += "<" + identification + ">"; |
212 | 1 | throw new LoaderNotFoundError(identification, message); |
213 | |
} |
214 | 1 | } |
215 | |
|
216 | |
public function containsChild(identification:VostokIdentification): Boolean |
217 | |
{ |
218 | 1 | validateDisposal(); |
219 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
220 | |
|
221 | 1 | if (loadingStatus.allLoaders.isEmpty()) return false; |
222 | 1 | if (loadingStatus.allLoaders.containsKey(identification.toString())) return true; |
223 | |
|
224 | 1 | var it:IIterator = loadingStatus.allLoaders.iterator(); |
225 | |
var child:ILoader; |
226 | |
|
227 | 1 | while (it.hasNext()) |
228 | |
{ |
229 | 1 | child = it.next(); |
230 | 1 | if (child.containsChild(identification)) return true; |
231 | |
} |
232 | |
|
233 | 1 | return false; |
234 | |
} |
235 | |
|
236 | |
public function dispose():void |
237 | |
{ |
238 | 1 | if (_disposed) return; |
239 | |
|
240 | 1 | doDispose(); |
241 | 1 | removePriorityListenerFromLoaders(); |
242 | |
|
243 | |
|
244 | |
|
245 | 1 | _disposed = true; |
246 | 1 | _loader = null; |
247 | 1 | _loadingStatus = null; |
248 | 1 | _policy = null; |
249 | 1 | } |
250 | |
|
251 | |
public function getChild(identification:VostokIdentification): ILoader |
252 | |
{ |
253 | 1 | validateDisposal(); |
254 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
255 | |
|
256 | 1 | if (loadingStatus.allLoaders.containsKey(identification.toString())) |
257 | |
{ |
258 | 1 | return loadingStatus.allLoaders.getValue(identification.toString()); |
259 | |
} |
260 | |
else |
261 | |
{ |
262 | 1 | var it:IIterator = loadingStatus.allLoaders.iterator(); |
263 | |
var child:ILoader; |
264 | |
|
265 | 1 | while (it.hasNext()) |
266 | |
{ |
267 | 1 | child = it.next(); |
268 | 1 | if (child.containsChild(identification)) return child.getChild(identification); |
269 | |
} |
270 | |
|
271 | 1 | var message:String = "There is no ILoader object stored with identification:\n"; |
272 | 1 | message += "<" + identification + ">"; |
273 | 1 | throw new LoaderNotFoundError(identification, message); |
274 | |
} |
275 | |
} |
276 | |
|
277 | |
public function getParent(identification:VostokIdentification): ILoader |
278 | |
{ |
279 | 1 | validateDisposal(); |
280 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
281 | |
|
282 | 1 | if (loadingStatus.allLoaders.containsKey(identification.toString())) |
283 | |
{ |
284 | 1 | return loader; |
285 | |
} |
286 | |
else |
287 | |
{ |
288 | 1 | var it:IIterator = loadingStatus.allLoaders.iterator(); |
289 | |
var child:ILoader; |
290 | |
|
291 | 1 | while (it.hasNext()) |
292 | |
{ |
293 | 1 | child = it.next(); |
294 | 1 | if (child.containsChild(identification)) return child.getParent(identification); |
295 | |
} |
296 | |
} |
297 | |
|
298 | 0 | return null; |
299 | |
} |
300 | |
|
301 | |
public function load():void |
302 | |
{ |
303 | |
|
304 | 1 | loader.setState(new LoadingQueueLoader(loader, loadingStatus, policy, _maxConcurrentConnections)); |
305 | 1 | loader.dispatchEvent(new LoaderEvent(LoaderEvent.CONNECTING)); |
306 | 1 | dispose(); |
307 | 1 | } |
308 | |
|
309 | |
public function removeChild(identification:VostokIdentification): void |
310 | |
{ |
311 | 1 | validateDisposal(); |
312 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
313 | |
|
314 | |
var child:ILoader; |
315 | |
|
316 | 1 | if (loadingStatus.allLoaders.containsKey(identification.toString())) |
317 | |
{ |
318 | 1 | child = loadingStatus.allLoaders.getValue(identification.toString()); |
319 | |
|
320 | 1 | loadingStatus.allLoaders.remove(identification.toString()); |
321 | 1 | loadingStatus.completeLoaders.remove(child); |
322 | 1 | loadingStatus.failedLoaders.remove(child); |
323 | 1 | loadingStatus.loadingLoaders.remove(child); |
324 | 1 | loadingStatus.queuedLoaders.remove(child); |
325 | 1 | loadingStatus.stoppedLoaders.remove(child); |
326 | |
|
327 | 1 | removePriorityListenerFromLoader(child); |
328 | 1 | childRemoved(child); |
329 | |
} |
330 | |
else |
331 | |
{ |
332 | 1 | var it:IIterator = loadingStatus.allLoaders.iterator(); |
333 | |
|
334 | 1 | while (it.hasNext()) |
335 | |
{ |
336 | 1 | child = it.next(); |
337 | 1 | if (child.containsChild(identification)) |
338 | |
{ |
339 | 0 | child.removeChild(identification); |
340 | 0 | return; |
341 | |
} |
342 | |
} |
343 | |
|
344 | 1 | var message:String = "There is no ILoader object stored with identification:\n"; |
345 | 1 | message += "<" + identification + ">"; |
346 | 1 | throw new LoaderNotFoundError(identification, message); |
347 | |
} |
348 | 1 | } |
349 | |
|
350 | |
public function resumeChild(identification:VostokIdentification): void |
351 | |
{ |
352 | 1 | validateDisposal(); |
353 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
354 | |
|
355 | |
var child:ILoader; |
356 | |
|
357 | 1 | if (loadingStatus.allLoaders.containsKey(identification.toString())) |
358 | |
{ |
359 | 1 | child = loadingStatus.allLoaders.getValue(identification.toString()); |
360 | |
|
361 | 1 | if (loadingStatus.queuedLoaders.contains(child) |
362 | 1 | || loadingStatus.loadingLoaders.contains(child)) return; |
363 | |
|
364 | |
var errorMessage:String; |
365 | |
|
366 | 1 | if (loadingStatus.failedLoaders.contains(child)) |
367 | |
{ |
368 | 0 | errorMessage = "ILoader object with identification:\n"; |
369 | 0 | errorMessage += identification + "\n"; |
370 | 0 | errorMessage += "has failed, therefore it is not allowed to resume it."; |
371 | |
|
372 | 0 | throw new IllegalOperationError(errorMessage); |
373 | |
} |
374 | |
|
375 | 1 | if (loadingStatus.completeLoaders.contains(child)) |
376 | |
{ |
377 | 0 | errorMessage = "ILoader object with identification:\n"; |
378 | 0 | errorMessage += identification + "\n"; |
379 | 0 | errorMessage += "is complete, therefore it is not allowed to resume it."; |
380 | |
|
381 | 0 | throw new IllegalOperationError(errorMessage); |
382 | |
} |
383 | |
|
384 | 1 | loadingStatus.queuedLoaders.add(child); |
385 | 1 | loadingStatus.stoppedLoaders.remove(child); |
386 | |
|
387 | 1 | childResumed(child); |
388 | |
} |
389 | |
else |
390 | |
{ |
391 | 1 | var it:IIterator = loadingStatus.allLoaders.iterator(); |
392 | |
|
393 | 1 | while (it.hasNext()) |
394 | |
{ |
395 | 1 | child = it.next(); |
396 | 1 | if (child.containsChild(identification)) |
397 | |
{ |
398 | 1 | child.resumeChild(identification); |
399 | 1 | return; |
400 | |
} |
401 | |
} |
402 | |
|
403 | 1 | var message:String = "There is no ILoader object stored with identification:\n"; |
404 | 1 | message += "<" + identification + ">"; |
405 | 1 | throw new LoaderNotFoundError(identification, message); |
406 | |
} |
407 | 1 | } |
408 | |
|
409 | |
public function setLoader(loader:ILoaderStateTransition):void |
410 | |
{ |
411 | 1 | _loader = loader; |
412 | 1 | } |
413 | |
|
414 | |
public function setMaxConcurrentConnections(value:int):void |
415 | |
{ |
416 | 1 | if (value < 1) throw new ArgumentError("Value must be greater than zero. Received: <" + value + ">"); |
417 | |
|
418 | 1 | _maxConcurrentConnections = value; |
419 | 1 | maxConcurrentConnectionsChanged(); |
420 | 1 | } |
421 | |
|
422 | |
public function stop():void |
423 | |
{ |
424 | 1 | validateDisposal(); |
425 | |
|
426 | |
|
427 | 1 | var it:IIterator = loadingStatus.loadingLoaders.iterator(); |
428 | |
var child:ILoader; |
429 | |
|
430 | 1 | while (it.hasNext()) |
431 | |
{ |
432 | 1 | child = it.next(); |
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | 1 | loadingStatus.queuedLoaders.add(child); |
441 | 1 | it.remove(); |
442 | |
|
443 | 1 | child.stop(); |
444 | |
} |
445 | |
|
446 | 1 | loader.setState(new StoppedQueueLoader(loader, loadingStatus, policy, _maxConcurrentConnections)); |
447 | 1 | loader.dispatchEvent(new LoaderEvent(LoaderEvent.STOPPED)); |
448 | 1 | dispose(); |
449 | 1 | } |
450 | |
|
451 | |
public function stopChild(identification:VostokIdentification): void |
452 | |
{ |
453 | 1 | validateDisposal(); |
454 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
455 | |
|
456 | |
var child:ILoader; |
457 | |
|
458 | 1 | if (loadingStatus.allLoaders.containsKey(identification.toString())) |
459 | |
{ |
460 | 1 | child = loadingStatus.allLoaders.getValue(identification.toString()); |
461 | |
|
462 | 1 | loadingStatus.loadingLoaders.remove(child); |
463 | 1 | loadingStatus.queuedLoaders.remove(child); |
464 | 1 | loadingStatus.stoppedLoaders.add(child); |
465 | |
|
466 | 1 | child.stop(); |
467 | 1 | childStopped(child); |
468 | |
} |
469 | |
else |
470 | |
{ |
471 | 1 | var it:IIterator = loadingStatus.allLoaders.iterator(); |
472 | |
|
473 | 1 | while (it.hasNext()) |
474 | |
{ |
475 | 1 | child = it.next(); |
476 | 1 | if (child.containsChild(identification)) |
477 | |
{ |
478 | 1 | child.stopChild(identification); |
479 | 1 | return; |
480 | |
} |
481 | |
} |
482 | |
|
483 | 1 | var message:String = "There is no ILoader object stored with identification:\n"; |
484 | 1 | message += "<" + identification + ">"; |
485 | 1 | throw new LoaderNotFoundError(identification, message); |
486 | |
} |
487 | 1 | } |
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
protected function doDispose():void |
493 | |
{ |
494 | |
|
495 | 1 | } |
496 | |
|
497 | |
|
498 | |
|
499 | |
|
500 | |
protected function childAdded(child:ILoader): void |
501 | |
{ |
502 | |
|
503 | 1 | } |
504 | |
|
505 | |
|
506 | |
|
507 | |
|
508 | |
protected function childCanceled(child:ILoader): void |
509 | |
{ |
510 | |
|
511 | 1 | } |
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
protected function childRemoved(child:ILoader): void |
517 | |
{ |
518 | |
|
519 | 1 | } |
520 | |
|
521 | |
|
522 | |
|
523 | |
|
524 | |
protected function childResumed(child:ILoader): void |
525 | |
{ |
526 | |
|
527 | 0 | } |
528 | |
|
529 | |
|
530 | |
|
531 | |
|
532 | |
protected function childStopped(child:ILoader): void |
533 | |
{ |
534 | |
|
535 | 1 | } |
536 | |
|
537 | |
|
538 | |
|
539 | |
|
540 | |
protected function maxConcurrentConnectionsChanged(): void |
541 | |
{ |
542 | |
|
543 | 1 | } |
544 | |
|
545 | |
|
546 | |
|
547 | |
|
548 | |
protected function priorityChildChanged(child:ILoader): void |
549 | |
{ |
550 | |
|
551 | 0 | } |
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
protected function validateDisposal():void |
557 | |
{ |
558 | 1 | if (_disposed) throw new ObjectDisposedError("This object was disposed, therefore no more operations can be performed."); |
559 | 1 | } |
560 | |
|
561 | |
private function addPriorityListenerToLoaders():void |
562 | |
{ |
563 | 1 | if (_loadingStatus.allLoaders.isEmpty()) return; |
564 | |
|
565 | 1 | var it:IIterator = _loadingStatus.allLoaders.iterator(); |
566 | |
var loader:ILoader; |
567 | |
|
568 | 1 | while (it.hasNext()) |
569 | |
{ |
570 | 1 | loader = it.next(); |
571 | 1 | addPriorityListenerToLoader(loader); |
572 | |
} |
573 | 1 | } |
574 | |
|
575 | |
private function addPriorityListenerToLoader(loader:ILoader):void |
576 | |
{ |
577 | 1 | loader.addEventListener(PriorityEvent.CHANGED, loaderPriorityChanged, false, 0, true); |
578 | 1 | } |
579 | |
|
580 | |
private function removePriorityListenerFromLoaders():void |
581 | |
{ |
582 | 1 | if (_loadingStatus.allLoaders.isEmpty()) return; |
583 | |
|
584 | 1 | var it:IIterator = _loadingStatus.allLoaders.iterator(); |
585 | |
var loader:ILoader; |
586 | |
|
587 | 1 | while (it.hasNext()) |
588 | |
{ |
589 | 1 | loader = it.next(); |
590 | 1 | removePriorityListenerFromLoader(loader); |
591 | |
} |
592 | 1 | } |
593 | |
|
594 | |
private function loaderPriorityChanged(event:PriorityEvent):void |
595 | |
{ |
596 | 1 | priorityChildChanged(event.target as ILoader); |
597 | 1 | } |
598 | |
|
599 | |
private function removePriorityListenerFromLoader(loader:ILoader):void |
600 | |
{ |
601 | 1 | loader.removeEventListener(PriorityEvent.CHANGED, loaderPriorityChanged, false); |
602 | 1 | } |
603 | |
|
604 | |
} |
605 | |
|
606 | |
} |