Search in sources :

Example 1 with KuraRequestPayload

use of org.eclipse.kura.message.KuraRequestPayload in project kura by eclipse.

the class MessageHandlerCallable method call.

@Override
public Void call() throws Exception {
    s_logger.debug("Control Arrived on topic: {}", this.m_appTopic);
    // Prepare the default response
    KuraRequestPayload reqPayload = KuraRequestPayload.buildFromKuraPayload(this.m_msg);
    KuraResponsePayload respPayload = new KuraResponsePayload(KuraResponsePayload.RESPONSE_CODE_OK);
    try {
        CloudletTopic reqTopic = CloudletTopic.parseAppTopic(this.m_appTopic);
        CloudletTopic.Method method = reqTopic.getMethod();
        switch(method) {
            case GET:
                s_logger.debug("Handling GET request topic: {}", this.m_appTopic);
                this.m_cloudApp.doGet(reqTopic, reqPayload, respPayload);
                break;
            case PUT:
                s_logger.debug("Handling PUT request topic: {}", this.m_appTopic);
                this.m_cloudApp.doPut(reqTopic, reqPayload, respPayload);
                break;
            case POST:
                s_logger.debug("Handling POST request topic: {}", this.m_appTopic);
                this.m_cloudApp.doPost(reqTopic, reqPayload, respPayload);
                break;
            case DEL:
                s_logger.debug("Handling DEL request topic: {}", this.m_appTopic);
                this.m_cloudApp.doDel(reqTopic, reqPayload, respPayload);
                break;
            case EXEC:
                s_logger.debug("Handling EXEC request topic: {}", this.m_appTopic);
                this.m_cloudApp.doExec(reqTopic, reqPayload, respPayload);
                break;
            default:
                s_logger.error("Bad request topic: {}", this.m_appTopic);
                respPayload.setResponseCode(KuraResponsePayload.RESPONSE_CODE_BAD_REQUEST);
                break;
        }
    } catch (IllegalArgumentException e) {
        s_logger.error("Bad request topic: {}", this.m_appTopic);
        respPayload.setResponseCode(KuraResponsePayload.RESPONSE_CODE_BAD_REQUEST);
    } catch (KuraException e) {
        s_logger.error("Error handling request topic: {}\n{}", this.m_appTopic, e);
        respPayload.setResponseCode(KuraResponsePayload.RESPONSE_CODE_ERROR);
        respPayload.setException(e);
    }
    try {
        CloudClient cloudClient = this.m_cloudApp.getCloudApplicationClient();
        respPayload.setTimestamp(new Date());
        StringBuilder sb = new StringBuilder("REPLY").append("/").append(reqPayload.getRequestId());
        String requesterClientId = reqPayload.getRequesterClientId();
        s_logger.debug("Publishing response topic: {}", sb.toString());
        cloudClient.controlPublish(requesterClientId, sb.toString(), respPayload, Cloudlet.DFLT_PUB_QOS, Cloudlet.DFLT_RETAIN, Cloudlet.DFLT_PRIORITY);
    } catch (KuraException e) {
        s_logger.error("Error publishing response for topic: {}\n{}", this.m_appTopic, e);
    }
    return null;
}
Also used : KuraException(org.eclipse.kura.KuraException) KuraResponsePayload(org.eclipse.kura.message.KuraResponsePayload) Date(java.util.Date) KuraRequestPayload(org.eclipse.kura.message.KuraRequestPayload)

Example 2 with KuraRequestPayload

use of org.eclipse.kura.message.KuraRequestPayload 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

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