Skip to content

Commit 8a9ac3c

Browse files
author
quaa
committed
Server receiving comms from client?
1 parent 0b27cdb commit 8a9ac3c

File tree

2 files changed

+205
-6
lines changed

2 files changed

+205
-6
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: isDirectory = FALSE
47+
SERVER: true
48+
CLIENT: PATH
49+
SERVER: true
50+
CLIENT: lastModified
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/Server.java

Lines changed: 126 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
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;
10+
import java.io.ObjectOutputStream;
11+
import java.io.OutputStream;
512
import java.net.ServerSocket;
613
import java.net.Socket;
714

815
public class Server {
916
private int PORT_NUMBER;
17+
private static final String DONE = "DONE";
1018

1119
public Server(int port) {
1220
PORT_NUMBER = port;
@@ -25,9 +33,90 @@ public void startServer() throws Exception {
2533

2634
File fBaseDir = new File(baseDir);
2735

28-
if(fBaseDir.exists() && fBaseDir.isDirectory()){
36+
Boolean baseDirExists = fBaseDir.exists();
37+
38+
if(!baseDirExists)
39+
fBaseDir.mkdir();
40+
41+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
42+
oos.writeBoolean(new Boolean(baseDirExists));
43+
oos.flush();
44+
45+
Boolean isClientDone = false;
46+
47+
while (!isClientDone) {
48+
String fName = (String) ois.readObject();
49+
50+
if(fName.equals(DONE)) { // check if we are done
51+
isClientDone = true;
52+
break;
53+
}
54+
55+
oos.writeBoolean(new Boolean(true));
56+
oos.flush();
57+
58+
Boolean isDirectory = ois.readBoolean();
59+
60+
if(isDirectory) {
61+
oos.writeBoolean(new Boolean(true));
62+
oos.flush();
63+
64+
String path = (String) ois.readObject();
65+
66+
File newDir = new File(baseDir, path);
67+
if (!newDir.exists())
68+
newDir.mkdir();
69+
70+
oos.writeBoolean(new Boolean(true));
71+
oos.flush();
72+
} else {
73+
oos.writeBoolean(new Boolean(true));
74+
oos.flush();
75+
76+
String path = (String) ois.readObject();
77+
78+
oos.writeBoolean(new Boolean(true));
79+
oos.flush();
80+
81+
Long lastModified = ois.readLong();
82+
83+
File newFile = new File(baseDir, path);
84+
Boolean updateFromClient = !newFile.exists() && (newFile.lastModified() <= lastModified);
85+
86+
if(updateFromClient) { // If true receive file from client
87+
newFile.delete();
88+
89+
oos.writeBoolean(new Boolean(updateFromClient));
90+
oos.flush();
91+
92+
receiveFile(newFile, sock);
93+
94+
newFile.setLastModified(lastModified);
95+
96+
oos.writeBoolean(new Boolean(true));
97+
} else { // if false send file to client
98+
oos.writeBoolean(new Boolean(updateFromClient));
99+
oos.flush();
100+
101+
ois.readBoolean();
102+
103+
sendFile(newFile, sock);
104+
105+
ois.readBoolean();
106+
107+
oos.writeLong(new Long(newFile.lastModified()));
108+
oos.flush();
109+
}
110+
}
111+
}
112+
113+
if(baseDirExists){
114+
115+
}
116+
117+
/* if(baseDirExists)
29118
System.out.println(baseDir + " is a directory and exists!");
30-
/* System.out.print("Sending file '" + fileName + "' to: " + sock.getInetAddress() + "...");
119+
System.out.print("Sending file '" + fileName + "' to: " + sock.getInetAddress() + "...");
31120
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
32121
oos.writeObject("1");
33122
byte[] mybytearray = new byte[(int) f.length()];
@@ -36,15 +125,46 @@ public void startServer() throws Exception {
36125
OutputStream os = sock.getOutputStream();
37126
os.write(mybytearray, 0, mybytearray.length);
38127
os.flush();
39-
System.out.println(" DONE!");
40-
*/ }else{
128+
System.out.println(" DONE!");
129+
}else{
41130
System.out.println(baseDir + " Exists?: " + fBaseDir.exists() + " IsDirectory?: " + fBaseDir.isDirectory());
42-
/* System.out.println(sock.getInetAddress() + " requested " + fileName + ", but it does not exist!");
131+
System.out.println(sock.getInetAddress() + " requested " + fileName + ", but it does not exist!");
43132
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
44133
oos.writeObject("0");
45134
oos.flush();
46-
*/ }
135+
} */
136+
oos.close();
137+
ois.close();
47138
sock.close();
48139
}
49140
}
141+
142+
public static void sendFile(File dir, Socket sock) throws Exception {
143+
byte[] mybytearray = new byte[(int) dir.length()];
144+
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(dir));
145+
bis.read(mybytearray, 0, mybytearray.length);
146+
OutputStream os = sock.getOutputStream();
147+
os.write(mybytearray, 0, mybytearray.length);
148+
os.flush();
149+
150+
os.close();
151+
bis.close();
152+
}
153+
154+
public static void receiveFile(File dir, Socket sock) throws Exception {
155+
byte[] mybytearray = new byte[1024]; // receive file from server
156+
InputStream is = sock.getInputStream();
157+
FileOutputStream fos = new FileOutputStream(dir);
158+
BufferedOutputStream bos = new BufferedOutputStream(fos);
159+
int bytesRead = 0;
160+
int current = 0;
161+
while((bytesRead = is.read(mybytearray, 0, mybytearray.length)) != -1){
162+
bos.write(mybytearray, 0, bytesRead);
163+
current = current + bytesRead;
164+
}
165+
166+
bos.close();
167+
fos.close();
168+
is.close();
169+
}
50170
}

0 commit comments

Comments
 (0)