KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import net.sf.drftpd.NoAvailableSlaveException;
29 import net.sf.drftpd.ObjectNotFoundException;
30 import net.sf.drftpd.master.RemoteSlave;
31 import net.sf.drftpd.master.config.FtpConfig;
32 import net.sf.drftpd.master.usermanager.User;
33 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
34
35 import org.drftpd.remotefile.LinkedRemoteFileUtils;
36 import org.drftpd.sections.SectionInterface;
37 import org.drftpd.slaveselection.filter.ScoreChart.SlaveScore;
38
39 /**
40  * @author mog
41  * @version $Id: SlavetopFilter.java,v 1.8 2004/05/16 05:44:55 zubov Exp $
42  */

43 public class SlavetopFilter extends Filter {
44
45     private long _assign;
46
47     private FilterChain _fc;
48
49     private int _topslaves;
50
51     public SlavetopFilter(FilterChain fc, int i, Properties JavaDoc p) {
52         _fc = fc;
53         _topslaves =
54             Integer.parseInt(FtpConfig.getProperty(p, i + ".topslaves"));
55         _assign = Long.parseLong(FtpConfig.getProperty(p, i + ".assign"));
56     }
57
58     public void process(
59         ScoreChart scorechart,
60         User user,
61         InetAddress JavaDoc peer,
62         char direction,
63         LinkedRemoteFileInterface dir)
64         throws NoAvailableSlaveException {
65
66         String JavaDoc path = dir.getPath();
67
68         //// find the section part of the path name
69
SectionInterface section =
70             _fc
71                 .getSlaveManager()
72                 .getConnectionManager()
73                 .getSectionManager()
74                 .lookup(
75                 path);
76         
77         LinkedRemoteFileInterface rls = section.getFirstDirInSection(dir);
78 // // string stuff
79
// if (section.getPath().endsWith("/")) // section is not the root dir - /
80
// path = path.substring(section.getPath().length());
81
// else path = path.substring(section.getPath().length()+1);
82
// int pos = path.indexOf('/');
83
// if (pos != -1)
84
// path = path.substring(0, pos);
85
// LinkedRemoteFileInterface rls;
86
// try {
87
// rls = section.getFile().getFile(path);
88
// } catch (FileNotFoundException e) {
89
// throw new RuntimeException(e);
90
// }
91

92
93         Hashtable JavaDoc slavesmap = new Hashtable JavaDoc();
94         for (Iterator JavaDoc iter =
95             scorechart.getSlaveScores().iterator();
96             iter.hasNext();
97             ) {
98             RemoteSlave rslave = ((ScoreChart.SlaveScore) iter.next()).getRSlave();
99             slavesmap.put(rslave, new ScoreChart.SlaveScore(rslave));
100         }
101
102         Collection JavaDoc files = LinkedRemoteFileUtils.getAllFiles(rls);
103         for (Iterator JavaDoc iter = files.iterator(); iter.hasNext();) {
104             LinkedRemoteFileInterface file =
105                 (LinkedRemoteFileInterface) iter.next();
106             for (Iterator JavaDoc iterator = file.getSlaves().iterator();
107                 iterator.hasNext();
108                 ) {
109                 RemoteSlave rslave = (RemoteSlave) iterator.next();
110                 ScoreChart.SlaveScore score =
111                     (SlaveScore) slavesmap.get(rslave);
112                 if (score == null)
113                     continue;
114                 score.addScore(1);
115             }
116         }
117
118         ArrayList JavaDoc slavescores = new ArrayList JavaDoc(slavesmap.values());
119         Collections.sort(slavescores, Collections.reverseOrder());
120         Iterator JavaDoc iter = slavescores.iterator();
121         for (int i = 0; i < _topslaves && iter.hasNext(); i++) {
122             ScoreChart.SlaveScore score = (SlaveScore) iter.next();
123             try {
124                 scorechart.getSlaveScore(score.getRSlave()).addScore(_assign);
125             } catch (ObjectNotFoundException e1) {
126                 throw new RuntimeException JavaDoc(e1);
127             }
128         }
129     }
130 }
131
Popular Tags