Skip to content

Commit 38cd924

Browse files
author
quaa
committed
Added first section of client
1 parent fc3ca12 commit 38cd924

File tree

3 files changed

+171
-74
lines changed

3 files changed

+171
-74
lines changed

pseudocode.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,29 @@ end
3131
2 way talking:
3232

3333
CLIENT: dirName
34-
Server: EXISTS or NOEXISTS
34+
Server: exists = TRUE or FALSE
3535

3636
if directory
3737
CLIENT: dirName DiR
38-
SERVER: OK
38+
SERVER: true
3939
else
4040
CLIENT: fileName lastModified
41-
SERVER: OK or NO
42-
if OK
41+
SERVER: TRUE or FALSE
42+
if TRUE
4343
CLIENT: SEND FILE
44-
SERVER: OK
45-
else
46-
CLIENT: OK
44+
SERVER: TRUE
45+
else if FALSE
46+
CLIENT: TRUE
4747
SERVER: SEND FILE
48-
CLIENT: OK
48+
CLIENT: TRUE
4949
SERVER: SEND LAST MODIFIED
5050
end
5151
end
5252

5353
CLIENT: DONE
5454

55-
if EXISTS
55+
if EXISTS = TRUE
56+
SERVER: fName
5657
if directory
5758
SERVER: dirName DiR
5859
CLIENT: OK or NO
@@ -91,3 +92,4 @@ if EXISTS
9192
end
9293
end
9394

95+
SERVER: DONE

src/net/quaa/jfs/Client.java

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

3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
35
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.FileOutputStream;
8+
import java.io.InputStream;
49
import java.io.ObjectInputStream;
510
import java.io.ObjectOutputStream;
11+
import java.io.OutputStream;
612
import java.net.Socket;
713

