Mercurial Hosting > luan
annotate src/goodjava/webserver/Server.java @ 1828:09e90d94b7b5
add DomainFilter
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 15 Sep 2024 19:51:16 -0600 |
parents | 8a7b6b32c691 |
children |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
1 package goodjava.webserver; |
1137 | 2 |
3 import java.io.IOException; | |
4 import java.net.Socket; | |
5 import java.net.ServerSocket; | |
1175
d6d0bd05ad8c
exit on bind conflict, serve "127.0.0.1" for serve_for_nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
6 import java.net.InetAddress; |
1176 | 7 import java.net.UnknownHostException; |
1142
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
8 import java.util.concurrent.Executors; |
1502 | 9 import java.util.concurrent.ExecutorService; |
1142
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
10 import java.util.concurrent.TimeUnit; |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
11 import goodjava.logging.Logger; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
12 import goodjava.logging.LoggerFactory; |
1137 | 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; | |
1502 | 20 public static final ExecutorService threadPool = Executors.newCachedThreadPool(); |
1137 | 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(); | |
1187 | 33 threadPool.execute(new Runnable(){public void run() { |
1142
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
34 try { |
1187 | 35 while(!threadPool.isShutdown()) { |
1142
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
36 final Socket socket = ss.accept(); |
1187 | 37 threadPool.execute(new Runnable(){public void run() { |
1142
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
38 Connection.handle(Server.this,socket); |
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
39 }}); |
1137 | 40 } |
1142
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
41 } catch(IOException e) { |
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
42 logger.error("",e); |
1137 | 43 } |
1142
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
44 }}); |
1137 | 45 logger.info("started server on port "+port); |
46 } | |
47 | |
1142
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
48 public synchronized boolean stop(long timeoutSeconds) { |
1137 | 49 try { |
1187 | 50 threadPool.shutdownNow(); |
51 boolean stopped = threadPool.awaitTermination(timeoutSeconds,TimeUnit.SECONDS); | |
1142
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
52 if(stopped) |
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
53 logger.info("stopped server on port "+port); |
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
54 else |
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
55 logger.warn("couldn't stop server on port "+port); |
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
56 return stopped; |
1137 | 57 } catch(InterruptedException e) { |
58 throw new RuntimeException(e); | |
59 } | |
60 } | |
61 | |
1176 | 62 public static class ForAddress extends Server { |
63 private final InetAddress addr; | |
64 | |
65 public ForAddress(InetAddress addr,int port,Handler handler) { | |
1175
d6d0bd05ad8c
exit on bind conflict, serve "127.0.0.1" for serve_for_nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
66 super(port,handler); |
1176 | 67 this.addr = addr; |
68 } | |
69 | |
70 public ForAddress(String addrName,int port,Handler handler) throws UnknownHostException { | |
71 this(InetAddress.getByName(addrName),port,handler); | |
1175
d6d0bd05ad8c
exit on bind conflict, serve "127.0.0.1" for serve_for_nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
72 } |
d6d0bd05ad8c
exit on bind conflict, serve "127.0.0.1" for serve_for_nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
73 |
d6d0bd05ad8c
exit on bind conflict, serve "127.0.0.1" for serve_for_nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
74 protected ServerSocket newServerSocket() throws IOException { |
1176 | 75 return new ServerSocket(port,0,addr); |
1175
d6d0bd05ad8c
exit on bind conflict, serve "127.0.0.1" for serve_for_nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
76 } |
d6d0bd05ad8c
exit on bind conflict, serve "127.0.0.1" for serve_for_nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
77 } |
1137 | 78 } |