From b1d473ae08c58dedfd5060a64b3b6f3242fa737d Mon Sep 17 00:00:00 2001 From: "jyong2.kim" Date: Sun, 6 Sep 2015 17:08:49 +0900 Subject: [PATCH] Include RCS client module in Resource Container. Soft-sensor using resource-container needs client functionality. but it is not supported on resource-container. so, resource container includes the RCS-client-module, and enable to use client-function at soft-sensor. This commit is initial version for resource container including client module. Update discover logic, and prevent multiple access to callback function. Signed-off-by: jyong2.kim Change-Id: I3ba81ed185b82ee05a95de493a6ff560838e1054 Reviewed-on: https://gerrit.iotivity.org/gerrit/2388 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- .../src/resourceContainer/SConscript | 5 +- .../bundle-api/include/SoftSensorResource.h | 3 + .../include/DiscomfortIndexSensorResource.h | 3 + .../src/DiscomfortIndexSensorResource.cpp | 8 +- .../src/resourceContainer/include/Configuration.h | 4 +- .../include/DiscoverResourceUnit.h | 89 +++++++++++ .../resourceContainer/include/RemoteResourceUnit.h | 72 +++++++++ .../include/ResourceContainerImpl.h | 7 +- .../src/resourceContainer/src/BundleActivator.cpp | 3 +- .../src/resourceContainer/src/Configuration.cpp | 30 +++- .../resourceContainer/src/DiscoverResourceUnit.cpp | 173 +++++++++++++++++++++ .../resourceContainer/src/RemoteResourceUnit.cpp | 87 +++++++++++ .../src/ResourceContainerImpl.cpp | 96 ++++++++++++ .../unittests/ResourceContainerTest.cpp | 3 + .../src/resourceContainer/unittests/SConscript | 2 +- 15 files changed, 573 insertions(+), 12 deletions(-) create mode 100644 service/resource-encapsulation/src/resourceContainer/include/DiscoverResourceUnit.h create mode 100644 service/resource-encapsulation/src/resourceContainer/include/RemoteResourceUnit.h create mode 100644 service/resource-encapsulation/src/resourceContainer/src/DiscoverResourceUnit.cpp create mode 100644 service/resource-encapsulation/src/resourceContainer/src/RemoteResourceUnit.cpp diff --git a/service/resource-encapsulation/src/resourceContainer/SConscript b/service/resource-encapsulation/src/resourceContainer/SConscript index aa6af09..8703b55 100644 --- a/service/resource-encapsulation/src/resourceContainer/SConscript +++ b/service/resource-encapsulation/src/resourceContainer/SConscript @@ -101,7 +101,7 @@ except KeyError: print '' -resource_container_env.PrependUnique(LIBS = ['rcs_server', 'rcs_common', 'oc','octbstack', 'oc_logger', 'oc_logger_core', 'connectivity_abstraction']) +resource_container_env.PrependUnique(LIBS = ['rcs_client', 'rcs_server', 'rcs_common', 'oc','octbstack', 'oc_logger', 'oc_logger_core', 'connectivity_abstraction']) resource_container_env.AppendUnique(LIBS = ['dl']) if int(containerJavaSupport): @@ -117,7 +117,8 @@ if int(containerJavaSupport): res_container_src = ['src/BaseActivator.cpp','src/BundleActivator.cpp','src/RCSBundleInfo.cpp', 'src/BundleInfoInternal.cpp', 'src/BundleResource.cpp', 'src/Configuration.cpp', 'src/JavaBundleResource.cpp', 'src/ProtocolBridgeResource.cpp', 'src/ProtocolBridgeConnector.cpp', 'src/RCSResourceContainer.cpp', 'src/ResourceContainerBundleAPI.cpp', 'src/ResourceContainerImpl.cpp', - 'src/SoftSensorResource.cpp'] + 'src/SoftSensorResource.cpp', 'src/DiscoverResourceUnit.cpp', 'src/RemoteResourceUnit.cpp', + ] res_container_static = resource_container_env.StaticLibrary('rcs_container', res_container_src) res_container_shared = resource_container_env.SharedLibrary('rcs_container', res_container_src) diff --git a/service/resource-encapsulation/src/resourceContainer/bundle-api/include/SoftSensorResource.h b/service/resource-encapsulation/src/resourceContainer/bundle-api/include/SoftSensorResource.h index bd9d1f0..86eb419 100644 --- a/service/resource-encapsulation/src/resourceContainer/bundle-api/include/SoftSensorResource.h +++ b/service/resource-encapsulation/src/resourceContainer/bundle-api/include/SoftSensorResource.h @@ -89,6 +89,9 @@ namespace OIC */ virtual void executeLogic() = 0; + virtual void onUpdatedInputResource(const std::string attributeName, + std::vector values) = 0; + public: std::list m_inputList; diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensorResource.h b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensorResource.h index 585cbda..e36321b 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensorResource.h +++ b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensorResource.h @@ -40,6 +40,9 @@ class DiscomfortIndexSensorResource : public SoftSensorResource virtual void executeLogic(); + virtual void onUpdatedInputResource(const std::string attributeName, + std::vector values); + private: DiscomfortIndexSensor *m_pDiscomfortIndexSensor; }; diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensorResource.cpp b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensorResource.cpp index de51815..3202a15 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensorResource.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensorResource.cpp @@ -68,4 +68,10 @@ void DiscomfortIndexSensorResource::executeLogic() setAttribute("discomfortIndex", RCSResourceAttributes::Value(strDiscomfortIndex.c_str())); } -} \ No newline at end of file +} + +void DiscomfortIndexSensorResource::onUpdatedInputResource(const std::string attributeName, + std::vector values) +{ + +} diff --git a/service/resource-encapsulation/src/resourceContainer/include/Configuration.h b/service/resource-encapsulation/src/resourceContainer/include/Configuration.h index 827823c..1a39353 100644 --- a/service/resource-encapsulation/src/resourceContainer/include/Configuration.h +++ b/service/resource-encapsulation/src/resourceContainer/include/Configuration.h @@ -56,7 +56,8 @@ namespace OIC Configuration(string configFile); ~Configuration(); - bool isLoaded(); + bool isLoaded() const; + bool isHasInput(std::string & bundleId) const; void getConfiguredBundles(configInfo *configOutput); void getBundleConfiguration(string bundleId, configInfo *configOutput); void getResourceConfiguration(string bundleId, vector< resourceInfo > *configOutput); @@ -68,6 +69,7 @@ namespace OIC string m_pathConfigFile; string m_strConfigData; rapidxml::xml_document< char > m_xmlDoc; + std::map m_mapisHasInput; // bundleId, isHasInput }; } } diff --git a/service/resource-encapsulation/src/resourceContainer/include/DiscoverResourceUnit.h b/service/resource-encapsulation/src/resourceContainer/include/DiscoverResourceUnit.h new file mode 100644 index 0000000..3c75630 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/include/DiscoverResourceUnit.h @@ -0,0 +1,89 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef DISCOVERRESOURCEUNIT_H_ +#define DISCOVERRESOURCEUNIT_H_ + +#include +#include +#include +#include +#include +#include + +#include "RCSDiscoveryManager.h" +#include "RCSRemoteResourceObject.h" +#include "RCSResourceAttributes.h" +#include "RemoteResourceUnit.h" + +namespace OIC +{ + namespace Service + { + class DiscoverResourceUnit + { + public: + struct DiscoverResourceInfo + { + DiscoverResourceInfo() + : resourceUri(), resourceType(), attributeName() {} + DiscoverResourceInfo(std::string uri, std::string type, std::string name) + : resourceUri(uri), resourceType(type), attributeName(name) {} + std::string resourceUri; + std::string resourceType; + std::string attributeName; + }; + + typedef std::shared_ptr Ptr; + typedef std::function UpdatedCBFromServer; + typedef std::function values)> + UpdatedCB; + typedef RemoteResourceUnit::UPDATE_MSG REMOTE_MSG; + + DiscoverResourceUnit(const std::string & bundleId); + ~DiscoverResourceUnit(); + + void startDiscover(DiscoverResourceInfo info, UpdatedCB updatedCB); + + private: + std::string m_bundleId; + std::string m_Uri; + std::string m_ResourceType; + std::string m_AttrubuteName; + std::atomic_bool isStartedDiscovery; + + std::vector m_vecRemoteResource; + RCSDiscoveryManager::ResourceDiscoveredCallback pDiscoveredCB; + UpdatedCBFromServer pUpdatedCBFromServer; + UpdatedCB pUpdatedCB; + + bool isAlreadyDiscoveredResource(RCSRemoteResourceObject::Ptr discoveredResource); + void discoverdCB(RCSRemoteResourceObject::Ptr remoteObject); + void onUpdate(REMOTE_MSG msg, RCSRemoteResourceObject::Ptr updatedResource); + + std::vector + buildInputResourceData(RCSRemoteResourceObject::Ptr updatedResource); + }; + } +} + +#endif // DISCOVERRESOURCEUNIT_H_ diff --git a/service/resource-encapsulation/src/resourceContainer/include/RemoteResourceUnit.h b/service/resource-encapsulation/src/resourceContainer/include/RemoteResourceUnit.h new file mode 100644 index 0000000..7025e3b --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/include/RemoteResourceUnit.h @@ -0,0 +1,72 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef REMOTERESOURCEINFO_H_ +#define REMOTERESOURCEINFO_H_ + +#include + +#include "RCSRemoteResourceObject.h" + +namespace OIC +{ + namespace Service + { + class RemoteResourceUnit + { + public: + enum class UPDATE_MSG + { + DATA_UPDATED, + RESOURCE_DELETED + }; + + typedef std::shared_ptr Ptr; + typedef std::function UpdatedCBFromServer; + + RemoteResourceUnit(); + ~RemoteResourceUnit(); + + private: + mutable std::mutex m_mutex; + RCSRemoteResourceObject::Ptr remoteObject; + + UpdatedCBFromServer pUpdatedCB; + RCSRemoteResourceObject::StateChangedCallback pStateChangedCB; + RCSRemoteResourceObject::CacheUpdatedCallback pCacheUpdateCB; + + void stateChangedCB(ResourceState changedState) const; + void cacheUpdateCB(const RCSResourceAttributes & updatedAtt) const; + + public: + static RemoteResourceUnit::Ptr createRemoteResourceInfo( + RCSRemoteResourceObject::Ptr ptr, + UpdatedCBFromServer updatedCB); + + RCSRemoteResourceObject::Ptr getRemoteResourceObject() const; + std::string getRemoteResourceUri() const; + + void startCaching() const; + void startMonitoring() const; + }; + } +} + +#endif // REMOTERESOURCEINFO_H_ diff --git a/service/resource-encapsulation/src/resourceContainer/include/ResourceContainerImpl.h b/service/resource-encapsulation/src/resourceContainer/include/ResourceContainerImpl.h index c9bb210..78314f5 100644 --- a/service/resource-encapsulation/src/resourceContainer/include/ResourceContainerImpl.h +++ b/service/resource-encapsulation/src/resourceContainer/include/ResourceContainerImpl.h @@ -31,6 +31,8 @@ #include "RCSResponse.h" #include "RCSResourceObject.h" +#include "DiscoverResourceUnit.h" + #if(JAVA_SUPPORT) #include #endif @@ -105,15 +107,18 @@ namespace OIC map< std::string, RCSResourceObject::Ptr > m_mapServers; // map< std::string, BundleResource * > m_mapResources; // map< std::string, list > m_mapBundleResources; //> + map< std::string, list> + m_mapDiscoverResourceUnits; // string m_configFile; Configuration *m_config; - void activateSoBundle(const std::string &bundleId); void deactivateSoBundle(const std::string &bundleId); void addSoBundleResource(const std::string &bundleId, resourceInfo newResourceInfo); void removeSoBundleResource(const std::string &bundleId, const std::string &resourceUri); void registerSoBundle(RCSBundleInfo *bundleInfo); + void discoverInputResource(const std::string & outputResourceUri); + void undiscoverInputResource(const std::string & outputResourceUri); #if(JAVA_SUPPORT) map m_bundleVM; diff --git a/service/resource-encapsulation/src/resourceContainer/src/BundleActivator.cpp b/service/resource-encapsulation/src/resourceContainer/src/BundleActivator.cpp index 9d0134a..f2f17ea 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/BundleActivator.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/BundleActivator.cpp @@ -40,7 +40,8 @@ namespace OIC void BundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer, std::string bundleId) { - + (void)(resourceContainer); + (void)(bundleId); } void BundleActivator::deactivateBundle() diff --git a/service/resource-encapsulation/src/resourceContainer/src/Configuration.cpp b/service/resource-encapsulation/src/resourceContainer/src/Configuration.cpp index 22b22e6..ac1693c 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/Configuration.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/Configuration.cpp @@ -18,8 +18,16 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +#include +#include + #include "Configuration.h" +namespace +{ + const std::string INPUT_RESOURCE = std::string("input"); +} + namespace OIC { namespace Service @@ -59,11 +67,22 @@ namespace OIC { } - bool Configuration::isLoaded() + bool Configuration::isLoaded() const { return m_loaded; } + bool Configuration::isHasInput(std::string & bundleId) const + { + try + { + return m_mapisHasInput.at(bundleId); + }catch (std::out_of_range &e) + { + return false; + } + } + void Configuration::getConfiguredBundles(configInfo *configOutput) { rapidxml::xml_node< char > *bundle; @@ -75,13 +94,10 @@ namespace OIC { try { - //cout << "Name of first node is: " << m_xmlDoc.first_node()->name() << endl; - for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling()) { std::map< std::string, std::string > bundleMap; - //cout << "Bundle: " << bundle->name() << endl; for (subItem = bundle->first_node(); subItem; subItem = subItem->next_sibling()) { @@ -92,7 +108,6 @@ namespace OIC { bundleMap.insert( std::make_pair(trim_both(strKey), trim_both(strValue))); - //cout << strKey << " " << strValue << endl; } } configOutput->push_back(bundleMap); @@ -209,6 +224,11 @@ namespace OIC strKey = subItem->name(); + if(strKey.compare(INPUT_RESOURCE)) + { + m_mapisHasInput[strBundleId] = true; + } + for (subItem2 = subItem->first_node(); subItem2; subItem2 = subItem2->next_sibling()) { diff --git a/service/resource-encapsulation/src/resourceContainer/src/DiscoverResourceUnit.cpp b/service/resource-encapsulation/src/resourceContainer/src/DiscoverResourceUnit.cpp new file mode 100644 index 0000000..f32add1 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/src/DiscoverResourceUnit.cpp @@ -0,0 +1,173 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "RCSRemoteResourceObject.h" +#include "DiscoverResourceUnit.h" +#include "RCSAddress.h" + +using namespace OIC::Service; + +DiscoverResourceUnit::DiscoverResourceUnit(const std::string & bundleId) +: m_bundleId(bundleId) +{ + pUpdatedCB = nullptr; + isStartedDiscovery = false; + pDiscoveredCB = std::bind(&DiscoverResourceUnit::discoverdCB, this, + std::placeholders::_1); + pUpdatedCBFromServer = std::bind(&DiscoverResourceUnit::onUpdate, this, + std::placeholders::_1, std::placeholders::_2); +} + +DiscoverResourceUnit::~DiscoverResourceUnit() +{ + m_vecRemoteResource.clear(); +} + +void DiscoverResourceUnit::startDiscover(DiscoverResourceInfo info, UpdatedCB updatedCB) +{ + if (isStartedDiscovery) + { + // Already start Discovery + return; + } + + m_Uri = info.resourceUri; + m_ResourceType = info.resourceType; + m_AttrubuteName = info.attributeName; + pUpdatedCB = updatedCB; + + try + { + // TODO may be will changed active discovery + if (m_Uri.empty()) + { + RCSDiscoveryManager::getInstance()->discoverResourceByType( + RCSAddress::multicast(), m_ResourceType, pDiscoveredCB); + } + else + { + RCSDiscoveryManager::getInstance()->discoverResourceByType( + RCSAddress::multicast(), m_Uri, m_ResourceType, pDiscoveredCB); + } + } + catch (InvalidParameterException &e) + { + // TODO Handle Exception + return; + } + + isStartedDiscovery = true; +} + +void DiscoverResourceUnit::discoverdCB(RCSRemoteResourceObject::Ptr remoteObject) +{ + if (!isAlreadyDiscoveredResource(remoteObject)) + { + RemoteResourceUnit::Ptr newDiscoveredResource = + RemoteResourceUnit::createRemoteResourceInfo(remoteObject, pUpdatedCBFromServer); + + m_vecRemoteResource.push_back(newDiscoveredResource); + + newDiscoveredResource->startMonitoring(); + newDiscoveredResource->startCaching(); + } + else + { + // Already Discovered Resource + } + +} + +void DiscoverResourceUnit::onUpdate(REMOTE_MSG msg, RCSRemoteResourceObject::Ptr updatedResource) +{ + if(msg == REMOTE_MSG::DATA_UPDATED) + { + if (updatedResource == nullptr) + { + return; + } + try + { + updatedResource->getCachedAttribute(m_AttrubuteName); + } + catch (InvalidKeyException &e) + { + // TODO Handle Exception + return; + } + catch (std::exception &e) + { + return; + } + + std::vector retVector + = buildInputResourceData(updatedResource); + if (!retVector.empty() && pUpdatedCB != nullptr) + { + pUpdatedCB(m_AttrubuteName, retVector); + } + } + else + { + // TODO find & delete + } +} + +std::vector DiscoverResourceUnit::buildInputResourceData( + RCSRemoteResourceObject::Ptr updatedResource) +{ + (void)updatedResource; + std::vector retVector = {}; + for(auto iter : m_vecRemoteResource) + { + if(iter->getRemoteResourceObject()->getCacheState() != CacheState::READY) + { + continue; + } + + try + { + RCSResourceAttributes::Value value = + iter->getRemoteResourceObject()->getCachedAttribute(m_AttrubuteName); + retVector.push_back(value); + + } + catch (InvalidKeyException &e) + { + // TODO Handle Exception + } + } + + return retVector; +} + +bool DiscoverResourceUnit::isAlreadyDiscoveredResource( + RCSRemoteResourceObject::Ptr discoveredResource) +{ + for (auto iter : m_vecRemoteResource) + { + if (discoveredResource->getUri().compare(iter->getRemoteResourceUri()) == 0 && + discoveredResource->getAddress().compare(iter->getRemoteResourceObject()->getAddress()) == 0 ) + { + return true; + } + } + return false; +} diff --git a/service/resource-encapsulation/src/resourceContainer/src/RemoteResourceUnit.cpp b/service/resource-encapsulation/src/resourceContainer/src/RemoteResourceUnit.cpp new file mode 100644 index 0000000..d6ab867 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/src/RemoteResourceUnit.cpp @@ -0,0 +1,87 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "RemoteResourceUnit.h" + +using namespace OIC::Service; + +RemoteResourceUnit::RemoteResourceUnit() +{ + pStateChangedCB = std::bind(&RemoteResourceUnit::stateChangedCB, this, + std::placeholders::_1); + pCacheUpdateCB = std::bind(&RemoteResourceUnit::cacheUpdateCB, this, + std::placeholders::_1); +} + +RemoteResourceUnit::~RemoteResourceUnit() +{ + if (remoteObject) + { + remoteObject->stopCaching(); + remoteObject->stopMonitoring(); + } +} + +RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfo( + RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB) +{ + RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared(); + retRemoteResourceUnit->remoteObject = ptr; + retRemoteResourceUnit->pUpdatedCB = updatedCB; + + return retRemoteResourceUnit; +} + +RCSRemoteResourceObject::Ptr RemoteResourceUnit::getRemoteResourceObject() const +{ + return remoteObject; +} + +std::string RemoteResourceUnit::getRemoteResourceUri() const +{ + return remoteObject->getUri(); +} + +void RemoteResourceUnit::startCaching() const +{ + remoteObject->startCaching(pCacheUpdateCB); +} + +void RemoteResourceUnit::startMonitoring() const +{ + remoteObject->startMonitoring(pStateChangedCB); +} + +void RemoteResourceUnit::stateChangedCB(ResourceState changedState) const +{ + std::lock_guard lock(m_mutex); + if(changedState == ResourceState::LOST_SIGNAL || + changedState == ResourceState::DESTROYED) + { + pUpdatedCB(UPDATE_MSG::RESOURCE_DELETED, remoteObject); + } +} + +void RemoteResourceUnit::cacheUpdateCB(const RCSResourceAttributes & updatedAtt) const +{ + std::lock_guard lock(m_mutex); + (void)updatedAtt; + pUpdatedCB(UPDATE_MSG::DATA_UPDATED, remoteObject); +} diff --git a/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp b/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp index f4dd364..88fc1ea 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp @@ -33,10 +33,21 @@ #include "BundleInfoInternal.h" #include "logger.h" #include "oc_logger.hpp" +#include "SoftSensorResource.h" using OC::oc_log_stream; using namespace OIC::Service; +namespace +{ + const std::string INPUT_RESOURCE = std::string("input"); + const std::string INPUT_RESOURCE_URI = std::string("resourceUri"); + const std::string INPUT_RESOURCE_TYPE = std::string("resourceType"); + const std::string INPUT_RESOURCE_ATTRIBUTENAME = std::string("name"); + + const std::string OUTPUT_RESOURCE_URI = std::string("resourceUri"); +} + auto error_logger = []() -> boost::iostreams::stream & { static OC::oc_log_stream ols(oc_make_ostream_logger); @@ -295,6 +306,11 @@ namespace OIC info_logger() << "Registration finished " << strUri << "," << strResourceType << endl; + + if (m_config->isHasInput(resource->m_bundleId)) + { + discoverInputResource(strUri); + } } } else @@ -311,6 +327,11 @@ namespace OIC info_logger() << "Unregistration of resource " << resource->m_uri << "," << resource->m_resourceType << endl; + if (m_config->isHasInput(resource->m_bundleId)) + { + undiscoverInputResource(strUri); + } + if (m_mapServers.find(strUri) != m_mapServers.end()) { m_mapServers[strUri].reset(); @@ -341,6 +362,7 @@ namespace OIC RCSGetResponse ResourceContainerImpl::getRequestHandler(const RCSRequest &request, const RCSResourceAttributes &attributes) { + (void)attributes; RCSResourceAttributes attr; if (m_mapServers.find(request.getResourceUri()) != m_mapServers.end() @@ -435,6 +457,7 @@ namespace OIC const std::string &bundlePath, std::map< string, string > params) { + (void)bundleUri; if (m_bundles.find(bundleId) != m_bundles.end()) error_logger() << "BundleId already exist" << endl; @@ -615,6 +638,79 @@ namespace OIC BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId]; bundleInfoInternal->setActivated(true); + + } + + void ResourceContainerImpl::undiscoverInputResource(const std::string &outputResourceUri) + { + auto foundDiscoverResource + = m_mapDiscoverResourceUnits.find(outputResourceUri); + if (foundDiscoverResource != m_mapDiscoverResourceUnits.end()) + { + m_mapDiscoverResourceUnits.erase(foundDiscoverResource); + } + } + + void ResourceContainerImpl::discoverInputResource(const std::string &outputResourceUri) + { + auto foundOutputResource = m_mapResources.find(outputResourceUri); + auto resourceProperty = foundOutputResource->second->m_mapResourceProperty; + + try + { + resourceProperty.at(INPUT_RESOURCE); + } + catch (std::out_of_range &e) + { + return; + } + + for (auto iter : resourceProperty) + { + if (iter.first.compare(INPUT_RESOURCE) == 0) + { + for (auto it : iter.second) + { + auto makeValue = [&](const std::string & reference) mutable -> std::string + { + std::string retStr = ""; + try + { + retStr = it.at(reference); + } + catch (std::out_of_range &e) + { + return ""; + } + return retStr; + }; + std::string uri = makeValue(INPUT_RESOURCE_URI); + std::string type = makeValue(INPUT_RESOURCE_TYPE); + std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME); + + DiscoverResourceUnit::Ptr newDiscoverUnit + = std::make_shared(outputResourceUri); + newDiscoverUnit->startDiscover( + DiscoverResourceUnit::DiscoverResourceInfo(uri, type, attributeName), + std::bind(&SoftSensorResource::onUpdatedInputResource, + (SoftSensorResource*)foundOutputResource->second, + std::placeholders::_1, std::placeholders::_2)); + + auto foundDiscoverResource + = m_mapDiscoverResourceUnits.find(outputResourceUri); + if (foundDiscoverResource != m_mapDiscoverResourceUnits.end()) + { + foundDiscoverResource->second.push_back(newDiscoverUnit); + } + else + { + m_mapDiscoverResourceUnits.insert( + std::make_pair(outputResourceUri, + std::list{newDiscoverUnit})); + } + } + } + } } void ResourceContainerImpl::deactivateSoBundle(const std::string &id) diff --git a/service/resource-encapsulation/src/resourceContainer/unittests/ResourceContainerTest.cpp b/service/resource-encapsulation/src/resourceContainer/unittests/ResourceContainerTest.cpp index 5823093..cbe1bcf 100644 --- a/service/resource-encapsulation/src/resourceContainer/unittests/ResourceContainerTest.cpp +++ b/service/resource-encapsulation/src/resourceContainer/unittests/ResourceContainerTest.cpp @@ -74,11 +74,14 @@ class TestBundleResource: public BundleResource public: string getAttribute(string attributeName) { + (void)attributeName; return "test"; } ; void setAttribute(string attributeName, string value) { + (void)attributeName; + (void)value; } ; void initAttributes() diff --git a/service/resource-encapsulation/src/resourceContainer/unittests/SConscript b/service/resource-encapsulation/src/resourceContainer/unittests/SConscript index 74b626f..3f516e4 100644 --- a/service/resource-encapsulation/src/resourceContainer/unittests/SConscript +++ b/service/resource-encapsulation/src/resourceContainer/unittests/SConscript @@ -127,7 +127,7 @@ if int(containerJavaSupport): container_gtest_env.AppendUnique(CCFLAGS = ['-Wnoexcept']) -container_gtest_env.PrependUnique(LIBS = ['rcs_container', 'rcs_server', 'rcs_common', 'oc','octbstack', 'oc_logger', 'oc_logger_core', 'connectivity_abstraction', gtest, gtest_main]) +container_gtest_env.PrependUnique(LIBS = ['rcs_container', 'rcs_client', 'rcs_server', 'rcs_common', 'oc','octbstack', 'oc_logger', 'oc_logger_core', 'connectivity_abstraction', gtest, gtest_main]) container_gtest_env.AppendUnique(LIBS = ['dl']) -- 2.7.4