Skip to content

Commit 491d667

Browse files
author
quaa
committed
Merge branch 'basicClient'
2 parents 2c5c562 + 4b035b3 commit 491d667

File tree

3 files changed

+271
-59
lines changed

3 files changed

+271
-59
lines changed

pseudocode.txt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,82 @@ if does not exist
2525
do nothing
2626
move to next file
2727
end
28+
29+
30+
31+
2 way talking:
32+
33+
CLIENT: dirName
34+
Server: exists = TRUE or FALSE
35+
36+
if directory
37+
CLIENT: dirName
38+
SERVER: true
39+
CLIENT: isDirectory = TRUE
40+
SERVER: true
41+
CLIENT: PATH
42+
SERVER: true
43+
else
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?)
52+
if TRUE
53+
CLIENT: SEND FILE
54+
SERVER: TRUE
55+
else if FALSE
56+
CLIENT: TRUE
57+
SERVER: SEND FILE
58+
CLIENT: TRUE
59+
SERVER: SEND LAST MODIFIED
60+
end
61+
end
62+
CLIENT: DIR..
63+
SERVER: OK
64+
65+
CLIENT: DONE
66+
67+
if EXISTS = TRUE
68+
if directory
69+
SERVER: dirName DiR
70+
CLIENT: TRUE or FALSE
71+
if FALSE
72+
SERVER: TRUE
73+
CLIENT: ask user if he/she would like to delete servers copy
74+
reply Y (delete) or N (get servers copy)
75+
or D (do nothing)
76+
if Y
77+
SERVER: delete directory
78+
if N
79+
CLIENT: make directory
80+
if D
81+
do nothing
82+
end
83+
end
84+
else if TRUE
85+
SERVER: fileName
86+
CLIENT: TRUE or FALSE
87+
if TRUE
88+
SERVER: TRUE
89+
CLIENT: ask user if he/she would like to delete servers copy
90+
reply Y (delete) or N (get servers copy)
91+
or D (do nothing)
92+
if Y
93+
SERVER: delete file
94+
if N
95+
SERVER: SEND FILE
96+
CLIENT: OK
97+
SERVER: SEND LASTMODIFIED
98+
CLIENT: OK
99+
if D
100+
do nothing
101+
end
102+
end
103+
end
104+
end
105+
106+
SERVER: DONE

src/net/quaa/jfs/Client.java

Lines changed: 139 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
package net.quaa.jfs;
22

3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.FileOutputStream;
8+
import java.io.InputStream;
9+
import java.io.ObjectInputStream;
310
import java.io.ObjectOutputStream;
11+
import java.io.OutputStream;
412
import java.net.Socket;
513

