iotivity 0.9.0
[platform/upstream/iotivity.git] / service / notification-manager / NotificationManager / src / VirtualRepresentation.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 "VirtualRepresentation.h"
22
23 AttributeMap VirtualRepresentation::s_attributeMap;
24
25 std::mutex VirtualRepresentation::s_mutexAttributeMap;
26 std::condition_variable VirtualRepresentation::s_conditionAttributeMap;
27 bool VirtualRepresentation::m_isReadyAttributeMap = true;
28
29 int g_num = 0;
30
31 VirtualRepresentation::VirtualRepresentation()
32 {
33     m_resourceHandle = NULL;
34     m_resourceProperty = 0x00;
35 }
36
37 VirtualRepresentation::~VirtualRepresentation()
38 {
39 }
40
41 std::string VirtualRepresentation::addVirtualTag(std::string uri)
42 {
43     std::string ret = uri;
44     return ret.append(VIRTURL_TAG + std::to_string(g_num++));
45 }
46
47 OCResourceHandle VirtualRepresentation::getResourceHandle()
48 {
49     return m_resourceHandle;
50 }
51
52 std::string VirtualRepresentation::getUri()
53 {
54     return m_virtualUri;
55 }
56
57 std::string VirtualRepresentation::getHostIP()
58 {
59     return m_originHostIP;
60 }
61
62 std::string VirtualRepresentation::getResourceTypeName()
63 {
64     return m_resourceTypeName;
65 }
66
67 std::string VirtualRepresentation::getResourceInterface()
68 {
69     return m_resourceInterface;
70 }
71
72 uint8_t VirtualRepresentation::getResourceProperty()
73 {
74     return m_resourceProperty;
75 }
76
77 int VirtualRepresentation::setUri(std::string uri)
78 {
79     m_virtualUri = addVirtualTag(uri);
80     return true;
81 }
82
83 int VirtualRepresentation::setHostIP(std::string ip)
84 {
85     m_originHostIP = ip;
86     return true;
87 }
88
89 int VirtualRepresentation::setResourceTypeName(std::string typeName)
90 {
91     m_resourceTypeName = typeName;
92     return true;
93 }
94
95 int VirtualRepresentation::setResourceInterface(std::string interface)
96 {
97     m_resourceInterface = interface;
98     return true;
99 }
100
101 int VirtualRepresentation::setResourceProperty(uint8_t property)
102 {
103     m_resourceProperty = property;
104     return true;
105 }
106
107 int VirtualRepresentation::setResourceHandle(OCResourceHandle & handle)
108 {
109     m_resourceHandle = handle;
110     return true;
111 }
112
113 int VirtualRepresentation::getRepresentation(OCRepresentation& oc)
114 {
115     oc.setAttributeMap(s_attributeMap);
116     oc.setUri(this->getUri().c_str());
117     return true;
118 }
119
120 OCEntityHandlerResult VirtualRepresentation::entityHandler(const std::shared_ptr<OCResourceRequest> request ,
121                 const std::shared_ptr<OCResourceResponse> response)
122 {
123     if(request)
124     {
125         std::string requestType = request->getRequestType();
126         int requestFlag = request->getRequestHandlerFlag();
127
128         if(requestFlag == RequestHandlerFlag::InitFlag)
129                 {
130                 }
131
132         else if(requestFlag == RequestHandlerFlag::RequestFlag)
133         {
134             if( (requestType == "GET") && response )
135             {
136
137                                 std::unique_lock< std::mutex > lck(s_mutexAttributeMap);
138                                 while(!m_isReadyAttributeMap)
139                                 {
140                                         s_conditionAttributeMap.wait(lck);
141                                 }
142                                 m_isReadyAttributeMap = false;
143
144                                 OCRepresentation rep;
145                                 getRepresentation(rep);
146
147                                 response->setErrorCode(200);
148                                 response->setResourceRepresentation(rep , DEFAULT_INTERFACE);
149
150                                 m_isReadyAttributeMap = true;
151                                 s_conditionAttributeMap.notify_all();
152                         }
153             else if(requestType == "PUT")
154             {
155                 // TODO
156             }
157             else if(requestType == "POST")
158             {
159                 // TODO
160             }
161             else if(requestType == "DELETE")
162             {
163                 // TODO
164             }
165             else
166             {
167                 // TODO
168             }
169         }
170         else if(requestFlag == RequestHandlerFlag::InitFlag)
171                 {
172                         // TODO
173                 }
174         else if(requestFlag == RequestHandlerFlag::ObserverFlag)
175         {
176                 // TODO
177                 cout << "requestFlag == RequestHandlerFlag::ObserverFlag\n";
178         }
179         else
180         {
181                 // requestFlag is not [Request, Init, Observer]
182                 // TODO
183         }
184     }
185     else
186     {
187         // Param(request) is empty.
188         // TODO
189     }
190     return OC_EH_OK;
191 }
192
193 void VirtualRepresentation::onObserve(const HeaderOptions &headerOption, const OCRepresentation &rep , const int eCode , const int sequenceNumber)
194 {
195         cout << "VirtualRepresentation::onObserve Enter\n";
196     if(eCode == SUCCESS_RESPONSE)
197     {
198         cout << "1\n";
199         AttributeMap inputAttributeMap = rep.getAttributeMap();
200
201         if(ResourceManager::getInstance()->isEmptyAttributeMap(inputAttributeMap))
202         {
203                 cout << "2\n";
204             return;
205         }
206         cout << "3\n";
207         VirtualRepresentation tmpObj = *this;
208         if(!tmpObj.getUri().empty())
209         {
210                 cout << "4\n";
211             AttributeMap tmpAttMap = ResourceManager::getInstance()->copyAttributeMap(inputAttributeMap);
212             cout << "5\n";
213             {
214                 cout << "6\n";
215                 std::unique_lock< std::mutex > lck(s_mutexAttributeMap);
216                 cout << "7\n";
217                 while(!m_isReadyAttributeMap)
218                 {
219                         cout << "8\n";
220                     s_conditionAttributeMap.wait(lck);
221                 }
222                 cout << "9\n";
223                 m_isReadyAttributeMap = false;
224                 cout << "10\n";
225                 s_attributeMap = tmpAttMap;
226                 cout << "11\n";
227                 m_isReadyAttributeMap = true;
228                 cout << "12\n";
229                 s_conditionAttributeMap.notify_all();
230                 cout << "13\n";
231             }
232
233             if(ResourceManager::getInstance()->m_onObserve)
234                         {
235                 cout << "14\n";
236                                 ResourceManager::getInstance()->m_onObserve(inputAttributeMap, tmpObj.getResourceHandle());
237                                 cout << "15\n";
238                         }
239         }
240     }
241     else
242     {
243         // Check the error.
244        // TODO
245     }
246     cout << "VirtualRepresentation::onObserve Out\n";
247 }