e6f0128675f57396ff0ea4042a8a9779997343f0
[platform/upstream/iotivity.git] / service / notification-manager / NotificationManager / src / ResourceManager.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 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 "ResourceManager.h"
22
23 ResourceManager *ResourceManager::s_instance = NULL;
24 std::list< VirtualRepresentation > ResourceManager::s_resourceList;
25
26 std::string ResourceManager::s_extraStr;
27
28 ResourceManager::ResourceManager()
29 {
30 }
31
32 ResourceManager::~ResourceManager()
33 {
34 }
35
36 ResourceManager *ResourceManager::getInstance()
37 {
38     if(!s_instance)
39     {
40         s_instance = new ResourceManager();
41     }
42     return s_instance;
43 }
44
45 VirtualRepresentation ResourceManager::findVirtualRepresentation(std::string uri)
46 {
47
48     VirtualRepresentation retObject;
49
50     for(auto it = s_resourceList.begin() ; it != s_resourceList.end() ; it++)
51     {
52         if(it->getUri().compare(uri) == 0)
53         {
54             retObject = *it;
55             return retObject;
56         }
57     }
58
59     return retObject;
60 }
61
62 OCStackResult ResourceManager::findNMResource(const std::string& host ,
63         const std::string& resourceName , bool ishosting)
64 {
65         return findResource(host , resourceName ,
66             std::function< void(std::shared_ptr< OCResource > resource) >(
67                     std::bind(&ResourceManager::foundResourceforhosting , this ,
68                             std::placeholders::_1)));
69 }
70
71 void ResourceManager::foundResourceforhosting(std::shared_ptr< OCResource > resource)
72 {
73     try
74     {
75         if(resource)
76         {
77             if(resource->uri().find("/a/NM") != std::string::npos)
78             {
79                 NotificationManager::getInstance()->getOnFoundHostingCandidate()(resource);
80             }
81         }
82         else
83         {
84         }
85
86     }
87     catch(std::exception& e)
88     {
89     }
90 }
91
92 void ResourceManager::startHosting(std::shared_ptr< OCResource > resource)
93 {
94     VirtualRepresentation tmp = findVirtualRepresentation( resource->uri() );
95
96     if( !tmp.getUri().empty() )
97     {
98         return;
99     }
100
101     VirtualRepresentation resourceObject;
102     resourceObject.setUri(resource->uri());
103
104     std::cout << "resourceObject uri: " << resourceObject.getUri() << std::endl;
105
106     std::string resourceHostIP;
107     std::string resourceType;
108     std::string resourceInterface;
109     uint8_t resourceProperty;
110
111     resourceHostIP = resource->host();
112     resourceType = *(resource->getResourceTypes().data());
113     resourceInterface = *(resource->getResourceInterfaces().data());
114     resourceProperty = (OC_DISCOVERABLE | resource->isObservable());
115
116     resourceObject.setHostIP(resourceHostIP);
117     resourceObject.setResourceTypeName(resourceType);
118     resourceObject.setResourceInterface(resourceInterface);
119     resourceObject.setResourceProperty(resourceProperty);
120
121     RegistrationManager::getInstance()->registerNMResource(resourceObject , resource);
122
123     s_resourceList.push_back(resourceObject);
124
125 }
126
127 AttributeMap ResourceManager::copyAttributeMap(AttributeMap &inputAttMap)
128 {
129
130     AttributeMap retAttMap;
131
132     retAttMap = inputAttMap;
133
134     return retAttMap;
135 }
136
137 bool ResourceManager::isEmptyAttributeMap(AttributeMap &inputAttMap)
138 {
139     for(auto it = inputAttMap.begin() ; it != inputAttMap.end() ; ++it)
140     {
141         if(inputAttMap.find(it->first) == inputAttMap.end())
142         {
143             return true;
144         }
145     }
146     return false;
147 }
148
149 void ResourceManager::onFoundReport(std::shared_ptr< OCResource > resource)
150 {
151     NotificationManager::getInstance()->getStartHosting()(resource);
152 }
153
154 void ResourceManager::printAttributeMap(AttributeMap &inputAttMap)
155 {
156     for(auto it = inputAttMap.begin() ; it != inputAttMap.end() ; ++it)
157     {
158         std::cout << "\tAttribute name: " << it->first << " value: ";
159
160         for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
161         {
162             std::cout << "\t" << *valueItr << " ";
163         }
164
165         std::cout << std::endl;
166     }
167 }
168
169 void ResourceManager::addExtraStr(std::string str)
170 {
171     s_extraStr = str;
172 }
173
174 std::string ResourceManager::getExtraStr()
175 {
176     return s_extraStr;
177 }
178
179 void ResourceManager::checkResourceDBPolicy()
180 {
181
182 }
183
184 void ResourceManager::saveResourceDB()
185 {
186
187 }