Skip to content

Commit 45d3a8c

Browse files
author
quaa
committed
Two way talking should be working. Need to test later.
1 parent cb1da05 commit 45d3a8c

File tree

5 files changed

+184
-51
lines changed

5 files changed

+184
-51
lines changed

TODO.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PROBLEMS
22

33
Server does not have file:
4-
Client will receive from server
4+
Client will send to server
55

66
Client does not have file:
77
Nothing happens.

pseudocode.txt

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,9 @@ CLIENT: dirName
3434
Server: exists = TRUE or FALSE
3535

3636
if directory
37-
old CLIENT: dirName
38-
old SERVER: true
39-
old CLIENT: isDirectory = TRUE
40-
old SERVER: true
41-
old CLIENT: PATH
42-
old SERVER: true
43-
4437
CLIENT: VECTOR<STRING> = dirName, PATH
4538
SERVER: true
4639
else
47-
old CLIENT: fileName
48-
old SERVER: true
49-
old CLIENT: isDirectory = FALSE
50-
old SERVER: true
51-
old CLIENT: PATH
52-
old SERVER: true
53-
old CLIENT: lastModified
54-
5540
CLIENT: VECTOR<STRING> = fileName, PATH, lastModified
5641
SERVER: TRUE or FALSE (update?)
5742
if TRUE
@@ -71,38 +56,39 @@ CLIENT: DONE
7156
// begin 2 way talking here.
7257

7358
if EXISTS = TRUE
59+
SERVER: PATH
60+
CLIENT: OK
61+
SERVER: TRUE or FALSE (dir)
7462
if directory
75-
SERVER: dirName DiR
76-
CLIENT: TRUE or FALSE
63+
CLIENT: TRUE or FALSE (exists)
7764
if FALSE
7865
SERVER: TRUE
7966
CLIENT: ask user if he/she would like to delete servers copy
8067
reply Y (delete) or N (get servers copy)
8168
or D (do nothing)
82-
if Y
69+
if Y (1)
8370
SERVER: delete directory
84-
if N
71+
if N (0)
8572
CLIENT: make directory
86-
if D
73+
if D (0) (default)
8774
do nothing
8875
end
8976
end
90-
else if TRUE
91-
SERVER: fileName
77+
else NOT DIRECTORY
9278
CLIENT: TRUE or FALSE
93-
if TRUE
79+
if FALSE (client does not have)
9480
SERVER: TRUE
9581
CLIENT: ask user if he/she would like to delete servers copy
9682
reply Y (delete) or N (get servers copy)
9783
or D (do nothing)
98-
if Y
84+
if Y (1)
9985
SERVER: delete file
100-
if N
86+
if N (0)
10187
SERVER: SEND FILE
10288
CLIENT: OK
10389
SERVER: SEND LASTMODIFIED
10490
CLIENT: OK
105-
if D
91+
if D (2)(DEFAULT)
10692
do nothing
10793
end
10894
end

src/net/quaa/jfs/Client.java

Lines changed: 98 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package net.quaa.jfs;
22

3+
import java.io.BufferedReader;
34
import java.io.File;
45
import java.io.FileInputStream;
56
import java.io.FileOutputStream;
7+
import java.io.IOException;
68
import java.io.InputStream;
9+
import java.io.InputStreamReader;
710
import java.io.ObjectInputStream;
811
import java.io.ObjectOutputStream;
912
import java.net.Socket;
@@ -43,17 +46,17 @@ public void runClient() throws Exception {
4346
File baseDir = new File(fullDirName); // skipping the base dir as it already should be set up on the server
4447
String[] children = baseDir.list();
4548

46-
for (int i=0; i<children.length; i++) {
47-
visitAllDirsAndFiles(new File(baseDir, children[i]));
48-
}
49-
Vector<String> vecDONE = new Vector<String>();
50-
vecDONE.add(DONE);
49+
for (int i=0; i < children.length; i++) {
50+
visitAllDirsAndFiles(new File(baseDir, children[i]));
51+
}
52+
Vector<String> vecDONE = new Vector<String>();
53+
vecDONE.add(DONE);
5154
oos.writeObject(vecDONE);
5255
oos.flush();
5356
reinitConn();
54-
57+
5558
if(fExists)
56-
updateFromServer(sock, fullDirName);
59+
updateFromServer();
5760

5861
oos.close();
5962
ois.close();
@@ -62,7 +65,7 @@ public void runClient() throws Exception {
6265

6366
// Process all files and directories under dir
6467
private static void visitAllDirsAndFiles(File dir) throws Exception{
65-
Vector<String> vec = new Vector<String>();
68+
Vector<String> vec = new Vector<String>();
6669
vec.add(dir.getName());
6770
vec.add(dir.getAbsolutePath().substring((dir.getAbsolutePath().indexOf(fullDirName) + fullDirName.length())));
6871

@@ -102,11 +105,11 @@ private static void visitAllDirsAndFiles(File dir) throws Exception{
102105
} // no need to check if update to server == 2 because we do nothing here
103106
}
104107
if (dir.isDirectory()) {
105-
String[] children = dir.list();
106-
for (int i=0; i<children.length; i++) {
107-
visitAllDirsAndFiles(new File(dir, children[i]));
108-
}
109-
}
108+
String[] children = dir.list();
109+
for (int i=0; i<children.length; i++) {
110+
visitAllDirsAndFiles(new File(dir, children[i]));
111+
}
112+
}
110113
}
111114

