11package net .quaa .jfs ;
22
3+ import java .io .BufferedInputStream ;
4+ import java .io .BufferedOutputStream ;
35import java .io .File ;
6+ import java .io .FileInputStream ;
7+ import java .io .FileOutputStream ;
8+ import java .io .InputStream ;
49import java .io .ObjectInputStream ;
510import java .io .ObjectOutputStream ;
11+ import java .io .OutputStream ;
612import java .net .Socket ;
713
814public class Client {
915 private String dirName ;
1016 private String serverIP ;
17+ private String fullDirName ;
1118 private int PORT_NUMBER ;
19+ private static final String DONE = "DONE" ;
1220
13- public Client (String dirName , String serverIP , int port ) {
21+ public Client (String dirName , String fullDirName , String serverIP , int port ) {
1422 this .dirName = dirName ;
1523 this .serverIP = serverIP ;
24+ this .fullDirName = fullDirName ;
1625 PORT_NUMBER = port ;
1726
1827 System .out .println ("Client Selected!" );
@@ -21,87 +30,128 @@ public Client(String dirName, String serverIP, int port) {
2130 }
2231
2332 public void runClient () throws Exception {
24- String localDirName = dirName ; //cleaning up the users input
25- if (dirName .contains ("/" )){
26- if (dirName .lastIndexOf ("/" ) != (dirName .length () - 1 )) {
27- localDirName = dirName .substring (dirName .lastIndexOf ("/" ));
28- } else {
29- localDirName = dirName .substring (0 , (dirName .length () - 1 ));
30- if (localDirName .contains ("/" ))
31- localDirName = localDirName .substring (localDirName .lastIndexOf ("/" ));
32- }
33- }
34-
35- if (localDirName .equals ("." )){
36- System .out .println ("Please input a dir name instead of ./ or ." );
37- Thread .sleep (10 );
38- System .exit (0 );
39- }
40-
41- if (!localDirName .startsWith ("./" )){ //still cleaning up their input
42- if (localDirName .startsWith ("/" ))
43- localDirName = "." + localDirName ;
44- else
45- localDirName = "./" + localDirName ;
46- }
4733
4834 Socket sock = new Socket (serverIP , PORT_NUMBER );
4935 ObjectOutputStream oos = new ObjectOutputStream (sock .getOutputStream ()); // send directory name to server
50- oos .writeObject (localDirName );
36+ oos .writeObject (dirName );
5137 oos .flush ();
5238
5339 ObjectInputStream ois = new ObjectInputStream (sock .getInputStream ()); // receive if this directory exists
54- String fExists = (String ) ois .readObject ();
40+ Boolean fExists = (Boolean ) ois .readObject ();
5541
56- visitAllDirsAndFiles (new File (dirName ), sock );
57-
58- if (fExists .equals ("EXISTS" )) {
59- updateFromServer (sock );
60- }
42+ visitAllDirsAndFiles (new File (fullDirName ), sock );
6143
62- /* //check to see if file exists on server
63- ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
64- String fExists = (String) ois.readObject();
65-
66- if(fExists.equals("1")){
67- System.out.print("Receiving file: ");
68- byte[] mybytearray = new byte[1024];
69- InputStream is = sock.getInputStream();
70- FileOutputStream fos = new FileOutputStream(fileName);
71- BufferedOutputStream bos = new BufferedOutputStream(fos);
72- int bytesRead = 0;
73- int current = 0;
74- while((bytesRead = is.read(mybytearray, 0, mybytearray.length)) != -1){
75- bos.write(mybytearray, 0, bytesRead);
76- current = current + bytesRead;
77- System.out.print(".");
78- }
79- System.out.println();
80- System.out.println("Done!");
81- oos.close();
82- ois.close();
83- bos.close();
84- }else{
85- System.out.println("Server replied that " + fileName + " does not exist, closing connection.");
86- }
87- */ oos .close ();
44+ oos .writeObject (DONE );
45+ oos .flush ();
46+
47+ if (fExists )
48+ updateFromServer (sock , fullDirName );
49+
50+ oos .close ();
8851 ois .close ();
8952 sock .close ();
9053 }
9154
9255 // Process all files and directories under dir
93- public static void visitAllDirsAndFiles (File dir , Socket sock ) {
94- // DO WORK HERE
95- System .out .println ("Name: " + dir .getName () + " Modified: " + dir .lastModified () + " Size: " + dir .length ());
96- if (dir .isDirectory ()) {
56+ public static void visitAllDirsAndFiles (File dir , Socket sock ) throws Exception {
57+ if (!dir .isDirectory ()) {
58+ ObjectOutputStream oos = new ObjectOutputStream (sock .getOutputStream ()); // send fileName LastModified to server
59+ oos .writeObject (dir .getName () + " " + dir .lastModified ());
60+ oos .flush ();
61+
62+ ObjectInputStream ois = new ObjectInputStream (sock .getInputStream ()); // receive SEND or RECEIVE
63+ Boolean updateToServer = (Boolean ) ois .readObject (); //if true update server, else update from server
64+
65+ if (updateToServer ) { // send file to server
66+ sendFile (dir , sock );
67+
68+ Boolean fileWasOk = (Boolean ) ois .readObject (); // make sure server got the file
69+
70+ oos .close ();
71+ ois .close ();
72+
73+ if (!fileWasOk ) // if the server replys true then continue, else repeat
74+ visitAllDirsAndFiles (dir , sock );
75+
76+ } else { // update file from server.
77+ dir .delete (); // first delete the current file
78+
79+ oos .writeObject (true ); // send "Ready"
80+ oos .flush ();
81+
82+ receiveFile (dir , sock );
83+
84+ oos .writeObject (true ); // send back ok
85+ oos .flush ();
86+
87+ Long updateLastModified = (Long ) ois .readObject (); // update the last modified date for this file from the server
88+ dir .setLastModified (updateLastModified );
89+
90+ oos .close ();
91+ ois .close ();
92+ }
93+
94+ } else {
95+ ObjectOutputStream oos = new ObjectOutputStream (sock .getOutputStream ()); // send directory name to server
96+ oos .writeObject (dir );
97+ oos .flush ();
98+
99+ ObjectInputStream ois = new ObjectInputStream (sock .getInputStream ()); // receive if this directory exists
100+ Boolean ok = (Boolean ) ois .readObject ();
101+
102+ oos .close ();
103+ ois .close ();
104+
105+ if (!ok ) // if did not receive an ok back from the server, re-run.
106+ visitAllDirsAndFiles (dir , sock );
107+ }
108+
109+ /* debug */ System .out .println ("Name: " + dir .getName () + " Dir: " + dir .isDirectory () + " Modified: " + dir .lastModified () + " Size: " + dir .length ());
110+
111+ if (dir .isDirectory ()) {
97112 String [] children = dir .list ();
98113 for (int i =0 ; i <children .length ; i ++) {
99114 visitAllDirsAndFiles (new File (dir , children [i ]), sock );
100115 }
101116 }
102117 }
103118
104- public static void updateFromServer (Socket sock ) {
119+ public static void sendFile (File dir , Socket sock ) throws Exception {
120+ byte [] mybytearray = new byte [(int ) dir .length ()];
121+ BufferedInputStream bis = new BufferedInputStream (new FileInputStream (dir ));
122+ bis .read (mybytearray , 0 , mybytearray .length );
123+ OutputStream os = sock .getOutputStream ();
124+ os .write (mybytearray , 0 , mybytearray .length );
125+ os .flush ();
126+
127+ os .close ();
128+ bis .close ();
129+ }
130+
131+ public static void receiveFile (File dir , Socket sock ) throws Exception {
132+ byte [] mybytearray = new byte [1024 ]; // receive file from server
133+ InputStream is = sock .getInputStream ();
134+ FileOutputStream fos = new FileOutputStream (dir );
135+ BufferedOutputStream bos = new BufferedOutputStream (fos );
136+ int bytesRead = 0 ;
137+ int current = 0 ;
138+ while ((bytesRead = is .read (mybytearray , 0 , mybytearray .length )) != -1 ){
139+ bos .write (mybytearray , 0 , bytesRead );
140+ current = current + bytesRead ;
141+ }
142+
143+ bos .close ();
144+ fos .close ();
145+ is .close ();
146+ }
147+
148+ public static void updateFromServer (Socket sock , String fullDirName ) throws Exception {
149+ ObjectOutputStream oos = new ObjectOutputStream (sock .getOutputStream ()); // send fileName LastModified to server
150+ ObjectInputStream ois = new ObjectInputStream (sock .getInputStream ()); // receive SEND or RECEIVE
151+
152+ File f = new File (fullDirName );
105153
154+ // need to implement this part
155+
106156 }
107157}
0 commit comments