31 /** 32 * This class implements a simple HTTP server. It uses multiple threads to 33 * handle connections in parallel, and also multiple connections/requests 34 * can be handled per thread. 35 * <p> 36 * It must be instantiated with a {@link HttpCallback} object to which 37 * requests are given and must be handled. 38 * <p> 39 * Simple synchronization between the client(s) and server can be done 40 * using the {@link #waitForCondition(String)}, {@link #setCondition(String)} and 41 * {@link #rendezvous(String,int)} methods. 42 * 43 * NOTE NOTE NOTE NOTE NOTE NOTE NOTE 44 * 45 * If changes are made here, please sure they are propagated to 46 * the HTTPS equivalent in the JSSE regression test suite. 47 * 48 * NOTE NOTE NOTE NOTE NOTE NOTE NOTE 49 */ 50 51 public class HttpServer { 52 53 ServerSocketChannel schan; 54 int threads; 55 int cperthread; 56 HttpCallback cb; 57 Server[] servers; 58 59 /** 60 * Create a <code>HttpServer<code> instance with the specified callback object 61 * for handling requests. One thread is created to handle requests, 62 * and up to ten TCP connections will be handled simultaneously. 63 * @param cb the callback object which is invoked to handle each 64 * incoming request 65 */ 66 67 public HttpServer (HttpCallback cb) throws IOException { 68 this (cb, 1, 10, 0); 69 } 70 71 /** 72 * Create a <code>HttpServer<code> instance with the specified number of 73 * threads and maximum number of connections per thread. This functions 74 * the same as the 4 arg constructor, where the port argument is set to zero. 75 * @param cb the callback object which is invoked to handle each 76 * incoming request 77 * @param threads the number of threads to create to handle requests 78 * in parallel 79 * @param cperthread the number of simultaneous TCP connections to 80 * handle per thread 81 */ 82 83 public HttpServer (HttpCallback cb, int threads, int cperthread) 84 throws IOException { 85 this (cb, threads, cperthread, 0); 86 } 87 88 /** 89 * Create a <code>HttpServer<code> instance with the specified number 90 * of threads and maximum number of connections per thread and running on 91 * the specified port. The specified number of threads are created to 92 * handle incoming requests, and each thread is allowed 93 * to handle a number of simultaneous TCP connections. 94 * @param cb the callback object which is invoked to handle 95 * each incoming request 96 * @param threads the number of threads to create to handle 97 * requests in parallel 98 * @param cperthread the number of simultaneous TCP connections 99 * to handle per thread 100 * @param port the port number to bind the server to. <code>Zero</code> 101 * means choose any free port. 102 */ 103 104 public HttpServer (HttpCallback cb, int threads, int cperthread, int port) 105 throws IOException { 106 schan = ServerSocketChannel.open (); 107 InetSocketAddress addr = new InetSocketAddress (port); 108 schan.socket().bind (addr); 109 this.threads = threads; 110 this.cb = cb; 111 this.cperthread = cperthread; 112 servers = new Server [threads]; 113 for (int i=0; i<threads; i++) { 114 servers[i] = new Server (cb, schan, cperthread); 115 servers[i].start(); 116 } 117 } 118 119 /** Tell all threads in the server to exit within 5 seconds. 120 * This is an abortive termination. Just prior to the thread exiting 121 * all channels in that thread waiting to be closed are forceably closed. 122 */ 123 124 public void terminate () { | 31 /** 32 * This class implements a simple HTTP server. It uses multiple threads to 33 * handle connections in parallel, and also multiple connections/requests 34 * can be handled per thread. 35 * <p> 36 * It must be instantiated with a {@link HttpCallback} object to which 37 * requests are given and must be handled. 38 * <p> 39 * Simple synchronization between the client(s) and server can be done 40 * using the {@link #waitForCondition(String)}, {@link #setCondition(String)} and 41 * {@link #rendezvous(String,int)} methods. 42 * 43 * NOTE NOTE NOTE NOTE NOTE NOTE NOTE 44 * 45 * If changes are made here, please sure they are propagated to 46 * the HTTPS equivalent in the JSSE regression test suite. 47 * 48 * NOTE NOTE NOTE NOTE NOTE NOTE NOTE 49 */ 50 51 public class TestHttpServer { 52 53 ServerSocketChannel schan; 54 int threads; 55 int cperthread; 56 HttpCallback cb; 57 Server[] servers; 58 59 /** 60 * Create a <code>TestHttpServer<code> instance with the specified callback object 61 * for handling requests. One thread is created to handle requests, 62 * and up to ten TCP connections will be handled simultaneously. 63 * @param cb the callback object which is invoked to handle each 64 * incoming request 65 */ 66 67 public TestHttpServer (HttpCallback cb) throws IOException { 68 this (cb, 1, 10, 0); 69 } 70 71 /** 72 * Create a <code>TestHttpServer<code> instance with the specified number of 73 * threads and maximum number of connections per thread. This functions 74 * the same as the 4 arg constructor, where the port argument is set to zero. 75 * @param cb the callback object which is invoked to handle each 76 * incoming request 77 * @param threads the number of threads to create to handle requests 78 * in parallel 79 * @param cperthread the number of simultaneous TCP connections to 80 * handle per thread 81 */ 82 83 public TestHttpServer (HttpCallback cb, int threads, int cperthread) 84 throws IOException { 85 this (cb, threads, cperthread, 0); 86 } 87 88 /** 89 * Create a <code>TestHttpServer<code> instance with the specified number 90 * of threads and maximum number of connections per thread and running on 91 * the specified port. The specified number of threads are created to 92 * handle incoming requests, and each thread is allowed 93 * to handle a number of simultaneous TCP connections. 94 * @param cb the callback object which is invoked to handle 95 * each incoming request 96 * @param threads the number of threads to create to handle 97 * requests in parallel 98 * @param cperthread the number of simultaneous TCP connections 99 * to handle per thread 100 * @param port the port number to bind the server to. <code>Zero</code> 101 * means choose any free port. 102 */ 103 104 public TestHttpServer (HttpCallback cb, int threads, int cperthread, int port) 105 throws IOException { 106 schan = ServerSocketChannel.open (); 107 InetSocketAddress addr = new InetSocketAddress (port); 108 schan.socket().bind (addr); 109 this.threads = threads; 110 this.cb = cb; 111 this.cperthread = cperthread; 112 servers = new Server [threads]; 113 for (int i=0; i<threads; i++) { 114 servers[i] = new Server (cb, schan, cperthread); 115 servers[i].start(); 116 } 117 } 118 119 /** Tell all threads in the server to exit within 5 seconds. 120 * This is an abortive termination. Just prior to the thread exiting 121 * all channels in that thread waiting to be closed are forceably closed. 122 */ 123 124 public void terminate () { |