Search in sources :

Example 1 with MessageInputStream

use of org.jboss.remoting3.MessageInputStream 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 2 with MessageInputStream

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

the class ChannelTestBase method testSimpleWriteMethodWithWrappedOuputStream.

@Test
public void testSimpleWriteMethodWithWrappedOuputStream() throws Exception {
    Byte[] bytes = new Byte[] { 1, 2, 3 };
    FilterOutputStream out = new FilterOutputStream(sendChannel.writeMessage());
    for (int i = 0; i < bytes.length; i++) {
        out.write(bytes[i]);
    }
    // The close() method of FilterOutputStream will flush the underlying output stream before closing it,
    // so we end up with two messages
    out.close();
    final CountDownLatch latch = new CountDownLatch(1);
    final ArrayList<Byte> result = 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");
            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) FilterOutputStream(java.io.FilterOutputStream) Test(org.junit.Test)

Example 3 with MessageInputStream

use of org.jboss.remoting3.MessageInputStream 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 MessageInputStream

use of org.jboss.remoting3.MessageInputStream 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 MessageInputStream

use of org.jboss.remoting3.MessageInputStream 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

IOException (java.io.IOException)17 MessageInputStream (org.jboss.remoting3.MessageInputStream)17 Channel (org.jboss.remoting3.Channel)12 MessageOutputStream (org.jboss.remoting3.MessageOutputStream)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.Test)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 ArrayList (java.util.ArrayList)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 SystemException (javax.transaction.SystemException)3 XAException (javax.transaction.xa.XAException)3 Unmarshaller (org.jboss.marshalling.Unmarshaller)3 BlockingInvocation (org.jboss.remoting3.util.BlockingInvocation)3 InvocationTracker (org.jboss.remoting3.util.InvocationTracker)3 Endpoint (org.jboss.remoting3.Endpoint)2 IoFuture (org.xnio.IoFuture)2 EOFException (java.io.EOFException)1 FilterOutputStream (java.io.FilterOutputStream)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1