Mercurial Hosting > luan
annotate src/luan/webserver/Server.java @ 1138:4189027691b7
webserver - add LogHandler
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 29 Jan 2018 19:48:21 -0700 |
parents | c123ee15f99b |
children | 0f59eab45f3d |
rev | line source |
---|---|
1137 | 1 package luan.webserver; |
2 | |
3 import java.io.IOException; | |
4 import java.net.Socket; | |
5 import java.net.ServerSocket; | |
6 import org.slf4j.Logger; | |
7 import org.slf4j.LoggerFactory; | |
8 | |
9 | |
10 public class Server { | |
11 private static final Logger logger = LoggerFactory.getLogger(Server.class); | |
12 | |
13 public final int port; | |
14 public final Handler handler; | |
15 private volatile boolean isRunning = false; | |
16 private Thread thread; | |
17 | |
18 public Server(int port,Handler handler) { | |
19 this.port = port; | |
20 this.handler = handler; | |
21 } | |
22 | |
23 protected ServerSocket newServerSocket() throws IOException { | |
24 return new ServerSocket(port); | |
25 } | |
26 | |
27 public synchronized void start() throws IOException { | |
28 isRunning = true; | |
29 final ServerSocket ss = newServerSocket(); | |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1137
diff
changeset
|
30 thread = new Thread("luan.webserver") { |
1137 | 31 public void run() { |
32 try { | |
33 while(isRunning) { | |
34 Socket socket = ss.accept(); | |
35 new Connection(Server.this,socket); | |
36 } | |
37 } catch(IOException e) { | |
38 logger.error("",e); | |
39 } | |
40 } | |
41 }; | |
42 thread.start(); | |
43 logger.info("started server on port "+port); | |
44 } | |
45 | |
46 public synchronized void stop() { | |
47 isRunning = false; | |
48 try { | |
49 thread.join(); | |
50 } catch(InterruptedException e) { | |
51 throw new RuntimeException(e); | |
52 } | |
53 logger.info("stopped server on port "+port); | |
54 } | |
55 | |
56 } |