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 ;
10+ import java .io .ObjectOutputStream ;
11+ import java .io .OutputStream ;
512import java .net .ServerSocket ;
613import java .net .Socket ;
714
815public class Server {
916 private int PORT_NUMBER ;
17+ private static final String DONE = "DONE" ;
1018
1119 public Server (int port ) {
1220 PORT_NUMBER = port ;
@@ -25,9 +33,90 @@ public void startServer() throws Exception {
2533
2634 File fBaseDir = new File (baseDir );
2735
28- if (fBaseDir .exists () && fBaseDir .isDirectory ()){
36+ Boolean baseDirExists = fBaseDir .exists ();
37+
38+ if (!baseDirExists )
39+ fBaseDir .mkdir ();
40+
41+ ObjectOutputStream oos = new ObjectOutputStream (sock .getOutputStream ());
42+ oos .writeBoolean (new Boolean (baseDirExists ));
43+ oos .flush ();
44+
45+ Boolean isClientDone = false ;
46+
47+ while (!isClientDone ) {
48+ String fName = (String ) ois .readObject ();
49+
50+ if (fName .equals (DONE )) { // check if we are done
51+ isClientDone = true ;
52+ break ;
53+ }
54+
55+ oos .writeBoolean (new Boolean (true ));
56+ oos .flush ();
57+
58+ Boolean isDirectory = ois .readBoolean ();
59+
60+ if (isDirectory ) {
61+ oos .writeBoolean (new Boolean (true ));
62+ oos .flush ();
63+
64+ String path = (String ) ois .readObject ();
65+
66+ File newDir = new File (baseDir , path );
67+ if (!newDir .exists ())
68+ newDir .mkdir ();
69+
70+ oos .writeBoolean (new Boolean (true ));
71+ oos .flush ();
72+ } else {
73+ oos .writeBoolean (new Boolean (true ));
74+ oos .flush ();
75+
76+ String path = (String ) ois .readObject ();
77+
78+ oos .writeBoolean (new Boolean (true ));
79+ oos .flush ();
80+
81+ Long lastModified = ois .readLong ();
82+
83+ File newFile = new File (baseDir , path );
84+ Boolean updateFromClient = !newFile .exists () && (newFile .lastModified () <= lastModified );
85+
86+ if (updateFromClient ) { // If true receive file from client
87+ newFile .delete ();
88+
89+ oos .writeBoolean (new Boolean (updateFromClient ));
90+ oos .flush ();
91+
92+ receiveFile (newFile , sock );
93+
94+ newFile .setLastModified (lastModified );
95+
96+ oos .writeBoolean (new Boolean (true ));
97+ } else { // if false send file to client
98+ oos .writeBoolean (new Boolean (updateFromClient ));
99+ oos .flush ();
100+
101+ ois .readBoolean ();
102+
103+ sendFile (newFile , sock );
104+
105+ ois .readBoolean ();
106+
107+ oos .writeLong (new Long (newFile .lastModified ()));
108+ oos .flush ();
109+ }
110+ }
111+ }
112+
113+ if (baseDirExists ){
114+
115+ }
116+
117+ /* if(baseDirExists)
29118 System.out.println(baseDir + " is a directory and exists!");
30- /* System.out.print("Sending file '" + fileName + "' to: " + sock.getInetAddress() + "...");
119+ System.out.print("Sending file '" + fileName + "' to: " + sock.getInetAddress() + "...");
31120 ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
32121 oos.writeObject("1");
33122 byte[] mybytearray = new byte[(int) f.length()];
@@ -36,15 +125,46 @@ public void startServer() throws Exception {
36125 OutputStream os = sock.getOutputStream();
37126 os.write(mybytearray, 0, mybytearray.length);
38127 os.flush();
39- System.out.println(" DONE!");
40- */ }else {
128+ System.out.println(" DONE!");
129+ }else{
41130 System.out.println(baseDir + " Exists?: " + fBaseDir.exists() + " IsDirectory?: " + fBaseDir.isDirectory());
42- /* System.out.println(sock.getInetAddress() + " requested " + fileName + ", but it does not exist!");
131+ System.out.println(sock.getInetAddress() + " requested " + fileName + ", but it does not exist!");
43132 ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
44133 oos.writeObject("0");
45134 oos.flush();
46- */ }
135+ } */
136+ oos .close ();
137+ ois .close ();
47138 sock .close ();
48139 }
49140 }
141+
142+ public static void sendFile (File dir , Socket sock ) throws Exception {
143+ byte [] mybytearray = new byte [(int ) dir .length ()];
144+ BufferedInputStream bis = new BufferedInputStream (new FileInputStream (dir ));
145+ bis .read (mybytearray , 0 , mybytearray .length );
146+ OutputStream os = sock .getOutputStream ();
147+ os .write (mybytearray , 0 , mybytearray .length );
148+ os .flush ();
149+
150+ os .close ();
151+ bis .close ();
152+ }
153+
154+ public static void receiveFile (File dir , Socket sock ) throws Exception {
155+ byte [] mybytearray = new byte [1024 ]; // receive file from server
156+ InputStream is = sock .getInputStream ();
157+ FileOutputStream fos = new FileOutputStream (dir );
158+ BufferedOutputStream bos = new BufferedOutputStream (fos );
159+ int bytesRead = 0 ;
160+ int current = 0 ;
161+ while ((bytesRead = is .read (mybytearray , 0 , mybytearray .length )) != -1 ){
162+ bos .write (mybytearray , 0 , bytesRead );
163+ current = current + bytesRead ;
164+ }
165+
166+ bos .close ();
167+ fos .close ();
168+ is .close ();
169+ }
50170}
0 commit comments