@@ -33,90 +33,106 @@ public void startServer() throws Exception {
3333 ois = new ObjectInputStream (sock .getInputStream ());
3434
3535 baseDir = (String ) ois .readObject ();
36- File fBaseDir = new File (baseDir );
37- Boolean baseDirExists = fBaseDir .exists ();
3836
39- System . out . println ( "New client connected! IP: " + sock .getInetAddress (). toString () + " Directory: " + baseDir );
37+ oos = new ObjectOutputStream ( sock .getOutputStream () );
4038
41- if (!baseDirExists )
42- fBaseDir .mkdir ();
39+ System .out .println ("New client connected! IP: " + sock .getInetAddress ().toString () + " Directory: " + baseDir );
4340
44- oos = new ObjectOutputStream (sock .getOutputStream ());
45- oos .writeObject (new Boolean (baseDirExists ));
46- oos .flush ();
41+ if (baseDir .equalsIgnoreCase ("-ls" )) {
42+ Vector <String > directories = new Vector <String >();
43+ File serverBase = new File ("./" );
44+ String [] children = serverBase .list ();
45+ for (int i =0 ; i <children .length ; i ++) {
46+ File newF = new File (serverBase , children [i ]);
47+ if (newF .isDirectory ())
48+ directories .add (newF .getName ());
49+ }
50+ oos .writeObject (directories );
51+ oos .flush ();
52+ } else {
53+ File fBaseDir = new File (baseDir );
54+ Boolean baseDirExists = fBaseDir .exists ();
55+
56+ if (!baseDirExists )
57+ fBaseDir .mkdir ();
58+
59+ oos .writeObject (new Boolean (baseDirExists ));
60+ oos .flush ();
4761
48- Boolean isClientDone = false ;
62+ Boolean isClientDone = false ;
4963
50- while (!isClientDone ) {
51- Vector <String > vec = (Vector <String >) ois .readObject ();
52- reinitConn ();
64+ while (!isClientDone ) {
65+ Vector <String > vec = (Vector <String >) ois .readObject ();
66+ reinitConn ();
5367
54- if (vec .elementAt (0 ).equals (DONE )) { // check if we are done
55- isClientDone = true ; // if so break out
56- break ;
57- }
68+ if (vec .elementAt (0 ).equals (DONE )) { // check if we are done
69+ isClientDone = true ; // if so break out
70+ break ;
71+ }
5872
59- if (vec .size () == 2 ) { // if the size is 2 then this is a directory
60- File newDir = new File (baseDir , vec .elementAt (1 ));
61- if (!newDir .exists ())
62- newDir .mkdir ();
73+ if (vec .size () == 2 ) { // if the size is 2 then this is a directory
74+ File newDir = new File (baseDir , vec .elementAt (1 ));
75+ if (!newDir .exists ())
76+ newDir .mkdir ();
6377
64- oos .writeObject (new Boolean (true )); // tell client that we are ready
65- oos .flush ();
66- } else {
67- File newFile = new File (baseDir , vec .elementAt (1 ));
68- Integer updateFromClient = 2 ; // default = do nothing
78+ oos .writeObject (new Boolean (true )); // tell client that we are ready
79+ oos .flush ();
80+ } else {
81+ File newFile = new File (baseDir , vec .elementAt (1 ));
82+ Integer updateFromClient = 2 ; // default = do nothing
6983
70- Long lastModified = new Long (vec .elementAt (2 ));
71- if (!newFile .exists () || (newFile .lastModified () <= lastModified ))
72- updateFromClient = 1 ;
73- else
74- updateFromClient = 0 ;
84+ Long lastModified = new Long (vec .elementAt (2 ));
85+ if (!newFile .exists () || (newFile .lastModified () <= lastModified ))
86+ updateFromClient = 1 ;
87+ else
88+ updateFromClient = 0 ;
7589
76- if (newFile .exists () && newFile .lastModified () == lastModified )
77- updateFromClient = 2 ;
90+ if (newFile .exists () && newFile .lastModified () == lastModified )
91+ updateFromClient = 2 ;
7892
79- if (updateFromClient == 1 ) { // If true receive file from client
80- newFile .delete ();
93+ if (updateFromClient == 1 ) { // If true receive file from client
94+ newFile .delete ();
8195
82- oos .writeObject (new Integer (updateFromClient ));
83- oos .flush ();
96+ oos .writeObject (new Integer (updateFromClient ));
97+ oos .flush ();
8498
85- receiveFile (newFile );
99+ receiveFile (newFile );
86100
87- newFile .setLastModified (lastModified );
101+ newFile .setLastModified (lastModified );
88102
89- oos .writeObject (new Boolean (true ));
90- } else if (updateFromClient == 0 ) { // if false send file to client
91- oos .writeObject (new Integer (updateFromClient ));
92- oos .flush ();
103+ oos .writeObject (new Boolean (true ));
104+ } else if (updateFromClient == 0 ) { // if false send file to client
105+ oos .writeObject (new Integer (updateFromClient ));
106+ oos .flush ();
93107
94- ois .readObject ();
108+ ois .readObject ();
95109
96- sendFile (newFile );
110+ sendFile (newFile );
97111
98- ois .readObject ();
112+ ois .readObject ();
99113
100- oos .writeObject (new Long (newFile .lastModified ()));
101- oos .flush ();
102- } else { //updateFromClient == 2 // do nothing
103- oos .writeObject (new Integer (updateFromClient ));
104- oos .flush ();
114+ oos .writeObject (new Long (newFile .lastModified ()));
115+ oos .flush ();
116+ } else { //updateFromClient == 2 // do nothing
117+ oos .writeObject (new Integer (updateFromClient ));
118+ oos .flush ();
119+ }
105120 }
106121 }
122+
123+ File baseDirFile = new File (baseDir );
124+ if (baseDirExists )
125+ visitAllDirsAndFiles (baseDirFile );
126+
127+ oos .writeObject (new String (DONE ));
128+ oos .flush ();
129+ System .out .print ("Finished sync..." );
130+
107131 }
108-
109- File baseDirFile = new File (baseDir );
110- if (baseDirExists )
111- visitAllDirsAndFiles (baseDirFile );
112-
113- oos .writeObject (new String (DONE ));
114- oos .flush ();
115-
116132 oos .close ();
117133 ois .close ();
118134 sock .close ();
119- System .out .println ("Finished sync " );
135+ System .out .println ("Client disconnected. " );
120136 }
121137 }
122138
@@ -165,12 +181,12 @@ private static void visitAllDirsAndFiles(File dir) throws Exception {
165181 }
166182 }
167183
168- if (dir .isDirectory ()) {
169- String [] children = dir .list ();
170- for (int i = 0 ; i < children .length ; i ++) {
171- visitAllDirsAndFiles (new File (dir , children [i ]));
172- }
173- }
184+ if (dir .isDirectory ()) {
185+ String [] children = dir .list ();
186+ for (int i = 0 ; i < children .length ; i ++) {
187+ visitAllDirsAndFiles (new File (dir , children [i ]));
188+ }
189+ }
174190 }
175191
176192 private static void sendFile (File dir ) throws Exception {
@@ -209,14 +225,14 @@ private static void reinitConn() throws Exception {
209225 oos = new ObjectOutputStream (sock .getOutputStream ());
210226 ois = new ObjectInputStream (sock .getInputStream ());
211227 }
212-
228+
213229 private static void deleteAllDirsAndFiles (File dir ) {
214- if (dir .isDirectory ()) {
215- String [] children = dir .list ();
216- for (int i =0 ; i <children .length ; i ++) {
217- deleteAllDirsAndFiles (new File (dir , children [i ]));
218- }
219- }
220- dir .delete ();
230+ if (dir .isDirectory ()) {
231+ String [] children = dir .list ();
232+ for (int i =0 ; i <children .length ; i ++) {
233+ deleteAllDirsAndFiles (new File (dir , children [i ]));
234+ }
235+ }
236+ dir .delete ();
221237 }
222238}
0 commit comments