-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer_delay.java
More file actions
201 lines (167 loc) · 5.15 KB
/
Server_delay.java
File metadata and controls
201 lines (167 loc) · 5.15 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import java.net.*;
import java.io.*;
import java.nio.file.Files;
import java.util.*;
public class Server {
private static int sPort;
public final int CHUNK_SIZE = 1024*100;
public static final int PEERS = 3;
public Server() {}
// Split the file
private ArrayList<File> SplitFile(String filepath) throws IOException {
File fileObj = new File(filepath);
byte[] buffer = new byte[CHUNK_SIZE];
ArrayList<File> partFiles = new ArrayList<File>();
int part = 0;
System.out.println("File size: " + fileObj.length());
try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(fileObj))) {
String name = fileObj.getName();
int tmp = 0;
while ((tmp = bis.read(buffer)) > 0) {
File newFile = new File(fileObj.getParent(), name
+ String.format("%d", part++));
try (FileOutputStream out = new FileOutputStream(newFile)) {
out.write(buffer, 0, tmp);
partFiles.add(newFile);
}
}
}
return partFiles;
}
// Read config file
private void ReadConfig() {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("resources/config.properties");
//input = new FileInputStream("../resources/config.properties");
// load the properties file
prop.load(input);
// Get the server port number on which the server will be listening on.
sPort = Integer.parseInt(prop.getProperty("server"));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception {
/*System.out.print("Enter the name of the file: ");
Scanner scanner = new Scanner(System.in);
String filename = scanner.next();
scanner.close();*/
String filename = "dat.jpg";
String filepath = "resources/" + filename;
Server S = new Server();
// Chop the file into 100KB chunks
ArrayList<File> partFiles = S.SplitFile(filepath);
int totalChunks = partFiles.size();
System.out.println("Total chunks = " + totalChunks);
S.ReadConfig();
System.out.println("The server is running.");
ServerSocket listener = new ServerSocket(sPort);
int clientNum = 0;
try {
while(true) {
clientNum++;
new Handler(listener.accept(), clientNum, partFiles, totalChunks, filename).start();
System.out.println("Client " + clientNum + " is connected!");
}
} finally {
listener.close();
}
}
private static class Handler extends Thread {
private Socket connection;
private ObjectInputStream in; // Stream read from the socket
private ObjectOutputStream out; // Stream write to the socket
private int no; // Client number
private ArrayList<File> partFiles; // Stores the file chunks objects
private int totalChunks;
private static int chunkNum = 0;
private static String filename = "";
private boolean flag;
private int chunksPerPeer;
public Handler(Socket connection, int no, ArrayList<File> partFiles, int totalChunks, String fname) {
this.connection = connection;
this.no = no;
this.partFiles = partFiles;
this.totalChunks = totalChunks;
filename = fname;
flag = true;
}
@Override
public void run() {
try{
//initialize Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
int msgCount = 0;
int limit = (int) java.lang.Math.floor(partFiles.size()/PEERS);
try{
while(chunkNum < partFiles.size() && msgCount < limit)
{
sendMessage(partFiles.get(chunkNum), totalChunks, chunkNum, no, filename);
chunkNum++;
msgCount++;
Thread.sleep(1000);
}
while ((no == PEERS - 1) && (chunkNum < partFiles.size())) {
sendMessage(partFiles.get(chunkNum), totalChunks, chunkNum, no, filename);
chunkNum++;
Thread.sleep(1000);
msgCount = 0;
}
}
catch(Exception e) {
System.out.println(e);
}
}
catch(IOException ioException){
System.out.println("Disconnect with Client " + no);
}
finally{
//Close connections
try{
in.close();
out.close();
connection.close();
}
catch(IOException ioException){
System.out.println("Disconnect with Client " + no);
}
}
}
//send a message to the output stream
public void sendMessage(File msg, int totalChunks, int chunkNum, int clientNum, String filename) {
try{
// Filename
if (flag) {
out.writeObject(filename);
out.flush();
out.writeObject(totalChunks + "");
out.flush();
flag = false;
}
// Current chunk number
out.writeObject(chunkNum + "");
out.flush();
byte [] mybytearray = Files.readAllBytes(msg.toPath());
out.writeObject(mybytearray);
out.flush();
System.out.println("Sent chunk " + chunkNum + " to Client " + clientNum);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
}