use of com.sun.nio.sctp.MessageInfo 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);
}
}
}
}
use of com.sun.nio.sctp.MessageInfo 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