KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > slaveselection > filter > BandwidthFilter


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.drftpd.slaveselection.filter;
19
20 import java.net.InetAddress JavaDoc;
21 import java.rmi.RemoteException JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Properties JavaDoc;
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 /**
34  * Removes bandwidth * multiplier from the score.
35  * @author mog
36  * @version $Id: BandwidthFilter.java,v 1.4 2004/05/17 11:27:26 mog Exp $
37  */

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 JavaDoc p) {
45         setMultiplier(FtpConfig.getProperty(p, i + ".multiplier"));
46     }
47
48     protected void setMultiplier(String JavaDoc s) {
49         _multiplier = parseMultiplier(s);
50     }
51     protected static float parseMultiplier(String JavaDoc 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 JavaDoc source,
88         char direction,
89         LinkedRemoteFileInterface file) {
90         Collection JavaDoc slavescores = scorechart.getSlaveScores();
91         for (Iterator JavaDoc 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 JavaDoc e) {
97                 if (e instanceof RemoteException JavaDoc) {
98                     score.getRSlave().handleRemoteException(
99                         (RemoteException JavaDoc) 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