Skip to content

Commit 96fb444

Browse files
author
quaa
committed
Fixed one way comms, need to implement 2 way to fix the things in TODO
1 parent 9a47052 commit 96fb444

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

TODO.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
PROBLEMS
2+
3+
Server does not have file:
4+
Client will receive from server
5+
6+
Client does not have file:
7+
Nothing happens.

src/net/quaa/jfs/Client.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,17 @@ public static void visitAllDirsAndFiles(File dir) throws Exception{
7979
oos.flush();
8080

8181
// receive SEND or RECEIVE
82-
Boolean updateToServer = (Boolean) ois.readObject(); //if true update server, else update from server
82+
Integer updateToServer = (Integer) ois.readObject(); //if true update server, else update from server
8383

84-
if (updateToServer) { // send file to server
84+
if (updateToServer == 1) { // send file to server
8585
sendFile(dir);
8686

8787
ois.readObject(); // make sure server got the file
8888

89-
} else { // update file from server.
89+
} else if (updateToServer == 0) { // update file from server.
9090
dir.delete(); // first delete the current file
9191

92-
oos.writeObject(new Boolean(true)); // send "Ready"
92+
oos.writeObject(new Boolean(true)); // send "Ready"
9393
oos.flush();
9494

9595
receiveFile(dir);
@@ -99,14 +99,9 @@ public static void visitAllDirsAndFiles(File dir) throws Exception{
9999

100100
Long updateLastModified = (Long) ois.readObject(); // update the last modified date for this file from the server
101101
dir.setLastModified(updateLastModified);
102-
103-
oos.close();
104-
ois.close();
105-
}
102+
103+
} // no need to check if update to server == 2 because we do nothing here
106104
}
107-
108-
/* debug */ System.out.println("Name: " + dir.getName() + " Dir: " + dir.isDirectory() + " Modified: " + dir.lastModified() + " Size: " + dir.length());
109-
110105
if (dir.isDirectory()) {
111106
String[] children = dir.list();
112107
for (int i=0; i<children.length; i++) {
@@ -125,7 +120,6 @@ public static void sendFile(File dir) throws Exception {
125120
{
126121
oos.write(buff,0,bytesRead);
127122
}
128-
oos.flush();
129123
in.close();
130124
// after sending a file you need to close the socket and reopen one.
131125
oos.flush();
@@ -135,6 +129,8 @@ public static void sendFile(File dir) throws Exception {
135129
sock = new Socket(serverIP, PORT_NUMBER);
136130
oos = new ObjectOutputStream(sock.getOutputStream());
137131
ois = new ObjectInputStream(sock.getInputStream());
132+
133+
printDebug(true, dir);
138134
}
139135

140136
public static void receiveFile(File dir) throws Exception {
@@ -154,6 +150,8 @@ public static void receiveFile(File dir) throws Exception {
154150
sock = new Socket(serverIP, PORT_NUMBER);
155151
ois = new ObjectInputStream(sock.getInputStream());
156152
oos = new ObjectOutputStream(sock.getOutputStream());
153+
154+
printDebug(false, dir);
157155
}
158156

159157
public static void updateFromServer(Socket sock, String fullDirName) throws Exception {
@@ -165,4 +163,12 @@ public static void updateFromServer(Socket sock, String fullDirName) throws Exce
165163
// need to implement this part
166164

167165
}
166+
167+
public static void printDebug(Boolean sending, File dir){
168+
if(sending)
169+
System.out.println("SEND=Name: " + dir.getName() + " Dir: " + dir.isDirectory() + " Modified: " + dir.lastModified() + " Size: " + dir.length());
170+
else
171+
System.out.println("RECV=Name: " + dir.getName() + " Dir: " + dir.isDirectory() + " Modified: " + dir.lastModified() + " Size: " + dir.length());
172+
173+
}
168174
}

src/net/quaa/jfs/Server.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,29 @@ public void startServer() throws Exception {
8282
Long lastModified = (Long) ois.readObject();
8383

8484
File newFile = new File(baseDir, path);
85-
Boolean updateFromClient = !newFile.exists() && (newFile.lastModified() <= lastModified);
86-
if(updateFromClient) { // If true receive file from client
85+
Integer updateFromClient = 2; // default = do nothing
86+
87+
if (!newFile.exists() || (newFile.lastModified() <= lastModified))
88+
updateFromClient = 1;
89+
else
90+
updateFromClient = 0;
91+
92+
if(newFile.exists() && newFile.lastModified() == lastModified)
93+
updateFromClient = 2;
94+
95+
if(updateFromClient == 1) { // If true receive file from client
8796
newFile.delete();
8897

89-
oos.writeObject(new Boolean(updateFromClient));
98+
oos.writeObject(new Integer(updateFromClient));
9099
oos.flush();
91100

92101
receiveFile(newFile);
93102

94103
newFile.setLastModified(lastModified);
95104

96105
oos.writeObject(new Boolean(true));
97-
} else { // if false send file to client
98-
oos.writeObject(new Boolean(updateFromClient));
106+
} else if (updateFromClient == 0) { // if false send file to client
107+
oos.writeObject(new Integer(updateFromClient));
99108
oos.flush();
100109

101110
ois.readObject();
@@ -106,6 +115,9 @@ public void startServer() throws Exception {
106115

107116
oos.writeObject(new Long(newFile.lastModified()));
108117
oos.flush();
118+
} else { //updateFromClient == 2 // do nothing
119+
oos.writeObject(new Integer(updateFromClient));
120+
oos.flush();
109121
}
110122
}
111123
}
@@ -130,7 +142,6 @@ public static void sendFile(File dir) throws Exception {
130142
{
131143
oos.write(buff,0,bytesRead);
132144
}
133-
oos.flush();
134145
in.close();
135146
// after sending a file you need to close the socket and reopen one.
136147
oos.flush();

0 commit comments

Comments
 (0)