test/jdk/java/net/httpclient/http2/server/Http2TestServer.java
branchhttp-client-branch
changeset 55983 e4a1f0c9d4c6
parent 55973 4d9b002587db
child 56008 bbd688c6fbbb
equal deleted inserted replaced
55982:b6ff245c0db6 55983:e4a1f0c9d4c6
   168 
   168 
   169     final ServerSocket initPlaintext(int port) throws Exception {
   169     final ServerSocket initPlaintext(int port) throws Exception {
   170         return new ServerSocket(port);
   170         return new ServerSocket(port);
   171     }
   171     }
   172 
   172 
   173     public void stop() {
   173     public synchronized void stop() {
   174         // TODO: clean shutdown GoAway
   174         // TODO: clean shutdown GoAway
   175         stopping = true;
   175         stopping = true;
   176         System.err.printf("Server stopping %d connections\n", connections.size());
   176         System.err.printf("Server stopping %d connections\n", connections.size());
   177         for (Http2TestServerConnection connection : connections.values()) {
   177         for (Http2TestServerConnection connection : connections.values()) {
   178             connection.close(ErrorFrame.NO_ERROR);
   178             connection.close(ErrorFrame.NO_ERROR);
   203 
   203 
   204     public String serverName() {
   204     public String serverName() {
   205         return serverName;
   205         return serverName;
   206     }
   206     }
   207 
   207 
       
   208     private synchronized void putConnection(InetSocketAddress addr, Http2TestServerConnection c) {
       
   209         if (!stopping)
       
   210             connections.put(addr, c);
       
   211     }
       
   212 
       
   213     private synchronized void removeConnection(InetSocketAddress addr, Http2TestServerConnection c) {
       
   214         connections.remove(addr, c);
       
   215     }
       
   216 
   208     /**
   217     /**
   209      * Starts a thread which waits for incoming connections.
   218      * Starts a thread which waits for incoming connections.
   210      */
   219      */
   211     public void start() {
   220     public void start() {
   212         exec.submit(() -> {
   221         exec.submit(() -> {
   214                 while (!stopping) {
   223                 while (!stopping) {
   215                     Socket socket = server.accept();
   224                     Socket socket = server.accept();
   216                     InetSocketAddress addr = (InetSocketAddress) socket.getRemoteSocketAddress();
   225                     InetSocketAddress addr = (InetSocketAddress) socket.getRemoteSocketAddress();
   217                     Http2TestServerConnection c =
   226                     Http2TestServerConnection c =
   218                             new Http2TestServerConnection(this, socket, exchangeSupplier);
   227                             new Http2TestServerConnection(this, socket, exchangeSupplier);
   219                     connections.put(addr, c);
   228                     putConnection(addr, c);
   220                     try {
   229                     try {
   221                         c.run();
   230                         c.run();
   222                     } catch (Throwable e) {
   231                     } catch (Throwable e) {
   223                         // we should not reach here, but if we do
   232                         // we should not reach here, but if we do
   224                         // the connection might not have been closed
   233                         // the connection might not have been closed
   225                         // and if so then the client might wait
   234                         // and if so then the client might wait
   226                         // forever.
   235                         // forever.
   227                         connections.remove(addr, c);
   236                         removeConnection(addr, c);
   228                         c.close(ErrorFrame.PROTOCOL_ERROR);
   237                         c.close(ErrorFrame.PROTOCOL_ERROR);
   229                         System.err.println("TestServer: start exception: " + e);
   238                         System.err.println("TestServer: start exception: " + e);
   230                         //throw e;
   239                         //throw e;
   231                     }
   240                     }
   232                 }
   241                 }