1 package com.aelitis.azureus.core.speedmanager.impl.v2;
2 
3 import org.gudy.azureus2.core3.util.SystemTime;
4 import com.aelitis.azureus.core.speedmanager.SpeedManagerLimitEstimate;
5 import com.aelitis.azureus.core.speedmanager.SpeedManagerPingMapper;
6 import com.aelitis.azureus.core.speedmanager.SpeedManager;
7 import com.aelitis.azureus.core.speedmanager.impl.SpeedManagerAlgorithmProviderAdapter;
8 
9 import java.util.List;
10 import java.util.ArrayList;
11 
12 /**
13  * Created on Jul 16, 2007
14  * Created by Alan Snyder
15  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
16  * <p/>
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * as published by the Free Software Foundation; either version 2
20  * of the License, or (at your option) any later version.
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28  */
29 
30 
31 public class PingSpaceMon
32 {
33 
34     private static final long INTERVAL = 1000 * 60 * 15L;
35 
36 
37     long nextCheck = System.currentTimeMillis() + INTERVAL;
38 
39     TransferMode mode;
40 
41 
42     List listeners = new ArrayList();//List<PSMonitorListener>
43 
44 
addListener(PSMonitorListener listener)45     public void addListener(PSMonitorListener listener){
46 
47         //don't register the same listener twice.
48         for(int i=0; i<listeners.size(); i++){
49             PSMonitorListener t = (PSMonitorListener) listeners.get(i);
50             if( t==listener ){
51                 SpeedManagerLogger.trace("Not logging same listener twice. listener="+listener.toString());
52                 return;
53             }
54         }
55 
56         listeners.add( listener );
57     }
58 
removeListener(PSMonitorListener listener)59     public boolean removeListener(PSMonitorListener listener){
60 
61         return listeners.remove(listener);
62 
63     }
64 
65 
checkForLowerLimits()66     boolean checkForLowerLimits(){
67 
68         long curr = SystemTime.getCurrentTime();
69         if( curr > nextCheck ){
70             SpeedManagerLogger.trace("PingSpaceMon checking for lower limits.");
71 
72             for(int i=0; i<listeners.size(); i++ ){
73                 PSMonitorListener l =(PSMonitorListener) listeners.get(i);
74 
75                 if(l!=null){
76                     l.notifyUpload( getUploadEstCapacity() );
77                 }else{
78                     SpeedManagerLogger.trace("listener index _"+i+"_ was null.");
79                 }
80             }
81 
82             resetTimer();
83             return true;
84         }
85         return false;
86     }
87 
88     /**
89      *
90      * @param tMode -
91      * @return - true if is has a new mode, and the clock starts over.
92      */
updateStatus(TransferMode tMode)93     boolean updateStatus(TransferMode tMode){
94 
95         if(mode==null){
96             mode=tMode;
97             return true;
98         }
99 
100         if( mode.getMode() != tMode.getMode() ){
101             mode = tMode;
102             resetTimer();
103             return true;
104         }
105         return checkForLowerLimits();
106     }//updateStatus
107 
resetTimer()108     void resetTimer(){
109         long curr = SystemTime.getCurrentTime();
110         nextCheck = curr + INTERVAL;
111         SpeedManagerLogger.trace("Monitor resetting time. Next check in interval.");
112     }
113 
114     /**
115      * Get the current estimated upload limit from the ping mapper.
116      * @param - true if the long-term persistent result should be used.
117      * @return - SpeedManagerLimitEstimate.
118      */
getUploadLimit(boolean persistent)119     public static SpeedManagerLimitEstimate getUploadLimit(boolean persistent){
120         try{
121             SMInstance pm = SMInstance.getInstance();
122             SpeedManagerAlgorithmProviderAdapter adapter = pm.getAdapter();
123             SpeedManagerPingMapper persistentMap = adapter.getPingMapper();
124             SpeedManagerLimitEstimate upEst = persistentMap.getEstimatedUploadLimit(true);
125 
126             return upEst;
127 
128         }catch(Throwable t){
129             //log this event and
130             SpeedManagerLogger.log( t.toString() );
131             t.printStackTrace();
132 
133             //something to return 1 and -1.0f results.
134             return new DefaultLimitEstimate();
135         }
136     }//getUploadLimit
137 
getUploadEstCapacity()138     public static SpeedManagerLimitEstimate getUploadEstCapacity()
139     {
140         try{
141             SMInstance pm = SMInstance.getInstance();
142             SpeedManagerAlgorithmProviderAdapter adapter = pm.getAdapter();
143             SpeedManager sm = adapter.getSpeedManager();
144             SpeedManagerLimitEstimate upEstCapacity = sm.getEstimatedUploadCapacityBytesPerSec();
145 
146             return upEstCapacity;
147 
148         }catch(Throwable t){
149             //log this event and
150             SpeedManagerLogger.log( t.toString() );
151             t.printStackTrace();
152 
153             //something to return 1 and -1.0f results.
154             return new DefaultLimitEstimate();
155         }
156     }
157 
158     /**
159      * Get the current estimated download limit from the ping mapper.
160      * @return - SpeedManagerLimitEstimate
161      */
getDownloadLimit()162     public static SpeedManagerLimitEstimate getDownloadLimit(){
163         try{
164             SMInstance pm = SMInstance.getInstance();
165             SpeedManagerAlgorithmProviderAdapter adapter = pm.getAdapter();
166             SpeedManagerPingMapper persistentMap = adapter.getPingMapper();
167             SpeedManagerLimitEstimate downEst = persistentMap.getEstimatedDownloadLimit(true);
168 
169             return downEst;
170 
171         }catch(Throwable t){
172             //log this event and
173             SpeedManagerLogger.log( t.toString() );
174             t.printStackTrace();
175 
176             //something to return 0 and -1.0f results.
177             return new DefaultLimitEstimate();
178         }
179     }//getDownloadLimit
180 
181     /**
182      * Get the estimated download capacity from the SpeedManager.
183      * @return - SpeedManagerLimitEstimate
184      */
getDownloadEstCapacity()185     public static SpeedManagerLimitEstimate getDownloadEstCapacity()
186     {
187         try{
188             SMInstance pm = SMInstance.getInstance();
189             SpeedManagerAlgorithmProviderAdapter adapter = pm.getAdapter();
190             SpeedManager sm = adapter.getSpeedManager();
191             SpeedManagerLimitEstimate downEstCapacity = sm.getEstimatedDownloadCapacityBytesPerSec();
192 
193             return downEstCapacity;
194 
195         }catch(Throwable t){
196             //log this event and
197             SpeedManagerLogger.log( t.toString() );
198             t.printStackTrace();
199 
200             //something to return 0 and -1.0f results.
201             return new DefaultLimitEstimate();
202         }
203     }
204 
205     static class DefaultLimitEstimate implements SpeedManagerLimitEstimate
206     {
207 
getBytesPerSec()208         public int getBytesPerSec() {
209             return 1;
210         }
211 
getEstimateType()212         public float getEstimateType() {
213         	return -1.0f;
214         }
getMetricRating()215         public float getMetricRating() {
216             return -1.0f;
217         }
218 
getSegments()219         public int[][] getSegments() {
220             return new int[0][];
221         }
222 
getWhen()223         public long getWhen(){ return(0);}
getString()224         public String getString() {
225             return "default";
226         }
227     }//class
228 
229 }
230