use of com.sun.nio.sctp.SctpChannel in project jdk8u_jdk by JetBrains.
the class Bind method testBindUnbind.
void testBindUnbind(boolean connected) {
SctpChannel channel = null;
SctpChannel peerChannel = null;
debug("testBindUnbind, connected: " + connected);
try {
channel = SctpChannel.open();
List<InetAddress> addresses = Util.getAddresses(true, false);
Iterator iterator = addresses.iterator();
InetSocketAddress a = new InetSocketAddress((InetAddress) iterator.next(), 0);
debug("channel.bind( " + a + ")");
channel.bind(a);
while (iterator.hasNext()) {
InetAddress ia = (InetAddress) iterator.next();
debug("channel.bindAddress(" + ia + ")");
channel.bindAddress(ia);
}
if (debug) {
Util.dumpAddresses(channel, out);
}
if (connected) {
/* Test with connected channel */
peerChannel = connectChannel(channel);
}
/* TEST 1: bind/unbindAddresses on the system addresses */
debug("bind/unbindAddresses on the system addresses");
List<InetAddress> addrs = Util.getAddresses(true, false);
for (InetAddress addr : addrs) {
try {
debug("unbindAddress: " + addr);
check(boundAddress(channel, addr), "trying to remove address that is not bound");
channel.unbindAddress(addr);
if (debug) {
Util.dumpAddresses(channel, out);
}
check(!boundAddress(channel, addr), "address was not removed");
debug("bindAddress: " + addr);
channel.bindAddress(addr);
if (debug) {
Util.dumpAddresses(channel, out);
}
check(boundAddress(channel, addr), "address is not bound");
} catch (IOException ioe) {
unexpected(ioe);
}
}
/* TEST 2: bindAddress - already bound address. */
InetAddress againAddress = addrs.get(0);
try {
debug("bind already bound address " + againAddress);
channel.bindAddress(againAddress);
} catch (AlreadyBoundException unused) {
debug("Caught AlreadyBoundException - OK");
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
/* TEST 3: bind non local address */
try {
InetAddress nla = InetAddress.getByName("123.123.123.123");
debug("bind non local address " + nla);
channel.bindAddress(nla);
} catch (IOException ioe) {
debug("Informative only " + ioe);
}
/* TEST 4: unbind address that is not bound */
try {
debug("unbind address that is not bound " + againAddress);
/* remove address first then again */
channel.unbindAddress(againAddress);
channel.unbindAddress(againAddress);
} catch (IllegalUnbindException unused) {
debug("Caught IllegalUnbindException - OK");
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
/* TEST 5: unbind address that is not bound */
try {
InetAddress nla = InetAddress.getByName("123.123.123.123");
debug("unbind address that is not bound " + nla);
channel.unbindAddress(nla);
} catch (IllegalUnbindException unused) {
debug("Caught IllegalUnbindException - OK");
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
if (connected) {
channel.shutdown();
BindNotificationHandler handler = new BindNotificationHandler();
ByteBuffer buffer = ByteBuffer.allocate(10);
MessageInfo info;
while ((info = peerChannel.receive(buffer, null, handler)) != null) {
if (info != null) {
if (info.bytes() == -1) {
debug("peerChannel Reached EOF");
break;
}
}
}
while ((info = channel.receive(buffer, null, handler)) != null) {
if (info != null) {
if (info.bytes() == -1) {
debug("channel Reached EOF");
break;
}
}
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (channel != null)
channel.close();
} catch (IOException ioe) {
unexpected(ioe);
}
}
}
use of com.sun.nio.sctp.SctpChannel in project jdk8u_jdk by JetBrains.
the class Bind method testBind.
void testBind() {
SctpChannel channel = null;
try {
channel = SctpChannel.open();
/* TEST 1: empty set if channel is not bound */
check(channel.getAllLocalAddresses().isEmpty(), "getAllLocalAddresses returned non empty set for unbound channel");
/* TEST 2: null to bind the channel to an automatically assigned
* socket address */
channel.bind(null);
/* TEST 3: non empty set if the channel is bound */
check(!channel.getAllLocalAddresses().isEmpty(), "getAllLocalAddresses returned empty set for bound channel");
debug("getAllLocalAddresses on channel bound to the wildcard:\n" + channel.getAllLocalAddresses());
/* TEST 4: AlreadyBoundException if this channel is already bound */
try {
channel.bind(null);
} catch (AlreadyBoundException unused) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
/* TEST 5: UnsupportedAddressTypeException */
try {
channel.close();
/* open a new unbound channel for test */
channel = SctpChannel.open();
channel.bind(new UnsupportedSocketAddress());
fail("UnsupportedSocketAddress expected");
} catch (UnsupportedAddressTypeException unused) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
/* TEST 6: AlreadyConnectedException */
try {
channel.close();
/* open a new unbound channel for test */
channel = SctpChannel.open();
connectChannel(channel);
channel.bind(null);
fail("AlreadyConnectedException expected");
} catch (AlreadyConnectedException unused) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
/* TEST 7: ClosedChannelException - If this channel is closed */
try {
channel.close();
/* open a new unbound channel for test */
channel = SctpChannel.open();
channel.close();
channel.bind(null);
fail("ClosedChannelException expected");
} catch (ClosedChannelException unused) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
/* TEST 8: ClosedChannelException if channel is closed */
try {
channel.getAllLocalAddresses();
fail("should have thrown ClosedChannelException");
} catch (ClosedChannelException cce) {
pass();
} catch (Exception ioe) {
unexpected(ioe);
}
} catch (IOException ioe) {
unexpected(ioe);
} finally {
try {
channel.close();
} catch (IOException ioe) {
unexpected(ioe);
}
}
}
use of com.sun.nio.sctp.SctpChannel in project jdk8u_jdk by JetBrains.
the class Shutdown method doTest.
void doTest(SocketAddress peerAddress) {
SctpChannel channel = null;
ByteBuffer buffer = ByteBuffer.allocate(Util.SMALL_BUFFER);
MessageInfo info;
try {
channel = SctpChannel.open();
/* TEST 1: Verify NotYetConnectedException thrown */
debug("Test 1: NotYetConnectedException");
try {
channel.shutdown();
fail("shutdown not throwing expected NotYetConnectedException");
} catch (NotYetConnectedException unused) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
channel.connect(peerAddress);
sentLatch.await();
channel.shutdown();
/* TEST 2: receive data sent before shutdown */
do {
debug("Test 2: invoking receive");
info = channel.receive(buffer, null, null);
if (info == null) {
fail("unexpected null from receive");
return;
}
} while (!info.isComplete());
buffer.flip();
check(info != null, "info is null");
check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").length, "bytes received not equal to message length");
check(info.bytes() == buffer.remaining(), "bytes != remaining");
check(Util.compare(buffer, Util.SMALL_MESSAGE), "received message not the same as sent message");
buffer.clear();
/* TEST 3: receive notifications on the SCTP stack */
debug("Test 3: receive notifications");
while ((info = channel.receive(buffer, null, null)) != null && info.bytes() != -1) ;
/* TEST 4: If the channel is already shutdown then invoking this
* method has no effect. */
debug("Test 4: no-op");
try {
channel.shutdown();
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
/* TEST 5: Further sends will throw ClosedChannelException */
debug("Test 5: ClosedChannelException");
info = MessageInfo.createOutgoing(null, 1);
try {
channel.send(buffer, info);
fail("shutdown not throwing expected ClosedChannelException");
} catch (ClosedChannelException unused) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
/* TEST 6: getRemoteAddresses */
debug("Test 6: getRemoteAddresses");
try {
java.util.Set<SocketAddress> remoteAddrs = channel.getRemoteAddresses();
check(remoteAddrs.isEmpty(), "A shutdown channel should not have remote addresses");
} catch (IOException ioe) {
unexpected(ioe);
}
} catch (IOException ioe) {
unexpected(ioe);
} catch (InterruptedException ie) {
unexpected(ie);
} finally {
finishedLatch.countDown();
try {
if (channel != null)
channel.close();
} catch (IOException e) {
unexpected(e);
}
}
}
use of com.sun.nio.sctp.SctpChannel in project jdk8u_jdk by JetBrains.
the class Bind method connectChannel.
SctpChannel connectChannel(SctpChannel channel) throws IOException {
debug("connecting channel...");
try {
SctpServerChannel ssc = SctpServerChannel.open();
ssc.bind(null);
Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
Iterator<SocketAddress> iterator = addrs.iterator();
SocketAddress addr = iterator.next();
debug("using " + addr + "...");
channel.connect(addr);
SctpChannel peerChannel = ssc.accept();
ssc.close();
debug("connected");
return peerChannel;
} catch (IOException ioe) {
debug("Cannot connect channel");
unexpected(ioe);
throw ioe;
}
}
use of com.sun.nio.sctp.SctpChannel in project jdk8u_jdk by JetBrains.
the class ReceiveIntoDirect method doTest.
void doTest(SocketAddress peerAddress, int bufferSize, int bufferOffset) throws IOException {
debug("\n\nTesting with bufferSize " + bufferSize + " and offset " + bufferOffset);
assert bufferOffset + msgBytes.length <= bufferSize : "buffer offset + message length greater than buffer size ";
ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
MessageInfo info;
try (SctpChannel channel = SctpChannel.open()) {
channel.connect(peerAddress);
ReceiveNotificationHandler handler = new ReceiveNotificationHandler();
/* TEST 1: Assoc/peer change notif into direct buffer with offest */
do {
debug("Test 1: Assoc/peer change with offset " + bufferOffset);
buffer.position(bufferOffset);
info = channel.receive(buffer, null, handler);
if (info == null) {
fail("unexpected null from receive");
return;
}
} while (!info.isComplete());
buffer.flip().position(bufferOffset);
check(handler.receivedCommUp(), "SCTP_COMM_UP not received");
check(info != null, "info is null");
check(info.address() != null, "address is null");
check(info.association() != null, "association is null");
check(info.isComplete(), "message is not complete");
check(info.isUnordered() != true, "message should not be unordered");
check(info.streamNumber() >= 0, "invalid stream number");
check(info.bytes() == msgBytes.length, "bytes received not equal to message length");
check(info.bytes() == buffer.remaining(), "bytes != remaining");
check(Util.compare(buffer, msgBytes), "received message not the same as sent message");
/* TEST 2: shutdown notification with offset */
debug("Test 2: shutdown notif with offset " + bufferOffset);
buffer.clear().position(bufferOffset);
while ((info = channel.receive(buffer, null, handler)) != null && info.bytes() != -1) ;
}
}
Aggregations