Source code: konspire/common/FileHost.java
1 // Jason Rohrer
2 // FileHost.java
3
4 /**
5 *
6 * Description of a network host that has files
7 *
8 * Created 5-4-2000
9 * Mods:
10 * Jason Rohrer 7-23-2000 Made scanDir( File, Vector ) method static.
11 * Jason Rohrer 8-14-2000 Added a server member variable to allow
12 * clients to be removed from database when
13 * their server goes down.
14 * Jason Rohrer 8-20-2000 Added a toString() method.
15 * Jason Rohrer 9-30-2000 Changed so that files are scanned from a
16 * FileDirectoryPath instead of just from
17 * a single directory File.
18 */
19
20 package konspire.common;
21 import java.util.StringTokenizer;
22 import konspire.common.Vector;
23
24 import konspire.common.FileDirectoryPath;
25
26 import java.io.File;
27
28 /**
29 * Message containing a description of a network host that is sharing files.
30 * <p>
31 * Contains the host address and a collection of <code>FileDescriptors</code>,
32 * one for each file that the host is sharing.
33 * <p>
34 * Includes functionality for building the collection of <code>FileDescriptors</code>
35 * from a shared files directory with subdirectories.
36 *
37 * @author Jason Rohrer
38 */
39 public class FileHost implements Message {
40
41
42
43 /**
44 * Address of this <code>FileHost</code>.
45 */
46 protected Host address;
47
48
49
50 /**
51 * Server that this <code>FileHost</code> is connected to.
52 */
53 protected Host mServer = null;
54
55
56
57 /**
58 * Vector of <code>FileDescriptors</code> for files on this host.
59 */
60 protected Vector fileVector;
61
62 protected Vector oldFileVector;
63
64
65 // true iff the download directory exists
66 // Doesn't depend on whether the upload directories exist or not.
67 private boolean dirExists = false;
68
69
70
71 /**
72 * Constructs a <code>FileHost</code from host address and <code>Vector</code>
73 * of <code>FileDescriptor</code>s.
74 *
75 * @param addr the address of this host
76 * @param fVector a <code>Vector</code> of <code>FileDescriptor</code>s
77 * describing files shared by this host.
78 */
79 public FileHost( Host addr, Vector fVector ) {
80 address = addr;
81 fileVector = fVector;
82 }
83
84
85
86 /**
87 * Constructs a <code>FileHost</code> from a host address and a shared file directory.
88 *
89 * @param addr the address of this host
90 * @param inFilesPath the directory path (with subdirectories)
91 * on this host where shared files reside.
92 */
93 public FileHost( Host addr, FileDirectoryPath inFilesPath ) {
94 address = addr;
95 rescanFileList( inFilesPath );
96 }
97
98
99
100 /**
101 * Rescans the file list on this host given a shared file directory.
102 * <p>
103 * Note that this method only works if it's called on this host.
104 *
105 * @param inFilesPath the directory path (with subdirectories)
106 * on this host where shared files reside.
107 */
108 public void rescanFileList( FileDirectoryPath inFilesPath ) {
109 fileVector = new Vector();
110
111 // vector of directories to scan
112 Vector directoryVector = new Vector();
113 directoryVector.addElement( inFilesPath.getDownloadDirectory() );
114 directoryVector.addAll( inFilesPath.getUploadDirectories() );
115
116 // make sure download directory exists
117 dirExists = inFilesPath.getDownloadDirectory().exists();
118
119 int numDirectories = directoryVector.size();
120
121 for( int i=0; i<numDirectories; i++ ) {
122 File filesDir = (File)( directoryVector.elementAt( i ) );
123
124 if( filesDir.exists() ) {
125 // add descriptors for all files in directory and subdirectories to fileVector
126 scanDir( address, filesDir, fileVector );
127 }
128 }
129 }
130
131
132
133 /**
134 * Gets whether the download directory specified by the file
135 * directory path exists on this host.
136 *
137 * @return true iff the download directory on this host exists
138 */
139 public boolean downloadDirectoryExists() {
140 return dirExists;
141 }
142
143
144
145 /**
146 * Gets the address of this host.
147 *
148 * @return the address of this host
149 */
150 public Host getHost() {
151 return address;
152 }
153
154
155
156 /**
157 * Replaces the host address for all <code>FileDescriptors</code> in this message.
158 *
159 * @param addr the new address to be attached to all <code>FileDescriptors</code> in this message
160 */
161 public void setHost( Host addr ) {
162 address = addr;
163
164 int numFiles = fileVector.size();
165 for( int i=0; i<numFiles; i++ ) {
166 FileDescriptor fDes = (FileDescriptor)( fileVector.elementAt(i) );
167 fDes.setHost( address );
168 }
169 }
170
171
172
173 /**
174 * Gets the server that this <code>FileHost</code> is connnected to.
175 *
176 * @return the server that this host is connected to.
177 */
178 public Host getServer() {
179 return mServer;
180 }
181
182
183
184 /**
185 * Sets the server that this <code>FileHost</code> is connnected to.
186 *
187 * @param inServer the server that this host is connected to.
188 */
189 public void setServer( Host inServer ) {
190 mServer = inServer;
191 }
192
193
194
195 /**
196 * Gets the <code>Vector</code> of <code>FileDescriptors</code> contained in this message.
197 *
198 * @return the <code>Vector</code> of <code>FileDescriptors</code> contained in this message
199 */
200 public Vector getFileVector() {
201 return fileVector;
202 }
203
204
205 public Vector getOldFileVector() {
206 return oldFileVector;
207 }
208
209 /**
210 * Sets the <code>Vector</code> of <code>FileDescriptors</code> contained in this message.
211 *
212 * @param fVector the <code>Vector</code> of <code>FileDescriptors</code> to be
213 * contained in this message
214 */
215 public void setFileVector( Vector fVector ) {
216 fileVector = fVector;
217 }
218
219 public void setOldFileVector( Vector fVector ) {
220 oldFileVector = fVector;
221 }
222
223
224
225
226 // overrides the Object equals( Object ) method
227 public boolean equals( Object o ) {
228 if( o instanceof FileHost ) {
229 FileHost other = (FileHost) o;
230 return address.equals( other.getHost() );
231 }
232 return false;
233 }
234
235
236
237 /**
238 * Recursively scans a directory, adding all files to a <code>Vector</code>
239 * as <code>FileDescriptor</code>s.
240 *
241 * @param inAddress address of host that is serving these files
242 * @param dirFile the directory to scan for files
243 * @param destVector the <code>Vector</code> to add <code>FileDescriptor</code>s to.
244 */
245 public static void scanDir( Host inAddress, File dirFile, Vector destVector ) {
246 String [] fileList;
247 fileList = dirFile.list();
248 for( int i=0; i<fileList.length; i++ ) {
249 // create file descriptor for file
250 File f = new File( dirFile.getPath() + File.separator + fileList[i] );
251
252 if( f.isDirectory() ) {
253 scanDir( inAddress, f, destVector );
254 }
255 else {
256 StringTokenizer stemp = new StringTokenizer(fileList[i].toString(), "-"
257 );
258 if (stemp.countTokens()>2) {
259
260 boolean ok = false;
261 while (stemp.hasMoreTokens() && !(ok)) {
262 String token = stemp.nextToken();
263 if (token.length() == 1 || token.length() == 2) {
264 if (stemp.hasMoreTokens()) {
265 char tempchar = stemp.nextToken().charAt(0);
266 if (tempchar >= '0' && tempchar <= '9')
267 ok = true;
268 }
269 }
270 }
271
272 if (ok) {
273
274 FileDescriptor fDes = new FileDescriptor( inAddress, fileList[i], f.getPath(), f.length() );
275 destVector.addElement( fDes );
276 } else {
277
278 }
279 } else {
280
281 }
282 }
283 }
284 }
285
286 public long getAmountOffered() {
287 long retval = 0;
288 int vectorSize = 0;
289 if( fileVector != null ) {
290 vectorSize = fileVector.size();
291
292 for (int i=0; i<vectorSize; i++) {
293 FileDescriptor ftemp = (FileDescriptor)fileVector.get(i);
294 retval += ftemp.getSize();
295 }
296 }
297 return retval;
298 }
299
300 // overrides the Object toString() method
301 public String toString() {
302 int vectorSize = 0;
303 if( fileVector != null ) {
304 vectorSize = fileVector.size();
305 }
306 return "FileHost: " + address
307 + "\n\tServer: " + mServer
308 + "\n\tNumFiles: " + vectorSize;
309 }
310
311 }