Search in sources :

Example 56 with Channel

use of com.google.cloud.video.livestream.v1.Channel 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 57 with Channel

use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.

the class ChannelTestBase method testRemoteChannelClose.

@Test
public void testRemoteChannelClose() throws Exception {
    final CountDownLatch closedLatch = new CountDownLatch(1);
    sendChannel.addCloseHandler(new CloseHandler<Channel>() {

        public void handleClose(final Channel closed, final IOException exception) {
            closedLatch.countDown();
        }
    });
    sendChannel.receiveMessage(new Channel.Receiver() {

        public void handleError(final Channel channel, final IOException error) {
            channel.closeAsync();
        }

        public void handleEnd(final Channel channel) {
            channel.closeAsync();
        }

        public void handleMessage(final Channel channel, final MessageInputStream message) {
            IoUtils.safeClose(message);
        }
    });
    recvChannel.receiveMessage(new Channel.Receiver() {

        public void handleError(final Channel channel, final IOException error) {
            channel.closeAsync();
        }

        public void handleEnd(final Channel channel) {
            channel.closeAsync();
        }

        public void handleMessage(final Channel channel, final MessageInputStream message) {
            IoUtils.safeClose(message);
        }
    });
    sendChannel.writeShutdown();
    IoUtils.safeClose(recvChannel);
    System.out.println("Waiting for closed");
    closedLatch.await();
    System.out.println("Closed");
}
Also used : MessageInputStream(org.jboss.remoting3.MessageInputStream) Channel(org.jboss.remoting3.Channel) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 58 with Channel

use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.

the class ChannelTestBase method testSimpleWriteMethod.

@Test
public void testSimpleWriteMethod() throws Exception {
    Byte[] bytes = new Byte[] { 1, 2, 3 };
    MessageOutputStream out = sendChannel.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>();
    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) MessageOutputStream(org.jboss.remoting3.MessageOutputStream) Test(org.junit.Test)

Example 59 with Channel

use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.

the class ConnectionTestCase method rejectUnknownService.

@Test
public void rejectUnknownService() throws Exception {
    final Connection connection = AuthenticationContext.empty().with(MatchRule.ALL, AuthenticationConfiguration.empty().useName("bob").usePassword("pass").setSaslMechanismSelector(SaslMechanismSelector.NONE.addMechanism("SCRAM-SHA-256"))).run(new PrivilegedAction<Connection>() {

        public Connection run() {
            try {
                return clientEndpoint.connect(new URI("remote://localhost:30123"), OptionMap.EMPTY).get();
            } catch (IOException | URISyntaxException e) {
                throw new RuntimeException(e);
            }
        }
    });
    final IoFuture<Channel> channelFuture = connection.openChannel("unknown", OptionMap.EMPTY);
    try {
        channelFuture.get();
        fail();
    } catch (CancellationException e) {
        throw e;
    } catch (IOException e) {
    // ok
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) Channel(org.jboss.remoting3.Channel) Connection(org.jboss.remoting3.Connection) IOException(java.io.IOException) URI(java.net.URI) Test(org.junit.Test)

Example 60 with Channel

use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.

the class RemoteServiceWithPredicateTest method tryToConnectToService.

/**
 * Method which registers a service with the provided validation predicate, then tests to see if the service
 * accepts or rejects service Channel creation attempts which are expected by the predicate provided.
 *
 * @param validationPredicate
 * @throws IOException
 * @throws URISyntaxException
 * @throws InterruptedException
 */
public void tryToConnectToService(Predicate<Connection> validationPredicate, boolean shouldSucceed) throws IOException {
    Connection connection;
    Registration serviceRegistration;
    Channel sendChannel = null, recvChannel = null;
    final FutureResult<Channel> passer = new FutureResult<Channel>();
    // register the service with the endpoint using the provided validation predicate
    serviceRegistration = endpoint.registerService("org.jboss.test", new OpenListener() {

        public void channelOpened(final Channel channel) {
            passer.setResult(channel);
        }

        public void registrationTerminated() {
        }
    }, OptionMap.EMPTY, validationPredicate);
    // create a connection to the endpoint's connector
    IoFuture<Connection> futureConnection = AuthenticationContext.empty().with(MatchRule.ALL, AuthenticationConfiguration.empty().useName("bob").usePassword("pass").setSaslMechanismSelector(SaslMechanismSelector.NONE.addMechanism("SCRAM-SHA-256"))).run(new PrivilegedAction<IoFuture<Connection>>() {

        public IoFuture<Connection> run() {
            try {
                return endpoint.connect(new URI("remote://localhost:30123"), OptionMap.EMPTY);
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }
        }
    });
    connection = futureConnection.get();
    assertNull("No SSLSession", connection.getSslSession());
    // use the connection to open a channel to the service
    IoFuture.Status status = IoFuture.Status.WAITING;
    IoFuture<Channel> futureChannel = connection.openChannel("org.jboss.test", OptionMap.EMPTY);
    try {
        status = futureChannel.awaitInterruptibly(2L, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    // verify the resukt of the service connection attempt
    Logger.getLogger("TEST").infof("service returned status %s", status);
    if (status == IoFuture.Status.DONE) {
        Logger.getLogger("TEST").infof("Channel creation succeeded");
        sendChannel = futureChannel.get();
        recvChannel = passer.getIoFuture().get();
        assertNotNull(recvChannel);
        assertNull("No SSLSession", recvChannel.getConnection().getSslSession());
        if (!shouldSucceed)
            fail("Channel creation succeeded when it should have failed!");
    } else if (status == IoFuture.Status.FAILED) {
        Exception exception = futureChannel.getException();
        Logger.getLogger("TEST").infof("Channel creation failed with exception %s", exception);
        if (shouldSucceed)
            fail("Channel creation failed when it should have succeeded!");
    }
    // service has been tested, we can shut down
    safeClose(sendChannel);
    safeClose(recvChannel);
    safeClose(connection);
    serviceRegistration.close();
}
Also used : OpenListener(org.jboss.remoting3.OpenListener) Channel(org.jboss.remoting3.Channel) Connection(org.jboss.remoting3.Connection) IoFuture(org.xnio.IoFuture) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FutureResult(org.xnio.FutureResult) Registration(org.jboss.remoting3.Registration)

Aggregations

Channel (org.jboss.remoting3.Channel)41 IOException (java.io.IOException)29 Test (org.junit.Test)14 LivestreamServiceClient (com.google.cloud.video.livestream.v1.LivestreamServiceClient)13 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 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 Event (com.google.cloud.video.livestream.v1.Event)4