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));
}
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;
}
Aggregations