Search in sources :

Example 16 with Channel

use of com.automatak.dnp3.Channel 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 17 with Channel

use of com.automatak.dnp3.Channel 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 18 with Channel

use of com.automatak.dnp3.Channel 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 19 with Channel

use of com.automatak.dnp3.Channel 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 20 with Channel

use of com.automatak.dnp3.Channel 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

Channel (org.jboss.remoting3.Channel)41 IOException (java.io.IOException)29 Test (org.junit.Test)14 Connection (org.jboss.remoting3.Connection)12 MessageInputStream (org.jboss.remoting3.MessageInputStream)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 OpenListener (org.jboss.remoting3.OpenListener)10 MessageOutputStream (org.jboss.remoting3.MessageOutputStream)9 InetSocketAddress (java.net.InetSocketAddress)8 URI (java.net.URI)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 IoFuture (org.xnio.IoFuture)7 Channel (com.google.cloud.video.livestream.v1.Channel)6 LivestreamServiceClient (com.google.cloud.video.livestream.v1.LivestreamServiceClient)6 URISyntaxException (java.net.URISyntaxException)6 ManagementClientChannelStrategy (org.jboss.as.protocol.mgmt.ManagementClientChannelStrategy)6 FutureResult (org.xnio.FutureResult)6 ManagementChannelHandler (org.jboss.as.protocol.mgmt.ManagementChannelHandler)5 Endpoint (org.jboss.remoting3.Endpoint)5 ProtocolConnectionConfiguration (org.jboss.as.protocol.ProtocolConnectionConfiguration)4