From e6a8f5c5e620d9e960ff72cea4530becdfeb6d2a Mon Sep 17 00:00:00 2001 From: Andriy Gudz Date: Thu, 6 Jul 2017 11:11:21 +0300 Subject: [PATCH] Removed root resource from internal logic of MqHandler --- device_core/iotivity_lib/inc/iotutils.h | 2 +- device_core/iotivity_lib/inc/mqhandler.h | 17 +++++---- device_core/iotivity_lib/inc/resource_callbacks.h | 2 ++ device_core/iotivity_lib/src/iotivity.cpp | 6 ++-- device_core/iotivity_lib/src/iotutils.cpp | 6 ++-- device_core/iotivity_lib/src/mqhandler.cpp | 40 ++++++++++++---------- .../iotivity_lib/src/resource_callbacks.cpp | 7 ++-- device_core/utest/test_mq.cpp | 13 +++---- 8 files changed, 51 insertions(+), 42 deletions(-) diff --git a/device_core/iotivity_lib/inc/iotutils.h b/device_core/iotivity_lib/inc/iotutils.h index 39efa62..00bdc44 100644 --- a/device_core/iotivity_lib/inc/iotutils.h +++ b/device_core/iotivity_lib/inc/iotutils.h @@ -84,7 +84,7 @@ void guardTimeout(bool timeout, const std::string& message); * @param resultCode result code in callback of signin/signup * @param message string to point where exception occurred */ -void guardSignErrorCode(int resultCode, const std::string& message); +void guardPostErrorCode(int resultCode, const std::string& message); /** * @brief guardErrorCode throws if EC_IOTIVITY_ERROR if any iotivity error code occurred diff --git a/device_core/iotivity_lib/inc/mqhandler.h b/device_core/iotivity_lib/inc/mqhandler.h index 0fe9f3d..910f630 100644 --- a/device_core/iotivity_lib/inc/mqhandler.h +++ b/device_core/iotivity_lib/inc/mqhandler.h @@ -25,41 +25,41 @@ public: /** * @brief MqHandler constructor * @param cloudHost address of cloud - * @param rootUri root topic of device (Ex: '/oic/ps/srv') */ - MqHandler(const std::string& cloudHost, const std::string& rootUri); + MqHandler(const std::string& cloudHost); /** * @brief publish method to send representation to topic - * @param subTopic sub topic that shoud be after root topic (Ex. '/policy') + * @param subTopic sub topic that shoud be after root topic (Ex. '/srv/policy') * @param rep representation that should be send */ void publish(const std::string& subTopic, OC::OCRepresentation rep); /** * @brief subscribe method to subscribe on topic - * @param subTopic sub topic that shoud be after root topic (Ex. '/policy') + * @param subTopic sub topic that shoud be after root topic (Ex. '/srv/policy') * @param subscribeCB method to that should be called after publish */ void subscribe(const std::string& subTopic, OC::ObserveCallback subscribeCB); /** * @brief unsubscribe method to unsubscribe from topic - * @param subTopic sub topic that shoud be after root topic (Ex. '/policy') + * @param subTopic sub topic that shoud be after root topic (Ex. '/srv/policy') */ void unsubscribe(const std::string& subTopic); -private: /** * @brief getTopic returns topic by uri. * Method will create topic if it doesn't exists. * Method will construct topic if it already exists. * Method will throw exception if topic can't be retrieve. + * Call this method to create parent sub topic before using child. * - * @param topic string uri to topic (Ex: '/oic/ps/srv/policy') + * @param subTopic sub topic that shoud be after root topic (Ex. '/srv/policy') * @return resource object that represent topic */ - OC::OCResource::Ptr getTopic(const std::string& topic); + OC::OCResource::Ptr getTopic(const std::string& subTopic); +private: /** * @brief topicCache contains cached topic resources to hold @@ -68,7 +68,6 @@ private: std::map topicCache; std::string cloudHost; - std::string rootUri; OC::OCResource::Ptr brokerResource; }; diff --git a/device_core/iotivity_lib/inc/resource_callbacks.h b/device_core/iotivity_lib/inc/resource_callbacks.h index 227ce0c..f3da899 100644 --- a/device_core/iotivity_lib/inc/resource_callbacks.h +++ b/device_core/iotivity_lib/inc/resource_callbacks.h @@ -141,6 +141,8 @@ struct PostResourceCallback : public CallbackBase { typedef std::shared_ptr Sptr; + int errorCode = OC_STACK_ERROR; + /** * @brief call - callback routine, called from iotivity framework * @param ctx [in] this struct self weak reference diff --git a/device_core/iotivity_lib/src/iotivity.cpp b/device_core/iotivity_lib/src/iotivity.cpp index b44bbf0..eab2a0a 100644 --- a/device_core/iotivity_lib/src/iotivity.cpp +++ b/device_core/iotivity_lib/src/iotivity.cpp @@ -324,7 +324,7 @@ void IoTivity::signUp(const std::string& host, const std::string& auth_provider, signUpCb->condVar.wait_for(lock, std::chrono::seconds(DEFAULT_TIMEOUT)); guardTimeout(signUpCb->timeout, SIGNUP); - guardSignErrorCode(signUpCb->resultCode, SIGNUP); + guardPostErrorCode(signUpCb->resultCode, SIGNUP); cloud_host = host; cloud_uid = signUpCb->uid; @@ -365,7 +365,7 @@ void IoTivity::signIn() signInCb->condVar.wait_for(lock, std::chrono::seconds(DEFAULT_TIMEOUT)); guardTimeout(signInCb->timeout, SIGNIN); - guardSignErrorCode(signInCb->resultCode, SIGNIN); + guardPostErrorCode(signInCb->resultCode, SIGNIN); if (cloud_sid.empty()) { @@ -408,7 +408,7 @@ void IoTivity::signOut() // No need to throw OC_STACK_UNAUTHORIZED_REQ error if (signOutCb->resultCode != OC_STACK_UNAUTHORIZED_REQ) - guardSignErrorCode(signOutCb->resultCode, SIGNOUT); + guardPostErrorCode(signOutCb->resultCode, SIGNOUT); } void IoTivity::devicePresenceHandle( diff --git a/device_core/iotivity_lib/src/iotutils.cpp b/device_core/iotivity_lib/src/iotutils.cpp index 9263cc3..7fa6a1e 100644 --- a/device_core/iotivity_lib/src/iotutils.cpp +++ b/device_core/iotivity_lib/src/iotutils.cpp @@ -68,10 +68,10 @@ void guardTimeout(bool timeout, const std::string& message) throw IoTInternalError(message + " timeout exceed", EC_TIMEOUT_ERROR); } -void guardSignErrorCode(int resultCode, const std::string& message) +void guardPostErrorCode(int resultCode, const std::string& message) { - if (resultCode != 4) - throw IoTInternalError(message + " callback error:" + std::to_string(resultCode), EC_AUTH_ERROR); + if (resultCode != OC_STACK_RESOURCE_CHANGED) + throw IoTInternalError(message + " callback error. OCStackResult=" + std::to_string(resultCode), EC_AUTH_ERROR); } void guardErrorCode(OCStackResult resultCode, const std::string& message) diff --git a/device_core/iotivity_lib/src/mqhandler.cpp b/device_core/iotivity_lib/src/mqhandler.cpp index a5b109f..106ae10 100644 --- a/device_core/iotivity_lib/src/mqhandler.cpp +++ b/device_core/iotivity_lib/src/mqhandler.cpp @@ -33,8 +33,8 @@ static const string DEFAULT_MQ_BROKER_URI_ROOT = "/oic/ps"; } -MqHandler::MqHandler(const std::string& cloudHost, const std::string& rootUri): - cloudHost(cloudHost), rootUri(rootUri) +MqHandler::MqHandler(const std::string& cloudHost): + cloudHost(cloudHost) { brokerResource = OCPlatform::constructResourceObject( cloudHost, @@ -46,13 +46,11 @@ MqHandler::MqHandler(const std::string& cloudHost, const std::string& rootUri): if (!brokerResource) throw IoTInternalError("MQ Broker resource construct error.", EC_IOTIVITY_ERROR); - - if (!getTopic(rootUri)) - throw IoTInternalError("Root resource construct error.", EC_IOTIVITY_ERROR); } -OC::OCResource::Ptr MqHandler::getTopic(const std::string& topic) +OC::OCResource::Ptr MqHandler::getTopic(const std::string& subTopic) { + std::string topic = DEFAULT_MQ_BROKER_URI_ROOT + subTopic; QueryParamsMap query; OCRepresentation rep; MqCreateTopicCallback::Sptr callback = std::make_shared(); @@ -60,7 +58,7 @@ OC::OCResource::Ptr MqHandler::getTopic(const std::string& topic) guardErrorCode(brokerResource->createMQTopic( rep, topic, query, bind_callback(callback, PH::_1, PH::_2, PH::_3), - QualityOfService::LowQos), "createMQTopic()"); + QualityOfService::HighQos), "createMQTopic()"); if (!callback->wait()) { @@ -91,23 +89,29 @@ OC::OCResource::Ptr MqHandler::getTopic(const std::string& topic) return nullptr; } -void publishMessageCB2(const HeaderOptions &, const OCRepresentation &, const int eCode) -{ - cout << "Publish message response received, code: " << eCode << endl; -} - void MqHandler::publish(const std::string& subTopic, OCRepresentation rep) { - OC::OCResource::Ptr topicResource = getTopic(rootUri + subTopic); + OC::OCResource::Ptr topicResource = getTopic(subTopic); + + PostResourceCallback::Sptr callback = std::make_shared(); QueryParamsMap query; guardErrorCode(topicResource->publishMQTopic(rep, query, - &publishMessageCB2, QualityOfService::LowQos), "publishMQTopic()"); + bind_callback(callback, PH::_1, PH::_2, PH::_3), + QualityOfService::HighQos), "publishMQTopic()"); + + if (!callback->wait()) + { + LOG_E(TAG, "publishMQTopic() cb not called."); + return; + } + + guardPostErrorCode(callback->errorCode, "publishMQTopic()"); } void MqHandler::subscribe(const std::string& subTopic, ObserveCallback subscribeCB) { - OC::OCResource::Ptr topicResource = getTopic(rootUri + subTopic); + OC::OCResource::Ptr topicResource = getTopic(subTopic); if (!topicResource) return; @@ -116,7 +120,7 @@ void MqHandler::subscribe(const std::string& subTopic, ObserveCallback subscribe QueryParamsMap query; guardErrorCode(topicResource->subscribeMQTopic(ObserveType::Observe, query, - subscribeCB, QualityOfService::LowQos), "subscribeMQTopic()"); + subscribeCB, QualityOfService::HighQos), "subscribeMQTopic()"); } void MqHandler::unsubscribe(const std::string& subTopic) @@ -126,10 +130,10 @@ void MqHandler::unsubscribe(const std::string& subTopic) if (cachedResource) topicResource = cachedResource; else - topicResource = getTopic(rootUri + subTopic); + topicResource = getTopic(subTopic); if (!topicResource) return; - guardErrorCode(topicResource->unsubscribeMQTopic(QualityOfService::LowQos), "unsubscribeMQTopic()"); + guardErrorCode(topicResource->unsubscribeMQTopic(QualityOfService::HighQos), "unsubscribeMQTopic()"); } diff --git a/device_core/iotivity_lib/src/resource_callbacks.cpp b/device_core/iotivity_lib/src/resource_callbacks.cpp index bd8932e..d9271c9 100644 --- a/device_core/iotivity_lib/src/resource_callbacks.cpp +++ b/device_core/iotivity_lib/src/resource_callbacks.cpp @@ -83,12 +83,15 @@ void FindDeviceListCallback::call(std::weak_ptr ctx, std } } -void PostResourceCallback::call(std::weak_ptr ctx, const OC::HeaderOptions & /*ho*/, const OC::OCRepresentation& /*rep*/, const int eCode) +void PostResourceCallback::call(std::weak_ptr ctx, + const OC::HeaderOptions & /*ho*/, + const OC::OCRepresentation& /*rep*/, + const int errorCode) { if (auto p = ctx.lock()) { - LOG_D(TAG, "Post callback return code=%d", eCode); p->signalize(); + p->errorCode = errorCode; } } diff --git a/device_core/utest/test_mq.cpp b/device_core/utest/test_mq.cpp index fcb04a8..3abcacc 100644 --- a/device_core/utest/test_mq.cpp +++ b/device_core/utest/test_mq.cpp @@ -111,21 +111,22 @@ TEST_F(TestIotMQ, publishCorrect) std::unique_lock notificationLock(notificationMtx); - MqHandler subMqHandler(cloud_host, "/oic/ps/srv"); - subMqHandler.subscribe("/test2", &subscribeCB); + MqHandler subMqHandler(cloud_host); + subMqHandler.getTopic("/srv"); + subMqHandler.subscribe("/srv/test2", &subscribeCB); - MqHandler pubMqHandler(cloud_host, "/oic/ps/srv"); - pubMqHandler.publish("/test2", rep); + MqHandler pubMqHandler(cloud_host); + pubMqHandler.publish("/srv/test2", rep); notificationCV.wait_for( notificationLock, std::chrono::seconds(3) ); - subMqHandler.unsubscribe("/test2"); + subMqHandler.unsubscribe("/srv/test2"); rep["message"] = stringToPublish2; - pubMqHandler.publish("/test2", rep); + pubMqHandler.publish("/srv/test2", rep); ASSERT_TRUE(firstCallbackFired); ASSERT_TRUE(lastCallbackFired); -- 2.7.4