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 net.sf.drftpd.NoAvailableSlaveException;
021: import net.sf.drftpd.master.BaseFtpConnection;
022: import net.sf.drftpd.master.config.ConfigInterface;
023: import net.sf.drftpd.master.config.FtpConfig;
024: import net.sf.drftpd.mirroring.Job;
025:
026: import org.drftpd.GlobalContext;
027:
028: import org.drftpd.master.RemoteSlave;
029: import org.drftpd.master.SlaveManager;
030:
031: import org.drftpd.remotefile.LinkedRemoteFileInterface;
032: import org.drftpd.slave.Transfer;
033: import org.drftpd.slaveselection.SlaveSelectionManagerInterface;
034:
035: import org.drftpd.usermanager.User;
036:
037: import java.io.FileNotFoundException;
038: import java.io.IOException;
039:
040: import java.net.InetAddress;
041:
042: import java.util.ArrayList;
043: import java.util.Collection;
044: import java.util.Iterator;
045:
046: /**
047: * @author mog
048: * @version $Id: SlaveSelectionManager.java 930 2005-01-30 15:16:42Z zubov $
049: */
050: public class SlaveSelectionManager implements
051: SlaveSelectionManagerInterface {
052: private GlobalContext _gctx;
053: private FilterChain _ssmiDown;
054: private FilterChain _ssmiJobDown;
055: private FilterChain _ssmiJobUp;
056: private FilterChain _ssmiUp;
057:
058: public SlaveSelectionManager(GlobalContext gctx)
059: throws FileNotFoundException, IOException {
060: _gctx = gctx;
061: reload();
062: }
063:
064: /**
065: * Checksums call us with null BaseFtpConnection.
066: */
067: public RemoteSlave getASlave(Collection<RemoteSlave> rslaves,
068: char direction, BaseFtpConnection conn,
069: LinkedRemoteFileInterface file)
070: throws NoAvailableSlaveException {
071: InetAddress source = ((conn != null) ? conn.getClientAddress()
072: : null);
073: String status;
074:
075: if (direction == Transfer.TRANSFER_RECEIVING_UPLOAD) {
076: status = "up";
077: } else if (direction == Transfer.TRANSFER_SENDING_DOWNLOAD) {
078: status = "down";
079: } else {
080: throw new IllegalArgumentException();
081: }
082:
083: return process(status, new ScoreChart(rslaves),
084: (conn != null) ? conn.getUserNull() : null, source,
085: direction, file, null);
086: }
087:
088: public RemoteSlave getASlaveForJobDownload(Job job)
089: throws NoAvailableSlaveException {
090: ArrayList<RemoteSlave> slaves = new ArrayList<RemoteSlave>(job
091: .getFile().getAvailableSlaves());
092: slaves.removeAll(job.getDestinationSlaves());
093:
094: if (slaves.isEmpty()) {
095: throw new NoAvailableSlaveException();
096: }
097:
098: return process("jobdown", new ScoreChart(slaves), null, null,
099: Transfer.TRANSFER_SENDING_DOWNLOAD, job.getFile(), null);
100: }
101:
102: public RemoteSlave getASlaveForJobUpload(Job job,
103: RemoteSlave sourceSlave) throws NoAvailableSlaveException {
104: ArrayList<RemoteSlave> slaves = new ArrayList<RemoteSlave>(job
105: .getDestinationSlaves());
106: slaves.removeAll(job.getFile().getAvailableSlaves());
107:
108: for (Iterator iter = slaves.iterator(); iter.hasNext();) {
109: if (!((RemoteSlave) iter.next()).isAvailable()) {
110: iter.remove();
111: }
112: }
113:
114: if (slaves.isEmpty()) {
115: throw new NoAvailableSlaveException();
116: }
117:
118: return process("jobup", new ScoreChart(slaves), null, null,
119: Transfer.TRANSFER_SENDING_DOWNLOAD, job.getFile(),
120: sourceSlave);
121: }
122:
123: public SlaveManager getSlaveManager() {
124: return getGlobalContext().getSlaveManager();
125: }
126:
127: private RemoteSlave process(String filterchain, ScoreChart sc,
128: User user, InetAddress peer, char direction,
129: LinkedRemoteFileInterface file, RemoteSlave sourceSlave)
130: throws NoAvailableSlaveException {
131: FilterChain ssmi;
132:
133: if (filterchain.equals("down")) {
134: ssmi = _ssmiDown;
135: } else if (filterchain.equals("up")) {
136: ssmi = _ssmiUp;
137: } else if (filterchain.equals("jobup")) {
138: ssmi = _ssmiJobUp;
139: } else if (filterchain.equals("jobdown")) {
140: ssmi = _ssmiJobDown;
141: } else {
142: throw new IllegalArgumentException();
143: }
144:
145: return ssmi.getBestSlave(sc, user, peer, direction, file,
146: sourceSlave);
147: }
148:
149: public void reload() throws FileNotFoundException, IOException {
150: _ssmiDown = new FilterChain(this ,
151: "conf/slaveselection-down.conf");
152: _ssmiUp = new FilterChain(this , "conf/slaveselection-up.conf");
153: _ssmiJobUp = new FilterChain(this ,
154: "conf/slaveselection-jobup.conf");
155: _ssmiJobDown = new FilterChain(this ,
156: "conf/slaveselection-jobdown.conf");
157: }
158:
159: public GlobalContext getGlobalContext() {
160: return _gctx;
161: }
162: }
|