1 18 package org.drftpd.slaveselection.filter; 19 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.net.InetAddress ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 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 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 , IOException { 53 _sm = sm; 54 reload(); 55 } 56 57 60 public RemoteSlave getASlave( 61 Collection rslaves, 62 char direction, 63 BaseFtpConnection conn, 64 LinkedRemoteFileInterface file) 65 throws NoAvailableSlaveException { 66 InetAddress source = (conn != null ? conn.getClientAddress() : null); 67 String 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 (); 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 slaves = new ArrayList (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 slaves = new ArrayList (job.getDestinationSlaves()); 102 for (Iterator 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 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 filterchain, 140 ScoreChart sc, 141 User user, 142 InetAddress 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 (); 159 } 160 return ssmi.getBestSlave(sc, user, peer, direction, file); 161 } 162 163 public void reload() throws FileNotFoundException , IOException { 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 |