annotate src/goodjava/webserver/Server.java @ 1427:a076e89a2879

remove literal
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 24 Nov 2019 18:23:57 -0700
parents 27efb1fcbcb5
children 8a7b6b32c691
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
1 package goodjava.webserver;
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.IOException;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.net.Socket;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
Franklin Schmidt <fschmidt@gmail.com>
parents: 1175
diff changeset
7 import java.net.UnknownHostException;
1187
83c8a5a47f70 fixes for luanhost
Franklin Schmidt <fschmidt@gmail.com>
parents: 1176
diff changeset
8 import java.util.concurrent.ThreadPoolExecutor;
1142
0f59eab45f3d webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents: 1138
diff changeset
9 import java.util.concurrent.Executors;
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
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 public class Server {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 private static final Logger logger = LoggerFactory.getLogger(Server.class);
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 public final int port;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 public final Handler handler;
1187
83c8a5a47f70 fixes for luanhost
Franklin Schmidt <fschmidt@gmail.com>
parents: 1176
diff changeset
20 public static final ThreadPoolExecutor threadPool = (ThreadPoolExecutor)Executors.newCachedThreadPool();
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 public Server(int port,Handler handler) {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 this.port = port;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 this.handler = handler;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 protected ServerSocket newServerSocket() throws IOException {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 return new ServerSocket(port);
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 public synchronized void start() throws IOException {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 final ServerSocket ss = newServerSocket();
1187
83c8a5a47f70 fixes for luanhost
Franklin Schmidt <fschmidt@gmail.com>
parents: 1176
diff changeset
33 threadPool.execute(new Runnable(){public void run() {
1142
0f59eab45f3d webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents: 1138
diff changeset
34 try {
1187
83c8a5a47f70 fixes for luanhost
Franklin Schmidt <fschmidt@gmail.com>
parents: 1176
diff changeset
35 while(!threadPool.isShutdown()) {
1142
0f59eab45f3d webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents: 1138
diff changeset
36 final Socket socket = ss.accept();
1187
83c8a5a47f70 fixes for luanhost
Franklin Schmidt <fschmidt@gmail.com>
parents: 1176
diff changeset
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
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 }
1142
0f59eab45f3d webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents: 1138
diff changeset
44 }});
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 logger.info("started server on port "+port);
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47
1142
0f59eab45f3d webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents: 1138
diff changeset
48 public synchronized boolean stop(long timeoutSeconds) {
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 try {
1187
83c8a5a47f70 fixes for luanhost
Franklin Schmidt <fschmidt@gmail.com>
parents: 1176
diff changeset
50 threadPool.shutdownNow();
83c8a5a47f70 fixes for luanhost
Franklin Schmidt <fschmidt@gmail.com>
parents: 1176
diff changeset
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
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 } catch(InterruptedException e) {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 throw new RuntimeException(e);
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61
1176
Franklin Schmidt <fschmidt@gmail.com>
parents: 1175
diff changeset
62 public static class ForAddress extends Server {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1175
diff changeset
63 private final InetAddress addr;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1175
diff changeset
64
Franklin Schmidt <fschmidt@gmail.com>
parents: 1175
diff changeset
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
Franklin Schmidt <fschmidt@gmail.com>
parents: 1175
diff changeset
67 this.addr = addr;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1175
diff changeset
68 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1175
diff changeset
69
Franklin Schmidt <fschmidt@gmail.com>
parents: 1175
diff changeset
70 public ForAddress(String addrName,int port,Handler handler) throws UnknownHostException {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1175
diff changeset
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
Franklin Schmidt <fschmidt@gmail.com>
parents: 1175
diff changeset
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
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 }