1 package com.aelitis.azureus.core.speedmanager.impl.v2; 2 3 import org.gudy.azureus2.core3.util.SystemTime; 4 5 /** 6 * Created on Jun 1, 2007 7 * Created by Alan Snyder 8 * Copyright (C) Azureus Software, Inc, All Rights Reserved. 9 * <p/> 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 2 13 * of the License, or (at your option) any later version. 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 21 */ 22 23 /** 24 * Is the application in a "download" mode? Or is it in a "seeding" mode? This is 25 * used to determine up we cut back on upload bandwidth limit. 26 * 27 * Here is how to determine the mode. If the download rate is LOW compared to the capacity 28 * for five minutes continously then it will be considered in a SEEDING mode. 29 * 30 * If the download bandwidth ever goes into the MED range then it switches to DOWNLOADING 31 * mode immediately. 32 * 33 * The application will favor DOWNLOADING mode to SEEDING mode. 34 * 35 */ 36 public class TransferMode 37 { 38 private State mode = State.DOWNLOADING; 39 40 private long lastTimeDownloadDetected = SystemTime.getCurrentTime(); 41 42 private static final long WAIT_TIME_FOR_SEEDING_MODE = 1000 * 60; 43 44 45 TransferMode()46 public TransferMode() 47 { 48 49 } 50 51 52 /** 53 * If the download bandwidth is ever in MED or above switch immediately to DOWNLOADING mode. 54 * If th download bandwidth is LOW or less for more then 5 min, switch to SEEDING mode. 55 * @param downloadBandwidth - current download status. 56 */ updateStatus(SaturatedMode downloadBandwidth)57 public void updateStatus(SaturatedMode downloadBandwidth){ 58 59 //this setting have no effect while testing the limits. 60 if( isConfTestingLimits() ){ 61 if( mode==State.DOWNLOAD_LIMIT_SEARCH ){ 62 lastTimeDownloadDetected = SystemTime.getCurrentTime(); 63 } 64 return; 65 } 66 67 if( downloadBandwidth.compareTo(SaturatedMode.LOW)<=0 ){ 68 //we don't seem to be downloading at the moment. 69 //see if this state has persisted for more then five minutes. 70 long time = SystemTime.getCurrentTime(); 71 72 if( time > lastTimeDownloadDetected+WAIT_TIME_FOR_SEEDING_MODE ){ 73 mode = State.SEEDING; 74 } 75 76 }else{ 77 //Some downloading is happening. Remove from SEEDING mode. 78 mode = State.DOWNLOADING; 79 lastTimeDownloadDetected = SystemTime.getCurrentTime(); 80 } 81 82 } 83 getString()84 public String getString(){ 85 return mode.getString(); 86 } 87 getMode()88 public State getMode(){ 89 return mode; 90 } 91 setMode( State newMode )92 public void setMode( State newMode ){ 93 94 SpeedManagerLogger.trace( " setting transfer mode to: "+newMode.getString() ); 95 96 mode = newMode; 97 } 98 99 /** 100 * Are we in downloading mode? 101 * @return - boolean - true if in downloading mode. Otherwise false. 102 */ isDownloadMode()103 public boolean isDownloadMode(){ 104 105 return ( mode==State.DOWNLOADING ); 106 107 }//isDownloadMode 108 109 /** 110 * We have two types of limit testing. If we are doing a "confidence test" for the limits then 111 * return true. This mode is putting one value at the min setting and the other at unlimited. 112 * @return - true if doing a "conf test of the limits" 113 */ isConfTestingLimits()114 public boolean isConfTestingLimits(){ 115 return ( mode==State.DOWNLOAD_LIMIT_SEARCH || mode==State.UPLOAD_LIMIT_SEARCH ); 116 } 117 118 119 /** 120 * Java 1.4 enumeration. - Seeding mode or Downloading mode. 121 */ 122 static class State{ 123 public static final State DOWNLOADING = new State("downloading"); 124 public static final State SEEDING = new State("seeding"); 125 public static final State DOWNLOAD_LIMIT_SEARCH = new State("download limit search"); 126 public static final State UPLOAD_LIMIT_SEARCH = new State("upload limit search"); 127 String mode; 128 State(String _mode)129 private State(String _mode){ 130 mode = _mode; 131 } 132 getString()133 public String getString(){ 134 return mode; 135 } 136 }//class State 137 138 } 139