Search in sources :

Example 16 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class ExpirationFilter method inspect.

public boolean inspect(Object message) {
    long expirationDate = 0;
    if (message instanceof ImmutableMessage) {
        ImmutableMessage immutableMessage = (ImmutableMessage) message;
        assert (immutableMessage.isTtlAbsolute());
        expirationDate = immutableMessage.getTtlMs();
        if (expirationDate < System.currentTimeMillis()) {
            log.warn("message expired: msgId: {}", immutableMessage.getId());
            return false;
        }
        log.trace("message has not expired: msgId: {}", immutableMessage.getId());
        return true;
    }
    log.warn("could not filter message NOT correct type {}", message);
    // let everything else pass
    return true;
}
Also used : ImmutableMessage(joynr.ImmutableMessage)

Example 17 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class MqttMessagingSkeletonTest method testRequestsAreAcceptedAgainWhenPreviousAreProcessedAfterMaxIncomingRequestsReached.

@Test
public void testRequestsAreAcceptedAgainWhenPreviousAreProcessedAfterMaxIncomingRequestsReached() throws Exception {
    ImmutableMessage rqMessage1 = createTestRequestMessage();
    final String messageId1 = rqMessage1.getId();
    subject.transmit(rqMessage1.getSerializedMessage(), failIfCalledAction);
    feedMqttSkeletonWithRequests(subject, maxIncomingMqttRequests - 1);
    verify(messageRouter, times(maxIncomingMqttRequests)).route(any(ImmutableMessage.class));
    // As the limit is reached, further requests should be dropped and not transmitted
    // until an already accepted request is marked as processed
    subject.transmit(createTestRequestMessage().getSerializedMessage(), failIfCalledAction);
    assertEquals(1, subject.getDroppedMessagesCount());
    verify(messageRouter, times(maxIncomingMqttRequests)).route(any(ImmutableMessage.class));
    subject.messageProcessed(messageId1);
    subject.transmit(createTestRequestMessage().getSerializedMessage(), failIfCalledAction);
    verify(messageRouter, times(maxIncomingMqttRequests + 1)).route(any(ImmutableMessage.class));
}
Also used : ImmutableMessage(joynr.ImmutableMessage) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 18 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class MqttMessagingSkeletonTest method testMessageRouterIsCalled.

