Mercurial Hosting > luan
comparison src/luan/webserver/Server.java @ 1187:83c8a5a47f70
fixes for luanhost
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 22 Feb 2018 18:38:45 -0700 |
parents | 79b1e9ffd0c0 |
children | 8b61c8c4e07a |
comparison
equal
deleted
inserted
replaced
1186:ef8cd42e23d5 | 1187:83c8a5a47f70 |
---|---|
3 import java.io.IOException; | 3 import java.io.IOException; |
4 import java.net.Socket; | 4 import java.net.Socket; |
5 import java.net.ServerSocket; | 5 import java.net.ServerSocket; |
6 import java.net.InetAddress; | 6 import java.net.InetAddress; |
7 import java.net.UnknownHostException; | 7 import java.net.UnknownHostException; |
8 import java.util.concurrent.ExecutorService; | 8 import java.util.concurrent.ThreadPoolExecutor; |
9 import java.util.concurrent.Executors; | 9 import java.util.concurrent.Executors; |
10 import java.util.concurrent.TimeUnit; | 10 import java.util.concurrent.TimeUnit; |
11 import org.slf4j.Logger; | 11 import org.slf4j.Logger; |
12 import org.slf4j.LoggerFactory; | 12 import org.slf4j.LoggerFactory; |
13 | 13 |
15 public class Server { | 15 public class Server { |
16 private static final Logger logger = LoggerFactory.getLogger(Server.class); | 16 private static final Logger logger = LoggerFactory.getLogger(Server.class); |
17 | 17 |
18 public final int port; | 18 public final int port; |
19 public final Handler handler; | 19 public final Handler handler; |
20 private static final ExecutorService exec = Executors.newCachedThreadPool(); | 20 public static final ThreadPoolExecutor threadPool = (ThreadPoolExecutor)Executors.newCachedThreadPool(); |
21 | 21 |
22 public Server(int port,Handler handler) { | 22 public Server(int port,Handler handler) { |
23 this.port = port; | 23 this.port = port; |
24 this.handler = handler; | 24 this.handler = handler; |
25 } | 25 } |
28 return new ServerSocket(port); | 28 return new ServerSocket(port); |
29 } | 29 } |
30 | 30 |
31 public synchronized void start() throws IOException { | 31 public synchronized void start() throws IOException { |
32 final ServerSocket ss = newServerSocket(); | 32 final ServerSocket ss = newServerSocket(); |
33 exec.execute(new Runnable(){public void run() { | 33 threadPool.execute(new Runnable(){public void run() { |
34 try { | 34 try { |
35 while(!exec.isShutdown()) { | 35 while(!threadPool.isShutdown()) { |
36 final Socket socket = ss.accept(); | 36 final Socket socket = ss.accept(); |
37 exec.execute(new Runnable(){public void run() { | 37 threadPool.execute(new Runnable(){public void run() { |
38 Connection.handle(Server.this,socket); | 38 Connection.handle(Server.this,socket); |
39 }}); | 39 }}); |
40 } | 40 } |
41 } catch(IOException e) { | 41 } catch(IOException e) { |
42 logger.error("",e); | 42 logger.error("",e); |
45 logger.info("started server on port "+port); | 45 logger.info("started server on port "+port); |
46 } | 46 } |
47 | 47 |
48 public synchronized boolean stop(long timeoutSeconds) { | 48 public synchronized boolean stop(long timeoutSeconds) { |
49 try { | 49 try { |
50 exec.shutdownNow(); | 50 threadPool.shutdownNow(); |
51 boolean stopped = exec.awaitTermination(timeoutSeconds,TimeUnit.SECONDS); | 51 boolean stopped = threadPool.awaitTermination(timeoutSeconds,TimeUnit.SECONDS); |
52 if(stopped) | 52 if(stopped) |
53 logger.info("stopped server on port "+port); | 53 logger.info("stopped server on port "+port); |
54 else | 54 else |
55 logger.warn("couldn't stop server on port "+port); | 55 logger.warn("couldn't stop server on port "+port); |
56 return stopped; | 56 return stopped; |