11package xdvrx1_serverProject ;
22
3- /*
3+ /**
44 * This is the actual server class.
55 * This will call the overridden
66 * method `run()` of `Runnable`.
77 */
88
99import java .util .concurrent .*;
10- import java .util .logging .*;
11-
1210import java .io .*;
1311import java .net .*;
1412
13+ import java .util .logging .*;
14+
1515public class FileWebServer {
16-
17- private final File rootDirectory ;
18- private final int port ;
19- private static final int pool_count = 10000 ;
20- private static final String defaultPage = "index.html" ;
21-
22- private static final Logger
23- serverLogger = Logger .getLogger (FileWebServer
24- .class .getCanonicalName ());
25-
26- //the constructor
27- public FileWebServer (File rootDirectory , int port )
28- throws IOException {
29-
30- if (!rootDirectory .isDirectory ()) {
31- throw new IOException (rootDirectory
32- + " is not a directory" );
33- }
34-
35- this .rootDirectory = rootDirectory ;
36- this .port = port ;
37-
38- }
39-
40- //void start
41- public void start ()
42- throws IOException {
43-
44- ExecutorService pool =
45- Executors .newFixedThreadPool (pool_count );
46-
47- try (ServerSocket server = new ServerSocket (port )) {
48-
49- serverLogger .info ("Listening on port " + server .getLocalPort ());
50- serverLogger .info ("@DocumentRoot" );
51-
52- while (true ) {
53- try {
54- Socket request = server .accept ();
55- Runnable r =
56- new ClientRequest (rootDirectory , defaultPage , request );
57- pool .submit (r );
58- } catch (IOException ex ) {
59- serverLogger .log (Level .WARNING , "Error accepting connection" , ex );
16+
17+ private final File rootDirectory ;
18+ private final int port ;
19+ private static final int pool_count = 1000 ;
20+ private static final String defaultPage = "index.html" ;
21+
22+ private static final Logger
23+ serverLogger = Logger .getLogger (FileWebServer
24+ .class .getCanonicalName ());
25+
26+ //the constructor
27+ public FileWebServer (File rootDirectory , int port )
28+ throws IOException {
29+
30+ if (!rootDirectory .isDirectory ()) {
31+ throw new IOException (rootDirectory
32+ + " is not a directory" );
33+ }
34+
35+ this .rootDirectory = rootDirectory ;
36+ this .port = port ;
37+
38+ }
39+
40+ //void start
41+ public void start ()
42+ throws IOException {
43+
44+ ExecutorService pool =
45+ Executors .newFixedThreadPool (pool_count );
46+
47+ try (ServerSocket server = new ServerSocket (port )) {
48+
49+ serverLogger .info ("Listening on port " + server .getLocalPort ());
50+ serverLogger .info ("@DocumentRoot" );
51+
52+ while (true ) {
53+ try {
54+ Socket request = server .accept ();
55+ Runnable r =
56+ new ClientRequest (rootDirectory , defaultPage , request );
57+ pool .submit (r );
58+ } catch (IOException ex ) {
59+ serverLogger .log (Level .WARNING , "Error accepting connection" , ex );
60+ }
6061 }
61- }
62- }
63- }
62+ }
63+ }
6464}
0 commit comments