@SuppressWarnings("unchecked")
@Test
public void testMessageRouterIsCalled() throws Exception {
    RawMessagingPreprocessor preprocessor = mock(RawMessagingPreprocessor.class);
    when(preprocessor.process(any(byte[].class), anyMap())).then(returnsFirstArg());
    subject = new MqttMessagingSkeleton(ownAddress, maxIncomingMqttRequests, messageRouter, mqttClientFactory, mqttTopicPrefixProvider, preprocessor, new HashSet<JoynrMessageProcessor>(), mqttStatusReceiver);
    ImmutableMessage rqMessage = createTestRequestMessage();
    subject.transmit(rqMessage.getSerializedMessage(), failIfCalledAction);
    ArgumentCaptor<ImmutableMessage> captor = ArgumentCaptor.forClass(ImmutableMessage.class);
    verify(messageRouter).route(captor.capture());
    assertArrayEquals(rqMessage.getSerializedMessage(), captor.getValue().getSerializedMessage());
}
Also used : ImmutableMessage(joynr.ImmutableMessage) NoOpRawMessagingPreprocessor(io.joynr.messaging.NoOpRawMessagingPreprocessor) RawMessagingPreprocessor(io.joynr.messaging.RawMessagingPreprocessor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class SplitSMRFTest method deserializeMessages.

@Test
public void deserializeMessages() throws Exception {
    String expectedSender1 = "expectedSender1";
    String expectedRecipient1 = "expectedRecipient1";
    String expectedSender2 = "expectedSender2";
    String expectedRecipient2 = "expectedRecipient2";
    ImmutableMessage message1 = createImmutableMessage(Message.VALUE_MESSAGE_TYPE_REQUEST, expectedSender1, expectedRecipient1);
    ImmutableMessage message2 = createImmutableMessage(Message.VALUE_MESSAGE_TYPE_REQUEST, expectedSender2, expectedRecipient2);
    byte[] message1Serialized = message1.getSerializedMessage();
    byte[] message2Serialized = message2.getSerializedMessage();
    byte[] concatenatedMessages = ArrayUtils.addAll(message2Serialized, message1Serialized);
    List<ImmutableMessage> splitMessages = Utilities.splitSMRF(concatenatedMessages);
    assertEquals(2, splitMessages.size());
    ImmutableMessage retrievedMessage1 = splitMessages.get(1);
    ImmutableMessage retrievedMessage2 = splitMessages.get(0);
    assertEquals(message1.getSender(), retrievedMessage1.getSender());
    assertEquals(message1.getRecipient(), retrievedMessage1.getRecipient());
    assertTrue(Arrays.equals(message1.getUnencryptedBody(), retrievedMessage1.getUnencryptedBody()));
    assertEquals(message2.getSender(), retrievedMessage2.getSender());
    assertEquals(message2.getRecipient(), retrievedMessage2.getRecipient());
    assertTrue(Arrays.equals(message2.getUnencryptedBody(), retrievedMessage2.getUnencryptedBody()));
}
Also used : ImmutableMessage(joynr.ImmutableMessage) Test(org.junit.Test)

Example 20 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class MessagingServiceRestAdapter method postMessage.

/**
 * Send a message.
 *
 * @param ccid
 *            channel id of the receiver.
 * @param message
 *            the content being sent (serialized SMRF message).
 * @return a location for querying the message status
 */
@POST
@Produces({ MediaType.APPLICATION_OCTET_STREAM })
public Response postMessage(@PathParam("ccid") String ccid, byte[] serializedMessage) {
    ImmutableMessage message;
    try {
        message = new ImmutableMessage(serializedMessage);
    } catch (EncodingException | UnsuppportedVersionException e) {
        throw new JoynrHttpException(Status.BAD_REQUEST, JOYNRMESSAGINGERROR_DESERIALIZATIONFAILED);
    }
    try {
        log.debug("POST message to channel: {} message: {}", ccid, message);
        // TODO Can this happen at all with empty path parameter???
        if (ccid == null) {
            log.error("POST message to channel: NULL. message: {} dropped because: channel Id was not set", message);
            throw new JoynrHttpException(Status.BAD_REQUEST, JOYNRMESSAGINGERROR_CHANNELNOTSET);
        }
        // send the message to the receiver.
        if (message.getTtlMs() == 0) {
            log.error("POST message to channel: {} message: {} dropped because: TTL not set", ccid, message);
            throw new JoynrHttpException(Status.BAD_REQUEST, JOYNRMESSAGINGERROR_EXPIRYDATENOTSET);
        }
        if (message.getTtlMs() < timestampProvider.getCurrentTime()) {
            log.warn("POST message {} to cluster controller: {} dropped because: TTL expired", ccid, message.getId());
            throw new JoynrHttpException(Status.BAD_REQUEST, JOYNRMESSAGINGERROR_EXPIRYDATEEXPIRED, request.getRemoteHost());
        }
        if (!messagingService.isAssignedForChannel(ccid)) {
            // scalability extensions: in case that other component should handle this channel
            log.debug("POST message {}: Bounce Proxy not assigned for channel: {}", message, ccid);
            if (messagingService.hasChannelAssignmentMoved(ccid)) {
                // scalability extension: in case that this component was responsible before but isn't any more
                log.debug("POST message {}: Bounce Proxy assignment moved for channel: {}", message, ccid);
                return Response.status(410).build();
            } else {
                log.debug("POST message {}: channel  unknown: {}", message, ccid);
                return Response.status(404).build();
            }
        }
        // content.
        if (!messagingService.hasMessageReceiver(ccid)) {
            log.debug("POST message {}: no receiver for channel: {}", message, ccid);
            return Response.noContent().build();
        }
        messagingService.passMessageToReceiver(ccid, serializedMessage);
        // the location that can be queried to get the message
        // status
        // TODO REST URL for message status?
        URI location = ui.getBaseUriBuilder().path("messages/" + message.getId()).build();
        // encode URL in case we use sessions
        String encodeURL = response.encodeURL(location.toString());
        // return the message status location to the sender.
        return Response.created(URI.create(encodeURL)).header("msgId", message.getId()).build();
    } catch (WebApplicationException e) {
        throw e;
    } catch (Exception e) {
        log.debug("POST message to channel: {} error: {}", ccid, e.getMessage());
        throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : JoynrHttpException(io.joynr.communications.exceptions.JoynrHttpException) WebApplicationException(javax.ws.rs.WebApplicationException) EncodingException(io.joynr.smrf.EncodingException) ImmutableMessage(joynr.ImmutableMessage) URI(java.net.URI) UnsuppportedVersionException(io.joynr.smrf.UnsuppportedVersionException) JoynrHttpException(io.joynr.communications.exceptions.JoynrHttpException) UnsuppportedVersionException(io.joynr.smrf.UnsuppportedVersionException) EncodingException(io.joynr.smrf.EncodingException) WebApplicationException(javax.ws.rs.WebApplicationException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Aggregations

ImmutableMessage (joynr.ImmutableMessage)63 Test (org.junit.Test)42 Response (com.jayway.restassured.response.Response)12 FailureAction (io.joynr.messaging.FailureAction)11 SuccessAction (io.joynr.messaging.SuccessAction)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 EncodingException (io.joynr.smrf.EncodingException)9 MutableMessage (joynr.MutableMessage)9 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)8 UnsuppportedVersionException (io.joynr.smrf.UnsuppportedVersionException)8 Semaphore (java.util.concurrent.Semaphore)7 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)6 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)6 MessagingQos (io.joynr.messaging.MessagingQos)6 Ignore (org.junit.Ignore)6 AbstractModule (com.google.inject.AbstractModule)5 JoynrMessageProcessor (io.joynr.messaging.JoynrMessageProcessor)5 Injector (com.google.inject.Injector)4 Module (com.google.inject.Module)4 CcMessageRouter (io.joynr.messaging.routing.CcMessageRouter)4