7eae06994cbaf6634fa194e9be5827525fd5b533
[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         else if(requestFlag == RequestHandlerFlag::RequestFlag)
132         {
133             if(requestType == "GET")
134             {
135
136                 if(response)
137                 {
138                     std::unique_lock< std::mutex > lck(s_mutexAttributeMap);
139                     while(!m_isReadyAttributeMap)
140                     {
141                         s_conditionAttributeMap.wait(lck);
142                     }
143                     m_isReadyAttributeMap = false;
144
145                     NotificationManager::getInstance()->getPrint()(s_attributeMap);
146
147                     OCRepresentation rep;
148                     getRepresentation(rep);
149
150                     response->setErrorCode(200);
151                     response->setResourceRepresentation(rep , "");
152
153                     m_isReadyAttributeMap = true;
154                     s_conditionAttributeMap.notify_all();
155                 }
156                 else
157                 {
158                 }
159
160             }
161             else if(requestType == "PUT")
162             {
163             }
164             else if(requestType == "POST")
165             {
166             }
167             else if(requestType == "DELETE")
168             {
169             }
170             else
171             {
172             }
173         }
174         else if(requestFlag == RequestHandlerFlag::ObserverFlag)
175         {
176         }
177         else
178         {
179         }
180     }
181     else
182     {
183     }
184
185     return OC_EH_OK;
186 }
187
188 void VirtualRepresentation::onObserve(const HeaderOptions &headerOption, const OCRepresentation &rep , const int eCode , const int sequenceNumber)
189 {
190     if(eCode == SUCCESS_RESPONSE)
191     {
192         AttributeMap tmp = rep.getAttributeMap();
193
194         if(ResourceManager::getInstance()->isEmptyAttributeMap(tmp))
195         {
196             return;
197         }
198
199         if(NotificationManager::getInstance()->getOnObserve())
200         {
201             NotificationManager::getInstance()->getOnObserve()(tmp);
202         }
203
204         NotificationManager::getInstance()->getPrint()(tmp);
205
206         OCStackResult result = OC_STACK_OK;
207
208         VirtualRepresentation tmpObj = *this;
209         if(!tmpObj.getUri().empty())
210         {
211
212             AttributeMap tmpAttMap = ResourceManager::getInstance()->copyAttributeMap(tmp);
213
214             {
215                 std::unique_lock< std::mutex > lck(s_mutexAttributeMap);
216                 while(!m_isReadyAttributeMap)
217                 {
218                     s_conditionAttributeMap.wait(lck);
219                 }
220                 m_isReadyAttributeMap = false;
221
222                 s_attributeMap = tmpAttMap;
223
224                 m_isReadyAttributeMap = true;
225                 s_conditionAttributeMap.notify_all();
226             }
227
228             result = notifyAllObservers(tmpObj.getResourceHandle());
229         }
230         else
231         {
232         }
233
234         if(OC_STACK_NO_OBSERVERS == result)
235         {
236         }
237
238     }
239     else
240     {
241         std::exit(-1);
242     }
243 }