KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > slaveselection > def > 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.def;
19
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Properties JavaDoc;
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 /**
42  * @author mog
43  * @version $Id: SlaveSelectionManager.java,v 1.8 2004/03/06 00:39:47 zubov Exp $
44  */

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 JavaDoc, IOException JavaDoc {
55         super();
56         _sm = sm;
57
58         
59     }
60
61     public void reload() throws FileNotFoundException JavaDoc, IOException JavaDoc {
62         Properties JavaDoc p = new Properties JavaDoc();
63         p.load(new FileInputStream JavaDoc("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 JavaDoc e) {
73             // jobmanager isn't loaded, don't load it's settings
74
}
75     }
76
77     public RemoteSlave getASlave(
78         Collection JavaDoc 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 JavaDoc slaves,
102         char direction)
103         throws NoAvailableSlaveException {
104         RemoteSlave bestslave;
105         SlaveStatus beststatus;
106         {
107             Iterator JavaDoc 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                         // throws SlaveUnavailableException
117
} 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                     // best slave has less space than "freespace.min" &&
140
// best slave has less space available than current slave
141
bestslave = slave;
142                     bestthroughput = throughput;
143                     beststatus = status;
144                     continue;
145                 }
146
147                 if (status.getDiskSpaceAvailable()
148                     < _minfreespace) {
149                     // current slave has less space available than "freespace.min"
150
// above check made sure bestslave has more space than us
151
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 JavaDoc slaves = _sm.getAvailableSlaves();
201             slaves.removeAll(job.getFile().getAvailableSlaves());
202             return getASlaveForJob(slaves,Transfer.TRANSFER_RECEIVING_UPLOAD);
203     }
204
205     public RemoteSlave getASlaveForJob(Collection JavaDoc 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