11package net .quaa .jfs ;
22
3+ import java .io .BufferedInputStream ;
4+ import java .io .BufferedOutputStream ;
5+ import java .io .File ;
6+ import java .io .FileInputStream ;
7+ import java .io .FileOutputStream ;
8+ import java .io .InputStream ;
9+ import java .io .ObjectInputStream ;
310import java .io .ObjectOutputStream ;
11+ import java .io .OutputStream ;
412import java .net .Socket ;
513
614public class Client {
7- private String dirName ;
8- private String serverIP ;
9- private int PORT_NUMBER ;
15+ private static String dirName ;
16+ private static String serverIP ;
17+ private static String fullDirName ;
18+ private static int PORT_NUMBER ;
19+ private static final String DONE = "DONE" ;
1020
11- public Client (String dirName , String serverIP , int port ) {
12- this .dirName = dirName ;
13- this .serverIP = serverIP ;
21+ public Client (String dirName , String fullDirName , String serverIP , int port ) {
22+ Client .dirName = dirName ;
23+ Client .serverIP = serverIP ;
24+ Client .fullDirName = fullDirName ;
1425 PORT_NUMBER = port ;
1526
1627 System .out .println ("Client Selected!" );
@@ -19,61 +30,134 @@ public Client(String dirName, String serverIP, int port) {
1930 }
2031
2132 public void runClient () throws Exception {
22- String localDirName = dirName ; //cleaning up the users input
23- if (dirName .contains ("/" )){
24- if (dirName .lastIndexOf ("/" ) != (dirName .length () - 1 )) {
25- localDirName = dirName .substring (dirName .lastIndexOf ("/" ));
26- } else {
27- localDirName = dirName .substring (0 , (dirName .length () - 1 ));
28- if (localDirName .contains ("/" ))
29- localDirName = localDirName .substring (localDirName .lastIndexOf ("/" ));
30- }
31- }
3233
33- if (localDirName .equals ("." )){
34- System .out .println ("Please input a dir name instead of ./ or ." );
35- Thread .sleep (10 );
36- System .exit (0 );
37- }
34+ Socket sock = new Socket (serverIP , PORT_NUMBER );
35+ ObjectOutputStream oos = new ObjectOutputStream (sock .getOutputStream ()); // send directory name to server
36+ oos .writeChars (new String (dirName ));
37+ oos .flush ();
3838
39- if (!localDirName .startsWith ("./" )){ //still cleaning up their input
40- if (localDirName .startsWith ("/" ))
41- localDirName = "." + localDirName ;
42- else
43- localDirName = "./" + localDirName ;
44- }
39+ ObjectInputStream ois = new ObjectInputStream (sock .getInputStream ()); // receive if this directory exists
40+ Boolean fExists = (Boolean ) ois .readObject ();
4541
46- Socket sock = new Socket (serverIP , PORT_NUMBER );
47- ObjectOutputStream oos = new ObjectOutputStream (sock .getOutputStream ());
48- oos .writeObject (localDirName );
42+ File baseDir = new File (fullDirName ); // skipping the base dir as it already should be set up on the server
43+ String [] children = baseDir .list ();
44+ for (int i =0 ; i <children .length ; i ++) {
45+ visitAllDirsAndFiles (new File (baseDir , children [i ]), sock );
46+ }
47+
48+ oos .writeChars (new String (DONE ));
4949 oos .flush ();
5050
51- /* //check to see if file exists on server
52- ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
53- String fExists = (String) ois.readObject();
54-
55- if(fExists.equals("1")){
56- System.out.print("Receiving file: ");
57- byte[] mybytearray = new byte[1024];
58- InputStream is = sock.getInputStream();
59- FileOutputStream fos = new FileOutputStream(fileName);
60- BufferedOutputStream bos = new BufferedOutputStream(fos);
61- int bytesRead = 0;
62- int current = 0;
63- while((bytesRead = is.read(mybytearray, 0, mybytearray.length)) != -1){
64- bos.write(mybytearray, 0, bytesRead);
65- current = current + bytesRead;
66- System.out.print(".");
51+ if (fExists )
52+ updateFromServer (sock , fullDirName );
53+
54+ oos .close ();
55+ ois .close ();
56+ sock .close ();
57+ }
58+
59+ // Process all files and directories under dir
60+ public static void visitAllDirsAndFiles (File dir , Socket sock ) throws Exception {
61+ ObjectOutputStream oos = new ObjectOutputStream (sock .getOutputStream ()); // send pathName fileName LastModified to server
62+ oos .writeChars (new String (dir .getName ()));
63+ oos .flush ();
64+
65+ ObjectInputStream ois = new ObjectInputStream (sock .getInputStream ()); // wait for server to say ok
66+ ois .readBoolean ();
67+
68+ oos .writeBoolean (new Boolean (dir .isDirectory ()));
69+ oos .flush ();
70+
71+ ois .readBoolean ();
72+
73+ oos .writeChars (new String (dir .getAbsolutePath ().substring ((dir .getAbsolutePath ().indexOf (fullDirName ) + fullDirName .length ()))));
74+ oos .flush ();
75+
76+ ois .readBoolean ();
77+
78+ if (!dir .isDirectory ()) {
79+ oos .writeLong (new Long (dir .lastModified ()));
80+ oos .flush ();
81+
82+ // receive SEND or RECEIVE
83+ Boolean updateToServer = (Boolean ) ois .readObject (); //if true update server, else update from server
84+
85+ if (updateToServer ) { // send file to server
86+ sendFile (dir , sock );
87+
88+ Boolean fileWasOk = (Boolean ) ois .readObject (); // make sure server got the file
89+
90+ oos .close ();
91+ ois .close ();
92+
93+ if (!fileWasOk ) // if the server replys true then continue, else repeat
94+ visitAllDirsAndFiles (dir , sock );
95+
96+ } else { // update file from server.
97+ dir .delete (); // first delete the current file
98+
99+ oos .writeBoolean (new Boolean (true )); // send "Ready"
100+ oos .flush ();
101+
102+ receiveFile (dir , sock );
103+
104+ oos .writeBoolean (new Boolean (true )); // send back ok
105+ oos .flush ();
106+
107+ Long updateLastModified = (Long ) ois .readObject (); // update the last modified date for this file from the server
108+ dir .setLastModified (updateLastModified );
109+
110+ oos .close ();
111+ ois .close ();
67112 }
68- System.out.println();
69- System.out.println("Done!");
70- oos.close();
71- ois.close();
72- bos.close();
73- }else{
74- System.out.println("Server replied that " + fileName + " does not exist, closing connection.");
75113 }
76- */ oos .close ();
77- sock .close ();
114+
115+ /* debug */ System .out .println ("Name: " + dir .getName () + " Dir: " + dir .isDirectory () + " Modified: " + dir .lastModified () + " Size: " + dir .length ());
116+
117+ if (dir .isDirectory ()) {
118+ String [] children = dir .list ();
119+ for (int i =0 ; i <children .length ; i ++) {
120+ visitAllDirsAndFiles (new File (dir , children [i ]), sock );
121+ }
122+ }
123+ }
124+
125+ public static void sendFile (File dir , Socket sock ) throws Exception {
126+ byte [] mybytearray = new byte [(int ) dir .length ()];
127+ BufferedInputStream bis = new BufferedInputStream (new FileInputStream (dir ));
128+ bis .read (mybytearray , 0 , mybytearray .length );
129+ OutputStream os = sock .getOutputStream ();
130+ os .write (mybytearray , 0 , mybytearray .length );
131+ os .flush ();
132+
133+ os .close ();
134+ bis .close ();
135+ }
136+
137+ public static void receiveFile (File dir , Socket sock ) throws Exception {
138+ byte [] mybytearray = new byte [1024 ]; // receive file from server
139+ InputStream is = sock .getInputStream ();
140+ FileOutputStream fos = new FileOutputStream (dir );
141+ BufferedOutputStream bos = new BufferedOutputStream (fos );
142+ int bytesRead = 0 ;
143+ int current = 0 ;
144+ while ((bytesRead = is .read (mybytearray , 0 , mybytearray .length )) != -1 ){
145+ bos .write (mybytearray , 0 , bytesRead );
146+ current = current + bytesRead ;
147+ }
148+
149+ bos .close ();
150+ fos .close ();
151+ is .close ();
152+ }
153+
154+ public static void updateFromServer (Socket sock , String fullDirName ) throws Exception {
155+ ObjectOutputStream oos = new ObjectOutputStream (sock .getOutputStream ()); // send fileName LastModified to server
156+ ObjectInputStream ois = new ObjectInputStream (sock .getInputStream ()); // receive SEND or RECEIVE
157+
158+ File f = new File (fullDirName );
159+
160+ // need to implement this part
161+
78162 }
79163}
0 commit comments