Mercurial Hosting > luan
comparison src/goodjava/webserver/Server.java @ 1402:27efb1fcbcb5
move luan.lib to goodjava
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Tue, 17 Sep 2019 01:35:01 -0400 |
| parents | src/luan/lib/webserver/Server.java@643cf1c37723 |
| children | 8a7b6b32c691 |
comparison
equal
deleted
inserted
replaced
| 1401:ef1620aa99cb | 1402:27efb1fcbcb5 |
|---|---|
| 1 package goodjava.webserver; | |
| 2 | |
| 3 import java.io.IOException; | |
| 4 import java.net.Socket; | |
| 5 import java.net.ServerSocket; | |
| 6 import java.net.InetAddress; | |
| 7 import java.net.UnknownHostException; | |
| 8 import java.util.concurrent.ThreadPoolExecutor; | |
| 9 import java.util.concurrent.Executors; | |
| 10 import java.util.concurrent.TimeUnit; | |
| 11 import goodjava.logging.Logger; | |
| 12 import goodjava.logging.LoggerFactory; | |
| 13 | |
| 14 | |
| 15 public class Server { | |
| 16 private static final Logger logger = LoggerFactory.getLogger(Server.class); | |
| 17 | |
| 18 public final int port; | |
| 19 public final Handler handler; | |
| 20 public static final ThreadPoolExecutor threadPool = (ThreadPoolExecutor)Executors.newCachedThreadPool(); | |
| 21 | |
| 22 public Server(int port,Handler handler) { | |
| 23 this.port = port; | |
| 24 this.handler = handler; | |
| 25 } | |
| 26 | |
| 27 protected ServerSocket newServerSocket() throws IOException { | |
| 28 return new ServerSocket(port); | |
| 29 } | |
| 30 | |
| 31 public synchronized void start() throws IOException { | |
| 32 final ServerSocket ss = newServerSocket(); | |
| 33 threadPool.execute(new Runnable(){public void run() { | |
| 34 try { | |
| 35 while(!threadPool.isShutdown()) { | |
| 36 final Socket socket = ss.accept(); | |
| 37 threadPool.execute(new Runnable(){public void run() { | |
| 38 Connection.handle(Server.this,socket); | |
| 39 }}); | |
| 40 } | |
| 41 } catch(IOException e) { | |
| 42 logger.error("",e); | |
| 43 } | |
| 44 }}); | |
| 45 logger.info("started server on port "+port); | |
| 46 } | |
| 47 | |
| 48 public synchronized boolean stop(long timeoutSeconds) { | |
| 49 try { | |
| 50 threadPool.shutdownNow(); | |
| 51 boolean stopped = threadPool.awaitTermination(timeoutSeconds,TimeUnit.SECONDS); | |
| 52 if(stopped) | |
| 53 logger.info("stopped server on port "+port); | |
| 54 else | |
| 55 logger.warn("couldn't stop server on port "+port); | |
| 56 return stopped; | |
| 57 } catch(InterruptedException e) { | |
| 58 throw new RuntimeException(e); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 public static class ForAddress extends Server { | |
| 63 private final InetAddress addr; | |
| 64 | |
| 65 public ForAddress(InetAddress addr,int port,Handler handler) { | |
| 66 super(port,handler); | |
| 67 this.addr = addr; | |
| 68 } | |
| 69 | |
| 70 public ForAddress(String addrName,int port,Handler handler) throws UnknownHostException { | |
| 71 this(InetAddress.getByName(addrName),port,handler); | |
| 72 } | |
| 73 | |
| 74 protected ServerSocket newServerSocket() throws IOException { | |
| 75 return new ServerSocket(port,0,addr); | |
| 76 } | |
| 77 } | |
| 78 } |
