@@ -37,7 +37,7 @@ public void run(final ServerSocket socket, final PCDPFilesystem fs,
37
37
while (true ) {
38
38
39
39
// TODO 1) Use socket.accept to get a Socket object
40
- final Socket s = socket .accept ();
40
+ Socket s = socket .accept ();
41
41
42
42
/*
43
43
* 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,
75
75
* If you wish to do so, you are free to re-use code from
76
76
* MiniProject 2 to help with completing this MiniProject.
77
77
*/
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 ];
90
86
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
+ }
99
103
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 ();
113
110
}
114
- printWriter .close ();
115
- outputStream .close ();
116
- s .close ();
117
111
}
118
112
}
0 commit comments