614
public class Client {
7-
private String dirName;
8-
private String serverIP;
9-
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;
19+
private static final String DONE = "DONE";
1020

11-
public Client(String dirName, String serverIP, int port) {
12-
this.dirName = dirName;
13-
this.serverIP = serverIP;
21+
public Client(String dirName, String fullDirName, String serverIP, int port) {
22+
Client.dirName = dirName;
23+
Client.serverIP = serverIP;
24+
Client.fullDirName = fullDirName;
1425
PORT_NUMBER = port;
1526

1627
System.out.println("Client Selected!");
@@ -19,61 +30,134 @@ public Client(String dirName, String serverIP, int port) {
1930
}
2031

2132
public void runClient() throws Exception {
22-
String localDirName = dirName; //cleaning up the users input
23-
if(dirName.contains("/")){
24-
if(dirName.lastIndexOf("/") != (dirName.length() - 1)) {
25-
localDirName = dirName.substring(dirName.lastIndexOf("/"));
26-
} else {
27-
localDirName = dirName.substring(0, (dirName.length() - 1));
28-
if(localDirName.contains("/"))
29-
localDirName = localDirName.substring(localDirName.lastIndexOf("/"));
30-
}
31-
}
3233

33-
if(localDirName.equals(".")){
34-
System.out.println("Please input a dir name instead of ./ or .");
35-
Thread.sleep(10);
36-
System.exit(0);
37-
}
34+
Socket sock = new Socket(serverIP, PORT_NUMBER);
35+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); // send directory name to server
36+
oos.writeChars(new String(dirName));
37+
oos.flush();
3838

39-
if(!localDirName.startsWith("./")){ //still cleaning up their input
40-
if(localDirName.startsWith("/"))
41-
localDirName = "." + localDirName;
42-
else
43-
localDirName = "./" + localDirName;
44-
}
39+
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); // receive if this directory exists
40+
Boolean fExists = (Boolean) ois.readObject();
4541

46-
Socket sock = new Socket(serverIP, PORT_NUMBER);
47-
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
48-
oos.writeObject(localDirName);
42+
File baseDir = new File(fullDirName); // skipping the base dir as it already should be set up on the server
43+
String[] children = baseDir.list();
44+
for (int i=0; i<children.length; i++) {
45+
visitAllDirsAndFiles(new File(baseDir, children[i]), sock);
46+
}
47+
48+
oos.writeChars(new String(DONE));
4949
oos.flush();
5050

51-
/* //check to see if file exists on server
52-
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
53-
String fExists = (String) ois.readObject();
54-
55-
if(fExists.equals("1")){
56-
System.out.print("Receiving file: ");
57-
byte[] mybytearray = new byte[1024];
58-
InputStream is = sock.getInputStream();
59-
FileOutputStream fos = new FileOutputStream(fileName);
60-
BufferedOutputStream bos = new BufferedOutputStream(fos);
61-
int bytesRead = 0;
62-
int current = 0;
63-
while((bytesRead = is.read(mybytearray, 0, mybytearray.length)) != -1){
64-
bos.write(mybytearray, 0, bytesRead);
65-
current = current + bytesRead;
66-
System.out.print(".");
51+
if(fExists)
52+
updateFromServer(sock, fullDirName);
53+
54+
oos.close();
55+
ois.close();
56+
sock.close();
57+
}
58+
59+
// Process all files and directories under dir
60+
public static void visitAllDirsAndFiles(File dir, Socket sock) throws Exception{
61+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); // send pathName fileName LastModified to server
62+
oos.writeChars(new String(dir.getName()));
63+
oos.flush();
64+
65+
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); // wait for server to say ok
66+
ois.readBoolean();
67+
68+
oos.writeBoolean(new Boolean(dir.isDirectory()));
69+
oos.flush();
70+
71+
ois.readBoolean();
72+
73+
oos.writeChars(new String(dir.getAbsolutePath().substring((dir.getAbsolutePath().indexOf(fullDirName) + fullDirName.length()))));
74+
oos.flush();
75+
76+
ois.readBoolean();
77+
78+
if(!dir.isDirectory()) {
79+
oos.writeLong(new Long(dir.lastModified()));
80+
oos.flush();
81+
82+
// receive SEND or RECEIVE
83+
Boolean updateToServer = (Boolean) ois.readObject(); //if true update server, else update from server
84+
85+
if (updateToServer) { // send file to server
86+
sendFile(dir, sock);
87+
88+
Boolean fileWasOk = (Boolean) ois.readObject(); // make sure server got the file
89+
90+
oos.close();
91+
ois.close();
92+
93+
if (!fileWasOk) // if the server replys true then continue, else repeat
94+
visitAllDirsAndFiles(dir, sock);
95+
96+
} else { // update file from server.
97+
dir.delete(); // first delete the current file
98+
99+
oos.writeBoolean(new Boolean(true)); // send "Ready"
100+
oos.flush();
101+
102+
receiveFile(dir, sock);
103+
104+
oos.writeBoolean(new Boolean(true)); // send back ok
105+
oos.flush();
106+
107+
Long updateLastModified = (Long) ois.readObject(); // update the last modified date for this file from the server
108+
dir.setLastModified(updateLastModified);
109+
110+
oos.close();
111+
ois.close();
67112
}
68-
System.out.println();
69-
System.out.println("Done!");
70-
oos.close();
71-
ois.close();
72-
bos.close();
73-
}else{
74-
System.out.println("Server replied that " + fileName + " does not exist, closing connection.");
75113
}
76-
*/ oos.close();
77-
sock.close();
114+
115+
/* debug */ System.out.println("Name: " + dir.getName() + " Dir: " + dir.isDirectory() + " Modified: " + dir.lastModified() + " Size: " + dir.length());
116+
117+
if (dir.isDirectory()) {
118+
String[] children = dir.list();
119+
for (int i=0; i<children.length; i++) {
120+
visitAllDirsAndFiles(new File(dir, children[i]), sock);
121+
}
122+
}
123+
}
124+
125+
public static void sendFile(File dir, Socket sock) throws Exception {
126+
byte[] mybytearray = new byte[(int) dir.length()];
127+
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(dir));
128+
bis.read(mybytearray, 0, mybytearray.length);
129+
OutputStream os = sock.getOutputStream();
130+
os.write(mybytearray, 0, mybytearray.length);
131+
os.flush();
132+
133+
os.close();
134+
bis.close();
135+
}
136+
137+
public static void receiveFile(File dir, Socket sock) throws Exception {
138+
byte[] mybytearray = new byte[1024]; // receive file from server
139+
InputStream is = sock.getInputStream();
140+
FileOutputStream fos = new FileOutputStream(dir);
141+
BufferedOutputStream bos = new BufferedOutputStream(fos);
142+
int bytesRead = 0;
143+
int current = 0;
144+
while((bytesRead = is.read(mybytearray, 0, mybytearray.length)) != -1){
145+
bos.write(mybytearray, 0, bytesRead);
146+
current = current + bytesRead;
147+
}
148+
149+
bos.close();
150+
fos.close();
151+
is.close();
152+
}
153+
154+
public static void updateFromServer(Socket sock, String fullDirName) throws Exception {
155+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); // send fileName LastModified to server
156+
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); // receive SEND or RECEIVE
157+
158+
File f = new File(fullDirName);
159+
160+
// need to implement this part
161+
78162
}
79163
}

