KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import net.sf.drftpd.NoAvailableSlaveException;
28 import net.sf.drftpd.master.BaseFtpConnection;
29 import net.sf.drftpd.master.RemoteSlave;
30 import net.sf.drftpd.master.SlaveManagerImpl;
31 import net.sf.drftpd.master.config.FtpConfig;
32 import net.sf.drftpd.master.usermanager.User;
33 import net.sf.drftpd.mirroring.Job;
34 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
35 import net.sf.drftpd.slave.Transfer;
36
37 import org.drftpd.slaveselection.SlaveSelectionManagerInterface;
38
39 /**
40  * @author mog
41  * @version $Id: SlaveSelectionManager.java,v 1.13.2.1 2004/06/29 03:51:50 zubov Exp $
42  */

43 public class SlaveSelectionManager implements SlaveSelectionManagerInterface {
44     private SlaveManagerImpl _sm;
45     private FilterChain _ssmiDown;
46     private FilterChain _ssmiJobDown;
47     private FilterChain _ssmiJobUp;
48     private FilterChain _ssmiMaster;
49     private FilterChain _ssmiUp;
50
51     public SlaveSelectionManager(SlaveManagerImpl sm)
52         throws FileNotFoundException JavaDoc, IOException JavaDoc {
53         _sm = sm;
54         reload();
55     }
56
57     /**
58      * Checksums call us with null BaseFtpConnection.
59      */

60     public RemoteSlave getASlave(
61         Collection JavaDoc rslaves,
62         char direction,
63         BaseFtpConnection conn,
64         LinkedRemoteFileInterface file)
65         throws NoAvailableSlaveException {
66         InetAddress JavaDoc source = (conn != null ? conn.getClientAddress() : null);
67         String JavaDoc status;
68         if (direction == Transfer.TRANSFER_RECEIVING_UPLOAD) {
69             status = "up";
70         } else if (direction == Transfer.TRANSFER_SENDING_DOWNLOAD) {
71             status = "down";
72         } else {
73             throw new IllegalArgumentException JavaDoc();
74         }
75         return process(
76             status,
77             new ScoreChart(rslaves),
78             conn != null ? conn.getUserNull() : null,
79             source,
80             direction,
81             file);
82     }
83
84     public RemoteSlave getASlaveForJobDownload(Job job)
85         throws NoAvailableSlaveException {
86         ArrayList JavaDoc slaves = new ArrayList JavaDoc(job.getFile().getAvailableSlaves());
87         slaves.removeAll(job.getDestinationSlaves());
88         if (slaves.isEmpty())
89             throw new NoAvailableSlaveException();
90         return process(
91             "jobdown",
92             new ScoreChart(slaves),
93             null,
94             null,
95             Transfer.TRANSFER_SENDING_DOWNLOAD,
96             job.getFile());
97     }
98
99     public RemoteSlave getASlaveForJobUpload(Job job)
100         throws NoAvailableSlaveException {
101         ArrayList JavaDoc slaves = new ArrayList JavaDoc(job.getDestinationSlaves());
102         for (Iterator JavaDoc iter = slaves.iterator(); iter.hasNext();) {
103             RemoteSlave rslave = (RemoteSlave) iter.next();
104             if (!rslave.isAvailable())
105                 iter.remove();
106         }
107         if (slaves.isEmpty())
108             throw new NoAvailableSlaveException();
109         return process(
110             "jobup",
111             new ScoreChart(slaves),
112             null,
113             null,
114             Transfer.TRANSFER_SENDING_DOWNLOAD,
115             job.getFile());
116     }
117
118     /**
119      * Get slave for transfer to master.
120      */

121     public RemoteSlave getASlaveForMaster(
122         LinkedRemoteFileInterface file,
123         FtpConfig cfg)
124         throws NoAvailableSlaveException {
125         return process(
126             "master",
127             new ScoreChart(file.getAvailableSlaves()),
128             null,
129             null,
130             Transfer.TRANSFER_SENDING_DOWNLOAD,
131             file);
132     }
133
134     public SlaveManagerImpl getSlaveManager() {
135         return _sm;
136     }
137
138     private RemoteSlave process(
139         String JavaDoc filterchain,
140         ScoreChart sc,
141         User user,
142         InetAddress JavaDoc peer,
143         char direction,
144         LinkedRemoteFileInterface file)
145         throws NoAvailableSlaveException {
146         FilterChain ssmi;
147         if (filterchain.equals("down")) {
148             ssmi = _ssmiDown;
149         } else if (filterchain.equals("up")) {
150             ssmi = _ssmiUp;
151         } else if (filterchain.equals("master")) {
152             ssmi = _ssmiMaster;
153         } else if (filterchain.equals("jobup")) {
154             ssmi = _ssmiJobUp;
155         } else if (filterchain.equals("jobdown")) {
156             ssmi = _ssmiJobDown;
157         } else {
158             throw new IllegalArgumentException JavaDoc();
159         }
160         return ssmi.getBestSlave(sc, user, peer, direction, file);
161     }
162
163     public void reload() throws FileNotFoundException JavaDoc, IOException JavaDoc {
164         _ssmiDown = new FilterChain(this, "conf/slaveselection-down.conf");
165         _ssmiMaster = new FilterChain(this, "conf/slaveselection-master.conf");
166         _ssmiUp = new FilterChain(this, "conf/slaveselection-up.conf");
167         if (_sm.getConnectionManager().isJobManagerLoaded()) {
168             _ssmiJobUp =
169                 new FilterChain(this, "conf/slaveselection-jobup.conf");
170             _ssmiJobDown =
171                 new FilterChain(this, "conf/slaveselection-jobdown.conf");
172         } else {
173             _ssmiJobUp = null;
174             _ssmiJobDown = null;
175         }
176     }
177 }
178
Popular Tags