11package jswerve ;
22
3- import java .io .BufferedReader ;
4- import java .io .PrintWriter ;
53import java .io .IOException ;
6- import java .io .InputStreamReader ;
7- import java .io .OutputStreamWriter ;
84import java .net .ServerSocket ;
95import java .net .Socket ;
10- import java .util .Date ;
6+ import java .util .ArrayList ;
117
12- public class BaseHTTPServer {
8+ public class BaseHTTPServer implements Runnable {
139
1410 private int port = 80 ;
15- private BufferedReader reader ;
16- private PrintWriter writer ;
1711 private ServerSocket ss ;
1812 private Socket s ;
19-
20- public void initServerSocket () {
13+ public static boolean isStopped = false ;
14+
15+ private void initServerSocket () {
2116 try {
2217 ss = new ServerSocket (port );
2318 } catch (IOException e ) {
24- System .out .println (e .getMessage ());
19+ System .out .println (e .getMessage ()); // replace with logging utility
2520 }
2621 }
2722
28- public void acceptConectionsAndThrowToHandlers () throws InterruptedException {
23+ private void acceptConectionsAndThrowToHandlers () {
2924 try {
30- while (true ) {
25+ while (! isStopped ) {
3126 s = ss .accept ();
3227
3328 (new Thread (new ConnectionHandler (s ))).start ();
@@ -38,26 +33,27 @@ public void acceptConectionsAndThrowToHandlers() throws InterruptedException {
3833 }
3934 }
4035
41- public BaseHTTPServer (int port ) throws InterruptedException {
36+ public BaseHTTPServer (int port ) {
4237 this .port = port ;
43- initServerSocket ();
44- try {
45- acceptConectionsAndThrowToHandlers ();
46- } catch (Exception e ) {
47- // TODO Auto-generated catch block
48- e .printStackTrace ();
49- }
5038 }
51-
52- public BaseHTTPServer () throws InterruptedException {
53- initServerSocket ();
54- try {
55- acceptConectionsAndThrowToHandlers ();
56- } catch (Exception e ) {
57- // TODO Auto-generated catch block
58- e .printStackTrace ();
59- }
39+
40+ public void stop () {
41+ isStopped = true ;
6042 }
43+
44+ public void run () {
45+ initServerSocket ();
46+
47+ try {
48+ acceptConectionsAndThrowToHandlers ();
49+ } catch (Exception e ) {
50+ e .printStackTrace (); // replace with logging utility
51+ }
6152
53+ }
54+
55+ public void setRoute (String route , RouteHandler rh ) {
56+
57+ }
58+ }
6259
63- }
0 commit comments