1 18 package net.sf.drftpd.master; 19 20 import java.io.IOException ; 21 import java.net.InetAddress ; 22 import java.rmi.RemoteException ; 23 import java.util.*; 24 25 import net.sf.drftpd.SlaveUnavailableException; 26 import net.sf.drftpd.event.SlaveEvent; 27 import net.sf.drftpd.master.config.FtpConfig; 28 import net.sf.drftpd.remotefile.LinkedRemoteFile; 29 import net.sf.drftpd.slave.Slave; 30 import net.sf.drftpd.slave.SlaveStatus; 31 import net.sf.drftpd.slave.Transfer; 32 33 import org.apache.log4j.Logger; 34 import org.jdom.Element; 35 36 40 public class RemoteSlave implements Comparable { 41 45 public RemoteSlave(String name, Collection list) { 46 Properties p = new Properties(); 47 p.setProperty("name", name); 48 _name = name; 49 updateConfig(p); 50 } 51 52 private int _maxPath; 53 54 private static final Logger logger = Logger.getLogger(RemoteSlave.class); 55 56 private InetAddress _inetAddress; 57 private long _lastDownloadSending = 0; 58 private long _lastPing; 59 private long _lastUploadReceiving = 0; 60 private SlaveManagerImpl _manager; 61 private Collection _masks; 62 private String _name; 63 private Slave _slave; 64 private SlaveStatus _status; 65 private Properties _config; 66 67 public void oldRemoteSlave(String name, Collection masks) { 68 if (name.equalsIgnoreCase("all")) 69 throw new IllegalArgumentException ( 70 name 71 + " is a reserved keyword, it can't be used as a slave name"); 72 _name = name; 73 _masks = masks; 74 } 75 76 public void oldRemoteSlave(String name, Collection masks, Element config) { 77 for (Iterator i = config.getChildren().iterator(); i.hasNext();) { 79 Element e = (Element) i.next(); 80 try { 81 _config.put(e.getName(), e.getText()); 82 } catch (Exception e1) { 83 } 84 } 85 } 86 87 public RemoteSlave(Properties config) { 88 String name = FtpConfig.getProperty(config, "name"); 89 _name = name; 90 updateConfig(config); 91 } 92 93 public void updateConfig(Properties config) { 94 String name = FtpConfig.getProperty(config, "name"); 95 if (name.equalsIgnoreCase("all")) { 96 throw new IllegalArgumentException ( 97 name 98 + " is a reserved keyword, it can't be used as a slave name"); 99 } 100 if (!name.equalsIgnoreCase(_name)) { 101 throw new IllegalArgumentException ( 102 name 103 + " is not the same name as currently set for this RemoteSlave"); 104 } 105 List masks = new ArrayList(); 106 String [] maskitems = config.getProperty("masks", "").split(","); 107 for (int i = 0; i < maskitems.length; i++) { 108 masks.add(maskitems[i]); 109 } 110 _masks = masks; 111 _config = config; 112 } 113 114 public Element getConfigXML() { 115 Element root = new org.jdom.Element("slave"); 116 Enumeration e = _config.keys(); 117 while (e.hasMoreElements()) { 118 String key = (String ) e.nextElement(); 119 Element tmp = new Element(key); 120 tmp.setText((String ) _config.get(key)); 121 root.addContent(tmp); 122 } 123 Iterator i = _masks.iterator(); 124 while (i.hasNext()) { 125 String mask = (String ) i.next(); 126 Element tmp = new Element("mask"); 127 tmp.setText(mask); 128 root.addContent(tmp); 129 } 130 return root; 131 } 132 133 public int compareTo(Object o) { 134 if (!(o instanceof RemoteSlave)) 135 throw new IllegalArgumentException (); 136 return getName().compareTo(((RemoteSlave) o).getName()); 137 } 138 139 public boolean equals(Object obj) { 140 if (obj instanceof RemoteSlave) { 141 RemoteSlave rslave = (RemoteSlave) obj; 142 if (rslave.getName().equals(getName())) { 143 return true; 144 } 145 } 146 return false; 147 } 148 149 public InetAddress getInetAddress() { 150 return _inetAddress; 151 } 152 153 public long getLastDownloadSending() { 154 return _lastDownloadSending; 155 } 156 157 public long getLastTransfer() { 158 return Math.max(getLastDownloadSending(), getLastUploadReceiving()); 159 } 160 161 public long getLastUploadReceiving() { 162 return _lastUploadReceiving; 163 } 164 165 public SlaveManagerImpl getManager() { 166 return _manager; 167 } 168 169 public Collection getMasks() { 170 return _masks; 171 } 172 173 public Hashtable getConfig() { 174 return _config; 175 } 176 177 180 public String getName() { 181 return _name; 182 } 183 184 187 public Slave getSlave() throws SlaveUnavailableException { 188 if (_slave == null) 189 throw new SlaveUnavailableException("slave is offline"); 190 return _slave; 191 } 192 193 196 public SlaveStatus getStatus() throws SlaveUnavailableException { 197 if (_status == null) { 198 throw new SlaveUnavailableException(); 199 } 200 return _status; 201 } 202 203 public synchronized void updateStatus() throws SlaveUnavailableException { 204 try { 205 _status = getSlave().getSlaveStatus(); 206 } catch (RemoteException e) { 207 _status = null; 208 } 209 } 210 211 215 public void handleRemoteException(RemoteException ex) { 216 logger.warn("Exception from " + getName() + ", removing", ex); 226 setOffline(ex.getCause().getMessage()); 227 } 228 229 public int hashCode() { 230 return getName().hashCode(); 231 } 232 233 public boolean isAvailable() { 234 return _slave != null; 235 } 236 237 public boolean isAvailablePing() { 238 try { 239 getSlave().ping(); 240 } catch (RemoteException e) { 241 handleRemoteException(e); 242 return false; 243 } catch (SlaveUnavailableException e) { 244 return false; 245 } 246 return isAvailable(); 247 } 248 249 254 public void ping() throws RemoteException , SlaveUnavailableException { 255 if (_slave == null) 256 throw new SlaveUnavailableException(getName() + " is offline"); 257 if (System.currentTimeMillis() > _lastPing + 1000) { 258 getSlave().ping(); 259 } 260 } 261 public void setLastDownloadSending(long lastDownloadSending) { 262 _lastDownloadSending = lastDownloadSending; 263 } 264 public void setLastUploadReceiving(long lastUploadReceiving) { 265 _lastUploadReceiving = lastUploadReceiving; 266 } 267 268 public void setManager(SlaveManagerImpl manager) { 269 if (_manager != null) 270 throw new IllegalStateException ("Can't overwrite manager"); 271 _manager = manager; 272 } 273 274 public void setMasks(Collection masks) { 275 _masks = masks; 276 } 277 278 public void setOffline(String reason) { 279 if (_manager == null) { 280 throw new RuntimeException ("_manager == null"); 281 } 282 if (_slave != null) { 283 _manager.getConnectionManager().dispatchFtpEvent( 284 new SlaveEvent("DELSLAVE", reason, this)); 285 } 286 _slave = null; 287 _status = null; 288 _inetAddress = null; 289 _maxPath = 0; 290 } 291 292 public void setSlave( 293 Slave slave, 294 InetAddress inetAddress, 295 SlaveStatus status, 296 int maxPath) { 297 if (slave == null) 298 throw new IllegalArgumentException (); 299 _slave = slave; 300 _inetAddress = inetAddress; 301 _status = status; 302 _maxPath = maxPath; 303 } 304 305 public String toString() { 306 try { 307 return getName() + "[slave=" + getSlave().toString() + "]"; 308 } catch (SlaveUnavailableException e) { 309 return getName() + "[slave=offline]"; 310 } 311 } 312 313 public static Hashtable rslavesToHashtable(Collection rslaves) { 314 Hashtable map = new Hashtable(rslaves.size()); 315 for (Iterator iter = rslaves.iterator(); iter.hasNext();) { 316 RemoteSlave rslave = (RemoteSlave) iter.next(); 317 map.put(rslave.getName(), rslave); 318 } 319 return map; 320 } 321 322 public long getLastTransferForDirection(char dir) { 323 if (dir == Transfer.TRANSFER_RECEIVING_UPLOAD) { 324 return getLastUploadReceiving(); 325 } else if (dir == Transfer.TRANSFER_SENDING_DOWNLOAD) { 326 return getLastDownloadSending(); 327 } else if (dir == Transfer.TRANSFER_THROUGHPUT) { 328 return getLastTransfer(); 329 } else { 330 throw new IllegalArgumentException (); 331 } 332 } 333 334 337 public LinkedRemoteFile getSlaveRoot() 338 throws IOException , SlaveUnavailableException { 339 return getSlave().getSlaveRoot(); 340 } 341 342 public void setLastDirection(char direction, long l) { 343 switch (direction) { 344 case Transfer.TRANSFER_RECEIVING_UPLOAD : 345 setLastUploadReceiving(l); 346 return; 347 case Transfer.TRANSFER_SENDING_DOWNLOAD : 348 setLastDownloadSending(l); 349 return; 350 default : 351 throw new IllegalArgumentException (); 352 } 353 } 354 } 355 | Popular Tags |