There are Two modifications.
[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 mutex ResourceManager::s_mutexForCreation;
25 std::list< VirtualRepresentation > ResourceManager::s_resourceList;
26
27 std::string ResourceManager::s_extraStr;
28
29 ResourceManager::ResourceManager()
30 {
31         m_onFoundforHosting     = NULL;
32         m_onObserve                     = NULL;
33         m_notify                                = NULL;
34 }
35
36 ResourceManager::~ResourceManager()
37 {
38 }
39
40 ResourceManager *ResourceManager::getInstance()
41 {
42     if(!s_instance)
43     {
44         s_mutexForCreation.lock();
45         if(!s_instance)
46         {
47                 s_instance = new ResourceManager();
48         }
49         s_mutexForCreation.unlock();
50     }
51     return s_instance;
52 }
53
54 VirtualRepresentation ResourceManager::findVirtualRepresentation(std::string uri)
55 {
56
57     VirtualRepresentation retObject;
58
59     for(auto it = s_resourceList.begin() ; it != s_resourceList.end() ; it++)
60     {
61         if(it->getUri().compare(uri) == 0)
62         {
63             retObject = *it;
64             return retObject;
65         }
66     }
67
68     return retObject;
69 }
70
71 void ResourceManager::findNMResource(bool isHosting)
72 {
73         if(isHosting)
74         {
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)));
79         }
80 }
81
82 void ResourceManager::foundResourceforhosting(std::shared_ptr< OCResource > resource)
83 {
84     try
85     {
86         if(resource)
87         {
88             if(resource->uri().find("/a/NM") != std::string::npos)
89             {
90                 ResourceManager::getInstance()->m_onFoundforHosting(resource);
91             }
92         }
93         else
94         {
95                 // TODO
96         }
97
98     }
99     catch(std::exception &e)
100     {
101     }
102 }
103
104 void ResourceManager::startHosting(std::shared_ptr< OCResource > resource)
105 {
106
107         cout << "start hosting" << endl;
108     VirtualRepresentation tmp = findVirtualRepresentation( resource->uri() );
109
110     if( !tmp.getUri().empty() )
111     {
112         return;
113     }
114
115     VirtualRepresentation resourceObject;
116     resourceObject.setUri(resource->uri());
117
118     std::cout << "resourceObject uri: " << resourceObject.getUri() << std::endl;
119
120     std::string resourceHostIP;
121     std::string resourceType;
122     std::string resourceInterface;
123     uint8_t resourceProperty;
124
125     resourceHostIP = resource->host();
126     resourceType = *(resource->getResourceTypes().data());
127     resourceInterface = *(resource->getResourceInterfaces().data());
128     resourceProperty = (OC_DISCOVERABLE | resource->isObservable());
129
130     resourceObject.setHostIP(resourceHostIP);
131     resourceObject.setResourceTypeName(resourceType);
132     resourceObject.setResourceInterface(resourceInterface);
133     resourceObject.setResourceProperty(resourceProperty);
134
135     RegistrationManager::getInstance()->registerNMResource(resourceObject , resource);
136
137     s_resourceList.push_back(resourceObject);
138
139 }
140
141 void ResourceManager::notifyObservers(OCResourceHandle resourceHandle)
142 {
143         OCStackResult result = OC_STACK_OK;
144
145         result = notifyAllObservers(resourceHandle);
146
147         if(OC_STACK_NO_OBSERVERS == result)
148         {
149                 // No observers.
150                 // TODO
151         }
152 }
153
154 AttributeMap ResourceManager::copyAttributeMap(AttributeMap &inputAttMap)
155 {
156
157     AttributeMap retAttMap;
158
159     retAttMap = inputAttMap;
160 //    for(auto it = inputAttMap.begin() ; it != inputAttMap.end() ; ++it)
161 //    {
162 //        AttributeValues tmpVal;
163 //
164 //        for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
165 //        {
166 //            std::string tmpStr;
167 //
168 //            tmpStr.append(*valueItr);
169 //
170 //            tmpVal.push_back(tmpStr);
171 //        }
172 //        retAttMap[it->first] = tmpVal;
173 //
174 //    }
175     return retAttMap;
176 }
177
178 bool ResourceManager::isEmptyAttributeMap(AttributeMap &inputAttMap)
179 {
180     for(auto it = inputAttMap.begin() ; it != inputAttMap.end() ; ++it)
181     {
182         if(inputAttMap.find(it->first) == inputAttMap.end())
183         {
184             return true;
185         }
186     }
187     return false;
188 }
189
190 void ResourceManager::onFoundforHostingDefault(std::shared_ptr< OCResource > resource)
191 {
192     ResourceManager::getInstance()->startHosting(resource);
193 }
194 void ResourceManager::onObserveDefault(AttributeMap &inputAttMap , OCResourceHandle resourceHandle)
195 {
196         ResourceManager::getInstance()->notifyObservers(resourceHandle);
197 }
198
199 void ResourceManager::printAttributeMap(AttributeMap &inputAttMap)
200 {
201     for(auto it = inputAttMap.begin() ; it != inputAttMap.end() ; ++it)
202     {
203         std::cout << "\tAttribute name: " << it->first << " value: ";
204
205         for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
206         {
207             std::cout << "\t" << *valueItr << " ";
208         }
209
210         std::cout << std::endl;
211     }
212 }
213
214 void ResourceManager::addExtraStr(std::string str)
215 {
216     s_extraStr = str;
217 }
218
219 std::string ResourceManager::getExtraStr()
220 {
221     return s_extraStr;
222 }
223
224 void ResourceManager::checkResourceDBPolicy()
225 {
226
227 }
228
229 void ResourceManager::saveResourceDB()
230 {
231
232 }