f10ab7f363cfd8fd4edc44dd8b526b6a04f88462
[platform/upstream/iotivity.git] / service / simulator / src / resource_manager.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "resource_manager.h"
22
23 int ResourceManager::id;
24 ResourceManager *ResourceManager::getInstance()
25 {
26     static ResourceManager s_instance;
27     return &s_instance;
28 }
29
30 ResourceManager::ResourceManager()
31     : m_resourceCreator(new SimulatorResourceCreator()) {}
32
33 ResourceManager::~ResourceManager()
34 {
35     delete m_resourceCreator;
36 }
37
38 SimulatorResourcePtr ResourceManager::createResource(const std::string &configPath,
39         SimulatorResource::ResourceModelChangedCB callback)
40 {
41     /**
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
44      */
45     SimulatorResourcePtr simulatorResource = m_resourceCreator->createLightResoure();
46     simulatorResource->setModelChangeCallback(callback);
47
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,
53                            uri,
54                            simulatorResource->getResourceType(),
55                            simulatorResource->getInterfaceType(),
56                            entityHandler,
57                            OC_DISCOVERABLE | OC_OBSERVABLE);
58     if (OC_STACK_OK != result)
59     {
60         return NULL;
61     }
62
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;
67 }
68
69 std::vector<SimulatorResourcePtr> ResourceManager::createResource(const std::string &configPath,
70         const int count, SimulatorResource::ResourceModelChangedCB callback)
71 {
72     std::vector<SimulatorResourcePtr> list;
73     return list;
74 }
75
76 std::vector<SimulatorResourcePtr> ResourceManager::getResources(void) const
77 {
78     std::vector<SimulatorResourcePtr> list;
79     return list;
80 }
81
82 SimulatorResult ResourceManager::deleteResource(SimulatorResourcePtr &resource)
83 {
84     if (!resource.get())
85         return SIMULATOR_RESOURCE_NOT_FOUND;
86
87     auto resourceTableEntry = m_resourceList.find(resource->getResourceType());
88     if (m_resourceList.end() != resourceTableEntry)
89     {
90         auto resourceEntry = resourceTableEntry->second.find(resource->getURI());
91         if (resourceTableEntry->second.end() != resourceEntry)
92         {
93             if (OC_STACK_OK == OC::OCPlatform::unregisterResource(resource->getHandle()))
94                 resourceTableEntry->second.erase(resourceEntry);
95         }
96     }
97
98     return SIMULATOR_SUCCESS;
99 }
100
101 SimulatorResult ResourceManager::deleteResources(const std::string &resourceType)
102 {
103     auto resourceTableEntry = m_resourceList.find(resourceType);
104     if (m_resourceList.end() != resourceTableEntry)
105     {
106         return SIMULATOR_RESOURCE_NOT_FOUND;
107     }
108
109     for (auto resourceEntry : resourceTableEntry->second)
110     {
111         SimulatorResourcePtr resource = resourceEntry.second;
112         if (OC_STACK_OK == OC::OCPlatform::unregisterResource(resource->getHandle()))
113             resourceTableEntry->second.erase(resourceTableEntry->second.find(resource->getURI()));
114     }
115
116     return SIMULATOR_SUCCESS;
117 }
118
119 std::string ResourceManager::getURI(std::string uri)
120 {
121     std::ostringstream os;
122     os << uri;
123     if (!uri.empty() && '/' != uri[uri.length() - 1])
124         os << '/';
125     os << "simulator/" << id++;
126     return os.str();
127 }
128