Search in sources :

Example 1 with KuraTimeoutException

use of org.eclipse.kura.KuraTimeoutException 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);
        }
    }
}
Also used : MqttException(org.eclipse.paho.client.mqttv3.MqttException) IMqttToken(org.eclipse.paho.client.mqttv3.IMqttToken) KuraTimeoutException(org.eclipse.kura.KuraTimeoutException) KuraNotConnectedException(org.eclipse.kura.KuraNotConnectedException)

Example 2 with KuraTimeoutException

use of org.eclipse.kura.KuraTimeoutException in project kura by eclipse.

the class CloudCallServiceImpl method call.

@Override
public synchronized KuraResponsePayload call(String deviceId, String appId, String appTopic, KuraPayload appPayload, int timeout) throws KuraConnectException, KuraTimeoutException, KuraStoreException, KuraException {
    // Generate the request ID
    String requestId = s_generator.next();
    StringBuilder sbReqTopic = new StringBuilder("$EDC").append("/").append(ACCOUNT_NAME_VAR_NAME).append("/").append(deviceId).append("/").append(appId).append("/").append(appTopic);
    StringBuilder sbRespTopic = new StringBuilder("$EDC").append("/").append(ACCOUNT_NAME_VAR_NAME).append("/").append(CLIENT_ID_VAR_NAME).append("/").append(appId).append("/").append("REPLY").append("/").append(requestId);
    KuraRequestPayload req = null;
    if (appPayload != null) {
        // Construct a request payload
        req = new KuraRequestPayload(appPayload);
    } else {
        req = new KuraRequestPayload();
    }
    req.setRequestId(requestId);
    req.setRequesterClientId(CLIENT_ID_VAR_NAME);
    CloudPayloadProtoBufEncoderImpl encoder = new CloudPayloadProtoBufEncoderImpl(req);
    byte[] rawPayload;
    try {
        rawPayload = encoder.getBytes();
    } catch (IOException e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e, "Cannot encode request");
    }
    this.m_respTopic = sbRespTopic.toString();
    this.m_resp = null;
    this.m_dataService.subscribe(this.m_respTopic, 0);
    synchronized (this.m_lock) {
        try {
            this.m_dataService.publish(sbReqTopic.toString(), rawPayload, DFLT_PUB_QOS, DFLT_RETAIN, DFLT_PRIORITY);
            this.m_lock.wait(timeout);
        } catch (KuraStoreException e) {
            throw e;
        } catch (InterruptedException e) {
            // Avoid re-throwing this exception which should not normally happen
            s_logger.warn("Interrupted while waiting for the response");
            Thread.interrupted();
        } finally {
            try {
                this.m_dataService.unsubscribe(this.m_respTopic);
            } catch (KuraException e) {
                s_logger.error("Cannot unsubscribe");
            }
            this.m_respTopic = null;
        }
    }
    if (this.m_resp == null) {
        throw new KuraTimeoutException("Timed out while waiting for the response");
    }
    return this.m_resp;
}
Also used : KuraException(org.eclipse.kura.KuraException) CloudPayloadProtoBufEncoderImpl(org.eclipse.kura.core.cloud.CloudPayloadProtoBufEncoderImpl) KuraTimeoutException(org.eclipse.kura.KuraTimeoutException) IOException(java.io.IOException) KuraRequestPayload(org.eclipse.kura.message.KuraRequestPayload) KuraStoreException(org.eclipse.kura.KuraStoreException)

Example 3 with KuraTimeoutException

use of org.eclipse.kura.KuraTimeoutException 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);
        }
    }
}
Also used : MqttException(org.eclipse.paho.client.mqttv3.MqttException) IMqttToken(org.eclipse.paho.client.mqttv3.IMqttToken) KuraTimeoutException(org.eclipse.kura.KuraTimeoutException) KuraNotConnectedException(org.eclipse.kura.KuraNotConnectedException)

Aggregations

KuraTimeoutException (org.eclipse.kura.KuraTimeoutException)3 KuraNotConnectedException (org.eclipse.kura.KuraNotConnectedException)2 IMqttToken (org.eclipse.paho.client.mqttv3.IMqttToken)2 MqttException (org.eclipse.paho.client.mqttv3.MqttException)2 IOException (java.io.IOException)1 KuraException (org.eclipse.kura.KuraException)1 KuraStoreException (org.eclipse.kura.KuraStoreException)1 CloudPayloadProtoBufEncoderImpl (org.eclipse.kura.core.cloud.CloudPayloadProtoBufEncoderImpl)1 KuraRequestPayload (org.eclipse.kura.message.KuraRequestPayload)1