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