Search in sources :

Example 1 with CloudPayloadProtoBufEncoderImpl

use of org.eclipse.kura.core.cloud.CloudPayloadProtoBufEncoderImpl 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)

Aggregations

IOException (java.io.IOException)1 KuraException (org.eclipse.kura.KuraException)1 KuraStoreException (org.eclipse.kura.KuraStoreException)1 KuraTimeoutException (org.eclipse.kura.KuraTimeoutException)1 CloudPayloadProtoBufEncoderImpl (org.eclipse.kura.core.cloud.CloudPayloadProtoBufEncoderImpl)1 KuraRequestPayload (org.eclipse.kura.message.KuraRequestPayload)1