Mercurial Hosting > luan
comparison src/luan/webserver/Server.java @ 1137:c123ee15f99b
add webserver
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 29 Jan 2018 18:49:59 -0700 |
parents | |
children | 4189027691b7 |
comparison
equal
deleted
inserted
replaced
1136:d30d400fd43d | 1137:c123ee15f99b |
---|---|
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(); | |
30 thread = new Thread("threebody.http.Server") { | |
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 } |