Search in sources :

Example 51 with Channel

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

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

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

Example 54 with Channel

use of com.automatak.dnp3.Channel in project jboss-remoting by jboss-remoting.

the class HeartbeatTestCase method testDisableHeartbeat.

/**
 * Test that heartbeat can be set and can be disabled by setting it to 0
 *
 * @throws Exception
 */
@Test
public void testDisableHeartbeat() throws Exception {
    Channel clientChannel = null;
    Channel serverChannel = null;
    Closeable streamServer = null;
    Connection connection = null;
    Registration serviceRegistration = null;
    final Endpoint endpoint = Endpoint.builder().setEndpointName("test").build();
    NetworkServerProvider networkServerProvider = endpoint.getConnectionProviderInterface("remote", NetworkServerProvider.class);
    final SecurityDomain.Builder domainBuilder = SecurityDomain.builder();
    final SimpleMapBackedSecurityRealm mainRealm = new SimpleMapBackedSecurityRealm();
    domainBuilder.addRealm("mainRealm", mainRealm).build();
    domainBuilder.setDefaultRealmName("mainRealm");
    domainBuilder.setPermissionMapper((permissionMappable, roles) -> PermissionVerifier.ALL);
    final PasswordFactory passwordFactory = PasswordFactory.getInstance("clear");
    mainRealm.setPasswordMap("bob", passwordFactory.generatePassword(new ClearPasswordSpec("pass".toCharArray())));
    final SaslServerFactory saslServerFactory = new ServiceLoaderSaslServerFactory(HeartbeatTestCase.class.getClassLoader());
    final SaslAuthenticationFactory.Builder builder = SaslAuthenticationFactory.builder();
    builder.setSecurityDomain(domainBuilder.build());
    builder.setFactory(saslServerFactory);
    builder.setMechanismConfigurationSelector(mechanismInformation -> SaslMechanismInformation.Names.SCRAM_SHA_256.equals(mechanismInformation.getMechanismName()) ? MechanismConfiguration.EMPTY : null);
    final SaslAuthenticationFactory saslAuthenticationFactory = builder.build();
    streamServer = networkServerProvider.createServer(new InetSocketAddress("localhost", 30123), OptionMap.create(Options.SSL_ENABLED, Boolean.FALSE), saslAuthenticationFactory, SSLContext.getDefault());
    final FutureResult<Channel> passer = new FutureResult<Channel>();
    serviceRegistration = endpoint.registerService("org.jboss.test", new OpenListener() {

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

        public void registrationTerminated() {
        }
    }, OptionMap.EMPTY);
    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.create(RemotingOptions.HEARTBEAT_INTERVAL, 0));
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }
        }
    });
    connection = futureConnection.get();
    IoFuture<Channel> futureChannel = connection.openChannel("org.jboss.test", OptionMap.EMPTY);
    clientChannel = futureChannel.get();
    serverChannel = passer.getIoFuture().get();
    assertNotNull(serverChannel);
    RemoteConnectionChannel remoteClientChannel = (RemoteConnectionChannel) clientChannel;
    assertEquals(0, Utils.getInstanceValue(remoteClientChannel.getRemoteConnection(), "heartbeatInterval"));
    RemoteWriteListener clientWriteListener = (RemoteWriteListener) Utils.getInstanceValue(remoteClientChannel.getRemoteConnection(), "writeListener");
    assertNull(Utils.getInstanceValue(clientWriteListener, "heartKey"));
    afterTest(clientChannel, serverChannel, connection, serviceRegistration);
    destroy(endpoint, streamServer);
}
Also used : RemoteWriteListener(org.jboss.remoting3.remote.RemoteConnection.RemoteWriteListener) InetSocketAddress(java.net.InetSocketAddress) Closeable(java.io.Closeable) ClearPasswordSpec(org.wildfly.security.password.spec.ClearPasswordSpec) IoFuture(org.xnio.IoFuture) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) SecurityDomain(org.wildfly.security.auth.server.SecurityDomain) Endpoint(org.jboss.remoting3.Endpoint) FutureResult(org.xnio.FutureResult) Registration(org.jboss.remoting3.Registration) NetworkServerProvider(org.jboss.remoting3.spi.NetworkServerProvider) ServiceLoaderSaslServerFactory(org.wildfly.security.sasl.util.ServiceLoaderSaslServerFactory) SimpleMapBackedSecurityRealm(org.wildfly.security.auth.realm.SimpleMapBackedSecurityRealm) SaslServerFactory(javax.security.sasl.SaslServerFactory) ServiceLoaderSaslServerFactory(org.wildfly.security.sasl.util.ServiceLoaderSaslServerFactory) OpenListener(org.jboss.remoting3.OpenListener) Channel(org.jboss.remoting3.Channel) Connection(org.jboss.remoting3.Connection) SaslAuthenticationFactory(org.wildfly.security.auth.server.sasl.SaslAuthenticationFactory) PasswordFactory(org.wildfly.security.password.PasswordFactory) Test(org.junit.Test)

Example 55 with Channel

use of com.automatak.dnp3.Channel in project jboss-ejb-client by wildfly.

the class DummyServer method hardKill.

public void hardKill() throws IOException {
    for (Channel i : currentConnections) {
        try {
            i.close();
        } catch (IOException e) {
            logger.error("failed to close", e);
        }
    }
    server.close();
    server = null;
    endpoint.close();
}
Also used : AcceptingChannel(org.xnio.channels.AcceptingChannel) Channel(org.jboss.remoting3.Channel) IOException(java.io.IOException)

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