Skip to content

Commit ce282cc

Browse files
author
quaa
committed
Made the client sent the path of the current directory that way it does not need to be tracked in the server
1 parent 115127c commit ce282cc

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

pseudocode.txt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,21 @@ CLIENT: dirName
3434
Server: exists = TRUE or FALSE
3535

3636
if directory
37-
CLIENT: dirName DiR
37+
CLIENT: dirName
38+
SERVER: true
39+
CLIENT: isDirectory = TRUE
40+
SERVER: true
41+
CLIENT: PATH
3842
SERVER: true
3943
else
40-
CLIENT: fileName lastModified
41-
SERVER: TRUE or FALSE
44+
CLIENT: fileName
45+
SERVER: true
46+
CLIENT: PATH
47+
SERVER: true
48+
CLIENT: lastModified
49+
SERVER: true
50+
CLIENT: isDirectory = FALSE
51+
SERVER: TRUE or FALSE (update?)
4252
if TRUE
4353
CLIENT: SEND FILE
4454
SERVER: TRUE
@@ -49,6 +59,8 @@ else
4959
SERVER: SEND LAST MODIFIED
5060
end
5161
end
62+
CLIENT: DIR..
63+
SERVER: OK
5264

5365
CLIENT: DONE
5466

src/net/quaa/jfs/Client.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
import java.net.Socket;
1313

1414
public class Client {
15-
private String dirName;
16-
private String serverIP;
17-
private String fullDirName;
18-
private int PORT_NUMBER;
15+
private static String dirName;
16+
private static String serverIP;
17+
private static String fullDirName;
18+
private static int PORT_NUMBER;
1919
private static final String DONE = "DONE";
2020

2121
public Client(String dirName, String fullDirName, String serverIP, int port) {
22-
this.dirName = dirName;
23-
this.serverIP = serverIP;
24-
this.fullDirName = fullDirName;
22+
Client.dirName = dirName;
23+
Client.serverIP = serverIP;
24+
Client.fullDirName = fullDirName;
2525
PORT_NUMBER = port;
2626

2727
System.out.println("Client Selected!");
@@ -33,15 +33,15 @@ public void runClient() throws Exception {
3333

3434
Socket sock = new Socket(serverIP, PORT_NUMBER);
3535
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); // send directory name to server
36-
oos.writeChars(dirName);
36+
oos.writeChars(new String(dirName));
3737
oos.flush();
3838

3939
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); // receive if this directory exists
4040
Boolean fExists = (Boolean) ois.readObject();
4141

4242
visitAllDirsAndFiles(new File(fullDirName), sock);
4343

44-
oos.writeChars(DONE);
44+
oos.writeChars(new String(DONE));
4545
oos.flush();
4646

4747
if(fExists)
@@ -54,12 +54,28 @@ public void runClient() throws Exception {
5454

5555
// Process all files and directories under dir
5656
public static void visitAllDirsAndFiles(File dir, Socket sock) throws Exception{
57+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); // send pathName fileName LastModified to server
58+
oos.writeChars(new String(dir.getName()));
59+
oos.flush();
60+
61+
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); // wait for server to say ok
62+
ois.readBoolean();
63+
64+
oos.writeBoolean(new Boolean(dir.isDirectory()));
65+
oos.flush();
66+
67+
ois.readBoolean();
68+
69+
oos.writeChars(new String(dir.getAbsolutePath().substring((dir.getAbsolutePath().indexOf(fullDirName) + fullDirName.length()))));
70+
oos.flush();
71+
72+
ois.readBoolean();
73+
5774
if(!dir.isDirectory()) {
58-
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); // send fileName LastModified to server
59-
oos.writeChars(dir.getName() + " " + dir.lastModified());
75+
oos.writeLong(new Long(dir.lastModified()));
6076
oos.flush();
6177

62-
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); // receive SEND or RECEIVE
78+
// receive SEND or RECEIVE
6379
Boolean updateToServer = (Boolean) ois.readObject(); //if true update server, else update from server
6480

