Coverage Report - org.vostokframework.application.monitoring.monitors.LoadingMonitor
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadingMonitor
87%
80/91
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.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  
          * @author Flavio
 48  
          * @version 1.0
 49  
          * @created 14-mai-2011 12:02:52
 50  
          */
 51  
         public class LoadingMonitor implements ILoadingMonitor
 52  
         {
 53  
                 /**
 54  
                  * @private
 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  
                  * @param loader
 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  
                  * @private
 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  
 }