1 18 package org.drftpd.slaveselection.filter; 19 20 import java.net.InetAddress ; 21 import java.rmi.RemoteException ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.Properties ; 25 26 import net.sf.drftpd.master.config.FtpConfig; 27 import net.sf.drftpd.master.usermanager.User; 28 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 29 import net.sf.drftpd.slave.SlaveStatus; 30 31 import org.apache.log4j.Logger; 32 33 38 public class BandwidthFilter extends Filter { 39 private static final Logger logger = 40 Logger.getLogger(BandwidthFilter.class); 41 42 protected float _multiplier; 43 44 public BandwidthFilter(FilterChain ssm, int i, Properties p) { 45 setMultiplier(FtpConfig.getProperty(p, i + ".multiplier")); 46 } 47 48 protected void setMultiplier(String s) { 49 _multiplier = parseMultiplier(s); 50 } 51 protected static float parseMultiplier(String string) { 52 if (string.equalsIgnoreCase("remove")) 53 return 0; 54 boolean isMultiplier; 55 float multiplier = 1; 56 while (string.length() != 0) { 57 char c = string.charAt(0); 58 if (c == '*') { 59 isMultiplier = true; 60 string = string.substring(1); 61 } else if (c == '/') { 62 isMultiplier = false; 63 string = string.substring(1); 64 } else { 65 isMultiplier = true; 66 } 67 68 int pos = string.indexOf('*'); 69 if (pos == -1) 70 pos = string.length(); 71 int tmp = string.indexOf('/'); 72 if (tmp != -1 && tmp < pos) 73 pos = tmp; 74 if (isMultiplier) { 75 multiplier *= Float.parseFloat(string.substring(0, pos)); 76 } else { 77 multiplier /= Float.parseFloat(string.substring(0, pos)); 78 } 79 string = string.substring(pos); 80 } 81 return multiplier; 82 } 83 84 public void process( 85 ScoreChart scorechart, 86 User user, 87 InetAddress source, 88 char direction, 89 LinkedRemoteFileInterface file) { 90 Collection slavescores = scorechart.getSlaveScores(); 91 for (Iterator iter = slavescores.iterator(); iter.hasNext();) { 92 ScoreChart.SlaveScore score = (ScoreChart.SlaveScore) iter.next(); 93 SlaveStatus status; 94 try { 95 status = score.getRSlave().getStatus(); 96 } catch (Exception e) { 97 if (e instanceof RemoteException ) { 98 score.getRSlave().handleRemoteException( 99 (RemoteException ) e); 100 } 101 iter.remove(); 102 continue; 103 } 104 score.addScore( 105 - (long) 106 (status.getThroughputDirection(direction)*_multiplier)); 107 } 108 } 109 110 } 111 | Popular Tags |