11package net .quaa .jfs ;
22
3+ import java .io .BufferedReader ;
34import java .io .File ;
45import java .io .FileInputStream ;
56import java .io .FileOutputStream ;
7+ import java .io .IOException ;
68import java .io .InputStream ;
9+ import java .io .InputStreamReader ;
710import java .io .ObjectInputStream ;
811import java .io .ObjectOutputStream ;
912import java .net .Socket ;
@@ -43,17 +46,17 @@ public void runClient() throws Exception {
4346 File baseDir = new File (fullDirName ); // skipping the base dir as it already should be set up on the server
4447 String [] children = baseDir .list ();
4548
46- for (int i =0 ; i < children .length ; i ++) {
47- visitAllDirsAndFiles (new File (baseDir , children [i ]));
48- }
49- Vector <String > vecDONE = new Vector <String >();
50- vecDONE .add (DONE );
49+ for (int i =0 ; i < children .length ; i ++) {
50+ visitAllDirsAndFiles (new File (baseDir , children [i ]));
51+ }
52+ Vector <String > vecDONE = new Vector <String >();
53+ vecDONE .add (DONE );
5154 oos .writeObject (vecDONE );
5255 oos .flush ();
5356 reinitConn ();
54-
57+
5558 if (fExists )
56- updateFromServer (sock , fullDirName );
59+ updateFromServer ();
5760
5861 oos .close ();
5962 ois .close ();
@@ -62,7 +65,7 @@ public void runClient() throws Exception {
6265
6366 // Process all files and directories under dir
6467 private static void visitAllDirsAndFiles (File dir ) throws Exception {
65- Vector <String > vec = new Vector <String >();
68+ Vector <String > vec = new Vector <String >();
6669 vec .add (dir .getName ());
6770 vec .add (dir .getAbsolutePath ().substring ((dir .getAbsolutePath ().indexOf (fullDirName ) + fullDirName .length ())));
6871
@@ -102,11 +105,11 @@ private static void visitAllDirsAndFiles(File dir) throws Exception{
102105 } // no need to check if update to server == 2 because we do nothing here
103106 }
104107 if (dir .isDirectory ()) {
105- String [] children = dir .list ();
106- for (int i =0 ; i <children .length ; i ++) {
107- visitAllDirsAndFiles (new File (dir , children [i ]));
108- }
109- }
108+ String [] children = dir .list ();
109+ for (int i =0 ; i <children .length ; i ++) {
110+ visitAllDirsAndFiles (new File (dir , children [i ]));
111+ }
112+ }
110113 }
111114
112115 private static void sendFile (File dir ) throws Exception {
@@ -141,11 +144,88 @@ private static void receiveFile(File dir) throws Exception {
141144 printDebug (false , dir );
142145 }
143146
144- private static void updateFromServer (Socket sock , String fullDirName ) throws Exception {
145- //oos = new ObjectOutputStream(sock.getOutputStream()); // send fileName LastModified to server
146- //ois = new ObjectInputStream(sock.getInputStream()); // receive SEND or RECEIVE
147- //File f = new File(fullDirName);
148- // need to implement this part
147+ private static void updateFromServer () throws Exception {
148+ Boolean isDone = false ;
149+ Boolean nAll = false ;
150+ while (!isDone ) {
151+ String path = (String ) ois .readObject ();
152+
153+ if (path .equals (DONE )) {
154+ isDone = true ;
155+ break ;
156+ }
157+
158+ oos .writeObject (new Boolean (true ));
159+ oos .flush ();
160+
161+ File newFile = new File (fullDirName + path );
162+
163+ Boolean isDirectory = (Boolean ) ois .readObject ();
164+
165+ oos .writeObject (new Boolean (newFile .exists ()));
166+ oos .flush ();
167+ if (!newFile .exists ()) {
168+ ois .readObject ();
169+ String userInput = null ;
170+ if (!nAll ) {
171+ if (isDirectory ) {
172+ System .out .println ("CONFLICT with " + fullDirName + path + "! The directory exists on the server but not this client." );
173+ System .out .println ("Would you like to delete the server's directory (if no we would create the directory on this client)?" );
174+ } else {
175+ System .out .println ("CONFLICT with " + fullDirName + path + "! The file exists on the server but not this client." );
176+ System .out .println ("Would you like to delete the server's file (if no we would create the file on this client)?" );
177+ }
178+ System .out .println ("Type 'y' for yes, 'n' for no, 'a' for no to all, 'd' to do nothing" );
179+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
180+ try {
181+ userInput = br .readLine ();
182+ } catch (IOException ioe ) {
183+ System .out .println ("You did not input a correct value, will do nothing." );
184+ }
185+ } else // if n to all then just set input to n!
186+ userInput = "n" ;
187+ if (userInput .equalsIgnoreCase ("a" ) || userInput .equalsIgnoreCase ("'a'" )) {
188+ nAll = true ;
189+ userInput = "n" ;
190+ }
191+ if (userInput .equalsIgnoreCase ("y" ) || userInput .equalsIgnoreCase ("'y'" )) {
192+ if (isDirectory ) {
193+ oos .writeObject (new Boolean (true )); // reply with yes to delete the server's copy
194+ oos .flush ();
195+ } else {
196+ oos .writeObject (new Integer (1 ));
197+ oos .flush ();
198+ }
199+ } else if (userInput .equalsIgnoreCase ("n" ) || userInput .equalsIgnoreCase ("'n'" )) {
200+ if (isDirectory ) {
201+ newFile .mkdir ();
202+ oos .writeObject (new Boolean (false ));
203+ oos .flush ();
204+ } else {
205+ oos .writeObject (new Integer (0 ));
206+ oos .flush ();
207+ receiveFile (newFile );
208+
209+ oos .writeObject (new Boolean (true ));
210+ oos .flush ();
211+
212+ Long lastModified = (Long ) ois .readObject ();
213+ newFile .setLastModified (lastModified );
214+
215+ oos .writeObject (new Boolean (true ));
216+ oos .flush ();
217+ }
218+ } else {
219+ if (isDirectory ) {
220+ oos .writeObject (new Boolean (false ));
221+ oos .flush ();
222+ } else {
223+ oos .writeObject (new Integer (2 ));
224+ oos .flush ();
225+ }
226+ }
227+ }
228+ }
149229 }
150230
151231 private static void printDebug (Boolean sending , File dir ){
0 commit comments