use of org.jboss.remoting3.MessageCancelledException in project jboss-remoting by jboss-remoting.
the class ChannelTestBase method testWriteCancel.
public void testWriteCancel(final byte[] data) throws IOException, InterruptedException {
final AtomicBoolean wasOk = new AtomicBoolean();
final AtomicReference<IOException> exRef = new AtomicReference<IOException>();
final CountDownLatch latch = new CountDownLatch(1);
recvChannel.receiveMessage(new Channel.Receiver() {
public void handleError(final Channel channel, final IOException error) {
error.printStackTrace();
exRef.set(error);
latch.countDown();
}
public void handleEnd(final Channel channel) {
System.out.println("End of channel");
latch.countDown();
}
public void handleMessage(final Channel channel, final MessageInputStream message) {
new Thread(new Runnable() {
public void run() {
final byte[] received = new byte[TEST_FILE_LENGTH];
int c = 0;
try {
System.out.println("Message received");
int r;
do {
r = message.read(received, c, TEST_FILE_LENGTH - c);
if (r == -1) {
break;
}
c += r;
} while (c < TEST_FILE_LENGTH);
if (r != -1) {
r = message.read();
}
message.close();
} catch (MessageCancelledException e) {
System.out.println("Value of c at message cancelled is " + c);
int i = 0;
while (i < c) {
if (data[i] != received[i]) {
break;
}
i++;
}
wasOk.set(i == c);
} catch (IOException e) {
exRef.set(e);
} finally {
IoUtils.safeClose(message);
latch.countDown();
}
}
}).start();
}
});
MessageOutputStream messageOutputStream = sendChannel.writeMessage();
messageOutputStream.write(data);
messageOutputStream.cancel();
messageOutputStream.close();
// close should be idempotent
messageOutputStream.close();
// no effect expected, since message is closed
messageOutputStream.flush();
latch.await();
IOException exception = exRef.get();
if (exception != null) {
throw exception;
}
assertTrue(wasOk.get());
}
use of org.jboss.remoting3.MessageCancelledException in project jboss-remoting by jboss-remoting.
the class InboundMessage method handleIncoming.
void handleIncoming(Pooled<ByteBuffer> pooledBuffer) {
boolean eof;
boolean free = true;
try {
synchronized (inputStream) {
ByteBuffer buffer = pooledBuffer.getResource();
final int bufRemaining = buffer.remaining();
if ((inboundWindow -= bufRemaining) < 0) {
channel.getRemoteConnection().handleException(new IOException("Input overrun"));
return;
}
if (log.isTraceEnabled()) {
log.tracef("Received message (chan %08x msg %04x) (%d-%d=%d remaining)", Integer.valueOf(channel.getChannelId()), Short.valueOf(messageId), Integer.valueOf(inboundWindow + bufRemaining), Integer.valueOf(bufRemaining), Integer.valueOf(inboundWindow));
}
buffer.position(buffer.position() - 1);
byte flags = buffer.get();
eof = (flags & Protocol.MSG_FLAG_EOF) != 0;
boolean cancelled = (flags & Protocol.MSG_FLAG_CANCELLED) != 0;
if (bufRemaining > remaining) {
cancelled = true;
doClose();
}
if (cancelled) {
this.cancelled = true;
// make sure it goes through
inputStream.pushException(new MessageCancelledException());
}
if (streamClosed) {
// ignore, but keep the bits flowing
if (!eof && !closeSent) {
// we don't need to acknowledge if it's EOF or if we sent a close msg since no more data is coming anyway
// "consume" everything
buffer.position(buffer.limit());
doAcknowledge(pooledBuffer);
}
} else if (!cancelled) {
remaining -= bufRemaining;
free = false;
inputStream.push(pooledBuffer);
}
if (eof) {
eofReceived = true;
if (!streamClosed) {
inputStream.pushEof();
}
channel.freeInboundMessage(messageId);
// if the peer is old, they might reuse the ID now regardless of us; if new, we have to send the close message to acknowledge the remainder
doSendCloseMessage();
}
}
} finally {
if (free)
pooledBuffer.free();
}
}
use of org.jboss.remoting3.MessageCancelledException in project jboss-remoting by jboss-remoting.
the class RemoteConnectionChannel method closeMessages.
private void closeMessages() {
final List<InboundMessage> exceptionMessages;
final List<OutboundMessage> cancelMessages;
final List<InboundMessage> terminateMessages;
synchronized (connection.getLock()) {
exceptionMessages = new ArrayList<InboundMessage>(inboundMessages);
cancelMessages = new ArrayList<OutboundMessage>(outboundMessages);
terminateMessages = new ArrayList<InboundMessage>(inboundMessageQueue);
inboundMessageQueue.clear();
}
for (final InboundMessage message : exceptionMessages) {
message.inputStream.pushException(new MessageCancelledException());
}
for (final OutboundMessage message : cancelMessages) {
message.cancel();
}
for (final InboundMessage message : terminateMessages) {
message.terminate();
}
}
Aggregations