use of org.eclipse.kura.KuraNotConnectedException in project kura by eclipse.
the class MqttDataTransport method publish.
/*
* (non-Javadoc)
*
* @see org.eclipse.kura.data.DataPublisherService#publish(java.lang.String
* , byte[], int, boolean)
*
* DataConnectException this can be easily recovered connecting the service.
* TooManyInflightMessagesException the caller SHOULD retry publishing the
* message at a later time. RuntimeException (unchecked) all other
* unrecoverable faults that are not recoverable by the caller.
*/
@Override
public DataTransportToken publish(String topic, byte[] payload, int qos, boolean retain) throws KuraTooManyInflightMessagesException, KuraException, KuraNotConnectedException {
if (this.m_mqttClient == null || !this.m_mqttClient.isConnected()) {
throw new KuraNotConnectedException("Not connected");
}
topic = replaceTopicVariables(topic);
s_logger.info("Publishing message on topic: {} with QoS: {}", topic, qos);
MqttMessage message = new MqttMessage();
message.setPayload(payload);
message.setQos(qos);
message.setRetained(retain);
Integer messageId = null;
try {
IMqttDeliveryToken token = this.m_mqttClient.publish(topic, message);
// At present Paho ALWAYS allocates (gets and increments) internally
// a message ID,
// even for messages published with QoS == 0.
// Of course, for QoS == 0 this "internal" message ID will not hit
// the wire.
// On top of that, messages published with QoS == 0 are confirmed
// in the deliveryComplete callback.
// Another implementation might behave differently
// and only allocate a message ID for messages published with QoS >
// 0.
// We don't want to rely on this and only return and confirm IDs
// of messages published with QoS > 0.
s_logger.debug("Published message with ID: {}", token.getMessageId());
if (qos > 0) {
messageId = Integer.valueOf(token.getMessageId());
}
} catch (MqttPersistenceException e) {
// This is probably an unrecoverable internal error
s_logger.error("Cannot publish on topic: {}", topic, e);
throw new IllegalStateException("Cannot publish on topic: " + topic, e);
} catch (MqttException e) {
if (e.getReasonCode() == MqttException.REASON_CODE_MAX_INFLIGHT) {
s_logger.info("Too many inflight messages");
throw new KuraTooManyInflightMessagesException(e, "Too many in-fligh messages");
} else {
s_logger.error("Cannot publish on topic: " + topic, e);
throw KuraException.internalError(e, "Cannot publish on topic: " + topic);
}
}
DataTransportToken token = null;
if (messageId != null) {
token = new DataTransportToken(messageId, this.m_sessionId);
}
return token;
}
use of org.eclipse.kura.KuraNotConnectedException in project kura by eclipse.
the class MqttDataTransport method unsubscribe.
@Override
public void unsubscribe(String topic) throws KuraTimeoutException, KuraException, KuraNotConnectedException {
if (this.m_mqttClient == null || !this.m_mqttClient.isConnected()) {
throw new KuraNotConnectedException("Not connected");
}
topic = replaceTopicVariables(topic);
s_logger.info("Unsubscribing to topic: {}", topic);
try {
IMqttToken token = this.m_mqttClient.unsubscribe(topic);
token.waitForCompletion(getTimeToWaitMillis());
} catch (MqttException e) {
if (e.getReasonCode() == MqttException.REASON_CODE_CLIENT_TIMEOUT) {
s_logger.warn("Timeout unsubscribing to topic: {}", topic);
throw new KuraTimeoutException("Timeout unsubscribing to topic: " + topic, e);
} else {
s_logger.error("Cannot unsubscribe to topic: " + topic, e);
throw KuraException.internalError(e, "Cannot unsubscribe to topic: " + topic);
}
}
}
use of org.eclipse.kura.KuraNotConnectedException in project kura by eclipse.
the class MqttDataTransport method subscribe.
// ---------------------------------------------------------
//
// Subscription Management Methods
//
// ---------------------------------------------------------
@Override
public void subscribe(String topic, int qos) throws KuraTimeoutException, KuraException, KuraNotConnectedException {
if (this.m_mqttClient == null || !this.m_mqttClient.isConnected()) {
throw new KuraNotConnectedException("Not connected");
}
topic = replaceTopicVariables(topic);
s_logger.info("Subscribing to topic: {} with QoS: {}", topic, qos);
try {
IMqttToken token = this.m_mqttClient.subscribe(topic, qos);
token.waitForCompletion(getTimeToWaitMillis());
} catch (MqttException e) {
if (e.getReasonCode() == MqttException.REASON_CODE_CLIENT_TIMEOUT) {
s_logger.warn("Timeout subscribing to topic: {}", topic);
throw new KuraTimeoutException("Timeout subscribing to topic: " + topic, e);
} else {
s_logger.error("Cannot subscribe to topic: " + topic, e);
throw KuraException.internalError(e, "Cannot subscribe to topic: " + topic);
}
}
}
Aggregations