test/jdk/java/nio/channels/unixdomain/Basic.java
branchunixdomainchannels
changeset 59090 fb91b01624be
parent 59073 832b8a28e17f
equal deleted inserted replaced
59082:5e250ee9259e 59090:fb91b01624be
    33 import java.net.*;
    33 import java.net.*;
    34 import java.nio.ByteBuffer;
    34 import java.nio.ByteBuffer;
    35 import java.nio.channels.*;
    35 import java.nio.channels.*;
    36 import java.nio.file.Files;
    36 import java.nio.file.Files;
    37 import java.nio.file.Path;
    37 import java.nio.file.Path;
       
    38 import java.nio.file.Paths;
    38 import java.util.LinkedList;
    39 import java.util.LinkedList;
    39 import java.util.List;
    40 import java.util.List;
    40 
    41 
    41 public class Basic {
    42 public class Basic {
    42     static int sockRxBufsize, sockTxBufsize;
    43     static int sockRxBufsize, sockTxBufsize;
    43     static boolean nagle;
    44     static boolean nagle;
       
    45     static String tempDir;
       
    46 
       
    47     static {
       
    48         try {
       
    49             Path parent = Paths.get(".");
       
    50             Path child = Files.createTempDirectory(parent, null);
       
    51             tempDir = child.toString();
       
    52         } catch (IOException e) {
       
    53             tempDir = null;
       
    54         }
       
    55     }
    44 
    56 
    45     public static void main(String args[]) throws Exception {
    57     public static void main(String args[]) throws Exception {
    46         if (args.length != 3)
    58         if (args.length != 3)
    47             usage();
    59             usage();
    48 
    60 
    49 	if (!supported()) {
    61         if (!supported()) {
    50 	    System.out.println("Unix domain channels not supported");
    62             System.out.println("Unix domain channels not supported");
    51 	    return;
    63             return;
    52 	}
    64         }
    53         sockRxBufsize = getInt(args[0]);
    65         sockRxBufsize = getInt(args[0]);
    54         sockTxBufsize = getInt(args[1]);
    66         sockTxBufsize = getInt(args[1]);
    55         if (args[2].equals("nagle-on"))
    67         if (args[2].equals("nagle-on"))
    56             nagle = true;
    68             nagle = true;
    57         else if (args[2].equals("nagle-off"))
    69         else if (args[2].equals("nagle-off"))
    63         test(16 * 1024, 10000);
    75         test(16 * 1024, 10000);
    64         test(32 * 1024, 10000);
    76         test(32 * 1024, 10000);
    65     }
    77     }
    66 
    78 
    67     static boolean supported() {
    79     static boolean supported() {
    68 	try {
    80         try {
    69 	    SocketChannel.open(StandardProtocolFamily.UNIX);
    81             SocketChannel.open(StandardProtocolFamily.UNIX);
    70 	} catch (UnsupportedAddressTypeException e) {
    82         } catch (UnsupportedAddressTypeException e) {
    71 	    return false;
    83             return false;
    72 	} catch (Exception e) {
    84         } catch (Exception e) {
    73 	    return true; // continue test to see what problem is
    85             return true; // continue test to see what problem is
    74 	}
    86         }
    75 	return true;
    87         return true;
    76     }
    88     }
    77 
    89 
    78     static int getInt(String s) {
    90     static int getInt(String s) {
    79         if (s.equalsIgnoreCase("default"))
    91         if (s.equalsIgnoreCase("default"))
    80             return -1;
    92             return -1;
   161         SocketAddress address;
   173         SocketAddress address;
   162         SocketChannel connection;
   174         SocketChannel connection;
   163         SelectionKey ckey;
   175         SelectionKey ckey;
   164         List<ByteBuffer> toSend = new LinkedList<>();
   176         List<ByteBuffer> toSend = new LinkedList<>();
   165         final int bufsize;
   177         final int bufsize;
       
   178         Path sockfile;
   166 
   179 
   167         Server(ProtocolFamily family, int bufsize) throws IOException {
   180         Server(ProtocolFamily family, int bufsize) throws IOException {
   168             setDaemon(true);
   181             //setDaemon(true);
   169             SocketAddress addr;
   182             SocketAddress addr;
   170             this.bufsize = bufsize;
   183             this.bufsize = bufsize;
   171             if (family == StandardProtocolFamily.UNIX) {
   184             if (family == StandardProtocolFamily.UNIX) {
   172                 server = ServerSocketChannel.open(family);
   185                 server = ServerSocketChannel.open(family);
   173                 Path sockfile = Path.of("server.sock");
   186                 sockfile = Path.of(tempDir, "server.sock");
   174                 Files.deleteIfExists(sockfile);
   187                 Files.deleteIfExists(sockfile);
   175                 addr = new UnixDomainSocketAddress(sockfile);
   188                 addr = new UnixDomainSocketAddress(sockfile);
       
   189                 System.out.println("ADDR = " + addr);
   176             } else {
   190             } else {
   177                 server = ServerSocketChannel.open();
   191                 server = ServerSocketChannel.open();
   178                 addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
   192                 addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
   179             }
   193             }
   180             server.bind(addr);
   194             server.bind(addr);
   210                             ByteBuffer buf = ByteBuffer.allocate(bufsize);
   224                             ByteBuffer buf = ByteBuffer.allocate(bufsize);
   211                             SocketChannel channel = (SocketChannel)key.attachment();
   225                             SocketChannel channel = (SocketChannel)key.attachment();
   212                             int m = channel.read(buf);
   226                             int m = channel.read(buf);
   213                             if (m == -1) {
   227                             if (m == -1) {
   214                                 channel.close();
   228                                 channel.close();
   215                                 server.close();
       
   216                                 return;
   229                                 return;
   217                             } else {
   230                             } else {
   218                                 buf.flip();
   231                                 buf.flip();
   219                                 // ECHO
   232                                 // ECHO
   220                                 toSend.add(buf);
   233                                 toSend.add(buf);
   234                     }
   247                     }
   235                     keys.clear();
   248                     keys.clear();
   236                 }
   249                 }
   237             } catch (IOException e) {
   250             } catch (IOException e) {
   238                 throw new RuntimeException(e);
   251                 throw new RuntimeException(e);
       
   252             } finally {
       
   253                 try {
       
   254                     if (server.isOpen()) server.close();
       
   255                     if (sockfile != null)
       
   256                         Files.deleteIfExists(sockfile);
       
   257                 } catch (IOException ee) {}
   239             }
   258             }
   240         }
   259         }
   241     }
   260     }
   242 
   261 
   243     static void fill(ByteBuffer buf) {
   262     static void fill(ByteBuffer buf) {