use of com.sun.nio.sctp.Association in project jdk8u_jdk by JetBrains.
the class Send method doTest.
void doTest(SocketAddress peerAddress) {
SctpMultiChannel channel = null;
ByteBuffer buffer = ByteBuffer.allocate(Util.LARGE_BUFFER);
MessageInfo info = MessageInfo.createOutgoing(null, 0);
try {
channel = SctpMultiChannel.open();
/* TEST 1: send small message */
int streamNumber = 0;
debug("sending to " + peerAddress + " on stream number: " + streamNumber);
info = MessageInfo.createOutgoing(peerAddress, streamNumber);
buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
buffer.flip();
int position = buffer.position();
int remaining = buffer.remaining();
debug("sending small message: " + buffer);
int sent = channel.send(buffer, info);
check(sent == remaining, "sent should be equal to remaining");
check(buffer.position() == (position + sent), "buffers position should have been incremented by sent");
/* TEST 2: receive the echoed message */
buffer.clear();
info = channel.receive(buffer, null, null);
buffer.flip();
check(info != null, "info is null");
check(info.streamNumber() == streamNumber, "message not sent on the correct stream");
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");
/* TEST 3: send large message */
Set<Association> assocs = channel.associations();
check(assocs.size() == 1, "there should be only one association");
Iterator<Association> it = assocs.iterator();
check(it.hasNext());
Association assoc = it.next();
streamNumber = assoc.maxOutboundStreams() - 1;
debug("sending on stream number: " + streamNumber);
info = MessageInfo.createOutgoing(assoc, null, streamNumber);
buffer.clear();
buffer.put(Util.LARGE_MESSAGE.getBytes("ISO-8859-1"));
buffer.flip();
position = buffer.position();
remaining = buffer.remaining();
debug("sending large message: " + buffer);
sent = channel.send(buffer, info);
check(sent == remaining, "sent should be equal to remaining");
check(buffer.position() == (position + sent), "buffers position should have been incremented by sent");
/* TEST 4: receive the echoed message */
buffer.clear();
info = channel.receive(buffer, null, null);
buffer.flip();
check(info != null, "info is null");
check(info.streamNumber() == streamNumber, "message not sent on the correct stream");
check(info.bytes() == Util.LARGE_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.LARGE_MESSAGE), "received message not the same as sent message");
/* TEST 5: InvalidStreamExcepton */
streamNumber = assoc.maxOutboundStreams() + 1;
info = MessageInfo.createOutgoing(assoc, null, streamNumber);
buffer.clear();
buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
buffer.flip();
position = buffer.position();
remaining = buffer.remaining();
debug("sending on stream number: " + streamNumber);
debug("sending small message: " + buffer);
try {
sent = channel.send(buffer, info);
fail("should have thrown InvalidStreamExcepton");
} catch (InvalidStreamException ise) {
pass();
} catch (IOException ioe) {
unexpected(ioe);
}
check(buffer.remaining() == remaining, "remaining should not be changed");
check(buffer.position() == position, "buffers position should not be changed");
/* TEST 5: getRemoteAddresses(Association) */
channel.getRemoteAddresses(assoc);
/* TEST 6: Send from heap buffer to force implementation to
* substitute with a native buffer, then check that its position
* is updated correctly */
info = MessageInfo.createOutgoing(assoc, null, 0);
buffer.clear();
buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
buffer.flip();
final int offset = 1;
buffer.position(offset);
remaining = buffer.remaining();
try {
sent = channel.send(buffer, info);
check(sent == remaining, "sent should be equal to remaining");
check(buffer.position() == (offset + sent), "buffers position should have been incremented by sent");
} catch (IllegalArgumentException iae) {
fail(iae + ", Error updating buffers position");
}
} catch (IOException ioe) {
unexpected(ioe);
} finally {
clientFinishedLatch.countDown();
try {
serverFinishedLatch.await(10L, TimeUnit.SECONDS);
} catch (InterruptedException ie) {
unexpected(ie);
}
if (channel != null) {
try {
channel.close();
} catch (IOException e) {
unexpected(e);
}
}
}
}
Aggregations