Search in sources :

Example 1 with MessageCancelledException

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());
}
Also used : MessageInputStream(org.jboss.remoting3.MessageInputStream) Channel(org.jboss.remoting3.Channel) MessageCancelledException(org.jboss.remoting3.MessageCancelledException) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageOutputStream(org.jboss.remoting3.MessageOutputStream)

Example 2 with MessageCancelledException

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();
    }
}
Also used : MessageCancelledException(org.jboss.remoting3.MessageCancelledException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 3 with MessageCancelledException

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();
    }
}
Also used : MessageCancelledException(org.jboss.remoting3.MessageCancelledException)

Aggregations

MessageCancelledException (org.jboss.remoting3.MessageCancelledException)3 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Channel (org.jboss.remoting3.Channel)1 MessageInputStream (org.jboss.remoting3.MessageInputStream)1 MessageOutputStream (org.jboss.remoting3.MessageOutputStream)1