KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import net.sf.drftpd.NoAvailableSlaveException;
26 import net.sf.drftpd.master.usermanager.User;
27 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
28
29 /**
30  * Checks ScoreChart for slaves with 0 bw usage and assigns 1 extra point to the one in that has been unused for the longest time.
31  *
32  * @author mog, zubov
33  * @version $Id: CycleFilter.java,v 1.4 2004/03/21 08:06:38 zubov Exp $
34  */

35 public class CycleFilter extends Filter {
36
37     public CycleFilter(FilterChain fc, int i, Properties JavaDoc p) {
38     }
39
40     public void process(
41         ScoreChart scorechart,
42         User user,
43         InetAddress JavaDoc peer,
44         char direction,
45         LinkedRemoteFileInterface dir)
46         throws NoAvailableSlaveException {
47
48         ArrayList JavaDoc tempList = new ArrayList JavaDoc(scorechart.getSlaveScores());
49         while (true) {
50             if (tempList.isEmpty())
51                 return;
52             ScoreChart.SlaveScore first =
53                 (ScoreChart.SlaveScore) tempList.get(0);
54             ArrayList JavaDoc equalList = new ArrayList JavaDoc();
55             equalList.add(first);
56             tempList.remove(first);
57             for (Iterator JavaDoc iter = tempList.iterator(); iter.hasNext();) {
58                 ScoreChart.SlaveScore match =
59                     (ScoreChart.SlaveScore) iter.next();
60                 if (match.compareTo(first) == 0) {
61                     equalList.add(match);
62                     iter.remove();
63                 }
64             }
65             ScoreChart.SlaveScore leastUsed = first;
66             for (Iterator JavaDoc iter = equalList.iterator(); iter.hasNext();) {
67                 ScoreChart.SlaveScore match =
68                     (ScoreChart.SlaveScore) iter.next();
69                 if (match.getRSlave().getLastTransferForDirection(direction)
70                     < leastUsed.getRSlave().getLastTransferForDirection(
71                         direction)) {
72                     leastUsed = match;
73                 }
74             }
75             if (leastUsed != null) {
76                 leastUsed.addScore(1);
77             }
78         }
79     }
80 }
81
Popular Tags