6581
if (updateToServer) { // send file to server
@@ -76,12 +92,12 @@ public static void visitAllDirsAndFiles(File dir, Socket sock) throws Exception{
7692
} else { // update file from server.
7793
dir.delete(); // first delete the current file
7894

79-
oos.writeBoolean(true); // send "Ready"
95+
oos.writeBoolean(new Boolean(true)); // send "Ready"
8096
oos.flush();
8197

8298
receiveFile(dir, sock);
8399

84-
oos.writeBoolean(true); // send back ok
100+
oos.writeBoolean(new Boolean(true)); // send back ok
85101
oos.flush();
86102

87103
Long updateLastModified = (Long) ois.readObject(); // update the last modified date for this file from the server
@@ -90,20 +106,6 @@ public static void visitAllDirsAndFiles(File dir, Socket sock) throws Exception{
90106
oos.close();
91107
ois.close();
92108
}
93-
94-
} else {
95-
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); // send directory name to server
96-
oos.writeChars(dir.getName() + "DiR");
97-
oos.flush();
98-
99-
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); // receive if this directory exists
100-
Boolean ok = (Boolean) ois.readObject();
101-
102-
oos.close();
103-
ois.close();
104-
105-
if(!ok) // if did not receive an ok back from the server, re-run.
106-
visitAllDirsAndFiles(dir, sock);
107109
}
108110

109111
/* debug */ System.out.println("Name: " + dir.getName() + " Dir: " + dir.isDirectory() + " Modified: " + dir.lastModified() + " Size: " + dir.length());

src/net/quaa/jfs/javaFileSync.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
public class javaFileSync {
66
private static final int PORT_NUMBER = 17555;
7+
private static String localName;
8+
private static String fullPathName;
79

810
public static void main(String[] args) {
911
try {
@@ -12,10 +14,11 @@ public static void main(String[] args) {
1214
server();
1315
} if (args[0].equals("-t")) {
1416
System.out.println("You have found the secret testing function!");
15-
String localName = cleanUpInput(args[1]);
16-
testing(new File(localName));
17+
fullPathName = args[1];
18+
localName = cleanUpInput(args[1]);
19+
testing(new File(localName), args[1]);
1720
} else if (args[0].equalsIgnoreCase("-c") && args.length > 1 && args.length < 4) {
18-
String localName = cleanUpInput(args[1]);
21+
localName = cleanUpInput(args[1]);
1922
client(localName, args[2], args[1]);
2023
} else {
2124
System.out.println("Invalid entry. Useage: java javaFileSync [-s] [-c [server IP] [dir to sync]]");
@@ -38,8 +41,9 @@ public static void client(String dirName, String fullDirName, String serverIP) t
3841
c.runClient();
3942
}
4043

41-
public static void testing(File dirName) throws Exception {
44+
public static void testing(File dirName, String fullPathName) throws Exception {
4245
visitAllDirsAndFiles(dirName);
46+
4347
}
4448

4549
public static String cleanUpInput(String userInput) throws Exception {
@@ -82,6 +86,7 @@ public static String cleanUpInput(String userInput) throws Exception {
8286
public static void visitAllDirsAndFiles(File dir) {
8387
//process(dir);
8488
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());
8590
if (dir.isDirectory()) {
8691
String[] children = dir.list();
8792
for (int i=0; i<children.length; i++) {
@@ -95,7 +100,6 @@ public static void visitAllDirs(File dir) {
95100
if (dir.isDirectory()) {
96101
//process(dir);
97102
System.out.println("Name: " + dir.getName() + " Modified: " + dir.lastModified() + " Size: " + dir.length());
98-
99103
String[] children = dir.list();
100104
for (int i=0; i<children.length; i++) {
101105
visitAllDirs(new File(dir, children[i]));

0 commit comments

Comments
 (0)