001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.slaveselection.filter;
019:
020: import org.apache.log4j.Logger;
021:
022: import org.drftpd.PropertyHelper;
023: import org.drftpd.master.RemoteSlave;
024: import org.drftpd.remotefile.LinkedRemoteFileInterface;
025: import org.drftpd.slave.SlaveStatus;
026: import org.drftpd.usermanager.User;
027:
028: import java.net.InetAddress;
029:
030: import java.util.Collection;
031: import java.util.Iterator;
032: import java.util.Properties;
033:
034: /**
035: * Removes bandwidth * multiplier from the score.
036: * @author mog
037: * @version $Id: BandwidthFilter.java 936 2005-01-31 22:25:52Z mog $
038: */
039: public class BandwidthFilter extends Filter {
040: private static final Logger logger = Logger
041: .getLogger(BandwidthFilter.class);
042: protected float _multiplier;
043:
044: public BandwidthFilter(FilterChain ssm, int i, Properties p) {
045: setMultiplier(parseMultiplier(PropertyHelper.getProperty(p, i
046: + ".multiplier")));
047: }
048:
049: /**
050: * @param multiplier
051: */
052: public BandwidthFilter(float multiplier) {
053: setMultiplier(multiplier);
054: }
055:
056: protected void setMultiplier(float multiplier) {
057: _multiplier = multiplier;
058: }
059:
060: protected static float parseMultiplier(String string) {
061: if (string.equalsIgnoreCase("remove")) {
062: return 0;
063: }
064:
065: boolean isMultiplier;
066: float multiplier = 1;
067:
068: while (string.length() != 0) {
069: char c = string.charAt(0);
070:
071: if (c == '*') {
072: isMultiplier = true;
073: string = string.substring(1);
074: } else if (c == '/') {
075: isMultiplier = false;
076: string = string.substring(1);
077: } else {
078: isMultiplier = true;
079: }
080:
081: int pos = string.indexOf('*');
082:
083: if (pos == -1) {
084: pos = string.length();
085: }
086:
087: int tmp = string.indexOf('/');
088:
089: if ((tmp != -1) && (tmp < pos)) {
090: pos = tmp;
091: }
092:
093: if (isMultiplier) {
094: multiplier *= Float
095: .parseFloat(string.substring(0, pos));
096: } else {
097: multiplier /= Float
098: .parseFloat(string.substring(0, pos));
099: }
100:
101: string = string.substring(pos);
102: }
103:
104: return multiplier;
105: }
106:
107: public void process(ScoreChart scorechart, User user,
108: InetAddress source, char direction,
109: LinkedRemoteFileInterface file, RemoteSlave sourceSlave) {
110: Collection slavescores = scorechart.getSlaveScores();
111:
112: for (Iterator iter = slavescores.iterator(); iter.hasNext();) {
113: ScoreChart.SlaveScore score = (ScoreChart.SlaveScore) iter
114: .next();
115: SlaveStatus status;
116:
117: try {
118: status = score.getRSlave().getSlaveStatusAvailable();
119: } catch (Exception e) {
120: iter.remove();
121:
122: continue;
123: }
124:
125: score
126: .addScore(-(long) (status
127: .getThroughputDirection(direction) * getMultiplier(score
128: .getRSlave())));
129: }
130: }
131:
132: protected float getMultiplier(RemoteSlave slave) {
133: return _multiplier;
134: }
135: }
|