Coverage Report - org.vostokframework.domain.loading.states.fileloader.algorithms.LatencyTimeoutFileLoadingAlgorithm
 
Classes in this File Line Coverage Branch Coverage Complexity
LatencyTimeoutFileLoadingAlgorithm
94%
53/56
N/A
0
 
 1  
 /*
 2  
  * Licensed under the MIT License
 3  
  * 
 4  
  * Copyright 2011 (c) Flávio Silva, flsilva.com
 5  
  *
 6  
  * Permission is hereby granted, free of charge, to any person
 7  
  * obtaining a copy of this software and associated documentation
 8  
  * files (the "Software"), to deal in the Software without
 9  
  * restriction, including without limitation the rights to use,
 10  
  * copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  
  * copies of the Software, and to permit persons to whom the
 12  
  * Software is furnished to do so, subject to the following
 13  
  * conditions:
 14  
  *
 15  
  * The above copyright notice and this permission notice shall be
 16  
  * included in all copies or substantial portions of the Software.
 17  
  *
 18  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 19  
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 20  
  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 21  
  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 22  
  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 23  
  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 24  
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 25  
  * OTHER DEALINGS IN THE SOFTWARE.
 26  
  * 
 27  
  * http://www.opensource.org/licenses/mit-license.php
 28  
  */
 29  1
 package org.vostokframework.domain.loading.states.fileloader.algorithms
 30  
 {
 31  
         import org.as3collections.IList;
 32  
         import org.as3collections.lists.ArrayList;
 33  
         import org.vostokframework.domain.loading.LoadError;
 34  
         import org.vostokframework.domain.loading.LoadErrorType;
 35  
         import org.vostokframework.domain.loading.states.fileloader.IFileLoadingAlgorithm;
 36  
         import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmErrorEvent;
 37  
         import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmEvent;
 38  
 
 39  
         import flash.events.TimerEvent;
 40  
         import flash.utils.Timer;
 41  
 
 42  
         /**
 43  
          * description
 44  
          * 
 45  
          * @author Flávio Silva
 46  
          */
 47  
         public class LatencyTimeoutFileLoadingAlgorithm extends FileLoadingAlgorithmBehavior
 48  
         {
 49  
                 /**
 50  
                  * @private
 51  
                   */
 52  
                 private static const LATENCY_TIMEOUT_ERROR_MESSAGE:String = "Internal VostokFramework Latency Timeout Error: Timeout of $LATENCY_TIMEOUT milliseconds.";
 53  
                 
 54  
                 private var _latencyTimeout:Number;
 55  
                 private var _timer:Timer;
 56  
                 
 57  
                 /**
 58  
                  * description
 59  
                  * 
 60  
                  * @param loader
 61  
                  * @param request
 62  
                  * @param context
 63  
                  */
 64  
                 public function LatencyTimeoutFileLoadingAlgorithm(wrapAlgorithm:IFileLoadingAlgorithm, latencyTimeout:Number = 12000)
 65  
                 {
 66  1
                         super(wrapAlgorithm);
 67  
                         
 68  1
                         if (latencyTimeout < 1000) throw new ArgumentError("Argument <latencyTimeout> must be greater than 999. Received: <" + latencyTimeout + ">");
 69  
                         
 70  1
                         _latencyTimeout = latencyTimeout;
 71  1
                         createTimer();
 72  1
                         addWrappedAlgorithmListeners();
 73  1
                 }
 74  
                 
 75  
                 /**
 76  
                  * description
 77  
                  * 
 78  
                  * @return
 79  
                   */
 80  
                 override public function cancel(): void
 81  
                 {
 82  1
                         validateDisposal();
 83  1
                         stopTimer();
 84  1
                         super.cancel();
 85  1
                 }
 86  
                 
 87  
                 /**
 88  
                  * description
 89  
                  * 
 90  
                  * @return
 91  
                   */
 92  
                 override public function load(): void
 93  
                 {
 94  1
                         validateDisposal();
 95  1
                         startTimer();
 96  1
                         super.load();
 97  1
                 }
 98  
                 
 99  
                 /**
 100  
                  * description
 101  
                  * 
 102  
                  * @return
 103  
                   */
 104  
                 override public function stop(): void
 105  
                 {
 106  1
                         validateDisposal();
 107  1
                         stopTimer();
 108  1
                         super.stop();
 109  1
                 }
 110  
                 
 111  
                 override protected function doDispose():void
 112  
                 {
 113  1
                         removeWrappedAlgorithmListeners();
 114  1
                         stopTimer();
 115  
                         
 116  1
                         _timer = null;
 117  1
                 }
 118  
                 
 119  
                 private function addWrappedAlgorithmListeners():void
 120  
                 {
 121  1
                         wrappedAlgorithm.addEventListener(FileLoadingAlgorithmEvent.OPEN, openHandler, false, int.MAX_VALUE, true);
 122  1
                         wrappedAlgorithm.addEventListener(FileLoadingAlgorithmErrorEvent.FAILED, failedHandler, false, int.MAX_VALUE, true);
 123  1
                 }
 124  
                 
 125  
                 private function createErrorMessage():String
 126  
                 {
 127  1
                         return LATENCY_TIMEOUT_ERROR_MESSAGE.replace("$LATENCY_TIMEOUT", _latencyTimeout);
 128  
                 }
 129  
                 
 130  
                 private function createTimer():void
 131  
                 {
 132  1
                         _timer = new Timer(_latencyTimeout);
 133  1
                 }
 134  
                 
 135  
                 private function failedHandler(event:FileLoadingAlgorithmErrorEvent):void
 136  
                 {
 137  0
                         validateDisposal();
 138  0
                         stopTimer();
 139  0
                 }
 140  
                 
 141  
                 private function openHandler(event:FileLoadingAlgorithmEvent):void
 142  
                 {
 143  1
                         validateDisposal();
 144  1
                         stopTimer();
 145  1
                 }
 146  
                 
 147  
                 private function startTimer():void
 148  
                 {
 149  1
                         _timer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true);
 150  1
                         _timer.start();
 151  1
                 }
 152  
                 
 153  
                 private function stopTimer():void
 154  
                 {
 155  1
                         _timer.stop();
 156  1
                         _timer.reset();
 157  1
                         _timer.removeEventListener(TimerEvent.TIMER, timerHandler, false);
 158  1
                 }
 159  
                 
 160  
                 private function timeout():void
 161  
                 {
 162  1
                         validateDisposal();
 163  1
                         removeWrappedAlgorithmListeners();
 164  1
                         stop();
 165  
                         
 166  1
                         var errors:IList = new ArrayList();
 167  1
                         var error:LoadError = new LoadError(LoadErrorType.LATENCY_TIMEOUT_ERROR, createErrorMessage());
 168  1
                         errors.add(error);
 169  
                         
 170  1
                         dispatchEvent(new FileLoadingAlgorithmErrorEvent(FileLoadingAlgorithmErrorEvent.FAILED, errors));
 171  1
                 }
 172  
                 
 173  
                 private function timerHandler(event:TimerEvent):void
 174  
                 {
 175  1
                         stopTimer();
 176  1
                         timeout();
 177  1
                 }
 178  
                 
 179  
                 private function removeWrappedAlgorithmListeners():void
 180  
                 {
 181  1
                         wrappedAlgorithm.removeEventListener(FileLoadingAlgorithmEvent.OPEN, openHandler, false);
 182  1
                         wrappedAlgorithm.removeEventListener(FileLoadingAlgorithmErrorEvent.FAILED, failedHandler, false);
 183  1
                 }
 184  
                 
 185  
         }
 186  
 
 187  
 }