Default threading strategy for NanoHTTPD. By default, the server spawns a new Thread for every incoming request. These are set to daemon status, and named according to the request number. The name is useful when profiling the application.
(1) Interface AsyncRunner
void closeAll();
void closed(ClientHandler clientHandler);
void exec(ClientHandler code);
(2) ClientHandler implements Runnable
private final InputStream inputStream;
private final Socket acceptSocket;
@Override
public void run() {
(1). get outputStream: outputStream = this.acceptSocket.getOutputStream();
(2). create temporary file: TempFileManager tempFileManager = NanoHTTPD.this.tempFileManagerFactory.create();
(3). Create HTTPSession via: session = new HTTPSession(tempFileManager, this.inputStream, outputStream, this.acceptSocket.getInetAddress());
(4). session.execute
(3) static class Cookie
(1) public static String getHTTPTime(int days):get dd MMM yyyy HH:mm:ss z via int days (2) constructor: Cookie(String name, String value, int numDays) numDays means expire days rudimentary: 基础的,初步的。
(4) CookieHandler:Provides rudimentary support for cookies. Doesn’t support ‘path’, ‘secure’ nor ‘httpOnly’. (1) HashMap<String, String> cookies = new HashMap<String, String>(); (2) ArrayList queue = new ArrayList(); (3) Set a cookie with an expiration date from a month ago, effectively deleting it on the client side. public void delete(String name) { set(name, “-delete-”, -30); }
(5) DefaultAsyncRunner implements AsyncRunner constructor:
int availableProcessors = Runtime.getRuntime().availableProcessors();
executor = new ThreadPoolExecutor(
5 * availableProcessors, // core size
15 * availableProcessors, // max size
2, TimeUnit.MINUTES, // 2 minutes timeout for thread to process request
new SynchronousQueue(),
new ThreadFactory(){
@Override
public Thread newThread(Runnable runnable) {
Thread t = Executors.defaultThreadFactory().newThread(runnable);
t.setPriority(Thread.MAX_PRIORITY);
t.setDaemon(true);
return t;
}
});
(1) private ThreadPoolExecutor executor; (2) private final List running = Collections.synchronizedList(new ArrayList()); (3) exec(ClientHandler clientHandler) a. this.running.add(clientHandler); b. executor.execute(clientHandler);
(6) DefaultTempFile implements TempFile
(1) private final File file; (2) private final OutputStream fstream; Constructor public DefaultTempFile(File tempdir) throws IOException { this.file = File.createTempFile(“NanoHTTPD-”, “”, tempdir); this.fstream = new FileOutputStream(this.file); }
(7) DefaultTempFileManager (1) private final File tmpdir; (2) private final List tempFiles; constructor this.tmpdir = new File(System.getProperty(“java.io.tmpdir”)); this.tempFiles = new ArrayList(); (1) createTempFile
(8) DefaultTempFileManagerFactory implements TempFileManagerFactory charset, boundary,
(9) SecureServerSocketFactory implements ServerSocketFactory (1) private SSLServerSocketFactory sslServerSocketFactory; (2) private String[] sslProtocols;
(10) HTTPSession implements IHTTPSession (1) params (2) headers (3) cookie (4) queryParameterString; (5) remoteIP (6) inputStream (7) outputStream (8) tempFileManager (9)
(11) Apache’s default header limit is 8KB. // Do NOT assume that a single read will get the entire header // at once!
(12) ServerRunnable implements Runnable