Search in sources :

Example 1 with ExpiryDate

use of io.joynr.common.ExpiryDate in project joynr by bmwcarit.

the class JoynrMessagingConnectorInvocationHandler method executeSyncMethod.

@CheckForNull
@Override
public Object executeSyncMethod(Method method, Object[] args) throws ApplicationException {
    // TODO does a method with 0 args pass in an empty args array, or null for args?
    if (method == null) {
        throw new IllegalArgumentException("Method cannot be null");
    }
    if (toDiscoveryEntries.size() > 1) {
        throw new JoynrIllegalStateException("You can't execute sync methods for multiple participants.");
    }
    if (toDiscoveryEntries.isEmpty()) {
        throw new JoynrIllegalStateException("You must have exactly one participant to be able to execute a sync method.");
    }
    MethodMetaInformation methodMetaInformation = JoynrMessagingConnectorFactory.ensureMethodMetaInformationPresent(method);
    Request request = new Request(method.getName(), args, method.getParameterTypes());
    Reply reply;
    String requestReplyId = request.getRequestReplyId();
    SynchronizedReplyCaller synchronizedReplyCaller = new SynchronizedReplyCaller(fromParticipantId, requestReplyId, request);
    ExpiryDate expiryDate = DispatcherUtils.convertTtlToExpirationDate(qosSettings.getRoundTripTtl_ms());
    replyCallerDirectory.addReplyCaller(requestReplyId, synchronizedReplyCaller, expiryDate);
    reply = (Reply) requestReplyManager.sendSyncRequest(fromParticipantId, toDiscoveryEntries.iterator().next(), request, synchronizedReplyCaller, qosSettings);
    if (reply.getError() == null) {
        if (method.getReturnType().equals(void.class)) {
            return null;
        }
        Object response = RpcUtils.reconstructReturnedObject(method, methodMetaInformation, reply.getResponse());
        logger.debug("REQUEST returns successful: requestReplyId: {}, method {}, response: {}", requestReplyId, method.getName(), response);
        return response;
    } else if (reply.getError() instanceof ApplicationException) {
        logger.debug("REQUEST returns error: requestReplyId: {}, method {}, response: {}", requestReplyId, method.getName(), reply.getError());
        throw (ApplicationException) reply.getError();
    } else {
        logger.debug("REQUEST returns error: requestReplyId: {}, method {}, response: {}", requestReplyId, method.getName(), reply.getError());
        throw (JoynrRuntimeException) reply.getError();
    }
}
Also used : ExpiryDate(io.joynr.common.ExpiryDate) ApplicationException(joynr.exceptions.ApplicationException) MethodMetaInformation(joynr.MethodMetaInformation) OneWayRequest(joynr.OneWayRequest) Request(joynr.Request) Reply(joynr.Reply) SynchronizedReplyCaller(io.joynr.dispatching.rpc.SynchronizedReplyCaller) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) CheckForNull(javax.annotation.CheckForNull)

Example 2 with ExpiryDate

use of io.joynr.common.ExpiryDate in project joynr by bmwcarit.

the class RequestReplyManagerTest method requestReplyMessagesRemoveCallBackByTtl.

@Test
public void requestReplyMessagesRemoveCallBackByTtl() throws Exception {
    TestProvider testResponder = new TestProvider(1);
    ExpiryDate ttlReplyCaller = ExpiryDate.fromRelativeTtl(1000L);
    final ReplyCaller replyCaller = mock(ReplyCaller.class);
    replyCallerDirectory.addReplyCaller(request1.getRequestReplyId(), replyCaller, ttlReplyCaller);
    Thread.sleep(ttlReplyCaller.getRelativeTtl() + 100);
    requestReplyManager.handleReply(new Reply(request1.getRequestReplyId(), testResponder.getSentPayloadFor(request1)));
    verify(replyCaller, never()).messageCallBack(any(Reply.class));
}
Also used : ExpiryDate(io.joynr.common.ExpiryDate) Reply(joynr.Reply) ReplyCaller(io.joynr.dispatching.rpc.ReplyCaller) Test(org.junit.Test)

Example 3 with ExpiryDate

use of io.joynr.common.ExpiryDate in project joynr by bmwcarit.

the class MutableMessageFactory method createMessage.

