Skip to content

Commit 8c6dba9

Browse files
committed
complete
1 parent 0ddba2a commit 8c6dba9

File tree

1 file changed

+31
-37
lines changed
  • Distributed Programming/miniproject_4/src/main/java/edu/coursera/distributed

1 file changed

+31
-37
lines changed

Distributed Programming/miniproject_4/src/main/java/edu/coursera/distributed/FileServer.java

+31-37
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void run(final ServerSocket socket, final PCDPFilesystem fs,
3737
while (true) {
3838

3939
// TODO 1) Use socket.accept to get a Socket object
40-
final Socket s = socket.accept();
40+
Socket s = socket.accept();
4141

4242
/*
4343
* TODO 2) Now that we have a new Socket object, handle the parsing
@@ -75,44 +75,38 @@ public void run(final ServerSocket socket, final PCDPFilesystem fs,
7575
* If you wish to do so, you are free to re-use code from
7676
* MiniProject 2 to help with completing this MiniProject.
7777
*/
78-
final ExecutorService executor = Executors.newFixedThreadPool(ncores);
79-
executor.submit(
80-
()->{
81-
try {
82-
handle(s, fs);
83-
} catch (final IOException io) {
84-
throw new RuntimeException(io);
85-
}
86-
}
87-
);
88-
}
89-
}
78+
Thread thread = new Thread(() -> {
79+
try {
80+
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
81+
82+
String line = br.readLine();
83+
assert line != null;
84+
assert line.startsWith("GET");
85+
final String path = line.split(" ")[1];
9086

91-
private void handle(final Socket s,final PCDPFilesystem fs)throws IOException {
92-
final InputStream inputStream = s.getInputStream();
93-
final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
94-
final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
95-
final String line = bufferedReader.readLine();
96-
assert line != null;
97-
assert line.startsWith("GET");
98-
final String path = line.split(" ")[1];
87+
PCDPPath pcdpPath = new PCDPPath(path);
88+
String fileContent = fs.readFile(pcdpPath);
89+
90+
OutputStream out = s.getOutputStream();
91+
PrintWriter pw = new PrintWriter(out);
92+
93+
if (fileContent != null) {
94+
pw.write("HTTP/1.0 200 OK\r\n");
95+
pw.write("Server: FileServer\r\n");
96+
pw.write("\r\n");
97+
pw.write(fileContent + "\r\n");
98+
} else {
99+
pw.write("HTTP/1.0 404 Not Found\r\n");
100+
pw.write("Server: FileServer\r\n");
101+
pw.write("\r\n");
102+
}
99103

100-
final PCDPPath pcdpPath = new PCDPPath(path);
101-
final String contents = fs.readFile(pcdpPath);
102-
final OutputStream outputStream = s.getOutputStream();
103-
final PrintWriter printWriter = new PrintWriter(outputStream);
104-
if (contents == null) {
105-
printWriter.write("HTTP/1.0 404 Not Found\r\n");
106-
printWriter.write("Server: FileServer\r\n");
107-
printWriter.write("\r\n");
108-
} else {
109-
printWriter.write("HTTP/1.0 200 OK\r\n");
110-
printWriter.write("Server: FileServer\r\n");
111-
printWriter.write("\r\n");
112-
printWriter.write(String.format("%s\r\n", contents));
104+
pw.close();
105+
} catch (IOException e) {
106+
throw new RuntimeException(e);
107+
}
108+
});
109+
thread.start();
113110
}
114-
printWriter.close();
115-
outputStream.close();
116-
s.close();
117111
}
118112
}

0 commit comments

Comments
 (0)