Search in sources :

Example 1 with TransportFacade

use of org.eclipse.kapua.transport.TransportFacade in project kapua by eclipse.

the class MqttFacadeTest method testMqttClientSend.

/**
 * Ignoring this test for a while. We should fix the build in the first place and then use embedded ActiveMQ
 * broker for tests.
 */
@Ignore
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testMqttClientSend() throws Exception {
    // 
    // Get facade
    KapuaLocator locator = KapuaLocator.getInstance();
    TransportClientFactory transportFacadeFactory = locator.getFactory(TransportClientFactory.class);
    TransportFacade transportFacade = transportFacadeFactory.getFacade();
    assertNotNull("client.clientId", transportFacade.getClientId());
    // 
    // Send
    String sendTopic = "$EDC/kapua-sys/" + transportFacade.getClientId() + "/" + MqttClientTest.class.getSimpleName() + "/testTransportFacadeSend";
    MqttTopic mqttTopic = new MqttTopic(sendTopic);
    MqttPayload mqttPayload = new MqttPayload("testTransportFacadeSendPayload".getBytes());
    MqttMessage mqttMessage = new MqttMessage(mqttTopic, new Date(), mqttPayload);
    TransportMessage responseMessage = null;
    try {
        responseMessage = transportFacade.sendSync(mqttMessage, null);
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // 
    // Verify
    assertNull("responseMessage", responseMessage);
    // Clean
    try {
        transportFacade.clean();
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) MqttMessage(org.eclipse.kapua.transport.message.mqtt.MqttMessage) MqttTopic(org.eclipse.kapua.transport.message.mqtt.MqttTopic) MqttPayload(org.eclipse.kapua.transport.message.mqtt.MqttPayload) TransportFacade(org.eclipse.kapua.transport.TransportFacade) TransportClientFactory(org.eclipse.kapua.transport.TransportClientFactory) Date(java.util.Date) TransportMessage(org.eclipse.kapua.transport.message.TransportMessage) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with TransportFacade

use of org.eclipse.kapua.transport.TransportFacade 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;
}
Also used : KuraResponseMessage(org.eclipse.kapua.service.device.call.message.app.response.kura.KuraResponseMessage) Random(java.util.Random) Translator(org.eclipse.kapua.translator.Translator) KuraRequestChannel(org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestChannel) KapuaException(org.eclipse.kapua.KapuaException) TransportFacade(org.eclipse.kapua.transport.TransportFacade) Date(java.util.Date) KuraRequestPayload(org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestPayload) TransportMessage(org.eclipse.kapua.transport.message.TransportMessage) KuraMqttDeviceCallException(org.eclipse.kapua.service.device.call.kura.exception.KuraMqttDeviceCallException)

Example 3 with TransportFacade

use of org.eclipse.kapua.transport.TransportFacade 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;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) TransportFacade(org.eclipse.kapua.transport.TransportFacade) TransportClientFactory(org.eclipse.kapua.transport.TransportClientFactory) KuraMqttDeviceCallException(org.eclipse.kapua.service.device.call.kura.exception.KuraMqttDeviceCallException) KapuaException(org.eclipse.kapua.KapuaException) KuraMqttDeviceCallException(org.eclipse.kapua.service.device.call.kura.exception.KuraMqttDeviceCallException)

Aggregations

TransportFacade (org.eclipse.kapua.transport.TransportFacade)3 Date (java.util.Date)2 KapuaException (org.eclipse.kapua.KapuaException)2 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)2 KuraMqttDeviceCallException (org.eclipse.kapua.service.device.call.kura.exception.KuraMqttDeviceCallException)2 TransportClientFactory (org.eclipse.kapua.transport.TransportClientFactory)2 TransportMessage (org.eclipse.kapua.transport.message.TransportMessage)2 Random (java.util.Random)1 KuraRequestChannel (org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestChannel)1 KuraRequestPayload (org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestPayload)1 KuraResponseMessage (org.eclipse.kapua.service.device.call.message.app.response.kura.KuraResponseMessage)1 Translator (org.eclipse.kapua.translator.Translator)1 MqttMessage (org.eclipse.kapua.transport.message.mqtt.MqttMessage)1 MqttPayload (org.eclipse.kapua.transport.message.mqtt.MqttPayload)1 MqttTopic (org.eclipse.kapua.transport.message.mqtt.MqttTopic)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1