1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "ResourceManager.h"
23 ResourceManager *ResourceManager::s_instance = NULL;
24 mutex ResourceManager::s_mutexForCreation;
25 std::list< VirtualRepresentation > ResourceManager::s_resourceList;
27 std::string ResourceManager::s_extraStr;
29 ResourceManager::ResourceManager()
31 m_onFoundforHosting = NULL;
36 ResourceManager::~ResourceManager()
40 ResourceManager *ResourceManager::getInstance()
44 s_mutexForCreation.lock();
47 s_instance = new ResourceManager();
49 s_mutexForCreation.unlock();
54 VirtualRepresentation ResourceManager::findVirtualRepresentation(std::string uri)
57 VirtualRepresentation retObject;
59 for(auto it = s_resourceList.begin() ; it != s_resourceList.end() ; it++)
61 if(it->getUri().compare(uri) == 0)
71 void ResourceManager::findNMResource(bool isHosting)
75 findResource("" , "coap://224.0.1.187/oc/core",
76 std::function< void(std::shared_ptr< OCResource > resource) >(
77 std::bind(&ResourceManager::foundResourceforhosting , ResourceManager::getInstance() ,
78 std::placeholders::_1)));
82 void ResourceManager::foundResourceforhosting(std::shared_ptr< OCResource > resource)
88 if(resource->uri().find("/a/NM") != std::string::npos)
90 ResourceManager::getInstance()->m_onFoundforHosting(resource);
99 catch(std::exception &e)
104 void ResourceManager::startHosting(std::shared_ptr< OCResource > resource)
107 cout << "start hosting" << endl;
108 VirtualRepresentation tmp = findVirtualRepresentation( resource->uri() );
110 if( !tmp.getUri().empty() )
115 VirtualRepresentation resourceObject;
116 resourceObject.setUri(resource->uri());
118 std::cout << "resourceObject uri: " << resourceObject.getUri() << std::endl;
120 std::string resourceHostIP;
121 std::string resourceType;
122 std::string resourceInterface;
123 uint8_t resourceProperty;
125 resourceHostIP = resource->host();
126 resourceType = *(resource->getResourceTypes().data());
127 resourceInterface = *(resource->getResourceInterfaces().data());
128 resourceProperty = (OC_DISCOVERABLE | resource->isObservable());
130 resourceObject.setHostIP(resourceHostIP);
131 resourceObject.setResourceTypeName(resourceType);
132 resourceObject.setResourceInterface(resourceInterface);
133 resourceObject.setResourceProperty(resourceProperty);
135 RegistrationManager::getInstance()->registerNMResource(resourceObject , resource);
137 s_resourceList.push_back(resourceObject);
141 void ResourceManager::notifyObservers(OCResourceHandle resourceHandle)
143 OCStackResult result = OC_STACK_OK;
145 result = notifyAllObservers(resourceHandle);
147 if(OC_STACK_NO_OBSERVERS == result)
154 AttributeMap ResourceManager::copyAttributeMap(AttributeMap &inputAttMap)
157 AttributeMap retAttMap;
159 retAttMap = inputAttMap;
160 // for(auto it = inputAttMap.begin() ; it != inputAttMap.end() ; ++it)
162 // AttributeValues tmpVal;
164 // for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
166 // std::string tmpStr;
168 // tmpStr.append(*valueItr);
170 // tmpVal.push_back(tmpStr);
172 // retAttMap[it->first] = tmpVal;
178 bool ResourceManager::isEmptyAttributeMap(AttributeMap &inputAttMap)
180 for(auto it = inputAttMap.begin() ; it != inputAttMap.end() ; ++it)
182 if(inputAttMap.find(it->first) == inputAttMap.end())
190 void ResourceManager::onFoundforHostingDefault(std::shared_ptr< OCResource > resource)
192 ResourceManager::getInstance()->startHosting(resource);
194 void ResourceManager::onObserveDefault(AttributeMap &inputAttMap , OCResourceHandle resourceHandle)
196 ResourceManager::getInstance()->notifyObservers(resourceHandle);
199 void ResourceManager::printAttributeMap(AttributeMap &inputAttMap)
201 for(auto it = inputAttMap.begin() ; it != inputAttMap.end() ; ++it)
203 std::cout << "\tAttribute name: " << it->first << " value: ";
205 for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
207 std::cout << "\t" << *valueItr << " ";
210 std::cout << std::endl;
214 void ResourceManager::addExtraStr(std::string str)
219 std::string ResourceManager::getExtraStr()
224 void ResourceManager::checkResourceDBPolicy()
229 void ResourceManager::saveResourceDB()