Search in sources :

Example 1 with MqttServiceObserver

use of org.eclipse.smarthome.io.transport.mqtt.MqttServiceObserver 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)

Example 2 with MqttServiceObserver

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

the class MqttServiceTests method brokerConnectionListenerTests.

// Tests addBrokersListener/removeBrokersListener
@Test
public void brokerConnectionListenerTests() throws ConfigurationException {
    MqttService service = new MqttServiceImpl();
    assertFalse(service.hasBrokerObservers());
    MqttServiceObserver observer = mock(MqttServiceObserver.class);
    service.addBrokersListener(observer);
    assertTrue(service.hasBrokerObservers());
    MqttBrokerConnectionEx connection = new MqttBrokerConnectionEx("tcp://123.123.123.123", null, false, "brokerConnectionListenerTests");
    assertTrue(service.addBrokerConnection("name", connection));
    ArgumentCaptor<MqttBrokerConnection> argumentCaptorConn = ArgumentCaptor.forClass(MqttBrokerConnection.class);
    ArgumentCaptor<String> argumentCaptorConnName = ArgumentCaptor.forClass(String.class);
    verify(observer).brokerAdded(argumentCaptorConnName.capture(), argumentCaptorConn.capture());
    assertThat(argumentCaptorConnName.getValue(), equalTo("name"));
    assertThat(argumentCaptorConn.getValue(), equalTo(connection));
    service.removeBrokerConnection("name");
    verify(observer).brokerRemoved(argumentCaptorConnName.capture(), argumentCaptorConn.capture());
    assertThat(argumentCaptorConnName.getValue(), equalTo("name"));
    assertThat(argumentCaptorConn.getValue(), equalTo(connection));
    service.removeBrokersListener(observer);
    assertFalse(service.hasBrokerObservers());
}
Also used : MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) MqttService(org.eclipse.smarthome.io.transport.mqtt.MqttService) MqttServiceObserver(org.eclipse.smarthome.io.transport.mqtt.MqttServiceObserver) MqttBrokerConnectionEx(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnectionEx) Test(org.junit.Test)

Aggregations

MqttBrokerConnection (org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection)2 MqttService (org.eclipse.smarthome.io.transport.mqtt.MqttService)2 MqttServiceObserver (org.eclipse.smarthome.io.transport.mqtt.MqttServiceObserver)2 Semaphore (java.util.concurrent.Semaphore)1 TimeUnit (java.util.concurrent.TimeUnit)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 MqttBrokerConnectionEx (org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnectionEx)1 MqttConnectionObserver (org.eclipse.smarthome.io.transport.mqtt.MqttConnectionObserver)1 MqttConnectionState (org.eclipse.smarthome.io.transport.mqtt.MqttConnectionState)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Test (org.junit.Test)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1