-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSharedFolderHandler.java
92 lines (58 loc) · 2.08 KB
/
SharedFolderHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.io.File;
import java.util.LinkedList;
import java.util.List;
public class SharedFolderHandler implements SimpleMessageHandler, Runnable {
MuxDemuxSimple md;
@Override
public void run() {
while(true) {
try {
Thread.sleep(Test.FOLDERCHECKINTERVAL);
File folder = new File(Test.SHAREDFOLDERPATH);
File[] listOfFiles = folder.listFiles();
List<String> names = new LinkedList<String>(); // list of the actual filenames in my shared folder
// check equality between myDatabase.stringQueue and listOfFiles
boolean hasChanged = false;
for (File file: listOfFiles) {
names.add(file.getName());
// new file in the folder, not in the database ?
if ( ! md.myDatabase.stringQueue.contains(file.getName())) {
md.myDatabase.stringQueue.add(file.getName());
hasChanged = true;
}
}
List<String> toBeRemoved = new LinkedList<String>();
for (String fileName: md.myDatabase.stringQueue) {
// filename has been deleted ?
if(! names.contains(fileName)) {
toBeRemoved.add(fileName);
hasChanged = true;
}
}
for(String fileName : toBeRemoved) {
md.myDatabase.stringQueue.remove(fileName);
}
if(hasChanged) {
if (Test.DEBUG) System.out.println("Database/shared folder update...");
md.myDatabase.update(); // increases sequence number of my database
}
if(Test.DEBUG) {
System.out.println("---------------\nCurrent database : seq# = " + md.myDatabase.getDatabaseSequenceNumber());
for (String fileName: md.myDatabase.stringQueue) System.out.println("| " + fileName);
System.out.println("---------------");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void handleMessage(String m) {
// TODO Auto-generated method stub
}
@Override
public void setMuxDemux(MuxDemuxSimple muxDemuxSimple) {
this.md = muxDemuxSimple;
}
}