forked from bttnns/JavaFileSync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
170 lines (130 loc) · 4.67 KB
/
Server.java
File metadata and controls
170 lines (130 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package net.quaa.jfs;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private int PORT_NUMBER;
private static final String DONE = "DONE";
public Server(int port) {
PORT_NUMBER = port;
}
public void startServer() throws Exception {
System.out.println("Starting File Sync Server!");
ServerSocket servsock = new ServerSocket(PORT_NUMBER);
while (true) {
Socket sock = servsock.accept();
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
String baseDir = (String) ois.readObject();
File fBaseDir = new File(baseDir);
Boolean baseDirExists = fBaseDir.exists();
if(!baseDirExists)
fBaseDir.mkdir();
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
oos.writeBoolean(new Boolean(baseDirExists));
oos.flush();
Boolean isClientDone = false;
while (!isClientDone) {
String fName = (String) ois.readObject();
if(fName.equals(DONE)) { // check if we are done
isClientDone = true;
break;
}
oos.writeBoolean(new Boolean(true));
oos.flush();
Boolean isDirectory = ois.readBoolean();
if(isDirectory) {
oos.writeBoolean(new Boolean(true));
oos.flush();
String path = (String) ois.readObject();
File newDir = new File(baseDir, path);
if (!newDir.exists())
newDir.mkdir();
oos.writeBoolean(new Boolean(true));
oos.flush();
} else {
oos.writeBoolean(new Boolean(true));
oos.flush();
String path = (String) ois.readObject();
oos.writeBoolean(new Boolean(true));
oos.flush();
Long lastModified = ois.readLong();
File newFile = new File(baseDir, path);
Boolean updateFromClient = !newFile.exists() && (newFile.lastModified() <= lastModified);
if(updateFromClient) { // If true receive file from client
newFile.delete();
oos.writeBoolean(new Boolean(updateFromClient));
oos.flush();
receiveFile(newFile, sock);
newFile.setLastModified(lastModified);
oos.writeBoolean(new Boolean(true));
} else { // if false send file to client
oos.writeBoolean(new Boolean(updateFromClient));
oos.flush();
ois.readBoolean();
sendFile(newFile, sock);
ois.readBoolean();
oos.writeLong(new Long(newFile.lastModified()));
oos.flush();
}
}
}
if(baseDirExists){
}
/* if(baseDirExists)
System.out.println(baseDir + " is a directory and exists!");
System.out.print("Sending file '" + fileName + "' to: " + sock.getInetAddress() + "...");
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
oos.writeObject("1");
byte[] mybytearray = new byte[(int) f.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
System.out.println(" DONE!");
}else{
System.out.println(baseDir + " Exists?: " + fBaseDir.exists() + " IsDirectory?: " + fBaseDir.isDirectory());
System.out.println(sock.getInetAddress() + " requested " + fileName + ", but it does not exist!");
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
oos.writeObject("0");
oos.flush();
} */
oos.close();
ois.close();
sock.close();
}
}
public static void sendFile(File dir, Socket sock) throws Exception {
byte[] mybytearray = new byte[(int) dir.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(dir));
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
os.close();
bis.close();
}
public static void receiveFile(File dir, Socket sock) throws Exception {
byte[] mybytearray = new byte[1024]; // receive file from server
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream(dir);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = 0;
int current = 0;
while((bytesRead = is.read(mybytearray, 0, mybytearray.length)) != -1){
bos.write(mybytearray, 0, bytesRead);
current = current + bytesRead;
}
bos.close();
fos.close();
is.close();
}
}