jdk/test/java/nio/channels/Selector/ByteServer.java
changeset 22043 e77b5937792c
parent 7668 d4a77089c587
equal deleted inserted replaced
22042:98541fec9c62 22043:e77b5937792c
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  *
    25  * Utility class for tests. A simple "in-thread" server to accept connections
    26  * Utility class for tests. A simple server, which waits for a connection,
    26  * and write bytes.
    27  * writes out n bytes and waits.
       
    28  * @author kladko
    27  * @author kladko
    29  */
    28  */
    30 
    29 
    31 import java.net.Socket;
    30 import java.net.Socket;
    32 import java.net.ServerSocket;
    31 import java.net.ServerSocket;
       
    32 import java.net.SocketAddress;
       
    33 import java.net.InetSocketAddress;
       
    34 import java.io.IOException;
       
    35 import java.io.Closeable;
    33 
    36 
    34 public class ByteServer {
    37 public class ByteServer implements Closeable {
    35 
    38 
    36     public static final String LOCALHOST = "localhost";
    39     private final ServerSocket ss;
    37     private int bytecount;
    40     private Socket s;
    38     private Socket  socket;
       
    39     private ServerSocket  serversocket;
       
    40     private Thread serverthread;
       
    41     volatile Exception savedException;
       
    42 
    41 
    43     public ByteServer(int bytecount) throws Exception{
    42     ByteServer() throws IOException {
    44         this.bytecount = bytecount;
    43         this.ss = new ServerSocket(0);
    45         serversocket = new ServerSocket(0);
       
    46     }
    44     }
    47 
    45 
    48     public int port() {
    46     SocketAddress address() {
    49         return serversocket.getLocalPort();
    47         return new InetSocketAddress(ss.getInetAddress(), ss.getLocalPort());
    50     }
    48     }
    51 
    49 
    52     public void start() {
    50     void acceptConnection() throws IOException {
    53         serverthread = new Thread() {
    51         if (s != null)
    54             public void run() {
    52             throw new IllegalStateException("already connected");
    55                 try {
    53         this.s = ss.accept();
    56                     socket = serversocket.accept();
       
    57                     socket.getOutputStream().write(new byte[bytecount]);
       
    58                     socket.getOutputStream().flush();
       
    59                 } catch (Exception e) {
       
    60                     System.err.println("Exception in ByteServer: " + e);
       
    61                     System.exit(1);
       
    62                 }
       
    63             }
       
    64         };
       
    65         serverthread.start();
       
    66     }
    54     }
    67 
    55 
    68     public void exit() throws Exception {
    56     void closeConnection() throws IOException {
    69         serverthread.join();
    57         Socket s = this.s;
    70         socket.close();
    58         if (s != null) {
    71         serversocket.close();
    59             this.s = null;
       
    60             s.close();
       
    61         }
       
    62     }
       
    63 
       
    64     void write(int count) throws IOException {
       
    65         if (s == null)
       
    66             throw new IllegalStateException("no connection");
       
    67         s.getOutputStream().write(new byte[count]);
       
    68     }
       
    69 
       
    70     public void close() throws IOException {
       
    71         if (s != null)
       
    72             s.close();
       
    73         ss.close();
    72     }
    74     }
    73 }
    75 }