use of org.eclipse.kapua.service.device.call.kura.exception.KuraMqttDeviceCallException in project kapua by eclipse.
the class KuraDeviceCallImpl method send.
@SuppressWarnings({ "unchecked" })
private KuraResponseMessage send(KuraRequestMessage requestMessage, Long timeout) throws KuraMqttDeviceCallException {
KuraResponseMessage response = null;
TransportFacade transportFacade = null;
try {
//
// Borrow a KapuaClient
transportFacade = borrowClient();
//
// Get Kura to transport translator for the request and vice versa
Translator translatorKuraTransport = getTranslator(KuraRequestMessage.class, transportFacade.getMessageClass());
Translator translatorTransportKura = getTranslator(transportFacade.getMessageClass(), KuraResponseMessage.class);
//
// Make the request
// Add requestId and requesterClientId to both payload and channel if response is expected
// Note: Adding to both payload and channel to let the translator choose what to do base on the transport used.
KuraRequestChannel requestChannel = requestMessage.getChannel();
KuraRequestPayload requestPayload = requestMessage.getPayload();
if (timeout != null) {
// FIXME: create an utilty class to use the same synchronized random instance to avoid duplicates
Random r = new Random();
String requestId = String.valueOf(r.nextLong());
requestChannel.setRequestId(requestId);
requestChannel.setRequesterClientId(transportFacade.getClientId());
requestPayload.setRequestId(requestId);
requestPayload.setRequesterClientId(transportFacade.getClientId());
}
// Do send
try {
// Set current timestamp
requestMessage.setTimestamp(new Date());
// Send
TransportMessage transportResponseMessage = transportFacade.sendSync((TransportMessage) translatorKuraTransport.translate(requestMessage), timeout);
// Translate response
response = (KuraResponseMessage) translatorTransportKura.translate(transportResponseMessage);
} catch (KapuaException e) {
throw new KuraMqttDeviceCallException(KuraMqttDeviceCallErrorCodes.CLIENT_SEND_ERROR, e, (Object[]) null);
}
} catch (KapuaException ke) {
throw new KuraMqttDeviceCallException(KuraMqttDeviceCallErrorCodes.CALL_ERROR, ke, (Object[]) null);
} finally {
if (transportFacade != null) {
transportFacade.clean();
}
}
return response;
}
use of org.eclipse.kapua.service.device.call.kura.exception.KuraMqttDeviceCallException in project kapua by eclipse.
the class KuraDeviceCallImpl method borrowClient.
//
// Private methods
//
private TransportFacade borrowClient() throws KuraMqttDeviceCallException {
TransportFacade transportFacade;
try {
KapuaLocator locator = KapuaLocator.getInstance();
TransportClientFactory transportClientFactory = locator.getFactory(TransportClientFactory.class);
transportFacade = transportClientFactory.getFacade();
} catch (Exception e) {
throw new KuraMqttDeviceCallException(KuraMqttDeviceCallErrorCodes.CALL_ERROR, e, (Object[]) null);
}
return transportFacade;
}
Aggregations