TEST_F(MessagingTest, routeMsgToHttpCommunicationMgr) { JoynrMessage message = messageFactory.createRequest( senderId, receiverId, qos, request); message.setHeaderReplyChannelId(senderChannelId); // InProcessMessagingSkeleton should not receive the message EXPECT_CALL(*inProcessMessagingSkeleton, transmit(Eq(message))) .Times(0); // HttpCommunicationManager should receive the message EXPECT_CALL(*mockMessageSender, sendMessage(Eq(receiverChannelId),Eq(message))) .Times(1).WillRepeatedly(ReleaseSemaphore(&semaphore)); std::shared_ptr<system::RoutingTypes::ChannelAddress> joynrMessagingEndpointAddr = std::shared_ptr<system::RoutingTypes::ChannelAddress>(new system::RoutingTypes::ChannelAddress()); joynrMessagingEndpointAddr->setChannelId(receiverChannelId); messageRouter->addNextHop(receiverId, joynrMessagingEndpointAddr); messageRouter->route(message); WaitXTimes(1); }
TEST_F(LibJoynrDbusCommunicationTests, transmit_message) { QString ccMessagingAddress("local:joynr.messaging:cc.message6"); // register skeletons MockMessaging* msgMock = new MockMessaging(); EXPECT_CALL(*msgMock, transmit(A<JoynrMessage&>())).Times(1); auto msgSkeleton = new IDbusSkeletonWrapper<DbusMessagingSkeleton, IMessaging>(*msgMock, ccMessagingAddress); // get stub DbusMessagingStubAdapter* msgStub = new DbusMessagingStubAdapter(ccMessagingAddress); ASSERT_TRUE(msgStub->isProxyAvailable()); // create message JoynrMessage msg; msg.setType(JoynrMessage::VALUE_MESSAGE_TYPE_ONE_WAY); msg.setHeaderTo(QString("local")); msg.setPayload("This is a test"); // create messaging qos msgStub->transmit(msg); // delete skeleton delete msgSkeleton; // error on transmission msgStub->transmit(msg); // stub not availabe ASSERT_FALSE(msgStub->isProxyAvailable()); delete msgStub; delete msgMock; }
JoynrMessage JoynrMessageFactory::createOneWay(const std::string& senderId, const std::string& receiverId, const MessagingQos& qos, const Reply& payload) const { JoynrMessage msg; msg.setType(JoynrMessage::VALUE_MESSAGE_TYPE_ONE_WAY); initMsg(msg, senderId, receiverId, qos.getTtl(), JsonSerializer::serialize<Reply>(payload)); return msg; }
TEST_F(JoynrMessageFactoryTest, createRequest_withContentType) { JoynrMessage joynrMessage = messageFactory.createRequest( senderID, receiverID, qos, request ); EXPECT_QSTREQ(senderID, joynrMessage.getHeaderFrom()); EXPECT_QSTREQ(receiverID, joynrMessage.getHeaderTo()); }
TEST_F(JoynrMessageFactoryTest, createPublication){ JoynrMessage joynrMessage = messageFactory.createSubscriptionPublication( senderID, receiverID, qos, subscriptionPublication ); checkParticipantIDs(joynrMessage); checkSubscriptionPublication(joynrMessage); EXPECT_QSTREQ(JoynrMessage::VALUE_MESSAGE_TYPE_PUBLICATION, joynrMessage.getType()); }
TEST_F(JoynrMessageFactoryTest, createOneWay){ JoynrMessage joynrMessage = messageFactory.createOneWay( senderID, receiverID, qos, reply ); checkParticipantIDs(joynrMessage); checkReply(joynrMessage); EXPECT_QSTREQ(JoynrMessage::VALUE_MESSAGE_TYPE_ONE_WAY, joynrMessage.getType()); }
JoynrMessage JoynrMessageFactory::createRequest(const std::string& senderId, const std::string& receiverId, const MessagingQos& qos, const Request& payload) const { // create message and set type JoynrMessage msg; msg.setType(JoynrMessage::VALUE_MESSAGE_TYPE_REQUEST); initMsg(msg, senderId, receiverId, qos.getTtl(), JsonSerializer::serialize<Request>(payload)); return msg; }
TEST_F(JoynrMessageFactoryTest, createSubscriptionStop){ QString subscriptionId("TEST-SubscriptionId"); SubscriptionStop subscriptionStop; subscriptionStop.setSubscriptionId(subscriptionId); JoynrMessage joynrMessage = messageFactory.createSubscriptionStop( senderID, receiverID, qos, subscriptionStop ); checkParticipantIDs(joynrMessage); EXPECT_QSTREQ(JoynrMessage::VALUE_MESSAGE_TYPE_SUBSCRIPTION_STOP, joynrMessage.getType()); }
JoynrMessage JoynrMessageFactory::createSubscriptionStop(const std::string& senderId, const std::string& receiverId, const MessagingQos& qos, const SubscriptionStop& payload) const { JoynrMessage msg; msg.setType(JoynrMessage::VALUE_MESSAGE_TYPE_SUBSCRIPTION_STOP); initMsg(msg, senderId, receiverId, qos.getTtl(), JsonSerializer::serialize<SubscriptionStop>(payload)); return msg; }
void DbusMessagingStubAdapter::transmit(JoynrMessage& message) { logMethodCall(QString("transmit message with ID: %1 and payload: %2") .arg(message.getHeaderMessageId()) .arg(QString(message.getPayload()))); // copy joynr message joynr::messaging::IMessaging::JoynrMessage dbusMsg; DbusMessagingUtil::copyJoynrMsgToDbusMsg(message, dbusMsg); // call CommonAPI::CallStatus status; proxy->transmit(dbusMsg, status); // print the status printCallStatus(status, "transmit"); }
void WebSocketLibJoynrMessagingSkeleton::onTextMessageReceived(const std::string& message) { // deserialize message and transmit JoynrMessage* joynrMsg = JsonSerializer::deserialize<JoynrMessage>(message); if (joynrMsg == nullptr || joynrMsg->getType().empty()) { JOYNR_LOG_ERROR(logger, "Unable to deserialize joynr message object from: {}", message); return; } JOYNR_LOG_TRACE(logger, "<<< INCOMING <<< {}", message); // message router copies joynr message when scheduling thread that handles // message delivery transmit(*joynrMsg); delete joynrMsg; }
void LongPollingMessageReceiver::processReceivedJsonObjects(const std::string& jsonObject) { JoynrMessage* msg = JsonSerializer::deserialize<JoynrMessage>(jsonObject); if (msg == nullptr) { JOYNR_LOG_ERROR(logger, "Unable to deserialize message. Raw message: {}", jsonObject); return; } if (msg->getType().empty()) { JOYNR_LOG_ERROR(logger, "received empty message - dropping Messages"); return; } if (!msg->containsHeaderExpiryDate()) { JOYNR_LOG_ERROR(logger, "received message [msgId=[{}] without decay time - dropping message", msg->getHeaderMessageId()); } if (msg->getType() == JoynrMessage::VALUE_MESSAGE_TYPE_REQUEST || msg->getType() == JoynrMessage::VALUE_MESSAGE_TYPE_SUBSCRIPTION_REQUEST || msg->getType() == JoynrMessage::VALUE_MESSAGE_TYPE_BROADCAST_SUBSCRIPTION_REQUEST) { // TODO ca: check if replyTo header info is available? std::string replyChannelId = msg->getHeaderReplyChannelId(); std::shared_ptr<system::RoutingTypes::ChannelAddress> address( new system::RoutingTypes::ChannelAddress(replyChannelId)); messageRouter->addNextHop(msg->getHeaderFrom(), address); } // messageRouter.route passes the message reference to the MessageRunnable, which copies it. // TODO would be nicer if the pointer would be passed to messageRouter, on to MessageRunnable, // and runnable should delete it. messageRouter->route(*msg); delete msg; }
TEST_F(JoynrMessageFactoryTest, createSubscriptionRequest){ auto subscriptionQos = QSharedPointer<SubscriptionQos>(new OnChangeSubscriptionQos()); SubscriptionRequest subscriptionRequest; subscriptionRequest.setSubscriptionId(QString("subscriptionId")); subscriptionRequest.setAttributeName(QString("attributeName")); subscriptionRequest.setQos(subscriptionQos); JoynrMessage joynrMessage = messageFactory.createSubscriptionRequest( senderID, receiverID, qos, subscriptionRequest ); checkParticipantIDs(joynrMessage); EXPECT_QSTREQ(JoynrMessage::VALUE_MESSAGE_TYPE_SUBSCRIPTION_REQUEST, joynrMessage.getType()); }
JoynrMessage JoynrMessageFactory::createSubscriptionPublication( const std::string& senderId, const std::string& receiverId, const MessagingQos& qos, const SubscriptionPublication& payload) const { JoynrMessage msg; msg.setType(JoynrMessage::VALUE_MESSAGE_TYPE_PUBLICATION); initMsg(msg, senderId, receiverId, qos.getTtl(), JsonSerializer::serialize<SubscriptionPublication>(payload)); return msg; }
JoynrMessage JoynrMessageFactory::createBroadcastSubscriptionRequest( const std::string& senderId, const std::string& receiverId, const MessagingQos& qos, const BroadcastSubscriptionRequest& payload) const { JoynrMessage msg; msg.setType(JoynrMessage::VALUE_MESSAGE_TYPE_BROADCAST_SUBSCRIPTION_REQUEST); initMsg(msg, senderId, receiverId, qos.getTtl(), JsonSerializer::serialize<BroadcastSubscriptionRequest>(payload)); return msg; }
TEST_F(JoynrMessageFactoryTest, testRequestContentType){ Request request; QVariantList params; params.append("test"); request.setMethodName(QString("methodName")); request.setParams(params); JoynrMessage message = messageFactory.createRequest( senderID, receiverID, qos, request ); EXPECT_QSTREQ(JoynrMessage::VALUE_CONTENT_TYPE_APPLICATION_JSON, message.getHeaderContentType()); }
void Dispatcher::receive(const JoynrMessage& message) { LOG_DEBUG(logger, QString("receive(message). Message payload: %1").arg(QString(message.getPayload()))); ReceivedMessageRunnable* receivedMessageRunnable = new ReceivedMessageRunnable(message, *this); handleReceivedMessageThreadPool.start(receivedMessageRunnable); }
TEST_F(MessagingTest, routeMultipleMessages) { JoynrMessage message = messageFactory.createRequest( senderId, receiverId, qos, request); message.setHeaderReplyChannelId(senderChannelId); std::string receiverId2("receiverId2"); JoynrMessage message2 = messageFactory.createRequest( senderId, receiverId2, qos, request); message2.setHeaderReplyChannelId(senderChannelId); // InProcessMessagingSkeleton should receive the message2 and message3 EXPECT_CALL(*inProcessMessagingSkeleton, transmit(Eq(message2))) .Times(2).WillRepeatedly(ReleaseSemaphore(&semaphore)); // MessageSender should receive the message EXPECT_CALL(*mockMessageSender, sendMessage(Eq(receiverChannelId), Eq(message))) .Times(1).WillRepeatedly(ReleaseSemaphore(&semaphore)); EXPECT_CALL(*mockMessageReceiver, getReceiveChannelId()) // .WillOnce(ReturnRefOfCopy(senderChannelId)); .WillRepeatedly(ReturnRefOfCopy(senderChannelId)); std::shared_ptr<InProcessMessagingAddress> messagingSkeletonEndpointAddr( new InProcessMessagingAddress(inProcessMessagingSkeleton) ); messageRouter->addNextHop(receiverId2, messagingSkeletonEndpointAddr); std::shared_ptr<system::RoutingTypes::ChannelAddress> joynrMessagingEndpointAddr = std::shared_ptr<system::RoutingTypes::ChannelAddress>(new system::RoutingTypes::ChannelAddress()); joynrMessagingEndpointAddr->setChannelId(receiverChannelId); messageRouter->addNextHop(receiverId, joynrMessagingEndpointAddr); messageRouter->route(message); messageRouter->route(message2); messageRouter->route(message2); WaitXTimes(3); }
TEST_F(LibJoynrDbusCommunicationTests, dbus_skeletonwrapper_register_unregister) { QString ccMessagingAddress("local:cc.messaging:cc.messaging8"); // craete mock and expect 2 calls MockMessaging* msgMock = new MockMessaging(); EXPECT_CALL(*msgMock, transmit(A<JoynrMessage&>())).Times(2); // create the skeleton LOG_INFO(logger, "Register skeleton"); auto msgSkeleton = new IDbusSkeletonWrapper<DbusMessagingSkeleton, IMessaging>(*msgMock, ccMessagingAddress); // create message JoynrMessage msg; msg.setType(JoynrMessage::VALUE_MESSAGE_TYPE_ONE_WAY); msg.setHeaderTo(QString("local")); msg.setPayload("This is a test"); // get stub DbusMessagingStubAdapter* msgStub = new DbusMessagingStubAdapter(ccMessagingAddress); ASSERT_TRUE(msgStub->isProxyAvailable()); // call method LOG_INFO(logger, "Transmit message: should work"); msgStub->transmit(msg); // delete skeleton LOG_INFO(logger, "Delete skeleton"); delete msgSkeleton; // call method LOG_INFO(logger, "Transmit message: should fail"); msgStub->transmit(msg); // register skeleton LOG_INFO(logger, "Register skeleton"); msgSkeleton = new IDbusSkeletonWrapper<DbusMessagingSkeleton, IMessaging>(*msgMock, ccMessagingAddress); // call method LOG_INFO(logger, "Transmit message: should work"); msgStub->transmit(msg); delete msgSkeleton; delete msgMock; }
void checkReply(const JoynrMessage& joynrMessage){ QString expectedPayload = QString( "{\"_typeName\":\"joynr.Reply\"," "\"requestReplyId\":\"%1\"," "\"response\":\"response\"}" ); expectedPayload = expectedPayload.arg(reply.getRequestReplyId()); EXPECT_EQ(expectedPayload, QString(joynrMessage.getPayload())); }
TEST_F(MessageQueueTest, queueDequeueMultipleMessagesForOneParticipant) { // add messages to the queue JoynrMessage msg; msg.setHeaderTo("TEST"); msg.setHeaderExpiryDate(expiryDate); messageQueue.queueMessage(msg); messageQueue.queueMessage(msg); EXPECT_EQ(messageQueue.getQueueLength(), 2); // get messages from queue MessageQueueItem* item = messageQueue.getNextMessageForParticipant("TEST"); EXPECT_EQ(item->getContent(), msg); EXPECT_EQ(messageQueue.getQueueLength(), 1); item = messageQueue.getNextMessageForParticipant("TEST"); EXPECT_EQ(item->getContent(), msg); EXPECT_EQ(messageQueue.getQueueLength(), 0); }
void checkSubscriptionPublication(const JoynrMessage& joynrMessage){ QString expectedPayload = QString( "{\"_typeName\":\"joynr.SubscriptionPublication\"," "\"response\":\"publication\"," "\"subscriptionId\":\"%1\"}" ); expectedPayload = expectedPayload.arg(subscriptionPublication.getSubscriptionId()); EXPECT_EQ(expectedPayload, QString(joynrMessage.getPayload())); }
void checkRequest(const JoynrMessage& joynrMessage){ //TODO create expected string from params and methodName QString expectedPayload = QString( "{\"_typeName\":\"joynr.Request\"," "\"methodName\":\"methodName\"," "\"paramDatatypes\":[\"java.lang.Integer\",\"java.lang.String\"]," "\"params\":[42,\"value\"]," "\"requestReplyId\":\"%1\"}" ); expectedPayload = expectedPayload.arg(request.getRequestReplyId()); EXPECT_EQ(expectedPayload, QString(joynrMessage.getPayload())); }
TEST_F(JoynrMessageFactoryTest, createRequest){ JoynrMessage joynrMessage = messageFactory.createRequest( senderID, receiverID, qos, request ); //warning if prepareRequest needs to long this assert will fail as it compares absolute timestamps QDateTime expectedExpiryDate = QDateTime::currentDateTime().addMSecs(qos.getTtl()); QDateTime expiryDate = joynrMessage.getHeaderExpiryDate(); EXPECT_NEAR(expectedExpiryDate.toMSecsSinceEpoch(), expiryDate.toMSecsSinceEpoch(), 100.); LOG_DEBUG(logger, QString("expiryDate: %1 [%2]") .arg(expiryDate.toString()) .arg(expiryDate.toMSecsSinceEpoch())); LOG_DEBUG(logger, QString("expectedExpiryDate: %1 [%2]") .arg(expectedExpiryDate.toString()) .arg(expectedExpiryDate.toMSecsSinceEpoch())); checkParticipantIDs(joynrMessage); checkRequest(joynrMessage); EXPECT_QSTREQ(JoynrMessage::VALUE_MESSAGE_TYPE_REQUEST, joynrMessage.getType()); }
void Dispatcher::handleSubscriptionStopReceived(const JoynrMessage& message) { LOG_DEBUG(logger, "handleSubscriptionStopReceived"); QByteArray jsonSubscriptionStop = message.getPayload(); SubscriptionStop* subscriptionStop = JsonSerializer::deserialize<SubscriptionStop>(jsonSubscriptionStop); if (subscriptionStop == Q_NULLPTR) { LOG_ERROR(logger, QString("Unable to deserialize subscription stop object from: %1") .arg(QString::fromUtf8(jsonSubscriptionStop))); return; } QString subscriptionId = subscriptionStop->getSubscriptionId(); assert(publicationManager != NULL); publicationManager->stopPublication(subscriptionId); }
void Dispatcher::handleReplyReceived(const JoynrMessage& message) { // json request // lookup necessary data std::string jsonReply = message.getPayload(); // deserialize the jsonReply try { Reply reply = JsonSerializer::deserialize<Reply>(jsonReply); std::string requestReplyId = reply.getRequestReplyId(); std::shared_ptr<IReplyCaller> caller = replyCallerDirectory.lookup(requestReplyId); if (caller == nullptr) { // This used to be a fatal error, but it is possible that the replyCallerDirectory // removed // the caller // because its lifetime exceeded TTL JOYNR_LOG_INFO( logger, "caller not found in the ReplyCallerDirectory for requestid {}, ignoring", requestReplyId); return; } // Get the reply interpreter - this has to be a reference to support ReplyInterpreter // polymorphism int typeId = caller->getTypeId(); IReplyInterpreter& interpreter = MetaTypeRegistrar::instance().getReplyInterpreter(typeId); // pass reply interpreter.execute(caller, reply); // Clean up removeReplyCaller(requestReplyId); } catch (const std::invalid_argument& e) { JOYNR_LOG_ERROR(logger, "Unable to deserialize reply object from: {} - error {}", jsonReply, e.what()); } }
void Dispatcher::handleSubscriptionStopReceived(const JoynrMessage& message) { JOYNR_LOG_DEBUG(logger, "handleSubscriptionStopReceived"); std::string jsonSubscriptionStop = message.getPayload(); std::string subscriptionId; try { SubscriptionStop subscriptionStop = JsonSerializer::deserialize<SubscriptionStop>(jsonSubscriptionStop); subscriptionId = subscriptionStop.getSubscriptionId(); } catch (const std::invalid_argument& e) { JOYNR_LOG_ERROR(logger, "Unable to deserialize subscription stop object from: {} - error: {}", jsonSubscriptionStop, e.what()); return; } assert(publicationManager != nullptr); publicationManager->stopPublication(subscriptionId); }
void Dispatcher::handleReplyReceived(const JoynrMessage& message) { // json request // lookup necessary data QByteArray jsonReply = message.getPayload(); // deserialize the jsonReply Reply* reply = JsonSerializer::deserialize<Reply>(jsonReply); if (reply == Q_NULLPTR) { LOG_ERROR(logger, QString("Unable to deserialize reply object from: %1") .arg(QString::fromUtf8(jsonReply))); return; } QString requestReplyId = reply->getRequestReplyId(); QSharedPointer<IReplyCaller> caller = replyCallerDirectory.lookup(requestReplyId.toStdString()); if (caller == NULL) { // This used to be a fatal error, but it is possible that the replyCallerDirectory removed // the caller // because its lifetime exceeded TTL LOG_INFO(logger, "caller not found in the ReplyCallerDirectory for requestid " + requestReplyId + ", ignoring"); return; } // Get the reply interpreter - this has to be a reference to support ReplyInterpreter // polymorphism int typeId = caller->getTypeId(); IReplyInterpreter& interpreter = MetaTypeRegistrar::instance().getReplyInterpreter(typeId); // pass reply interpreter.execute(caller, *reply); // Clean up delete reply; removeReplyCaller(requestReplyId.toStdString()); }
void Dispatcher::handlePublicationReceived(const JoynrMessage& message) { std::string jsonSubscriptionPublication = message.getPayload(); try { SubscriptionPublication subscriptionPublication = JsonSerializer::deserialize<SubscriptionPublication>(jsonSubscriptionPublication); std::string subscriptionId = subscriptionPublication.getSubscriptionId(); assert(subscriptionManager != nullptr); std::shared_ptr<ISubscriptionCallback> callback = subscriptionManager->getSubscriptionCallback(subscriptionId); if (!callback) { JOYNR_LOG_ERROR(logger, "Dropping reply for non/no more existing subscription with id = {}", subscriptionId); return; } subscriptionManager->touchSubscriptionState(subscriptionId); int typeId = callback->getTypeId(); // Get the publication interpreter - this has to be a reference to support // PublicationInterpreter polymorphism IPublicationInterpreter& interpreter = MetaTypeRegistrar::instance().getPublicationInterpreter(typeId); interpreter.execute(callback, subscriptionPublication); } catch (const std::invalid_argument& e) { JOYNR_LOG_ERROR( logger, "Unable to deserialize subscription publication object from: {} - error: {}", jsonSubscriptionPublication, e.what()); } }
void Dispatcher::handlePublicationReceived(const JoynrMessage& message) { QByteArray jsonSubscriptionPublication = message.getPayload(); SubscriptionPublication* subscriptionPublication = JsonSerializer::deserialize<SubscriptionPublication>(jsonSubscriptionPublication); if (subscriptionPublication == Q_NULLPTR) { LOG_ERROR(logger, QString("Unable to deserialize subscription publication object from: %1") .arg(QString::fromUtf8(jsonSubscriptionPublication))); return; } QString subscriptionId = subscriptionPublication->getSubscriptionId(); assert(subscriptionManager != NULL); QSharedPointer<ISubscriptionCallback> callback = subscriptionManager->getSubscriptionCallback(subscriptionId); if (callback.isNull()) { LOG_ERROR(logger, "Dropping reply for non/no more existing subscription with id=" + subscriptionId); delete subscriptionPublication; return; } subscriptionManager->touchSubscriptionState(subscriptionId); int typeId = callback->getTypeId(); // Get the publication interpreter - this has to be a reference to support // PublicationInterpreter polymorphism IPublicationInterpreter& interpreter = MetaTypeRegistrar::instance().getPublicationInterpreter(typeId); interpreter.execute(callback, *subscriptionPublication); delete subscriptionPublication; }