Search in sources :

Example 1 with MessageOutputStream

use of org.jboss.remoting3.MessageOutputStream in project wildfly-core by wildfly.

the class AbstractMessageHandler method writeErrorResponse.

/**
 * Write an error response.
 *
 * @param channel the channel
 * @param header the request
 * @param error the error
 * @throws IOException
 */
protected static void writeErrorResponse(final Channel channel, final ManagementRequestHeader header, final Throwable error) throws IOException {
    final ManagementResponseHeader response = ManagementResponseHeader.create(header, error);
    final MessageOutputStream output = channel.writeMessage();
    try {
        writeHeader(response, output);
        output.close();
    } finally {
        StreamUtils.safeClose(output);
    }
}
Also used : MessageOutputStream(org.jboss.remoting3.MessageOutputStream)

Example 2 with MessageOutputStream

use of org.jboss.remoting3.MessageOutputStream in project jboss-remoting by jboss-remoting.

the class ChannelTestBase method testSimpleWriteMethodTwoWay.

@Test
public void testSimpleWriteMethodTwoWay() throws Exception {
    Byte[] bytes = new Byte[] { 1, 2, 3 };
    Byte[] manipulatedBytes = new Byte[] { 2, 4, 6 };
    MessageOutputStream out = sendChannel.writeMessage();
    for (int i = 0; i < bytes.length; i++) {
        out.write(bytes[i]);
    }
    out.close();
    final CountDownLatch latch = new CountDownLatch(2);
    final ArrayList<Byte> senderResult = new ArrayList<Byte>();
    final ArrayList<Byte> receiverResult = new ArrayList<Byte>();
    final AtomicReference<IOException> exRef = new AtomicReference<IOException>();
    recvChannel.receiveMessage(new Channel.Receiver() {

        public void handleError(final Channel channel, final IOException error) {
            error.printStackTrace();
            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) {
            System.out.println("Message received on receiver");
            try {
                int i = message.read();
                while (i != -1) {
                    receiverResult.add((byte) i);
                    System.out.println("read " + i);
                    i = message.read();
                }
                message.close();
                MessageOutputStream out = channel.writeMessage();
                try {
                    for (Byte b : receiverResult) {
                        byte send = (byte) (b * 2);
                        System.out.println("Sending back " + send);
                        out.write(send);
                    }
                } finally {
                    out.close();
                    // close should be idempotent
                    out.close();
                    // no effect expected, since message is closed
                    out.flush();
                }
                System.out.println("Done writing");
            } catch (IOException e) {
                exRef.set(e);
            } finally {
                IoUtils.safeClose(message);
                latch.countDown();
            }
        }
    });
    sendChannel.receiveMessage(new Channel.Receiver() {

        public void handleError(final Channel channel, final IOException error) {
            error.printStackTrace();
            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) {
            System.out.println("Message received on sender");
            try {
                int i = message.read();
                while (i != -1) {
                    senderResult.add((byte) i);
                    i = message.read();
                }
                message.close();
            } catch (IOException e) {
                exRef.set(e);
            } finally {
                IoUtils.safeClose(message);
                latch.countDown();
            }
        }
    });
    latch.await();
    assertNull(exRef.get());
    Byte[] receiverBytes = receiverResult.toArray(new Byte[receiverResult.size()]);
    assertArrayEquals(bytes, receiverBytes);
    Byte[] senderBytes = senderResult.toArray(new Byte[senderResult.size()]);
    assertArrayEquals(manipulatedBytes, senderBytes);
}
Also used : MessageInputStream(org.jboss.remoting3.MessageInputStream) Channel(org.jboss.remoting3.Channel) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) MessageOutputStream(org.jboss.remoting3.MessageOutputStream) Test(org.junit.Test)

Example 3 with MessageOutputStream

use of org.jboss.remoting3.MessageOutputStream in project jboss-remoting by jboss-remoting.

the class ChannelTestBase method testEmptyMessage.

