From 8666548d3b7e82073fe14f28ce684d5c46c28bd6 Mon Sep 17 00:00:00 2001 From: "jyong2.kim" Date: Mon, 15 Feb 2016 15:45:35 +0900 Subject: [PATCH] Apply to Resource Hosting to RE resource server added feature. Resource Hosting(RH) feature updated for mirrored resource functionality. When this patch merged, RH will support seperate response, bind multiple resource types and interface names. Change-Id: Ie448f915466c413fa2ddec8a2fb159ca1740a35e Signed-off-by: jyong2.kim Reviewed-on: https://gerrit.iotivity.org/gerrit/5003 Reviewed-by: Madan Lanka Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../linux/sampleConsumer/SampleConsumer.cpp | 24 +- .../linux/sampleProvider/SampleProvider.cpp | 16 ++ service/resource-hosting/src/HostingObject.cpp | 298 +++++++++++---------- service/resource-hosting/src/HostingObject.h | 82 +++--- service/resource-hosting/src/RequestObject.cpp | 100 ++++--- service/resource-hosting/src/RequestObject.h | 60 ++--- service/resource-hosting/src/ResourceHosting.cpp | 214 ++++++++------- service/resource-hosting/src/ResourceHosting.h | 75 +++--- .../unittest/RequestObjectUnitTest.cpp | 14 +- .../unittest/ResourceEncapsulationTestSimulator.h | 1 + 10 files changed, 461 insertions(+), 423 deletions(-) diff --git a/service/resource-hosting/SampleApp/linux/sampleConsumer/SampleConsumer.cpp b/service/resource-hosting/SampleApp/linux/sampleConsumer/SampleConsumer.cpp index f46b880..c9b9e9b 100644 --- a/service/resource-hosting/SampleApp/linux/sampleConsumer/SampleConsumer.cpp +++ b/service/resource-hosting/SampleApp/linux/sampleConsumer/SampleConsumer.cpp @@ -38,7 +38,7 @@ const int SUCCESS_RESPONSE = OC_STACK_OK; #define OBSERVE 1 #define GET 2 -#define PUT 3 +#define POST 3 #define DELETE 4 std::shared_ptr< OCResource > g_curResource; @@ -49,7 +49,7 @@ OCStackResult nmfindResource(const std::string &host , const std::string &resour void onObserve(const HeaderOptions &headerOption , const OCRepresentation &rep , const int &eCode, const int &sequenceNumber); -void onPut(const HeaderOptions &headerOption, const OCRepresentation &rep, const int eCode); +void onPost(const HeaderOptions &headerOption, const OCRepresentation &rep, const int eCode); void onGet(const HeaderOptions &headerOption , const OCRepresentation &rep , const int eCode); void onDelete(const HeaderOptions &headerOption , const int eCode); @@ -113,7 +113,7 @@ void startGet(std::shared_ptr< OCResource > resource) std::cout << "To Fail resource get() process" << std::endl; } -void startPut(std::shared_ptr< OCResource > resource) +void startPost(std::shared_ptr< OCResource > resource) { if (resource == NULL) { @@ -127,8 +127,8 @@ void startPut(std::shared_ptr< OCResource > resource) rep.setValue("humidity", 10); QueryParamsMap test; - if (OC_STACK_OK != resource->put(rep, test, &onPut)) - std::cout << "To Fail resource put() process" << std::endl; + if (OC_STACK_OK != resource->post(rep, test, &onPost)) + std::cout << "To Fail resource post() process" << std::endl; } void startDelete(std::shared_ptr< OCResource > resource) @@ -236,13 +236,13 @@ void getRepresentation(std::shared_ptr< OCResource > resource) } } -void onPut(const HeaderOptions &/*headerOption*/, const OCRepresentation &rep, const int eCode) +void onPost(const HeaderOptions &/*headerOption*/, const OCRepresentation &rep, const int eCode) { try { if (eCode == OC_STACK_OK) { - std::cout << "PUT request was successful" << std::endl; + std::cout << "POST request was successful" << std::endl; int humidity; int temperature; rep.getValue("temperature", temperature); @@ -254,13 +254,13 @@ void onPut(const HeaderOptions &/*headerOption*/, const OCRepresentation &rep, c } else { - std::cout << "onPut Response error: " << eCode << std::endl; + std::cout << "onPost Response error: " << eCode << std::endl; std::exit(-1); } } catch (std::exception &e) { - std::cout << "Exception: " << e.what() << " in onPut" << std::endl; + std::cout << "Exception: " << e.what() << " in onPost" << std::endl; } } @@ -328,7 +328,7 @@ void PRINT() std::cout << "********************************************" << std::endl; std::cout << "* method Type : 1 - Observe *" << std::endl; std::cout << "* method Type : 2 - Get *" << std::endl; - std::cout << "* method Type : 3 - Put *" << std::endl; + std::cout << "* method Type : 3 - Post *" << std::endl; std::cout << "********************************************" << std::endl; std::cout << std::endl; } @@ -377,8 +377,8 @@ int main() case GET: startGet(g_curResource); break; - case PUT: - startPut(g_curResource); + case POST: + startPost(g_curResource); break; default: std::cout << "Invalid input, please try again" << std::endl; diff --git a/service/resource-hosting/SampleApp/linux/sampleProvider/SampleProvider.cpp b/service/resource-hosting/SampleApp/linux/sampleProvider/SampleProvider.cpp index 0aaa0cf..bb99443 100644 --- a/service/resource-hosting/SampleApp/linux/sampleProvider/SampleProvider.cpp +++ b/service/resource-hosting/SampleApp/linux/sampleProvider/SampleProvider.cpp @@ -273,6 +273,22 @@ OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request else if (requestType == "POST") { cout << "\t\t\trequestType : POST\n"; + OCRepresentation rep = request->getResourceRepresentation(); + myResource.put(rep); + + if (pResponse) + { + pResponse->setErrorCode(200); + pResponse->setResourceRepresentation(myResource.get()); + } + if (OC_STACK_OK == OCPlatform::sendResponse(pResponse)) + { + ehResult = OC_EH_OK; + } + else + { + cout << "post request Error\n"; + } } else if (requestType == "DELETE") diff --git a/service/resource-hosting/src/HostingObject.cpp b/service/resource-hosting/src/HostingObject.cpp index 0addfb7..ffbd056 100644 --- a/service/resource-hosting/src/HostingObject.cpp +++ b/service/resource-hosting/src/HostingObject.cpp @@ -20,181 +20,189 @@ #include "HostingObject.h" +#include "RCSSeparateResponse.h" +#include "RequestObject.h" + namespace OIC { -namespace Service -{ + namespace Service + { -namespace -{ - const auto sizeofHostingTag = strlen("/hosting"); -} + namespace + { + const auto sizeofHostingTag = strlen("/hosting"); + } -HostingObject::HostingObject() -: remoteObject(nullptr), mirroredServer(nullptr), - pDataUpdateCB(nullptr), pDestroyCB(nullptr) -{ -} + HostingObject::HostingObject() + : remoteObject(nullptr), mirroredServer(nullptr), + pDataUpdateCB(nullptr), pDestroyCB(nullptr) + { + } -HostingObject::~HostingObject() -{ - pDataUpdateCB = {}; + HostingObject::~HostingObject() + { + pDataUpdateCB = {}; - if (remoteObject) - { - remoteObject->stopMonitoring(); - remoteObject->stopCaching(); - } -} + if (remoteObject) + { + remoteObject->stopMonitoring(); + remoteObject->stopCaching(); + } + } -auto HostingObject::getRemoteResource() const -> RemoteObjectPtr -{ - return remoteObject; -} + auto HostingObject::getRemoteResource() const -> RemoteObjectPtr + { + return remoteObject; + } -auto HostingObject::createHostingObject(const RemoteObjectPtr & rResource, - DestroyedCallback destroyCB) -> Ptr -{ - auto newObject = std::make_shared(); + auto HostingObject::createHostingObject(const RemoteObjectPtr & rResource, + DestroyedCallback destroyCB) -> Ptr + { + auto newObject = std::make_shared(); - newObject->remoteObject = rResource; - newObject->pDestroyCB = destroyCB; + newObject->remoteObject = rResource; + newObject->pDestroyCB = destroyCB; - newObject->pDataUpdateCB = std::bind(&HostingObject::dataChangedCB, newObject, - std::placeholders::_1); + newObject->pDataUpdateCB = std::bind(&HostingObject::dataChangedCB, newObject, + std::placeholders::_1); - newObject->remoteObject->startMonitoring( - std::bind(&HostingObject::stateChangedCB, newObject, - std::placeholders::_1)); - newObject->remoteObject->startCaching(newObject->pDataUpdateCB); + newObject->remoteObject->startMonitoring( + std::bind(&HostingObject::stateChangedCB, newObject, + std::placeholders::_1)); + newObject->remoteObject->startCaching(newObject->pDataUpdateCB); - return newObject; -} + return newObject; + } -void HostingObject::destroyHostingObject() -{ - if(pDestroyCB) pDestroyCB(); -} + void HostingObject::destroyHostingObject() + { + if (pDestroyCB) pDestroyCB(); + } -void HostingObject::stateChangedCB(ResourceState state) -{ - switch (state) - { - case ResourceState::ALIVE: - { - if(!this->remoteObject->isCaching()) + void HostingObject::stateChangedCB(ResourceState state) { - try + switch (state) { - this->remoteObject->startCaching(pDataUpdateCB); - }catch(const RCSException &e) + case ResourceState::ALIVE: { - OIC_HOSTING_LOG(DEBUG, - "[HostingObject::stateChangedCB]startCaching InvalidParameterException:%s", - e.what()); + if (!this->remoteObject->isCaching()) + { + try + { + this->remoteObject->startCaching(pDataUpdateCB); + } catch (const RCSException & e) + { + OIC_HOSTING_LOG(DEBUG, + "[HostingObject::stateChangedCB]startCaching InvalidParameterException:%s", + e.what()); + } + } + break; + } + case ResourceState::LOST_SIGNAL: + case ResourceState::DESTROYED: + { + try + { + this->remoteObject->stopCaching(); + this->remoteObject->stopMonitoring(); + } catch (const RCSException & e) + { + OIC_HOSTING_LOG(DEBUG, + "[HostingObject::stateChangedCB]stopWatching InvalidParameterException:%s", + e.what()); + } + mirroredServer.reset(); + destroyHostingObject(); + break; + } + default: + // not support of state + break; } } - break; - } - case ResourceState::LOST_SIGNAL: - case ResourceState::DESTROYED: - { - try - { - this->remoteObject->stopCaching(); - this->remoteObject->stopMonitoring(); - } - catch(const RCSException &e) + + void HostingObject::dataChangedCB(const RCSResourceAttributes & attributes) { - OIC_HOSTING_LOG(DEBUG, - "[HostingObject::stateChangedCB]stopWatching InvalidParameterException:%s", - e.what()); + if (attributes.empty()) + { + return; + } + + std::unique_lock lock(mutexForCB); + if (mirroredServer == nullptr) + { + try + { + mirroredServer = createMirroredServer(this->remoteObject); + } catch (const RCSException & e) + { + OIC_HOSTING_LOG(DEBUG, + "[HostingObject::dataChangedCB]createMirroredServer Exception:%s", + e.what()); + return; + } + } + lock.unlock(); + + RCSResourceObject::LockGuard guard(mirroredServer); + mirroredServer->getAttributes() = std::move(attributes); } - mirroredServer.reset(); - destroyHostingObject(); - break; - } - default: - // not support of state - break; - } -} - -void HostingObject::dataChangedCB(const RCSResourceAttributes & attributes) -{ - if(attributes.empty()) - { - return; - } - { - std::unique_lock lock(mutexForCB); - if(mirroredServer == nullptr) + auto HostingObject::createMirroredServer(RemoteObjectPtr rObject) -> ResourceObjectPtr { + if (rObject == nullptr) + { + throw RCSPlatformException(OC_STACK_ERROR); + } + + std::string fulluri = rObject->getUri(); + std::string uri = fulluri.substr(0, fulluri.size() - sizeofHostingTag); + std::vector types = rObject->getTypes(); + std::vector interfaces = rObject->getInterfaces(); try { - mirroredServer = createMirroredServer(this->remoteObject); - }catch(const RCSException &e) + auto resourceBuild = RCSResourceObject::Builder(uri, types[0], interfaces[0]); + for (unsigned int i = 1; i < types.size(); ++i) + { + resourceBuild.addType(types[i]); + } + for (unsigned int i = 1; i < interfaces.size(); ++i) + { + resourceBuild.addInterface(interfaces[i]); + } + auto retResource = resourceBuild.build(); + + retResource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED); + retResource->setSetRequestHandler( + std::bind(&HostingObject::setRequestHandler, this, + std::placeholders::_1, std::placeholders::_2)); + return retResource; + } catch (...) { - OIC_HOSTING_LOG(DEBUG, - "[HostingObject::dataChangedCB]createMirroredServer Exception:%s", - e.what()); - return; + OIC_HOSTING_LOG(DEBUG, "[HostingObject::createMirroredServer] %s", "Exception"); + throw; } } - } - - RCSResourceObject::LockGuard guard(mirroredServer); - mirroredServer->getAttributes() = std::move(attributes); -} -auto HostingObject::createMirroredServer(RemoteObjectPtr rObject) -> ResourceObjectPtr -{ - if(rObject == nullptr) - { - throw RCSPlatformException(OC_STACK_ERROR); - } - - std::string fulluri = rObject->getUri(); - std::string uri = fulluri.substr(0, fulluri.size() - sizeofHostingTag); - std::vector types = rObject->getTypes(); - std::vector interfaces = rObject->getInterfaces(); - try - { - auto retResource = RCSResourceObject::Builder(uri, types[0], interfaces[0]).build(); - - // TODO need to bind types and interfaces - retResource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED); - retResource->setSetRequestHandler( - std::bind(&HostingObject::setRequestHandler, this, - std::placeholders::_1, std::placeholders::_2)); - return retResource; - }catch(...) - { - OIC_HOSTING_LOG(DEBUG, "[HostingObject::createMirroredServer] %s", "Exception"); - throw; - } -} + RCSSetResponse HostingObject::setRequestHandler(const RCSRequest & primitiveRequest, + RCSResourceAttributes & resourceAttibutes) + { + try + { + RequestObject::invokeRequest(getRemoteResource(), + primitiveRequest, RequestObject::RequestMethod::Set, resourceAttibutes); -RCSSetResponse HostingObject::setRequestHandler(const RCSRequest & primitiveRequest, - RCSResourceAttributes & resourceAttibutes) -{ - (void)primitiveRequest; - try - { - RequestObject newRequest = { }; - newRequest.invokeRequest(remoteObject, RequestObject::RequestMethod::Set, - resourceAttibutes); - }catch(const RCSPlatformException &e) - { - OIC_HOSTING_LOG(DEBUG, - "[HostingObject::setRequestHandler] PlatformException:%s", - e.what()); - throw; - } + } catch (const RCSPlatformException & e) + { + OIC_HOSTING_LOG(DEBUG, + "[HostingObject::setRequestHandler] PlatformException:%s", + e.what()); + throw; + } - return RCSSetResponse::defaultAction(); -} + return RCSSetResponse::separate(); + } -} /* namespace Service */ + } /* namespace Service */ } /* namespace OIC */ diff --git a/service/resource-hosting/src/HostingObject.h b/service/resource-hosting/src/HostingObject.h index c5c9a34..4ff3df8 100755 --- a/service/resource-hosting/src/HostingObject.h +++ b/service/resource-hosting/src/HostingObject.h @@ -22,69 +22,69 @@ #define RH_HOSTINGOBJECT_H_ #include "logger.h" + #include "RCSRemoteResourceObject.h" #include "RCSResourceObject.h" -#include "RequestObject.h" #define OIC_HOSTING_LOG(level, fmt, args...) OIC_LOG_V((level), PCF("Hosting"), fmt, ##args) namespace OIC { -namespace Service -{ + namespace Service + { -class HostingObject : public std::enable_shared_from_this -{ -private: - typedef RCSResourceObject::Ptr ResourceObjectPtr; - typedef RequestObject::Ptr RequestObjectPtr; - typedef RCSRemoteResourceObject::Ptr RemoteObjectPtr; + class HostingObject + { + private: + typedef RCSResourceObject::Ptr ResourceObjectPtr; + typedef RCSRemoteResourceObject::Ptr RemoteObjectPtr; + + public: + typedef std::shared_ptr Ptr; + typedef std::weak_ptr wPtr; -public: - typedef std::shared_ptr Ptr; - typedef std::weak_ptr wPtr; + typedef std::function DestroyedCallback; + typedef RCSRemoteResourceObject::StateChangedCallback BrokerCallback; + typedef RCSRemoteResourceObject::CacheUpdatedCallback CacheCallback; + typedef RCSResourceObject::SetRequestHandler SetRequestHandler; - typedef std::function DestroyedCallback; - typedef RCSRemoteResourceObject::StateChangedCallback BrokerCallback; - typedef RCSRemoteResourceObject::CacheUpdatedCallback CacheCallback; - typedef RCSResourceObject::SetRequestHandler SetRequestHandler; + public: + HostingObject(); + ~HostingObject(); -public: - HostingObject(); - ~HostingObject(); + HostingObject(const HostingObject &) = delete; + HostingObject & operator = (const HostingObject &) = delete; - HostingObject(const HostingObject&) = delete; - HostingObject(HostingObject&&) = delete; - HostingObject& operator=(HostingObject&&) = delete; - HostingObject& operator=(const HostingObject&) = delete; + HostingObject(HostingObject &&) = delete; + HostingObject & operator = (HostingObject &&) = delete; - static HostingObject::Ptr createHostingObject(const RemoteObjectPtr & rResource, - DestroyedCallback destroyCB); + static HostingObject::Ptr createHostingObject(const RemoteObjectPtr & rResource, + DestroyedCallback destroyCB); - RemoteObjectPtr getRemoteResource() const; + RemoteObjectPtr getRemoteResource() const; -private: - RemoteObjectPtr remoteObject; - ResourceObjectPtr mirroredServer; + private: + RemoteObjectPtr remoteObject; + ResourceObjectPtr mirroredServer; - CacheCallback pDataUpdateCB; - DestroyedCallback pDestroyCB; + CacheCallback pDataUpdateCB; + DestroyedCallback pDestroyCB; - std::mutex mutexForCB; + std::mutex mutexForCB; - ResourceObjectPtr createMirroredServer(RemoteObjectPtr rObject); + ResourceObjectPtr createMirroredServer(RemoteObjectPtr rObject); - RCSSetResponse setRequestHandler( - const RCSRequest & request, RCSResourceAttributes & attributes); + RCSSetResponse setRequestHandler( + const RCSRequest & request, RCSResourceAttributes & attributes); - void destroyHostingObject(); + void destroyHostingObject(); -public: - void stateChangedCB(ResourceState state); - void dataChangedCB(const RCSResourceAttributes & attributes); -}; + public: + void stateChangedCB(ResourceState state); + void dataChangedCB(const RCSResourceAttributes & attributes); + }; -} /* namespace Service */ + } /* namespace Service */ } /* namespace OIC */ #endif /* RH_HOSTINGOBJECT_H_ */ diff --git a/service/resource-hosting/src/RequestObject.cpp b/service/resource-hosting/src/RequestObject.cpp index 7851458..c3215f4 100644 --- a/service/resource-hosting/src/RequestObject.cpp +++ b/service/resource-hosting/src/RequestObject.cpp @@ -20,63 +20,61 @@ #include "RequestObject.h" -namespace OIC -{ -namespace Service -{ - -RequestObject::RequestObject() -: pSetRequestCB(nullptr) -{ -} - -RequestObject::RequestObject(SetRequestCallback cb) -: pSetRequestCB(cb) -{ -} - -RequestObject::~RequestObject() -{ - pSetRequestCB = {}; -} +#include "RCSResourceObject.h" +#include "RCSSeparateResponse.h" -void RequestObject::invokeRequest(RemoteObjectPtr remoteObject, RequestMethod method, - const RCSResourceAttributes & resourceAttibutes) +namespace OIC { - switch (method) - { - case RequestMethod::Set: + namespace Service { - if(pSetRequestCB == nullptr) + void RequestObject::invokeRequest(RCSRemoteResourceObject::Ptr remoteObject, + const RCSRequest & request, RequestMethod method, + const RCSResourceAttributes & resourceAttibutes, SetRequestCallback setCB) { - remoteObject->setRemoteAttributes(resourceAttibutes, - std::bind(&RequestObject::setRequestCB, this, - std::placeholders::_1, resourceAttibutes)); + RequestObject::Ptr createdRequestObject = std::make_shared(); + + RCSRequest req(request.getResourceObject().lock(), request.getOCRequest()); + switch (method) + { + case RequestMethod::Set: + { + if (!setCB) + { + remoteObject->setRemoteAttributes(resourceAttibutes, + std::bind(&RequestObject::setRequestCB, createdRequestObject, + std::placeholders::_1, std::placeholders::_2, + req, createdRequestObject)); + } + else + { + remoteObject->setRemoteAttributes(resourceAttibutes, + std::bind(std::move(setCB), + std::placeholders::_1, std::placeholders::_2, + req, createdRequestObject)); + } + } + break; + case RequestMethod::Get: + case RequestMethod::Delete: + default: + // unknown type of method. + break; + } } - else + + void RequestObject::setRequestCB( + const RCSResourceAttributes & returnedAttributes, int /*eCode*/, + const RCSRequest & request, RequestObject::Ptr /*this_ptr*/) { - remoteObject->setRemoteAttributes(resourceAttibutes, - std::bind(pSetRequestCB, - std::placeholders::_1, resourceAttibutes)); - } - } - break; - case RequestMethod::Get: - case RequestMethod::Delete: - default: - // unknown type of method. - break; - } -} + auto server = request.getResourceObject().lock(); + if (!server) return; -void RequestObject::setRequestCB(const RCSResourceAttributes & returnedAttributes, - const RCSResourceAttributes & putAttibutes) -{ - if(putAttibutes != returnedAttributes) - { - // TODO fail set attributes - } -} + RCSResourceObject::LockGuard guard(server); + server->getAttributes() = RCSResourceAttributes(returnedAttributes); + + // TODO need to set error code. + RCSSeparateResponse(request).set(); + } -} /* namespace Service */ + } /* namespace Service */ } /* namespace OIC */ diff --git a/service/resource-hosting/src/RequestObject.h b/service/resource-hosting/src/RequestObject.h index 9f61510..94fa946 100644 --- a/service/resource-hosting/src/RequestObject.h +++ b/service/resource-hosting/src/RequestObject.h @@ -22,47 +22,45 @@ #define RH_REQUESTOBJECT_H_ #include "RCSRemoteResourceObject.h" -#include "RCSResourceObject.h" +#include "RCSRequest.h" namespace OIC { -namespace Service -{ - -class RequestObject -{ -public: - typedef std::shared_ptr Ptr; - typedef std::function< - void(const RCSResourceAttributes &, const RCSResourceAttributes &)> SetRequestCallback; - - enum class RequestMethod + namespace Service { - Get = 0, - Set, - Post, - Delete - }; + class RequestObject + { + public: + typedef std::shared_ptr Ptr; + typedef std::function SetRequestCallback; -private: - typedef RCSRemoteResourceObject::Ptr RemoteObjectPtr; + enum class RequestMethod + { + Get = 0, + Set, + Post, + Delete + }; - SetRequestCallback pSetRequestCB; + private: + typedef RCSRemoteResourceObject::Ptr RemoteObjectPtr; -public: - RequestObject(); - RequestObject(SetRequestCallback cb); - ~RequestObject(); + public: + RequestObject() = default; + ~RequestObject() = default; - void invokeRequest(RemoteObjectPtr remoteObject, RequestMethod method, - const RCSResourceAttributes & resourceAttibutes); + static void invokeRequest(RCSRemoteResourceObject::Ptr remoteObject, + const RCSRequest & request, RequestMethod method, + const RCSResourceAttributes & resourceAttibutes, + SetRequestCallback setCB = nullptr); -private: - void setRequestCB(const RCSResourceAttributes & returnedAttributes, - const RCSResourceAttributes & putAttibutes); -}; + private: + void setRequestCB(const RCSResourceAttributes & returnedAttributes, int eCode, + const RCSRequest & request, RequestObject::Ptr this_ptr); + }; -} /* namespace Service */ + } /* namespace Service */ } /* namespace OIC */ #endif /* RH_REQUESTOBJECT_H_ */ diff --git a/service/resource-hosting/src/ResourceHosting.cpp b/service/resource-hosting/src/ResourceHosting.cpp index f7e9913..2ead1e4 100755 --- a/service/resource-hosting/src/ResourceHosting.cpp +++ b/service/resource-hosting/src/ResourceHosting.cpp @@ -20,111 +20,125 @@ #include "ResourceHosting.h" -#include "PresenceSubscriber.h" -#include "OCPlatform.h" #include "RCSDiscoveryManager.h" #include "RCSAddress.h" namespace OIC { -namespace Service -{ - -namespace -{ - const std::string HOSTING_TAG = "/hosting"; - const auto HOSTING_TAG_SIZE = HOSTING_TAG.size(); - const std::string MULTICAST_PRESENCE_ADDRESS = std::string("coap://") + OC_MULTICAST_PREFIX; - const std::string HOSTING_RESOURSE_TYPE = "oic.r.resourcehosting"; -} - -ResourceHosting::ResourceHosting() -: m_mutexForList(), - m_isStartedHosting(false), - m_hostingObjects(), - m_discoveryTask() -{ -} - -ResourceHosting * ResourceHosting::getInstance() -{ - static ResourceHosting instance; - return & instance; -} - -void ResourceHosting::startHosting() -{ - if(m_isStartedHosting) return; - m_isStartedHosting = true; - createDiscoveryListener(); -} - -void ResourceHosting::stopHosting() -{ - if(!m_isStartedHosting) return; - - if(!m_discoveryTask->isCanceled()) - { - m_discoveryTask->cancel(); - } - - m_isStartedHosting = false; - - RHLock lock(m_mutexForList); - m_hostingObjects.clear(); -} - -void ResourceHosting::createDiscoveryListener() -{ - m_discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType( - RCSAddress::multicast(), OC_RSRVD_WELL_KNOWN_URI, HOSTING_RESOURSE_TYPE, - std::bind(&ResourceHosting::discoveryHandler, this, - std::placeholders::_1)); -} - -void ResourceHosting::discoveryHandler(RemoteObjectPtr remoteResource) -{ - auto discoverdUri = remoteResource->getUri(); - if(discoverdUri.compare( - discoverdUri.size()-HOSTING_TAG_SIZE, HOSTING_TAG_SIZE, HOSTING_TAG) != 0) - { - return; - } - - auto foundHostingObject = findRemoteResource(remoteResource); - if(foundHostingObject != nullptr) return; - - try + namespace Service { - HostingObjectKey key = generateHostingObjectKey(remoteResource); - foundHostingObject = HostingObject::createHostingObject(remoteResource, - std::bind(&ResourceHosting::destroyedHostingObject, this, key)); - - RHLock lock(m_mutexForList); - m_hostingObjects.insert(std::make_pair(key, foundHostingObject)); - - }catch(const RCSException &e) - { - OIC_HOSTING_LOG(DEBUG, - "[ResourceHosting::discoverHandler]InvalidParameterException:%s", e.what()); - } -} - -HostingObject::Ptr ResourceHosting::findRemoteResource(RemoteObjectPtr remoteResource) -{ - RHLock lock(m_mutexForList); - - auto iter = m_hostingObjects.find(generateHostingObjectKey(remoteResource)); - if(iter != m_hostingObjects.end()) return iter->second; - - return nullptr; -} - -void ResourceHosting::destroyedHostingObject(const HostingObjectKey & key) -{ - RHLock lock(m_mutexForList); - m_hostingObjects.erase(key); -} -} /* namespace Service */ + namespace + { + const std::string HOSTING_TAG = "/hosting"; + const auto HOSTING_TAG_SIZE = HOSTING_TAG.size(); + const std::string HOSTING_RESOURSE_TYPE = "oic.r.resourcehosting"; + } + + ResourceHosting::ResourceHosting() + : m_mutexForList(), + m_isStartedHosting(false), + m_hostingObjects(), + m_discoveryTask() + { + } + + ResourceHosting * ResourceHosting::getInstance() + { + static ResourceHosting instance; + return & instance; + } + + void ResourceHosting::startHosting() + { + if (m_isStartedHosting) return; + m_isStartedHosting = true; + createDiscoveryListener(); + } + + void ResourceHosting::stopHosting() + { + if (!m_isStartedHosting) return; + + if (!m_discoveryTask->isCanceled()) + { + m_discoveryTask->cancel(); + } + + m_isStartedHosting = false; + + RHLock lock(m_mutexForList); + m_hostingObjects.clear(); + } + + void ResourceHosting::createDiscoveryListener() + { + m_discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType( + RCSAddress::multicast(), OC_RSRVD_WELL_KNOWN_URI, HOSTING_RESOURSE_TYPE, + std::bind(&ResourceHosting::discoveryHandler, this, + std::placeholders::_1)); + } + + void ResourceHosting::discoveryHandler(RemoteObjectPtr remoteResource) + { + auto discoverdUri = remoteResource->getUri(); + if (discoverdUri.compare( + discoverdUri.size()-HOSTING_TAG_SIZE, HOSTING_TAG_SIZE, HOSTING_TAG) != 0) + { + return; + } + + auto foundHostingObject = findRemoteResource(remoteResource); + if (foundHostingObject != nullptr) return; + + try + { + HostingObjectKey key = generateHostingObjectKey(remoteResource); + foundHostingObject = HostingObject::createHostingObject(remoteResource, + std::bind(&ResourceHosting::destroyedHostingObject, this, key)); + + RHLock lock(m_mutexForList); + m_hostingObjects.insert(std::make_pair(key, foundHostingObject)); + + } catch (const RCSException & e) + { + OIC_HOSTING_LOG(DEBUG, + "[ResourceHosting::discoverHandler]InvalidParameterException:%s", e.what()); + } + } + + HostingObject::Ptr ResourceHosting::findRemoteResource(RemoteObjectPtr remoteResource) + { + RHLock lock(m_mutexForList); + + auto iter = m_hostingObjects.find(generateHostingObjectKey(remoteResource)); + if (iter != m_hostingObjects.end()) return iter->second; + + return nullptr; + } + + void ResourceHosting::destroyedHostingObject(const HostingObjectKey & key) + { + RHLock lock(m_mutexForList); + m_hostingObjects.erase(key); + } + + ResourceHosting::HostingObjectKey ResourceHosting::generateHostingObjectKey( + std::string && address, std::string && uri) + { + return HostingObjectKey(address + uri); + } + + ResourceHosting::HostingObjectKey ResourceHosting::generateHostingObjectKey( + const std::string & address, const std::string & uri) + { + return generateHostingObjectKey(std::string(address), std::string(uri)); + } + ResourceHosting::HostingObjectKey ResourceHosting::generateHostingObjectKey( + RemoteObjectPtr rResource) + { + return generateHostingObjectKey(rResource->getAddress(), rResource->getUri()); + } + + } /* namespace Service */ } /* namespace OIC */ diff --git a/service/resource-hosting/src/ResourceHosting.h b/service/resource-hosting/src/ResourceHosting.h index 239b28a..39866d4 100755 --- a/service/resource-hosting/src/ResourceHosting.h +++ b/service/resource-hosting/src/ResourceHosting.h @@ -33,58 +33,55 @@ namespace OIC { -namespace Service -{ + namespace Service + { -class ResourceHosting -{ -private: - typedef RCSRemoteResourceObject::Ptr RemoteObjectPtr; - typedef std::lock_guard RHLock; - typedef std::string HostingObjectKey; + class ResourceHosting + { + private: + typedef RCSRemoteResourceObject::Ptr RemoteObjectPtr; + typedef std::lock_guard RHLock; + typedef std::string HostingObjectKey; - typedef std::function DiscoveryCallback; - typedef HostingObject::DestroyedCallback DestroyedCallback; + typedef std::function DiscoveryCallback; + typedef HostingObject::DestroyedCallback DestroyedCallback; -public: - void startHosting(); - void stopHosting(); + public: + void startHosting(); + void stopHosting(); - static ResourceHosting * getInstance(); + static ResourceHosting * getInstance(); -private: - ResourceHosting(); - ~ResourceHosting() = default; + private: + ResourceHosting(); + ~ResourceHosting() = default; - ResourceHosting(ResourceHosting&&) = delete; - ResourceHosting(const ResourceHosting&) = delete; - ResourceHosting& operator=(ResourceHosting&&) = delete; - ResourceHosting& operator=(const ResourceHosting&) = delete; + ResourceHosting(const ResourceHosting&) = delete; + ResourceHosting & operator = (const ResourceHosting &) = delete; - std::mutex m_mutexForList; - std::atomic_bool m_isStartedHosting; + ResourceHosting(ResourceHosting &&) = delete; + ResourceHosting & operator = (ResourceHosting &&) = delete; - std::unordered_map m_hostingObjects; - RCSDiscoveryManager::DiscoveryTask::Ptr m_discoveryTask; + std::mutex m_mutexForList; + std::atomic_bool m_isStartedHosting; - void createDiscoveryListener(); - void discoveryHandler(RemoteObjectPtr remoteResource); + std::unordered_map m_hostingObjects; + RCSDiscoveryManager::DiscoveryTask::Ptr m_discoveryTask; - inline HostingObjectKey generateHostingObjectKey(std::string address, std::string uri) - { - return HostingObjectKey(address + uri); - } - inline HostingObjectKey generateHostingObjectKey(RemoteObjectPtr rResource) - { - return HostingObjectKey(rResource->getAddress() + rResource->getUri()); - } + void createDiscoveryListener(); + void discoveryHandler(RemoteObjectPtr remoteResource); + + HostingObjectKey generateHostingObjectKey(RemoteObjectPtr rResource); + HostingObjectKey generateHostingObjectKey(std::string && address, std::string && uri); + HostingObjectKey generateHostingObjectKey( + const std::string & address, const std::string & uri); - HostingObject::Ptr findRemoteResource(RemoteObjectPtr remoteResource); + HostingObject::Ptr findRemoteResource(RemoteObjectPtr remoteResource); - void destroyedHostingObject(const HostingObjectKey & key); -}; + void destroyedHostingObject(const HostingObjectKey & key); + }; -} /* namespace Service */ + } /* namespace Service */ } /* namespace OIC */ #endif /* RH_RESOURCEHOSTING_H_ */ diff --git a/service/resource-hosting/unittest/RequestObjectUnitTest.cpp b/service/resource-hosting/unittest/RequestObjectUnitTest.cpp index c56b58c..48d1025 100644 --- a/service/resource-hosting/unittest/RequestObjectUnitTest.cpp +++ b/service/resource-hosting/unittest/RequestObjectUnitTest.cpp @@ -21,14 +21,18 @@ #include "UnitTestHelper.h" #include "ResourceEncapsulationTestSimulator.h" +#include "OCResourceRequest.h" #include "RequestObject.h" +#include "RCSRequest.h" using namespace testing; using namespace OIC::Service; namespace { - void setRequestCB(const RCSResourceAttributes &, const RCSResourceAttributes & ) { } + void setRequestCB( + const RCSResourceAttributes &, int, + const RCSRequest &, RequestObject::Ptr) { } } class RequestObjectTest : public TestWithMock @@ -87,17 +91,19 @@ public: TEST_F(RequestObjectTest, invokeRequestExpectCallwithSetter) { bool isCalled = false; - RequestObject::Ptr instance = std::make_shared(setRequestCB); mocks.ExpectCallFunc(setRequestCB).Do( - [this, &isCalled](const RCSResourceAttributes &, const RCSResourceAttributes &) + [this, &isCalled](const RCSResourceAttributes &, int, + const RCSRequest &, RequestObject::Ptr) { isCalled = true; notifyCondition(); }); RCSResourceAttributes att; - instance->invokeRequest(remoteObject, RequestObject::RequestMethod::Set, att); + std::shared_ptr request; + RequestObject::invokeRequest(remoteObject, RCSRequest(testObject->getResourceServer(), request), + RequestObject::RequestMethod::Set, att, setRequestCB); waitForCondition(); diff --git a/service/resource-hosting/unittest/ResourceEncapsulationTestSimulator.h b/service/resource-hosting/unittest/ResourceEncapsulationTestSimulator.h index 6f4bd63..7bf761b 100644 --- a/service/resource-hosting/unittest/ResourceEncapsulationTestSimulator.h +++ b/service/resource-hosting/unittest/ResourceEncapsulationTestSimulator.h @@ -25,6 +25,7 @@ #include "UnitTestHelper.h" +#include "RCSResourceObject.h" #include "RCSDiscoveryManager.h" #include "RCSRemoteResourceObject.h" #include "RCSResourceAttributes.h" -- 2.7.4