Search in sources :

Example 1 with MqttConnectionObserver

use of org.eclipse.smarthome.io.transport.mqtt.MqttConnectionObserver in project smarthome by eclipse.

the class MqttEmbeddedBrokerServiceTest method connectToEmbeddedServer.

@Test
public void connectToEmbeddedServer() throws InterruptedException, IOException {
    ServiceConfiguration config = new ServiceConfiguration();
    config.username = "username";
    config.password = "password";
    config.port = 12345;
    config.secure = false;
    config.persistenceFile = "";
    subject.initialize(config);
    Semaphore semaphore = new Semaphore(1);
    semaphore.acquire();
    MqttBrokerConnection c = subject.getConnection();
    MqttConnectionObserver mqttConnectionObserver = (state, error) -> {
        if (state == MqttConnectionState.CONNECTED) {
            semaphore.release();
        }
    };
    c.addConnectionObserver(mqttConnectionObserver);
    if (c.connectionState() == MqttConnectionState.CONNECTED) {
        semaphore.release();
    }
    // Start the connection and wait until timeout or connected callback returns.
    semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS);
    c.removeConnectionObserver(mqttConnectionObserver);
    assertThat(c.getUser(), is("username"));
    assertThat(c.getPassword(), is("password"));
    assertThat(c.connectionState(), is(MqttConnectionState.CONNECTED));
    verify(service).addBrokerConnection(anyString(), eq(c));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) X509Certificate(java.security.cert.X509Certificate) Socket(java.net.Socket) ArgumentMatchers(org.mockito.ArgumentMatchers) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) Semaphore(java.util.concurrent.Semaphore) Mock(org.mockito.Mock) IOException(java.io.IOException) Test(org.junit.Test) CertificateException(java.security.cert.CertificateException) ConfigurationException(javax.naming.ConfigurationException) MqttConnectionState(org.eclipse.smarthome.io.transport.mqtt.MqttConnectionState) Mockito.verify(org.mockito.Mockito.verify) SSLEngine(javax.net.ssl.SSLEngine) Assert.assertThat(org.junit.Assert.assertThat) TimeUnit(java.util.concurrent.TimeUnit) MockitoAnnotations(org.mockito.MockitoAnnotations) MqttService(org.eclipse.smarthome.io.transport.mqtt.MqttService) GeneralSecurityException(java.security.GeneralSecurityException) X509ExtendedTrustManager(javax.net.ssl.X509ExtendedTrustManager) MqttException(org.eclipse.smarthome.io.transport.mqtt.MqttException) After(org.junit.After) MqttConnectionObserver(org.eclipse.smarthome.io.transport.mqtt.MqttConnectionObserver) Before(org.junit.Before) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) MqttConnectionObserver(org.eclipse.smarthome.io.transport.mqtt.MqttConnectionObserver) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 2 with MqttConnectionObserver

use of org.eclipse.smarthome.io.transport.mqtt.MqttConnectionObserver in project smarthome by eclipse.

the class EmbeddedBrokerTools method waitForConnection.

/**
 * Request the embedded broker connection from the {@link MqttService} and wait for a connection to be established.
 *
 * @throws InterruptedException
 */
public MqttBrokerConnection waitForConnection(MqttService mqttService) throws InterruptedException {
    embeddedConnection = mqttService.getBrokerConnection(Constants.CLIENTID);
    if (embeddedConnection == null) {
        Semaphore semaphore = new Semaphore(1);
        semaphore.acquire();
        MqttServiceObserver observer = new MqttServiceObserver() {

            @Override
            public void brokerAdded(@NonNull String brokerID, @NonNull MqttBrokerConnection broker) {
                if (brokerID.equals(Constants.CLIENTID)) {
                    embeddedConnection = broker;
                    semaphore.release();
                }
            }

            @Override
            public void brokerRemoved(@NonNull String brokerID, @NonNull MqttBrokerConnection broker) {
            }
        };
        mqttService.addBrokersListener(observer);
        assertTrue("Wait for embedded connection client failed", semaphore.tryAcquire(700, TimeUnit.MILLISECONDS));
    }
    MqttBrokerConnection embeddedConnection = this.embeddedConnection;
    if (embeddedConnection == null) {
        throw new IllegalStateException();
    }
    logger.warn("waitForConnection {}", embeddedConnection.connectionState());
    Semaphore semaphore = new Semaphore(1);
    semaphore.acquire();
    MqttConnectionObserver mqttConnectionObserver = (state, error) -> {
        if (state == MqttConnectionState.CONNECTED) {
            semaphore.release();
        }
    };
    embeddedConnection.addConnectionObserver(mqttConnectionObserver);
    if (embeddedConnection.connectionState() == MqttConnectionState.CONNECTED) {
        semaphore.release();
    }
    assertTrue("Connection " + embeddedConnection.getClientId() + " failed. State: " + embeddedConnection.connectionState(), semaphore.tryAcquire(500, TimeUnit.MILLISECONDS));
    return embeddedConnection;
}
Also used : NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) Semaphore(java.util.concurrent.Semaphore) LoggerFactory(org.slf4j.LoggerFactory) Assert.assertTrue(org.junit.Assert.assertTrue) MqttConnectionState(org.eclipse.smarthome.io.transport.mqtt.MqttConnectionState) MqttServiceObserver(org.eclipse.smarthome.io.transport.mqtt.MqttServiceObserver) TimeUnit(java.util.concurrent.TimeUnit) MqttService(org.eclipse.smarthome.io.transport.mqtt.MqttService) Constants(org.eclipse.smarthome.io.mqttembeddedbroker.Constants) Nullable(org.eclipse.jdt.annotation.Nullable) MqttConnectionObserver(org.eclipse.smarthome.io.transport.mqtt.MqttConnectionObserver) NonNull(org.eclipse.jdt.annotation.NonNull) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) MqttConnectionObserver(org.eclipse.smarthome.io.transport.mqtt.MqttConnectionObserver) NonNull(org.eclipse.jdt.annotation.NonNull) Semaphore(java.util.concurrent.Semaphore) MqttServiceObserver(org.eclipse.smarthome.io.transport.mqtt.MqttServiceObserver)

Aggregations

Semaphore (java.util.concurrent.Semaphore)2 TimeUnit (java.util.concurrent.TimeUnit)2 MqttBrokerConnection (org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection)2 MqttConnectionObserver (org.eclipse.smarthome.io.transport.mqtt.MqttConnectionObserver)2 MqttConnectionState (org.eclipse.smarthome.io.transport.mqtt.MqttConnectionState)2 MqttService (org.eclipse.smarthome.io.transport.mqtt.MqttService)2 IOException (java.io.IOException)1 Socket (java.net.Socket)1 GeneralSecurityException (java.security.GeneralSecurityException)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 ConfigurationException (javax.naming.ConfigurationException)1 SSLEngine (javax.net.ssl.SSLEngine)1 X509ExtendedTrustManager (javax.net.ssl.X509ExtendedTrustManager)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 Constants (org.eclipse.smarthome.io.mqttembeddedbroker.Constants)1 MqttException (org.eclipse.smarthome.io.transport.mqtt.MqttException)1 MqttServiceObserver (org.eclipse.smarthome.io.transport.mqtt.MqttServiceObserver)1