Skip to content
This repository was archived by the owner on Mar 6, 2020. It is now read-only.

Commit fea73cc

Browse files
author
quaa
committed
Added option to list directories that the server hosts
1 parent f962a79 commit fea73cc

File tree

5 files changed

+145
-101
lines changed

5 files changed

+145
-101
lines changed

README

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ You will need to run Jfs client as:
3232
ex: java -jar jfs.jar -c 192.168.1.100 ~/working/
3333

3434
The Jfs client will connect to the given server and close when
35-
syncing is finished.
35+
syncing is finished.
36+
37+
To list the directories that the server hosts use:
38+
java -jar jfs.jar -c Server.IP -ls
39+
ex: java -jar jfs.jar -c 192.168.1.100 -ls
40+
41+
You then can create a hosted directory and sync it with the
42+
server by executing the command above.

executable/jfs.jar

709 Bytes
Binary file not shown.

src/net/quaa/jfs/Client.java

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public Client(String dirName, String fullDirName, String serverIP, int port) {
3131
System.out.println("Client Selected!");
3232
System.out.println("Dir to sync: " + dirName);
3333
System.out.println("Server IP: " + serverIP);
34-
System.out.print("Syncing");
3534
}
3635

3736
public void runClient() throws Exception {
@@ -41,29 +40,42 @@ public void runClient() throws Exception {
4140
oos.writeObject(new String(dirName));
4241
oos.flush();
4342

44-
ois = new ObjectInputStream(sock.getInputStream()); // receive if this directory exists
45-
Boolean fExists = (Boolean) ois.readObject();
43+
ois = new ObjectInputStream(sock.getInputStream());
44+
if(dirName.equalsIgnoreCase("-ls")) {
45+
Vector<String> directories = (Vector<String>) ois.readObject();
46+
System.out.println("The directories available are: ");
47+
for (int x = 0; x < directories.size(); x++) {
48+
System.out.print(directories.elementAt(x) + " ");
49+
}
50+
System.out.println();
51+
System.out.println();
52+
System.out.println("To sync one of these directories please create the one you are wanting to sync and run the client again with that directory.");
53+
} else {
54+
System.out.print("Syncing");
55+
// receive if this directory exists
56+
Boolean fExists = (Boolean) ois.readObject();
4657

47-
File baseDir = new File(fullDirName); // skipping the base dir as it already should be set up on the server
48-
String[] children = baseDir.list();
58+
File baseDir = new File(fullDirName); // skipping the base dir as it already should be set up on the server
59+
String[] children = baseDir.list();
4960

50-
for (int i=0; i < children.length; i++) {
51-
visitAllDirsAndFiles(new File(baseDir, children[i]));
52-
}
53-
Vector<String> vecDONE = new Vector<String>();
54-
vecDONE.add(DONE);
55-
oos.writeObject(vecDONE);
56-
oos.flush();
57-
reinitConn();
61+
for (int i=0; i < children.length; i++) {
62+
visitAllDirsAndFiles(new File(baseDir, children[i]));
63+
}
64+
Vector<String> vecDONE = new Vector<String>();
65+
vecDONE.add(DONE);
66+
oos.writeObject(vecDONE);
67+
oos.flush();
68+
reinitConn();
5869

59-
if(fExists)
60-
updateFromServer();
70+
if(fExists)
71+
updateFromServer();
6172

73+
System.out.println();
74+
System.out.println("Finished sync");
75+
}
6276
oos.close();
6377
ois.close();
6478
sock.close();
65-
System.out.println();
66-
System.out.println("Finished sync");
6779
}
6880

6981
// Process all files and directories under dir
@@ -130,7 +142,7 @@ private static void sendFile(File dir) throws Exception {
130142
oos.flush();
131143
reinitConn();
132144

133-
// printDebug(true, dir);
145+
// printDebug(true, dir);
134146
}
135147

136148
private static void receiveFile(File dir) throws Exception {
@@ -145,7 +157,7 @@ private static void receiveFile(File dir) throws Exception {
145157

146158
reinitConn();
147159

148-
// printDebug(false, dir);
160+
// printDebug(false, dir);
149161
}
150162

151163
private static void updateFromServer() throws Exception {

src/net/quaa/jfs/Server.java

Lines changed: 89 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -33,90 +33,106 @@ public void startServer() throws Exception {
3333
ois = new ObjectInputStream(sock.getInputStream());
3434

3535
baseDir = (String) ois.readObject();
36-
File fBaseDir = new File(baseDir);
37-
Boolean baseDirExists = fBaseDir.exists();
3836

39-
System.out.println("New client connected! IP: " + sock.getInetAddress().toString() + " Directory: " + baseDir);
37+
oos = new ObjectOutputStream(sock.getOutputStream());
4038

41-
if(!baseDirExists)
42-
fBaseDir.mkdir();
39+
System.out.println("New client connected! IP: " + sock.getInetAddress().toString() + " Directory: " + baseDir);
4340

44-
oos = new ObjectOutputStream(sock.getOutputStream());
45-
oos.writeObject(new Boolean(baseDirExists));
46-
oos.flush();
41+
if(baseDir.equalsIgnoreCase("-ls")) {
42+
Vector<String> directories = new Vector<String>();
43+
File serverBase = new File("./");
44+
String[] children = serverBase.list();
45+
for (int i=0; i<children.length; i++) {
46+
File newF = new File(serverBase, children[i]);
47+
if(newF.isDirectory())
48+
directories.add(newF.getName());
49+
}
50+
oos.writeObject(directories);
51+
oos.flush();
52+
} else {
53+
File fBaseDir = new File(baseDir);
54+
Boolean baseDirExists = fBaseDir.exists();
55+
56+
if(!baseDirExists)
57+
fBaseDir.mkdir();
58+
59+
oos.writeObject(new Boolean(baseDirExists));
60+
oos.flush();
4761

48-
Boolean isClientDone = false;
62+
Boolean isClientDone = false;
4963

50-
while (!isClientDone) {
51-
Vector<String> vec = (Vector<String>) ois.readObject();
52-
reinitConn();
64+
while (!isClientDone) {
65+
Vector<String> vec = (Vector<String>) ois.readObject();
66+
reinitConn();
5367

54-
if(vec.elementAt(0).equals(DONE)) { // check if we are done
55-
isClientDone = true; // if so break out
56-
break;
57-
}
68+
if(vec.elementAt(0).equals(DONE)) { // check if we are done
69+
isClientDone = true; // if so break out
70+
break;
71+
}
5872

59-
if(vec.size() == 2) { // if the size is 2 then this is a directory
60-
File newDir = new File(baseDir, vec.elementAt(1));
61-
if (!newDir.exists())
62-
newDir.mkdir();
73+
if(vec.size() == 2) { // if the size is 2 then this is a directory
74+
File newDir = new File(baseDir, vec.elementAt(1));
75+
if (!newDir.exists())
76+
newDir.mkdir();
6377

64-
oos.writeObject(new Boolean(true)); // tell client that we are ready
65-
oos.flush();
66-
} else {
67-
File newFile = new File(baseDir, vec.elementAt(1));
68-
Integer updateFromClient = 2; // default = do nothing
78+
oos.writeObject(new Boolean(true)); // tell client that we are ready
79+
oos.flush();
80+
} else {
81+
File newFile = new File(baseDir, vec.elementAt(1));
82+
Integer updateFromClient = 2; // default = do nothing
6983

70-
Long lastModified = new Long(vec.elementAt(2));
71-
if (!newFile.exists() || (newFile.lastModified() <= lastModified))
72-
updateFromClient = 1;
73-
else
74-
updateFromClient = 0;
84+
Long lastModified = new Long(vec.elementAt(2));
85+
if (!newFile.exists() || (newFile.lastModified() <= lastModified))
86+
updateFromClient = 1;
87+
else
88+
updateFromClient = 0;
7589

76-
if(newFile.exists() && newFile.lastModified() == lastModified)
77-
updateFromClient = 2;
90+
if(newFile.exists() && newFile.lastModified() == lastModified)
91+
updateFromClient = 2;
7892

79-
if(updateFromClient == 1) { // If true receive file from client
80-
newFile.delete();
93+
if(updateFromClient == 1) { // If true receive file from client
94+
newFile.delete();
8195

82-
oos.writeObject(new Integer(updateFromClient));
83-
oos.flush();
96+
oos.writeObject(new Integer(updateFromClient));
97+
oos.flush();
8498

85-
receiveFile(newFile);
99+
receiveFile(newFile);
86100

87-
newFile.setLastModified(lastModified);
101+
newFile.setLastModified(lastModified);
88102

89-
oos.writeObject(new Boolean(true));
90-
} else if (updateFromClient == 0) { // if false send file to client
91-
oos.writeObject(new Integer(updateFromClient));
92-
oos.flush();
103+
oos.writeObject(new Boolean(true));
104+
} else if (updateFromClient == 0) { // if false send file to client
105+
oos.writeObject(new Integer(updateFromClient));
106+
oos.flush();
93107

94-
ois.readObject();
108+
ois.readObject();
95109

96-
sendFile(newFile);
110+
sendFile(newFile);
97111

98-
ois.readObject();
112+
ois.readObject();
99113

100-
oos.writeObject(new Long(newFile.lastModified()));
101-
oos.flush();
102-
} else { //updateFromClient == 2 // do nothing
103-
oos.writeObject(new Integer(updateFromClient));
104-
oos.flush();
114+
oos.writeObject(new Long(newFile.lastModified()));
115+
oos.flush();
116+
} else { //updateFromClient == 2 // do nothing
117+
oos.writeObject(new Integer(updateFromClient));
118+
oos.flush();
119+
}
105120
}
106121
}
122+
123+
File baseDirFile = new File(baseDir);
124+
if(baseDirExists)
125+
visitAllDirsAndFiles(baseDirFile);
126+
127+
oos.writeObject(new String(DONE));
128+
oos.flush();
129+
System.out.print("Finished sync...");
130+
107131
}
108-
109-
File baseDirFile = new File(baseDir);
110-
if(baseDirExists)
111-
visitAllDirsAndFiles(baseDirFile);
112-
113-
oos.writeObject(new String(DONE));
114-
oos.flush();
115-
116132
oos.close();
117133
ois.close();
118134
sock.close();
119-
System.out.println("Finished sync");
135+
System.out.println("Client disconnected.");
120136
}
121137
}
122138

@@ -165,12 +181,12 @@ private static void visitAllDirsAndFiles(File dir) throws Exception {
165181
}
166182
}
167183

168-
if (dir.isDirectory()) {
169-
String[] children = dir.list();
170-
for (int i = 0; i < children.length; i++) {
171-
visitAllDirsAndFiles(new File(dir, children[i]));
172-
}
173-
}
184+
if (dir.isDirectory()) {
185+
String[] children = dir.list();
186+
for (int i = 0; i < children.length; i++) {
187+
visitAllDirsAndFiles(new File(dir, children[i]));
188+
}
189+
}
174190
}
175191

176192
private static void sendFile(File dir) throws Exception {
@@ -209,14 +225,14 @@ private static void reinitConn() throws Exception {
209225
oos = new ObjectOutputStream(sock.getOutputStream());
210226
ois = new ObjectInputStream(sock.getInputStream());
211227
}
212-
228+
213229
private static void deleteAllDirsAndFiles(File dir) {
214-
if (dir.isDirectory()) {
215-
String[] children = dir.list();
216-
for (int i=0; i<children.length; i++) {
217-
deleteAllDirsAndFiles(new File(dir, children[i]));
218-
}
219-
}
220-
dir.delete();
230+
if (dir.isDirectory()) {
231+
String[] children = dir.list();
232+
for (int i=0; i<children.length; i++) {
233+
deleteAllDirsAndFiles(new File(dir, children[i]));
234+
}
235+
}
236+
dir.delete();
221237
}
222238
}

src/net/quaa/jfs/javaFileSync.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ public static void main(String[] args) {
2121
localName = cleanUpInput(args[2]);
2222
client(localName, args[2], args[1]);
2323
} else {
24-
System.out.println("Invalid entry. Usage: java javaFileSync [-s] [-c [server IP] [dir to sync]]");
24+
System.out.println("Invalid entry. Usage: java javaFileSync [-s] [-c [server IP] [dir to sync OR -ls]]");
2525
}
2626
} else {
27-
System.out.println("Invalid entry. Usage: java javaFileSync [-s] [-c [server IP] [dir to sync]]");
27+
System.out.println("Invalid entry. Usage: java javaFileSync [-s] [-c [server IP] [dir to sync OR -ls]]");
2828
}
2929
} catch (Exception e) {
3030
e.printStackTrace();
@@ -42,20 +42,29 @@ public static void client(String dirName, String fullDirName, String serverIP) t
4242
}
4343

4444
public static void testing(File dirName, String fullPathName) throws Exception {
45-
visitAllDirsAndFiles(dirName);
46-
45+
File f = new File("./");
46+
String[] children = f.list();
47+
for (int i=0; i<children.length; i++) {
48+
File newF = new File(f, children[i]);
49+
if(newF.isDirectory())
50+
System.out.print(newF.getName() + " ");
51+
}
52+
System.out.println();
4753
}
4854

4955
public static String cleanUpInput(String userInput) throws Exception {
50-
56+
57+
if(userInput.equalsIgnoreCase("-ls"))
58+
return userInput;
59+
5160
File f = new File(userInput);
52-
61+
5362
if(!f.isDirectory()) {
5463
System.out.println("Please input a directory instead of a file!");
5564
Thread.sleep(10);
5665
System.exit(0);
5766
}
58-
67+
5968
String localDirName = userInput; //cleaning up the users input
6069
if(userInput.contains("/")){
6170
if(userInput.lastIndexOf("/") != (userInput.length() - 1)) {
@@ -66,7 +75,7 @@ public static String cleanUpInput(String userInput) throws Exception {
6675
localDirName = localDirName.substring(localDirName.lastIndexOf("/"));
6776
}
6877
}
69-
78+
7079
if(localDirName.equals(".")){
7180
System.out.println("Please input a dir name instead of ./ or .");
7281
Thread.sleep(10);

0 commit comments

Comments
 (0)