1 18 package org.drftpd.slaveselection.def; 19 20 import java.io.FileInputStream ; 21 import java.io.FileNotFoundException ; 22 import java.io.IOException ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.Properties ; 26 27 import net.sf.drftpd.Bytes; 28 import net.sf.drftpd.NoAvailableSlaveException; 29 import net.sf.drftpd.SlaveUnavailableException; 30 import net.sf.drftpd.master.BaseFtpConnection; 31 import net.sf.drftpd.master.RemoteSlave; 32 import net.sf.drftpd.master.SlaveManagerImpl; 33 import net.sf.drftpd.master.config.FtpConfig; 34 import net.sf.drftpd.mirroring.Job; 35 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 36 import net.sf.drftpd.slave.SlaveStatus; 37 import net.sf.drftpd.slave.Transfer; 38 39 import org.drftpd.slaveselection.SlaveSelectionManagerInterface; 40 41 45 public class SlaveSelectionManager implements SlaveSelectionManagerInterface { 46 47 private long _minfreespace; 48 private int _maxTransfers; 49 private long _maxBandwidth; 50 51 private SlaveManagerImpl _sm; 52 53 public SlaveSelectionManager(SlaveManagerImpl sm) 54 throws FileNotFoundException , IOException { 55 super(); 56 _sm = sm; 57 58 59 } 60 61 public void reload() throws FileNotFoundException , IOException { 62 Properties p = new Properties (); 63 p.load(new FileInputStream ("conf/slaveselection-old.conf")); 64 _minfreespace = Bytes.parseBytes(FtpConfig.getProperty(p, "minfreespace")); 65 try { 66 if (_sm.getConnectionManager().getJobManager() != null ) { 67 _maxTransfers = Integer.parseInt(FtpConfig.getProperty(p, 68 "maxTransfers")); 69 _maxBandwidth = Bytes.parseBytes(FtpConfig.getProperty(p, 70 "maxBandwidth")); 71 } 72 } catch (IllegalStateException e) { 73 } 75 } 76 77 public RemoteSlave getASlave( 78 Collection rslaves, 79 char direction, 80 BaseFtpConnection conn, 81 LinkedRemoteFileInterface file) 82 throws NoAvailableSlaveException { 83 return getASlaveInternal(rslaves, direction); 84 } 85 86 public RemoteSlave getASlaveForMaster(LinkedRemoteFileInterface file, FtpConfig cfg) 87 throws NoAvailableSlaveException { 88 return getASlaveInternal(file.getAvailableSlaves(), Transfer.TRANSFER_SENDING_DOWNLOAD); 89 } 90 91 public SlaveManagerImpl getSlaveManager() { 92 return _sm; 93 } 94 95 public RemoteSlave getASlaveForJobDownload(Job job) 96 throws NoAvailableSlaveException { 97 return getASlaveInternal(job.getFile().getAvailableSlaves(), Transfer.TRANSFER_SENDING_DOWNLOAD); 98 } 99 100 private RemoteSlave getASlaveInternal( 101 Collection slaves, 102 char direction) 103 throws NoAvailableSlaveException { 104 RemoteSlave bestslave; 105 SlaveStatus beststatus; 106 { 107 Iterator i = slaves.iterator(); 108 int bestthroughput; 109 110 while (true) { 111 if (!i.hasNext()) 112 throw new NoAvailableSlaveException(); 113 bestslave = (RemoteSlave) i.next(); 114 try { 115 beststatus = bestslave.getStatus(); 116 } catch (SlaveUnavailableException ex) { 118 continue; 119 } 120 bestthroughput = beststatus.getThroughputDirection(direction); 121 break; 122 } 123 while (i.hasNext()) { 124 RemoteSlave slave = (RemoteSlave) i.next(); 125 SlaveStatus status; 126 127 try { 128 status = slave.getStatus(); 129 } catch (SlaveUnavailableException ex) { 130 continue; 131 } 132 133 int throughput = status.getThroughputDirection(direction); 134 135 if (beststatus.getDiskSpaceAvailable() 136 < _minfreespace 137 && beststatus.getDiskSpaceAvailable() 138 < status.getDiskSpaceAvailable()) { 139 bestslave = slave; 142 bestthroughput = throughput; 143 beststatus = status; 144 continue; 145 } 146 147 if (status.getDiskSpaceAvailable() 148 < _minfreespace) { 149 continue; 152 } 153 154 if (throughput == bestthroughput) { 155 if (direction == Transfer.TRANSFER_RECEIVING_UPLOAD) { 156 if (bestslave.getLastUploadReceiving() 157 > slave.getLastUploadReceiving()) { 158 bestslave = slave; 159 bestthroughput = throughput; 160 beststatus = status; 161 } 162 } else if ( 163 direction == Transfer.TRANSFER_SENDING_DOWNLOAD) { 164 if (bestslave.getLastDownloadSending() 165 > slave.getLastDownloadSending()) { 166 bestslave = slave; 167 bestthroughput = throughput; 168 beststatus = status; 169 } 170 } else if (direction == Transfer.TRANSFER_THROUGHPUT) { 171 if (bestslave.getLastTransfer() 172 > slave.getLastTransfer()) { 173 bestslave = slave; 174 bestthroughput = throughput; 175 beststatus = status; 176 } 177 } 178 } 179 if (throughput < bestthroughput) { 180 bestslave = slave; 181 bestthroughput = throughput; 182 beststatus = status; 183 } 184 } 185 } 186 if (direction == Transfer.TRANSFER_RECEIVING_UPLOAD) { 187 bestslave.setLastUploadReceiving(System.currentTimeMillis()); 188 } else if (direction == Transfer.TRANSFER_SENDING_DOWNLOAD) { 189 bestslave.setLastDownloadSending(System.currentTimeMillis()); 190 } else { 191 bestslave.setLastUploadReceiving(System.currentTimeMillis()); 192 bestslave.setLastDownloadSending(System.currentTimeMillis()); 193 } 194 return bestslave; 195 } 196 197 198 public RemoteSlave getASlaveForJobUpload(Job job) 199 throws NoAvailableSlaveException { 200 Collection slaves = _sm.getAvailableSlaves(); 201 slaves.removeAll(job.getFile().getAvailableSlaves()); 202 return getASlaveForJob(slaves,Transfer.TRANSFER_RECEIVING_UPLOAD); 203 } 204 205 public RemoteSlave getASlaveForJob(Collection slaves, char direction) throws NoAvailableSlaveException { 206 RemoteSlave rslave = this.getASlaveInternal(slaves,direction); 207 SlaveStatus status = null; 208 try { 209 status = rslave.getStatus(); 210 } catch (SlaveUnavailableException e) { 211 throw new NoAvailableSlaveException(); 212 } 213 if (status.getThroughputDirection(direction) > _maxBandwidth ) { 214 throw new NoAvailableSlaveException(); 215 } 216 if (direction == Transfer.TRANSFER_RECEIVING_UPLOAD) 217 if (status.getTransfersReceiving() > _maxTransfers ) { 218 throw new NoAvailableSlaveException(); 219 } 220 if (direction == Transfer.TRANSFER_SENDING_DOWNLOAD) 221 if (status.getTransfersSending() > _maxTransfers ) { 222 throw new NoAvailableSlaveException(); 223 } 224 return rslave; 225 } 226 227 } 228 | Popular Tags |