814
public class Client {
915
private String dirName;
1016
private String serverIP;
17+
private String fullDirName;
1118
private int PORT_NUMBER;
19+
private static final String DONE = "DONE";
1220

13-
public Client(String dirName, String serverIP, int port) {
21+
public Client(String dirName, String fullDirName, String serverIP, int port) {
1422
this.dirName = dirName;
1523
this.serverIP = serverIP;
24+
this.fullDirName = fullDirName;
1625
PORT_NUMBER = port;
1726

1827
System.out.println("Client Selected!");
@@ -21,87 +30,128 @@ public Client(String dirName, String serverIP, int port) {
2130
}
2231

2332
public void runClient() throws Exception {
24-
String localDirName = dirName; //cleaning up the users input
25-
if(dirName.contains("/")){
26-
if(dirName.lastIndexOf("/") != (dirName.length() - 1)) {
27-
localDirName = dirName.substring(dirName.lastIndexOf("/"));
28-
} else {
29-
localDirName = dirName.substring(0, (dirName.length() - 1));
30-
if(localDirName.contains("/"))
31-
localDirName = localDirName.substring(localDirName.lastIndexOf("/"));
32-
}
33-
}
34-
35-
if(localDirName.equals(".")){
36-
System.out.println("Please input a dir name instead of ./ or .");
37-
Thread.sleep(10);
38-
System.exit(0);
39-
}
40-
41-
if(!localDirName.startsWith("./")){ //still cleaning up their input
42-
if(localDirName.startsWith("/"))
43-
localDirName = "." + localDirName;
44-
else
45-
localDirName = "./" + localDirName;
46-
}
4733

4834
Socket sock = new Socket(serverIP, PORT_NUMBER);
4935
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); // send directory name to server
50-
oos.writeObject(localDirName);
36+
oos.writeObject(dirName);
5137
oos.flush();
5238

5339
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); // receive if this directory exists
54-
String fExists = (String) ois.readObject();
40+
Boolean fExists = (Boolean) ois.readObject();
5541

56-
visitAllDirsAndFiles(new File(dirName), sock);
57-
58-
if(fExists.equals("EXISTS")) {
59-
updateFromServer(sock);
60-
}
42+
visitAllDirsAndFiles(new File(fullDirName), sock);
6143

62-
/* //check to see if file exists on server
63-
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
64-
String fExists = (String) ois.readObject();
65-
66-
if(fExists.equals("1")){
67-
System.out.print("Receiving file: ");
68-
byte[] mybytearray = new byte[1024];
69-
InputStream is = sock.getInputStream();
70-
FileOutputStream fos = new FileOutputStream(fileName);
71-
BufferedOutputStream bos = new BufferedOutputStream(fos);
72-
int bytesRead = 0;
73-
int current = 0;
74-
while((bytesRead = is.read(mybytearray, 0, mybytearray.length)) != -1){
75-
bos.write(mybytearray, 0, bytesRead);
76-
current = current + bytesRead;
77-
System.out.print(".");
78-
}
79-
System.out.println();
80-
System.out.println("Done!");
81-
oos.close();
82-
ois.close();
83-
bos.close();
84-
}else{
85-
System.out.println("Server replied that " + fileName + " does not exist, closing connection.");
86-
}
87-
*/ oos.close();
44+
oos.writeObject(DONE);
45+
oos.flush();
46+
47+
if(fExists)
48+
updateFromServer(sock, fullDirName);
49+
50+
oos.close();
8851
ois.close();
8952
sock.close();
9053
}
9154

9255
// Process all files and directories under dir
93-
public static void visitAllDirsAndFiles(File dir, Socket sock) {
94-
// DO WORK HERE
95-
System.out.println("Name: " + dir.getName() + " Modified: " + dir.lastModified() + " Size: " + dir.length());
96-
if (dir.isDirectory()) {
56+
public static void visitAllDirsAndFiles(File dir, Socket sock) throws Exception{
57+
if(!dir.isDirectory()) {
58+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); // send fileName LastModified to server
59+
oos.writeObject(dir.getName() + " " + dir.lastModified());
60+
oos.flush();
61+
62+
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); // receive SEND or RECEIVE
63+
Boolean updateToServer = (Boolean) ois.readObject(); //if true update server, else update from server
64+
65+
if (updateToServer) { // send file to server
66+
sendFile(dir, sock);
67+
68+
Boolean fileWasOk = (Boolean) ois.readObject(); // make sure server got the file
69+
70+
oos.close();
71+
ois.close();
72+
73+
if (!fileWasOk) // if the server replys true then continue, else repeat
74+
visitAllDirsAndFiles(dir, sock);
75+
76+
} else { // update file from server.
77+
dir.delete(); // first delete the current file
78+
79+
oos.writeObject(true); // send "Ready"
80+
oos.flush();
81+
82+
receiveFile(dir, sock);
83+
84+
oos.writeObject(true); // send back ok
85+
oos.flush();
86+
87+
Long updateLastModified = (Long) ois.readObject(); // update the last modified date for this file from the server
88+
dir.setLastModified(updateLastModified);
89+
90+
oos.close();
91+
ois.close();
92+
}
93+
94+
} else {
95+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); // send directory name to server
96+
oos.writeObject(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);
107+
}
108+
109+
/* debug */ System.out.println("Name: " + dir.getName() + " Dir: " + dir.isDirectory() + " Modified: " + dir.lastModified() + " Size: " + dir.length());
110+
111+
if (dir.isDirectory()) {
97112
String[] children = dir.list();
98113
for (int i=0; i<children.length; i++) {
99114
visitAllDirsAndFiles(new File(dir, children[i]), sock);
100115
}
101116
}
102117
}
103118