@Test
public void testEmptyMessage() throws IOException, InterruptedException {
    final AtomicBoolean wasEmpty = 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) {
            System.out.println("Message received");
            try {
                if (message.read() == -1) {
                    wasEmpty.set(true);
                }
                message.close();
            } catch (IOException e) {
                exRef.set(e);
            } finally {
                IoUtils.safeClose(message);
                latch.countDown();
            }
        }
    });
    MessageOutputStream messageOutputStream = sendChannel.writeMessage();
    messageOutputStream.close();
    latch.await();
    IOException exception = exRef.get();
    if (exception != null) {
        throw exception;
    }
    assertTrue(wasEmpty.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageOutputStream(org.jboss.remoting3.MessageOutputStream) MessageInputStream(org.jboss.remoting3.MessageInputStream) Channel(org.jboss.remoting3.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with MessageOutputStream

use of org.jboss.remoting3.MessageOutputStream in project jboss-remoting by jboss-remoting.

the class ChannelTestBase method testLotsOfContent.

@Test
public void testLotsOfContent() throws IOException, InterruptedException {
    final AtomicBoolean wasOk = new AtomicBoolean();
    final AtomicReference<IOException> exRef = new AtomicReference<IOException>();
    final CountDownLatch latch = new CountDownLatch(1);
    InputStream stream = ChannelTestBase.class.getResourceAsStream("/test-content.bin");
    assertNotNull(stream);
    final byte[] data;
    try {
        data = new byte[TEST_FILE_LENGTH];
        int c = 0;
        do {
            int r = stream.read(data, c, TEST_FILE_LENGTH - c);
            if (r == -1) {
                break;
            }
            c += r;
        } while (c < TEST_FILE_LENGTH);
        stream.close();
    } finally {
        IoUtils.safeClose(stream);
    }
    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() {
                    try {
                        System.out.println("Message received");
                        final byte[] received = new byte[TEST_FILE_LENGTH];
                        int c = 0;
                        do {
                            int r = message.read(received, c, TEST_FILE_LENGTH - c);
                            if (r == -1) {
                                break;
                            }
                            c += r;
                        } while (c < TEST_FILE_LENGTH);
                        message.close();
                        assertArrayEquals(data, received);
                        wasOk.set(true);
                    } catch (IOException e) {
                        exRef.set(e);
                    } finally {
                        IoUtils.safeClose(message);
                        latch.countDown();
                    }
                }
            }).start();
        }
    });
    MessageOutputStream messageOutputStream = sendChannel.writeMessage();
    messageOutputStream.write(data);
    messageOutputStream.close();
    // close should be idempotent
    messageOutputStream.close();
    // no effect expected, since message is closed
    messageOutputStream.flush();
    messageOutputStream.flush();
    messageOutputStream.flush();
    latch.await();
    IOException exception = exRef.get();
    if (exception != null) {
        throw exception;
    }
    assertTrue(wasOk.get());
}
Also used : MessageInputStream(org.jboss.remoting3.MessageInputStream) MessageInputStream(org.jboss.remoting3.MessageInputStream) InputStream(java.io.InputStream) Channel(org.jboss.remoting3.Channel) 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) Test(org.junit.Test)

Example 5 with MessageOutputStream

use of org.jboss.remoting3.MessageOutputStream in project jboss-remoting by jboss-remoting.

the class ChannelTestBase method testSimpleWriteMethodFromNonInitiatingSide.

@Test
public void testSimpleWriteMethodFromNonInitiatingSide() throws Exception {
    Byte[] bytes = new Byte[] { 1, 2, 3 };
    MessageOutputStream out = recvChannel.writeMessage();
    for (int i = 0; i < bytes.length; i++) {
        out.write(bytes[i]);
    }
    out.close();
    final CountDownLatch latch = new CountDownLatch(1);
    final ArrayList<Byte> result = new ArrayList<Byte>();
    final AtomicReference<IOException> exRef = new AtomicReference<IOException>();
    sendChannel.receiveMessage(new Channel.Receiver() {

        public void handleError(final Channel channel, final IOException error) {
            error.printStackTrace();
            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) {
            System.out.println("Message received");
            try {
                int i = message.read();
                while (i != -1) {
                    result.add((byte) i);
                    i = message.read();
                }
                message.close();
            } catch (IOException e) {
                exRef.set(e);
            } finally {
                IoUtils.safeClose(message);
                latch.countDown();
            }
        }
    });
    latch.await();
    assertNull(exRef.get());
    Byte[] resultBytes = result.toArray(new Byte[result.size()]);
    assertArrayEquals(bytes, resultBytes);
}
Also used : MessageInputStream(org.jboss.remoting3.MessageInputStream) Channel(org.jboss.remoting3.Channel) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) MessageOutputStream(org.jboss.remoting3.MessageOutputStream) Test(org.junit.Test)

Aggregations

MessageOutputStream (org.jboss.remoting3.MessageOutputStream)19 IOException (java.io.IOException)15 MessageInputStream (org.jboss.remoting3.MessageInputStream)12 Channel (org.jboss.remoting3.Channel)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Test (org.junit.Test)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 ArrayList (java.util.ArrayList)3 BlockingInvocation (org.jboss.remoting3.util.BlockingInvocation)3 InvocationTracker (org.jboss.remoting3.util.InvocationTracker)3 SystemException (javax.transaction.SystemException)2 XAException (javax.transaction.xa.XAException)2 ManagementResponseHeader (org.jboss.as.protocol.mgmt.ManagementResponseHeader)2 RequestSendFailedException (org.jboss.ejb.client.RequestSendFailedException)2 Marshaller (org.jboss.marshalling.Marshaller)2 EOFException (java.io.EOFException)1 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 URI (java.net.URI)1