1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 "resource_manager.h"
23 int ResourceManager::id;
24 ResourceManager *ResourceManager::getInstance()
26 static ResourceManager s_instance;
30 ResourceManager::ResourceManager()
31 : m_resourceCreator(new SimulatorResourceCreator()) {}
33 ResourceManager::~ResourceManager()
35 delete m_resourceCreator;
38 SimulatorResourcePtr ResourceManager::createResource(const std::string &configPath,
39 SimulatorResource::ResourceModelChangedCB callback)
42 * TODO: Temporarily creating the light resource for testing the basic flow
43 * Once the config parser is included this method will simulate the resource based on the config file
45 SimulatorResourcePtr simulatorResource = m_resourceCreator->createLightResoure();
46 simulatorResource->setModelChangeCallback(callback);
48 simulatorResource->setURI(getURI(simulatorResource->getURI()));
49 OC::EntityHandler entityHandler = std::bind(&SimulatorResource::entityHandler,
50 simulatorResource.get(), std::placeholders::_1);
51 std::string uri = simulatorResource->getURI();
52 OCStackResult result = OC::OCPlatform::registerResource(simulatorResource->m_resourceHandle,
54 simulatorResource->getResourceType(),
55 simulatorResource->getInterfaceType(),
57 OC_DISCOVERABLE | OC_OBSERVABLE);
58 if (OC_STACK_OK != result)
63 // Add the resource to resource list table
64 m_resourceList[simulatorResource->getResourceType()].insert(
65 std::pair<std::string, SimulatorResourcePtr>(simulatorResource->getURI(), simulatorResource));
66 return simulatorResource;
69 std::vector<SimulatorResourcePtr> ResourceManager::createResource(const std::string &configPath,
70 const int count, SimulatorResource::ResourceModelChangedCB callback)
72 std::vector<SimulatorResourcePtr> list;
76 std::vector<SimulatorResourcePtr> ResourceManager::getResources(void) const
78 std::vector<SimulatorResourcePtr> list;
82 SimulatorResult ResourceManager::deleteResource(SimulatorResourcePtr &resource)
85 return SIMULATOR_RESOURCE_NOT_FOUND;
87 auto resourceTableEntry = m_resourceList.find(resource->getResourceType());
88 if (m_resourceList.end() != resourceTableEntry)
90 auto resourceEntry = resourceTableEntry->second.find(resource->getURI());
91 if (resourceTableEntry->second.end() != resourceEntry)
93 if (OC_STACK_OK == OC::OCPlatform::unregisterResource(resource->getHandle()))
94 resourceTableEntry->second.erase(resourceEntry);
98 return SIMULATOR_SUCCESS;
101 SimulatorResult ResourceManager::deleteResources(const std::string &resourceType)
103 auto resourceTableEntry = m_resourceList.find(resourceType);
104 if (m_resourceList.end() != resourceTableEntry)
106 return SIMULATOR_RESOURCE_NOT_FOUND;
109 for (auto resourceEntry : resourceTableEntry->second)
111 SimulatorResourcePtr resource = resourceEntry.second;
112 if (OC_STACK_OK == OC::OCPlatform::unregisterResource(resource->getHandle()))
113 resourceTableEntry->second.erase(resourceTableEntry->second.find(resource->getURI()));
116 return SIMULATOR_SUCCESS;
119 std::string ResourceManager::getURI(std::string uri)
121 std::ostringstream os;
123 if (!uri.empty() && '/' != uri[uri.length() - 1])
125 os << "simulator/" << id++;