1 18 package net.sf.drftpd.mirroring; 19 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.Set ; 25 26 import net.sf.drftpd.master.RemoteSlave; 27 import net.sf.drftpd.master.usermanager.User; 28 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 29 30 35 public class Job { 36 protected int _transferNum; 37 protected Set _destSlaves; 38 protected LinkedRemoteFileInterface _file; 39 protected User _owner; 40 protected int _priority; 41 protected Object _source; 42 protected long _timeCreated; 43 protected long _timeSpent; 44 45 public Job( 46 LinkedRemoteFileInterface file, 47 Set destSlaves, 48 Object source, 49 User owner, 50 int priority, 51 int transferNum) { 52 super(); 53 _destSlaves = new HashSet (destSlaves); 54 _file = file; 55 _source = source; 56 _owner = owner; 57 _priority = priority; 58 _timeCreated = System.currentTimeMillis(); 59 _timeSpent = 0; 60 _transferNum = transferNum; 61 if (_transferNum > destSlaves.size()) 62 throw new IllegalArgumentException ("transferNum cannot be greater than destSlaves.size()"); 63 } 64 65 public void addTimeSpent(long time) { 66 _timeSpent += time; 67 } 68 69 72 public Set getDestinationSlaves() { 73 return Collections.unmodifiableSet(_destSlaves); 74 } 75 76 80 public LinkedRemoteFileInterface getFile() { 81 return _file; 82 } 83 84 87 public User getOwner() { 88 return _owner; 89 } 90 91 94 public int getPriority() { 95 return _priority; 96 } 97 98 105 public Object getSource() { 106 return _source; 107 } 108 109 112 public long getTimeCreated() { 113 return _timeCreated; 114 } 115 118 public long getTimeSpent() { 119 return _timeSpent; 120 } 121 122 125 public boolean isDone() { 126 return _transferNum < 1; 127 } 128 129 public synchronized void sentToSlave(RemoteSlave slave) { 130 if (_destSlaves.remove(slave)) { 131 _transferNum--; 132 } else { 133 throw new IllegalArgumentException ("Slave " + slave.getName() + " does not exist as a destination slave for job " + this); 134 } 135 if (_destSlaves.isEmpty() && _transferNum > 0) { 136 throw new IllegalStateException ("Job cannot have a destSlaveSet of size 0 with transferNum > 0"); 137 } 138 } 139 140 private String outputDestinationSlaves() { 141 String toReturn = new String (); 142 for (Iterator iter = getDestinationSlaves().iterator();iter.hasNext();) { 143 RemoteSlave rslave = (RemoteSlave) iter.next(); 144 if (!iter.hasNext()) 145 return toReturn + rslave.getName(); 146 toReturn = toReturn + rslave.getName() + ","; 147 } 148 return toReturn; 149 } 150 151 public String toString() { 152 String toReturn = 153 "Job[file=" 154 + getFile().getName() 155 + "],dest=[" 156 + outputDestinationSlaves() 157 + "],owner="; 158 if (getOwner() != null) { 159 toReturn += getOwner().getUsername(); 160 } else { 161 toReturn += "null"; 162 } 163 toReturn += "]"; 164 return toReturn; 165 } 166 167 public void setDone() { 168 _transferNum = 0; 169 } 170 } | Popular Tags |