6525
|
1 |
/*
|
|
2 |
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
import com.oracle.net.Sdp;
|
|
25 |
|
|
26 |
import java.net.*;
|
|
27 |
import java.io.*;
|
|
28 |
import java.nio.channels.*;
|
|
29 |
import java.util.*;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Exercise com.oracle.net.Sdp with each IP address plumbed to InfiniBand
|
|
33 |
* interfaces listed in a given file.
|
|
34 |
*/
|
|
35 |
|
|
36 |
public class Sanity {
|
|
37 |
public static void main(String[] args) throws Exception {
|
|
38 |
// The file is a list of interfaces to test.
|
|
39 |
Scanner s = new Scanner(new File(args[0]));
|
|
40 |
try {
|
|
41 |
while (s.hasNextLine()) {
|
|
42 |
String link = s.nextLine();
|
|
43 |
NetworkInterface ni = NetworkInterface.getByName(link);
|
|
44 |
if (ni != null) {
|
|
45 |
Enumeration<InetAddress> addrs = ni.getInetAddresses();
|
|
46 |
while (addrs.hasMoreElements()) {
|
|
47 |
InetAddress addr = addrs.nextElement();
|
|
48 |
System.out.format("Testing %s: %s\n", link, addr.getHostAddress());
|
|
49 |
test(addr);
|
|
50 |
}
|
|
51 |
}
|
|
52 |
}
|
|
53 |
} finally {
|
|
54 |
s.close();
|
|
55 |
}
|
|
56 |
}
|
|
57 |
|
|
58 |
static void test(InetAddress addr) throws Exception {
|
|
59 |
// Test SocketChannel and ServerSocketChannel
|
|
60 |
ServerSocketChannel ssc = Sdp.openServerSocketChannel();
|
|
61 |
try {
|
|
62 |
ssc.socket().bind(new InetSocketAddress(addr, 0));
|
|
63 |
int port = ssc.socket().getLocalPort();
|
|
64 |
|
|
65 |
// SocketChannel.connect (implicit bind)
|
|
66 |
SocketChannel client = Sdp.openSocketChannel();
|
|
67 |
try {
|
|
68 |
client.connect(new InetSocketAddress(addr, port));
|
|
69 |
SocketChannel peer = ssc.accept();
|
|
70 |
try {
|
|
71 |
testConnection(Channels.newOutputStream(client),
|
|
72 |
Channels.newInputStream(peer));
|
|
73 |
} finally {
|
|
74 |
peer.close();
|
|
75 |
}
|
|
76 |
} finally {
|
|
77 |
client.close();
|
|
78 |
}
|
|
79 |
|
|
80 |
// SocketChannel.connect (explicit bind)
|
|
81 |
client = Sdp.openSocketChannel();
|
|
82 |
try {
|
|
83 |
client.socket().bind(new InetSocketAddress(addr, 0));
|
|
84 |
client.connect(new InetSocketAddress(addr, port));
|
|
85 |
ssc.accept().close();
|
|
86 |
} finally {
|
|
87 |
client.close();
|
|
88 |
}
|
|
89 |
} finally {
|
|
90 |
ssc.close();
|
|
91 |
}
|
|
92 |
|
|
93 |
// Test Socket and ServerSocket
|
|
94 |
ServerSocket ss = Sdp.openServerSocket();
|
|
95 |
try {
|
|
96 |
ss.bind(new InetSocketAddress(addr, 0));
|
|
97 |
int port = ss.getLocalPort();
|
|
98 |
|
|
99 |
// Socket.connect (implicit bind)
|
|
100 |
Socket s = Sdp.openSocket();
|
|
101 |
try {
|
|
102 |
s.connect(new InetSocketAddress(addr, port));
|
|
103 |
Socket peer = ss.accept();
|
|
104 |
try {
|
|
105 |
testConnection(s.getOutputStream(), peer.getInputStream());
|
|
106 |
} finally {
|
|
107 |
peer.close();
|
|
108 |
}
|
|
109 |
} finally {
|
|
110 |
s.close();
|
|
111 |
}
|
|
112 |
|
|
113 |
// Socket.connect (explicit bind)
|
|
114 |
s = Sdp.openSocket();
|
|
115 |
try {
|
|
116 |
s.bind(new InetSocketAddress(addr, 0));
|
|
117 |
s.connect(new InetSocketAddress(addr, port));
|
|
118 |
ss.accept().close();
|
|
119 |
} finally {
|
|
120 |
s.close();
|
|
121 |
}
|
|
122 |
} finally {
|
|
123 |
ss.close();
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
static void testConnection(OutputStream out, InputStream in)
|
|
128 |
throws IOException
|
|
129 |
{
|
|
130 |
byte[] msg = "hello".getBytes();
|
|
131 |
out.write(msg);
|
|
132 |
|
|
133 |
byte[] ba = new byte[100];
|
|
134 |
int nread = 0;
|
|
135 |
while (nread < msg.length) {
|
|
136 |
int n = in.read(ba);
|
|
137 |
if (n < 0)
|
|
138 |
throw new IOException("EOF not expected!");
|
|
139 |
nread += n;
|
|
140 |
}
|
|
141 |
}
|
|
142 |
}
|