src/net/quaa/jfs/javaFileSync.java

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
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 {
1012
if(args.length > 0) {
1113
if (args[0].equalsIgnoreCase("-s")) {
1214
server();
15+
} if (args[0].equals("-t")) {
16+
System.out.println("You have found the secret testing function!");
17+
fullPathName = args[1];
18+
localName = cleanUpInput(args[1]);
19+
testing(new File(localName), args[1]);
1320
} else if (args[0].equalsIgnoreCase("-c") && args.length > 1 && args.length < 4) {
14-
client(args[2], args[1]);
21+
localName = cleanUpInput(args[1]);
22+
client(localName, args[2], args[1]);
1523
} else {
1624
System.out.println("Invalid entry. Useage: java javaFileSync [-s] [-c [server IP] [dir to sync]]");
1725
}
@@ -28,15 +36,57 @@ public static void server() throws Exception {
2836
s.startServer();
2937
}
3038

31-
public static void client(String dirName, String serverIP) throws Exception {
32-
Client c = new Client(dirName, serverIP, PORT_NUMBER);
39+
public static void client(String dirName, String fullDirName, String serverIP) throws Exception {
40+
Client c = new Client(dirName, fullDirName, serverIP, PORT_NUMBER);
3341
c.runClient();
3442
}
3543

44+
public static void testing(File dirName, String fullPathName) throws Exception {
45+
visitAllDirsAndFiles(dirName);
46+
47+
}
48+
49+
public static String cleanUpInput(String userInput) throws Exception {
50+
51+
File f = new File(userInput);
52+
53+
if(!f.isDirectory()) {
54+
System.out.println("Please input a directory instead of a file!");
55+
Thread.sleep(10);
56+
System.exit(0);
57+
}
58+
59+
String localDirName = userInput; //cleaning up the users input
60+
if(userInput.contains("/")){
61+
if(userInput.lastIndexOf("/") != (userInput.length() - 1)) {
62+
localDirName = userInput.substring(userInput.lastIndexOf("/"));
63+
} else {
64+
localDirName = userInput.substring(0, (userInput.length() - 1));
65+
if(localDirName.contains("/"))
66+
localDirName = localDirName.substring(localDirName.lastIndexOf("/"));
67+
}
68+
}
69+
70+
if(localDirName.equals(".")){
71+
System.out.println("Please input a dir name instead of ./ or .");
72+
Thread.sleep(10);
73+
System.exit(0);
74+
}
75+
76+
if(!localDirName.startsWith("./")){ //still cleaning up their input
77+
if(localDirName.startsWith("/"))
78+
localDirName = "." + localDirName;
79+
else
80+
localDirName = "./" + localDirName;
81+
}
82+
return localDirName;
83+
}
84+
3685
// Process all files and directories under dir
3786
public static void visitAllDirsAndFiles(File dir) {
3887
//process(dir);
3988
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());
4090
if (dir.isDirectory()) {
4191
String[] children = dir.list();
4292
for (int i=0; i<children.length; i++) {
@@ -50,7 +100,6 @@ public static void visitAllDirs(File dir) {
50100
if (dir.isDirectory()) {
51101
//process(dir);
52102
System.out.println("Name: " + dir.getName() + " Modified: " + dir.lastModified() + " Size: " + dir.length());
53-
54103
String[] children = dir.list();
55104
for (int i=0; i<children.length; i++) {
56105
visitAllDirs(new File(dir, children[i]));

0 commit comments

Comments
 (0)