Coverage Report - org.vostokframework.domain.loading.states.fileloader.adapters.ProgressNetStream
 
Classes in this File Line Coverage Branch Coverage Complexity
ProgressNetStream
0%
0/54
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  0
 package org.vostokframework.domain.loading.states.fileloader.adapters
 30  
 {
 31  
         import org.vostokframework.domain.loading.states.fileloader.IDataLoader;
 32  
         import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmMediaEvent;
 33  
 
 34  
         import flash.events.Event;
 35  
         import flash.events.ProgressEvent;
 36  
         import flash.events.TimerEvent;
 37  
         import flash.net.NetStream;
 38  
         import flash.utils.Timer;
 39  
 
 40  
         /**
 41  
          * description
 42  
          * 
 43  
          * @author Flávio Silva
 44  
          */
 45  
         public class ProgressNetStream extends DataLoaderBehavior
 46  
         {
 47  
                 /**
 48  
                  * @private
 49  
                   */
 50  
                 private var _bufferCompleteEventDispatched:Boolean;
 51  
                 private var _completeEventDispatched:Boolean;
 52  
                 private var _netStream:NetStream;
 53  
                 private var _openEventDispatched:Boolean;
 54  
                 private var _percentToBuffer:int;
 55  
                 private var _timerProgress:Timer;
 56  
                 
 57  
                 /**
 58  
                  * description
 59  
                  * 
 60  
                  * @param loader
 61  
                  * @param request
 62  
                  * @param context
 63  
                  */
 64  
                 public function ProgressNetStream(wrappedDataLoader:IDataLoader, netStream:NetStream, percentToBuffer:int = 0)
 65  
                 {
 66  0
                         super(wrappedDataLoader);
 67  
                         
 68  0
                         if (!netStream) throw new ArgumentError("Argument <netStream> must not be null.");
 69  
                         
 70  0
                         _netStream = netStream;
 71  0
                         _percentToBuffer = percentToBuffer;
 72  
                         
 73  0
                         _timerProgress = new Timer(50);
 74  0
                 }
 75  
                 
 76  
                 override public function cancel(): void
 77  
                 {
 78  0
                         stopTimer();
 79  0
                         super.cancel();
 80  0
                 }
 81  
                 
 82  
                 /**
 83  
                  * description
 84  
                  */
 85  
                 override public function load(): void
 86  
                 {
 87  0
                         super.load();
 88  0
                         startTimer();
 89  0
                 }
 90  
                 
 91  
                 /**
 92  
                  * description
 93  
                  */
 94  
                 override public function stop():void
 95  
                 {
 96  0
                         stopTimer();
 97  0
                         _completeEventDispatched = false;
 98  0
                         _bufferCompleteEventDispatched = false;
 99  0
                         _openEventDispatched = false;
 100  
                         
 101  0
                         super.stop();
 102  0
                 }
 103  
                 
 104  
                 override protected function doDispose():void
 105  
                 {
 106  0
                         stopTimer();
 107  
                         
 108  0
                         _netStream = null;
 109  0
                         _timerProgress = null;
 110  0
                 }
 111  
                 
 112  
                 private function dispatchProgressEvent():void
 113  
                 {
 114  0
                         _netStream.dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, _netStream.bytesLoaded, _netStream.bytesTotal));
 115  0
                 }
 116  
                 
 117  
                 private function loadingBufferComplete():void
 118  
                 {
 119  0
                         _bufferCompleteEventDispatched = true;
 120  0
                         dispatchProgressEvent();
 121  0
                         _netStream.dispatchEvent(new FileLoadingAlgorithmMediaEvent(FileLoadingAlgorithmMediaEvent.BUFFER_COMPLETE));
 122  0
                 }
 123  
                 
 124  
                 private function loadingComplete():void
 125  
                 {
 126  0
                         stopTimer();
 127  0
                         _completeEventDispatched = true;
 128  
                         
 129  
                         //enforces ProgressEvent.PROGRESS with bytesLoaded == bytesTotal
 130  0
                         _netStream.dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, _netStream.bytesTotal, _netStream.bytesTotal));
 131  0
                         _netStream.dispatchEvent(new Event(Event.COMPLETE));
 132  0
                 }
 133  
                 
 134  
                 private function loadingOpen():void
 135  
                 {
 136  0
                         _openEventDispatched = true;
 137  0
                         _netStream.dispatchEvent(new Event(Event.OPEN));
 138  0
                 }
 139  
                 
 140  
                 private function progressHandler(event:TimerEvent):void
 141  
                 {
 142  0
                         var bufferBytesTotal:int = (_netStream.bytesTotal * _percentToBuffer) / 100;
 143  0
                         var currentBufferPercent:int = (_netStream.bytesTotal > 0) ? Math.floor((_netStream.bytesLoaded * 100) / bufferBytesTotal) : 0;
 144  
                         
 145  0
                         if (!_openEventDispatched)
 146  
                         {
 147  0
                                 loadingOpen();
 148  
                         }
 149  0
                         else if (_percentToBuffer > 0 && currentBufferPercent >= _percentToBuffer && !_bufferCompleteEventDispatched)
 150  
                         {
 151  0
                                 loadingBufferComplete();
 152  
                         }
 153  0
                         else if (_netStream.bytesLoaded == _netStream.bytesTotal && _netStream.bytesLoaded > 0 && !_completeEventDispatched)
 154  
                         {
 155  0
                                 loadingComplete();
 156  
                         }
 157  
                         else
 158  
                         {
 159  0
                                 dispatchProgressEvent();
 160  
                         }
 161  0
                 }
 162  
                 
 163  
                 private function startTimer():void
 164  
                 {
 165  0
                         _timerProgress.addEventListener(TimerEvent.TIMER, progressHandler, false, 0, true);
 166  0
                         _timerProgress.start();
 167  0
                 }
 168  
                 
 169  
                 private function stopTimer():void
 170  
                 {
 171  0
                         _timerProgress.stop();
 172  0
                         _timerProgress.reset();
 173  0
                         _timerProgress.removeEventListener(TimerEvent.TIMER, progressHandler, false);
 174  0
                 }
 175  
                 
 176  
         }
 177  
 
 178  
 }