private MutableMessage createMessage(String joynrMessageType, String fromParticipantId, String toParticipantId, Object payload, MessagingQos messagingQos, boolean upliftTtl) {
    ExpiryDate expiryDate;
    if (!upliftTtl) {
        expiryDate = DispatcherUtils.convertTtlToExpirationDate(messagingQos.getRoundTripTtl_ms());
    } else if (messagingQos.getRoundTripTtl_ms() > (Long.MAX_VALUE - ttlUpliftMs)) {
        expiryDate = DispatcherUtils.convertTtlToExpirationDate(Long.MAX_VALUE);
    } else {
        expiryDate = DispatcherUtils.convertTtlToExpirationDate(messagingQos.getRoundTripTtl_ms() + ttlUpliftMs);
    }
    MutableMessage message = new MutableMessage();
    message.setType(joynrMessageType);
    if (messagingQos.getEffort() != null && !MessagingQosEffort.NORMAL.equals(messagingQos.getEffort())) {
        message.setEffort(String.valueOf(messagingQos.getEffort()));
    }
    message.setSender(fromParticipantId);
    message.setRecipient(toParticipantId);
    message.setTtlAbsolute(true);
    message.setTtlMs(expiryDate.getValue());
    message.setPayload(serializePayload(payload));
    message.setCustomHeaders(messagingQos.getCustomMessageHeaders());
    message.setCompressed(messagingQos.getCompress());
    for (JoynrMessageProcessor processor : messageProcessors) {
        message = processor.processOutgoing(message);
    }
    logger.debug("Message {} has expiry date: {}", message.getId(), expiryDate);
    return message;
}
Also used : ExpiryDate(io.joynr.common.ExpiryDate) MutableMessage(joynr.MutableMessage) JoynrMessageProcessor(io.joynr.messaging.JoynrMessageProcessor)

Example 4 with ExpiryDate

use of io.joynr.common.ExpiryDate in project joynr by bmwcarit.

the class JoynrMessagingConnectorInvocationHandler method executeAsyncMethod.

@SuppressWarnings("unchecked")
@Override
public Future<?> executeAsyncMethod(Method method, Object[] params, Future<?> future) {
    if (method == null) {
        throw new IllegalArgumentException("Method cannot be null");
    }
    if (toDiscoveryEntries.size() > 1) {
        throw new JoynrIllegalStateException("You can't execute async methods for multiple participants.");
    }
    if (toDiscoveryEntries.isEmpty()) {
        throw new JoynrIllegalStateException("You must have exactly one participant to be able to execute an async method.");
    }
    MethodMetaInformation methodMetaInformation = JoynrMessagingConnectorFactory.ensureMethodMetaInformationPresent(method);
    if (methodMetaInformation.getCallbackAnnotation() == null) {
        throw new JoynrIllegalStateException("All async methods need to have a annotated callback parameter.");
    }
    int callbackIndex = methodMetaInformation.getCallbackIndex();
    ICallback callback = (ICallback) params[callbackIndex];
    Object[] paramsWithoutCallback = new Object[params.length - 1];
    copyArrayWithoutElement(params, paramsWithoutCallback, callbackIndex);
    Class<?>[] paramDatatypes = method.getParameterTypes();
    Class<?>[] paramDatatypesWithoutCallback = new Class<?>[paramDatatypes.length - 1];
    copyArrayWithoutElement(paramDatatypes, paramDatatypesWithoutCallback, callbackIndex);
    Request request = new Request(method.getName(), paramsWithoutCallback, paramDatatypesWithoutCallback);
    String requestReplyId = request.getRequestReplyId();
    @SuppressWarnings("rawtypes") RpcAsyncRequestReplyCaller<?> callbackWrappingReplyCaller = new RpcAsyncRequestReplyCaller(requestReplyId, callback, future, method, methodMetaInformation);
    ExpiryDate expiryDate = DispatcherUtils.convertTtlToExpirationDate(qosSettings.getRoundTripTtl_ms());
    replyCallerDirectory.addReplyCaller(requestReplyId, callbackWrappingReplyCaller, expiryDate);
    requestReplyManager.sendRequest(fromParticipantId, toDiscoveryEntries.iterator().next(), request, qosSettings);
    return future;
}
Also used : RpcAsyncRequestReplyCaller(io.joynr.dispatching.rpc.RpcAsyncRequestReplyCaller) ExpiryDate(io.joynr.common.ExpiryDate) MethodMetaInformation(joynr.MethodMetaInformation) OneWayRequest(joynr.OneWayRequest) Request(joynr.Request) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException)

Aggregations

ExpiryDate (io.joynr.common.ExpiryDate)4 JoynrIllegalStateException (io.joynr.exceptions.JoynrIllegalStateException)2 MethodMetaInformation (joynr.MethodMetaInformation)2 OneWayRequest (joynr.OneWayRequest)2 Reply (joynr.Reply)2 Request (joynr.Request)2 ReplyCaller (io.joynr.dispatching.rpc.ReplyCaller)1 RpcAsyncRequestReplyCaller (io.joynr.dispatching.rpc.RpcAsyncRequestReplyCaller)1 SynchronizedReplyCaller (io.joynr.dispatching.rpc.SynchronizedReplyCaller)1 JoynrMessageProcessor (io.joynr.messaging.JoynrMessageProcessor)1 CheckForNull (javax.annotation.CheckForNull)1 MutableMessage (joynr.MutableMessage)1 ApplicationException (joynr.exceptions.ApplicationException)1 Test (org.junit.Test)1