package tinycdxserver;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.nio.channels.Channel;
import java.nio.channels.ServerSocketChannel;
import java.util.function.Predicate;

public class Main {
    public static void usage() {
        System.err.println("Usage: java " + Main.class.getName() + " [options...]");
        System.err.println("");
        System.err.println("  -b bindaddr   Bind to a particular IP address");
        System.err.println("  -d datadir    Directory to store index data under");
        System.err.println("  -i            Inherit the server socket via STDIN (for use with systemd, inetd etc)");
        System.err.println("  -p port       Local port to listen on");
        System.err.println("  -v            Verbose logging");
        System.exit(1);
    }

    public static void main(String args[]) {
        String host = null;
        int port = 8080;
        boolean inheritSocket = false;
        File dataPath = new File("data");
        boolean verbose = false;
        Predicate<Capture> filter = null;

        for (int i = 0; i < args.length; i++) {
            switch (args[i]) {
                case "-p":
                    port = Integer.parseInt(args[++i]);
                    break;
                case "-b":
                    host = args[++i];
                    break;
                case "-i":
                    inheritSocket = true;
                    break;
                case "-d":
                    dataPath = new File(args[++i]);
                    break;
                case "-v":
                    verbose = true;
                    break;
                default:
                    usage();
                    break;
            }
        }

        try (DataStore dataStore = new DataStore(dataPath)) {
            Webapp controller = new Webapp(dataStore, verbose);
            ServerSocket socket = openSocket(host, port, inheritSocket);
            Web.Server server = new Web.Server(socket, controller);
            server.start();
            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                server.stop();
                dataStore.close();
            }));
            Thread.sleep(Long.MAX_VALUE);
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }
    }

    private static ServerSocket openSocket(String host, int port, boolean inheritSocket) throws IOException {
        ServerSocket socket;Channel channel = System.inheritedChannel();
        if (inheritSocket && channel != null && channel instanceof ServerSocketChannel) {
            socket = ((ServerSocketChannel) channel).socket();
        } else {
            socket = new ServerSocket(port, -1, InetAddress.getByName(host));
        }
        return socket;
    }
}

@@ -197,32 +197,154 @@
 rSocketChannel;%0A
+import java.util.concurrent.Executor;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0A
 import java.util
@@ -364,16 +364,16 @@
 dicate;%0A
-
 %0Apublic 
@@ -1077,16 +1077,85 @@
 = 8080;%0A
+        int webThreads = Runtime.getRuntime().availableProcessors();%0A
         
@@ -1868,32 +1868,148 @@
          break;%0A
+                case %22-t%22:%0A                    webThreads = Integer.parseInt(args%5B++i%5D);%0A                    break;%0A
                 
@@ -2365,16 +2365,181 @@
 oller);%0A
+            ExecutorService threadPool = Executors.newFixedThreadPool(webThreads);%0A            try %7B%0A                server.setAsyncRunner(threadPool::execute);%0A    
         
@@ -2554,24 +2554,28 @@
 er.start();%0A
+    
             
@@ -2638,32 +2638,36 @@
                 
+    
 server.stop();%0A 
@@ -2661,24 +2661,28 @@
 ver.stop();%0A
+    
             
@@ -2700,24 +2700,28 @@
 re.close();%0A
+    
             
@@ -2737,16 +2737,20 @@
         
+    
 Thread.s
@@ -2771,16 +2771,93 @@
 VALUE);%0A
+            %7D finally %7B%0A                threadPool.shutdown();%0A            %7D%0A
         

Add an option to set the number of web threads