Changeset for reviewing RI-CA integration changes.
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / SensorProcessor / ResourceFinder.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 #include "ResourceFinder.h"
21
22 SSMRESULT CResourceFinder::finalConstruct()
23 {
24     SSMRESULT res = SSM_E_FAIL;
25
26     OC::PlatformConfig cfg(OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0,
27                            OC::QualityOfService::LowQos);
28
29     SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ITasker, (IBase **)&m_pTasker));
30
31     OC::OCPlatform::Configure(cfg);
32
33     m_pResourceFinderEvent = NULL;
34
35     m_multicastPresenceHandle = nullptr;
36
37 CLEANUP:
38     return res;
39 }
40
41 void CResourceFinder::finalRelease()
42 {
43 }
44
45 SSMRESULT CResourceFinder::registerResourceFinderEvent(IN IResourceFinderEvent *pEvent)
46 {
47     m_pResourceFinderEvent = pEvent;
48     return SSM_S_OK;
49 }
50
51 void CResourceFinder::onResourceFound(std::shared_ptr< OC::OCResource > resource)
52 {
53     if (resource)
54     {
55         std::string path = resource->host() + resource->uri();
56
57         if (m_mapResourceHandler.find(path) != m_mapResourceHandler.end())
58             return;
59
60         intptr_t      *pMessage = new intptr_t [2];
61         pMessage[0] = RESOURCE_DISCOVER_REQUESTPROFILE;
62         pMessage[1] = reinterpret_cast<intptr_t> (new  std::shared_ptr<OC::OCResource>(resource));
63
64         m_pTasker->addTask(this, pMessage);
65     }
66 }
67
68 void CResourceFinder::presenceHandler(OCStackResult result, const unsigned int nonce,
69                                       const std::string &hostAddress)
70 {
71     SSMRESULT res = SSM_E_FAIL;
72     OCStackResult ret = OC_STACK_ERROR;
73     intptr_t *pMessage = NULL;
74
75     switch (result)
76     {
77         case OC_STACK_OK:
78             ret = OC::OCPlatform::findResource("",
79                                                "coap://" + hostAddress + ":6298" + "/oc/core?rt=SoftSensorManager.Sensor", OC_ALL,
80                                                std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
81
82             if (ret != OC_STACK_OK)
83                 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
84
85             break;
86
87         case OC_STACK_PRESENCE_STOPPED:
88         case OC_STACK_PRESENCE_TIMEOUT:
89             if (m_mapResources.find(hostAddress) != m_mapResources.end())
90             {
91                 while (!m_mapResources[hostAddress].empty())
92                 {
93                     pMessage = new intptr_t[2];
94                     pMessage[0] = RESOURCE_DISCOVER_UNINSTALL_RESOURCE;
95                     pMessage[1] = reinterpret_cast<intptr_t> (m_mapResourceHandler[m_mapResources[hostAddress].back()]);
96                     m_mapResources[hostAddress].pop_back();
97                     m_pTasker->addTask(this, pMessage);
98                 }
99
100                 m_mapResources.erase(hostAddress);
101             }
102             break;
103
104         case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
105             break;
106
107         default:
108             break;
109     }
110
111 CLEANUP:
112     ;
113 }
114
115 SSMRESULT CResourceFinder::startResourceFinder()
116 {
117     SSMRESULT res = SSM_E_FAIL;
118     OCStackResult ret = OC_STACK_ERROR;
119
120     ret = OC::OCPlatform::findResource("",
121                                        "coap://224.0.1.187:6298/oc/core?rt=SoftSensorManager.Sensor", OC_ALL,
122                                        std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
123
124     if (ret != OC_STACK_OK)
125         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
126
127     ret = OC::OCPlatform::subscribePresence(m_multicastPresenceHandle, "coap://224.0.1.187",
128                                             "SoftSensorManager.Sensor", OC_ALL,
129                                             std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
130                                                     std::placeholders::_2, std::placeholders::_3));
131
132     if (ret != OC_STACK_OK)
133         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
134
135     res = SSM_S_OK;
136
137 CLEANUP:
138     return res;
139 }
140
141 SSMRESULT CResourceFinder::stopResourceFinder()
142 {
143     SSMRESULT res = SSM_E_FAIL;
144     OCStackResult ret = OC_STACK_ERROR;
145
146     ret = OC::OCPlatform::unsubscribePresence(m_multicastPresenceHandle);
147
148     if (ret != OC_STACK_OK)
149         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
150
151     m_multicastPresenceHandle = nullptr;
152
153     res = SSM_S_OK;
154
155 CLEANUP:
156     return res;
157 }
158
159 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
160 {
161     return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
162 }
163
164 SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
165 {
166     return m_mapResourceHandler[pSensor->name]->stopObserve();
167 }
168
169 void CResourceFinder::onExecute(IN void *pArg)
170 {
171     SSMRESULT res = SSM_E_FAIL;
172     OCStackResult ret = OC_STACK_ERROR;
173     OC::QueryParamsMap queryParams;
174     OICResourceHandler *pResourceHandler = NULL;
175     intptr_t                 *pMessage =  reinterpret_cast<intptr_t *>(pArg);
176     std::shared_ptr< OC::OCResource > *pResource = NULL;
177     OC::OCPlatform::OCPresenceHandle presenceHandle = NULL;
178
179     std::string resourceHostAddress = "";
180     std::string resourceFullPath = "";
181
182     switch (pMessage[0])
183     {
184         case RESOURCE_DISCOVER_REQUESTPROFILE:
185             pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
186             pResourceHandler = new OICResourceHandler();
187             SSM_CLEANUP_ASSERT(pResourceHandler->initHandler(*pResource, this));
188
189             resourceFullPath = pResource->get()->host() + pResource->get()->uri();
190
191             resourceHostAddress = pResource->get()->host();
192             resourceHostAddress.erase(0, 7);        // erase 'coap://'
193
194             m_mapResourceHandler[resourceFullPath] = pResourceHandler;
195
196             m_mapResources[resourceHostAddress].push_back(resourceFullPath);
197
198             ret = pResource->get()->get(queryParams,
199                                         std::bind(&OICResourceHandler::onGetResourceProfile, pResourceHandler,
200                                                   std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
201
202             if (ret != OC_STACK_OK)
203                 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
204
205             break;
206
207         case RESOURCE_DISCOVER_INSTALL_RESOURCE:
208             if (m_mapResourcePresenceHandles.find(((ISSMResource *)pMessage[1])->ip) ==
209                 m_mapResourcePresenceHandles.end())
210             {
211                 ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
212                                                         "SoftSensorManager.Sensor", OC_ALL,
213                                                         std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
214                                                                 std::placeholders::_2, std::placeholders::_3));
215
216                 if (ret != OC_STACK_OK)
217                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
218
219                 m_mapResourcePresenceHandles[((ISSMResource *)pMessage[1])->ip] = presenceHandle;
220             }
221
222             m_pResourceFinderEvent->onResourceFound((ISSMResource *) pMessage[1]);
223             break;
224
225         case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
226             m_pResourceFinderEvent->onResourceLost(&((OICResourceHandler *) pMessage[1])->m_SSMResource);
227
228             if (m_mapResourcePresenceHandles.find(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip) !=
229                 m_mapResourcePresenceHandles.end())
230             {
231                 ret = OC::OCPlatform::unsubscribePresence(
232                           m_mapResourcePresenceHandles[((OICResourceHandler *)pMessage[1])->m_SSMResource.ip]);
233
234                 if (ret != OC_STACK_OK)
235                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
236
237                 m_mapResourcePresenceHandles.erase(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip);
238             }
239
240             m_mapResourceHandler.erase(((OICResourceHandler *) pMessage[1])->m_SSMResource.name);
241             break;
242     }
243
244 CLEANUP:
245     ;
246 }
247
248 void CResourceFinder::onTerminate(IN void *pArg)
249 {
250     std::shared_ptr< OC::OCResource > *pResource = NULL;
251     intptr_t *pMessage = (intptr_t *)pArg;
252
253     switch (pMessage[0])
254     {
255         case RESOURCE_DISCOVER_REQUESTPROFILE:
256             pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
257             delete pResource;
258             break;
259
260         case RESOURCE_DISCOVER_INSTALL_RESOURCE:
261             break;
262
263         case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
264             break;
265     }
266
267     delete[] pMessage;
268 }
269