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); |