112115
private static void sendFile(File dir) throws Exception {
@@ -141,11 +144,88 @@ private static void receiveFile(File dir) throws Exception {
141144
printDebug(false, dir);
142145
}
143146

144-
private static void updateFromServer(Socket sock, String fullDirName) throws Exception {
145-
//oos = new ObjectOutputStream(sock.getOutputStream()); // send fileName LastModified to server
146-
//ois = new ObjectInputStream(sock.getInputStream()); // receive SEND or RECEIVE
147-
//File f = new File(fullDirName);
148-
// need to implement this part
147+
private static void updateFromServer() throws Exception {
148+
Boolean isDone = false;
149+
Boolean nAll = false;
150+
while(!isDone) {
151+
String path = (String) ois.readObject();
152+
153+
if(path.equals(DONE)) {
154+
isDone = true;
155+
break;
156+
}
157+
158+
oos.writeObject(new Boolean(true));
159+
oos.flush();
160+
161+
File newFile = new File(fullDirName + path);
162+
163+
Boolean isDirectory = (Boolean) ois.readObject();
164+
165+
oos.writeObject(new Boolean(newFile.exists()));
166+
oos.flush();
167+
if (!newFile.exists()) {
168+
ois.readObject();
169+
String userInput = null;
170+
if (!nAll) {
171+
if (isDirectory) {
172+
System.out.println("CONFLICT with " + fullDirName + path + "! The directory exists on the server but not this client.");
173+
System.out.println("Would you like to delete the server's directory (if no we would create the directory on this client)?");
174+
} else {
175+
System.out.println("CONFLICT with " + fullDirName + path + "! The file exists on the server but not this client.");
176+
System.out.println("Would you like to delete the server's file (if no we would create the file on this client)?");
177+
}
178+
System.out.println("Type 'y' for yes, 'n' for no, 'a' for no to all, 'd' to do nothing");
179+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
180+
try {
181+
userInput = br.readLine();
182+
} catch (IOException ioe) {
183+
System.out.println("You did not input a correct value, will do nothing.");
184+
}
185+
} else // if n to all then just set input to n!
186+
userInput = "n";
187+
if (userInput.equalsIgnoreCase("a") || userInput.equalsIgnoreCase("'a'")) {
188+
nAll = true;
189+
userInput = "n";
190+
}
191+
if (userInput.equalsIgnoreCase("y") || userInput.equalsIgnoreCase("'y'")) {
192+
if (isDirectory) {
193+
oos.writeObject(new Boolean(true)); // reply with yes to delete the server's copy
194+
oos.flush();
195+
} else {
196+
oos.writeObject(new Integer(1));
197+
oos.flush();
198+
}
199+
} else if (userInput.equalsIgnoreCase("n") || userInput.equalsIgnoreCase("'n'")) {
200+
if (isDirectory) {
201+
newFile.mkdir();
202+
oos.writeObject(new Boolean(false));
203+
oos.flush();
204+
} else {
205+
oos.writeObject(new Integer(0));
206+
oos.flush();
207+
receiveFile(newFile);
208+
209+
oos.writeObject(new Boolean(true));
210+
oos.flush();
211+
212+
Long lastModified = (Long) ois.readObject();
213+
newFile.setLastModified(lastModified);
214+
215+
oos.writeObject(new Boolean(true));
216+
oos.flush();
217+
}
218+
} else {
219+
if (isDirectory) {
220+
oos.writeObject(new Boolean(false));
221+
oos.flush();
222+
} else {
223+
oos.writeObject(new Integer(2));
224+
oos.flush();
225+
}
226+
}
227+
}
228+
}
149229
}
150230

151231
private static void printDebug(Boolean sending, File dir){

src/net/quaa/jfs/Server.java

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Server {
1717
private static ObjectOutputStream oos;
1818
private static ObjectInputStream ois;
1919
private static ServerSocket servsock;
20+
private static String baseDir;
2021

2122
public Server(int port) {
2223
PORT_NUMBER = port;
@@ -30,7 +31,7 @@ public void startServer() throws Exception {
3031
sock = servsock.accept();
3132
ois = new ObjectInputStream(sock.getInputStream());
3233

33-
String baseDir = (String) ois.readObject();
34+
baseDir = (String) ois.readObject();
3435
File fBaseDir = new File(baseDir);
3536
Boolean baseDirExists = fBaseDir.exists();
3637

@@ -101,17 +102,73 @@ public void startServer() throws Exception {
101102
}
102103
}
103104
}
105+
106+
File baseDirFile = new File(baseDir);
107+
if(baseDirExists)
108+
visitAllDirsAndFiles(baseDirFile);
104109

105-
if(baseDirExists){
106-
// need to implement this section
107-
}
108-
110+
oos.writeObject(new String(DONE));
111+
oos.flush();
112+
109113
oos.close();
110114
ois.close();
111115
sock.close();
112116
}
113117
}
114118

119+
private static void visitAllDirsAndFiles(File dir) throws Exception {
120+
oos.writeObject(new String(dir.getAbsolutePath().substring((dir.getAbsolutePath().indexOf(baseDir) + baseDir.length()))));
121+
oos.flush();
122+
123+
ois.readObject();
124+
125+
Boolean isDirectory = dir.isDirectory();
126+
oos.writeObject(new Boolean(isDirectory));
127+
oos.flush();
128+
129+
if (isDirectory) {
130+
if (!(Boolean) ois.readObject()) {
131+
oos.writeObject(new Boolean(true));
132+
oos.flush();
133+
134+
Boolean delete = (Boolean) ois.readObject();
135+
136+
if (delete) {
137+
deleteAllDirsAndFiles(dir);
138+
return;
139+
} //ELSE DO NOTHING
140+
}
141+
} else {
142+
if (!(Boolean) ois.readObject()) {
143+
oos.writeObject(new Boolean(true));
144+
oos.flush();
145+
146+
Integer delete = (Integer) ois.readObject();
147+
148+
if (delete == 1) {
149+
dir.delete();
150+
return;
151+
} else if (delete == 0) {
152+
sendFile(dir);
153+
154+
ois.readObject();
155+
156+
oos.writeObject(new Long(dir.lastModified()));
157+
oos.flush();
158+
159+
ois.readObject();
160+
} // ELSE DO NOTHING!
161+
}
162+
}
163+
164+
if (dir.isDirectory()) {
165+
String[] children = dir.list();
166+
for (int i = 0; i < children.length; i++) {
167+
visitAllDirsAndFiles(new File(dir, children[i]));
168+
}
169+
}
170+
}
171+
115172
private static void sendFile(File dir) throws Exception {
116173
byte[] buff = new byte[sock.getSendBufferSize()];
117174
int bytesRead = 0;
@@ -148,4 +205,14 @@ private static void reinitConn() throws Exception {
148205
oos = new ObjectOutputStream(sock.getOutputStream());
149206
ois = new ObjectInputStream(sock.getInputStream());
150207
}
208+
209+
private static void deleteAllDirsAndFiles(File dir) {
210+
if (dir.isDirectory()) {
211+
String[] children = dir.list();
212+
for (int i=0; i<children.length; i++) {
213+
deleteAllDirsAndFiles(new File(dir, children[i]));
214+
}
215+
}
216+
dir.delete();
217+
}
151218
}

src/net/quaa/jfs/javaFileSync.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static String cleanUpInput(String userInput) throws Exception {
8686
public static void visitAllDirsAndFiles(File dir) {
8787
//process(dir);
8888
System.out.println("Name: " + dir.getName() + " Modified: " + dir.lastModified() + " Size: " + dir.length());
89-
System.out.println(dir.getAbsolutePath().substring((dir.getAbsolutePath().indexOf(fullPathName) + fullPathName.length())) + " Directory? " + dir.isDirectory());
89+
System.out.println(fullPathName + dir.getAbsolutePath().substring((dir.getAbsolutePath().indexOf(fullPathName) + fullPathName.length())) + " Directory? " + dir.isDirectory());
9090
if (dir.isDirectory()) {
9191
String[] children = dir.list();
9292
for (int i=0; i<children.length; i++) {

0 commit comments

Comments
 (0)