104-
public static void updateFromServer(Socket sock) {
119+
public static void sendFile(File dir, Socket sock) throws Exception {
120+
byte[] mybytearray = new byte[(int) dir.length()];
121+
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(dir));
122+
bis.read(mybytearray, 0, mybytearray.length);
123+
OutputStream os = sock.getOutputStream();
124+
os.write(mybytearray, 0, mybytearray.length);
125+
os.flush();
126+
127+
os.close();
128+
bis.close();
129+
}
130+
131+
public static void receiveFile(File dir, Socket sock) throws Exception {
132+
byte[] mybytearray = new byte[1024]; // receive file from server
133+
InputStream is = sock.getInputStream();
134+
FileOutputStream fos = new FileOutputStream(dir);
135+
BufferedOutputStream bos = new BufferedOutputStream(fos);
136+
int bytesRead = 0;
137+
int current = 0;
138+
while((bytesRead = is.read(mybytearray, 0, mybytearray.length)) != -1){
139+
bos.write(mybytearray, 0, bytesRead);
140+
current = current + bytesRead;
141+
}
142+
143+
bos.close();
144+
fos.close();
145+
is.close();
146+
}
147+
148+
public static void updateFromServer(Socket sock, String fullDirName) throws Exception {
149+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); // send fileName LastModified to server
150+
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); // receive SEND or RECEIVE
151+
152+
File f = new File(fullDirName);
105153

154+
// need to implement this part
155+
106156
}
107157
}

src/net/quaa/jfs/javaFileSync.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ public static void main(String[] args) {
1010
if(args.length > 0) {
1111
if (args[0].equalsIgnoreCase("-s")) {
1212
server();
13+
} if (args[0].equals("-t")) {
14+
System.out.println("You have found the secret testing function!");
15+
String localName = cleanUpInput(args[1]);
16+
testing(new File(localName));
1317
} else if (args[0].equalsIgnoreCase("-c") && args.length > 1 && args.length < 4) {
14-
client(args[2], args[1]);
18+
String localName = cleanUpInput(args[1]);
19+
client(localName, args[2], args[1]);
1520
} else {
1621
System.out.println("Invalid entry. Useage: java javaFileSync [-s] [-c [server IP] [dir to sync]]");
1722
}
@@ -28,11 +33,51 @@ public static void server() throws Exception {
2833
s.startServer();
2934
}
3035

31-
public static void client(String dirName, String serverIP) throws Exception {
32-
Client c = new Client(dirName, serverIP, PORT_NUMBER);
36+
public static void client(String dirName, String fullDirName, String serverIP) throws Exception {
37+
Client c = new Client(dirName, fullDirName, serverIP, PORT_NUMBER);
3338
c.runClient();
3439
}
3540

41+
public static void testing(File dirName) throws Exception {
42+
visitAllDirsAndFiles(dirName);
43+
}
44+
45+
public static String cleanUpInput(String userInput) throws Exception {
46+
47+
File f = new File(userInput);
48+
49+
if(!f.isDirectory()) {
50+
System.out.println("Please input a directory instead of a file!");
51+
Thread.sleep(10);
52+
System.exit(0);
53+
}
54+
55+
String localDirName = userInput; //cleaning up the users input
56+
if(userInput.contains("/")){
57+
if(userInput.lastIndexOf("/") != (userInput.length() - 1)) {
58+
localDirName = userInput.substring(userInput.lastIndexOf("/"));
59+
} else {
60+
localDirName = userInput.substring(0, (userInput.length() - 1));
61+
if(localDirName.contains("/"))
62+
localDirName = localDirName.substring(localDirName.lastIndexOf("/"));
63+
}
64+
}
65+
66+
if(localDirName.equals(".")){
67+
System.out.println("Please input a dir name instead of ./ or .");
68+
Thread.sleep(10);
69+
System.exit(0);
70+
}
71+
72+
if(!localDirName.startsWith("./")){ //still cleaning up their input
73+
if(localDirName.startsWith("/"))
74+
localDirName = "." + localDirName;
75+
else
76+
localDirName = "./" + localDirName;
77+
}
78+
return localDirName;
79+
}
80+
3681
// Process all files and directories under dir
3782
public static void visitAllDirsAndFiles(File dir) {
3883
//process(dir);

0 commit comments

Comments
 (0)