57dbe2cdc403dddab38c40d842a83268ea206f14
[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 CLEANUP:
36     return res;
37 }
38
39 void CResourceFinder::finalRelease()
40 {
41 }
42
43 SSMRESULT CResourceFinder::registerResourceFinderEvent(IN IResourceFinderEvent *pEvent)
44 {
45     m_pResourceFinderEvent = pEvent;
46     return SSM_S_OK;
47 }
48
49 void CResourceFinder::onResourceFound(std::shared_ptr< OC::OCResource > resource)
50 {
51     if (resource)
52     {
53         std::string path = resource->host() + resource->uri();
54
55         if (m_mapResourceHandler.find(path) != m_mapResourceHandler.end())
56             return;
57
58         intptr_t      *pMessage = new intptr_t [2];
59         pMessage[0] = RESOURCE_DISCOVER_REQUESTPROFILE;
60         pMessage[1] = reinterpret_cast<intptr_t> (new  std::shared_ptr<OC::OCResource>(resource));
61
62         std::cout << "Resource Found !! >> " << path << std::endl;
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 + "/oc/core?rt=SoftSensorManager.Sensor",
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     OC::OCPlatform::OCPresenceHandle presenceHandle = nullptr;
121
122     ret = OC::OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=SoftSensorManager.Sensor",
123                                        std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
124
125     if (ret != OC_STACK_OK)
126         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
127
128     ret = OC::OCPlatform::subscribePresence(presenceHandle, "coap://224.0.1.187",
129                                             "SoftSensorManager.Sensor",
130                                             std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
131                                                     std::placeholders::_2, std::placeholders::_3));
132
133     if (ret != OC_STACK_OK)
134         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
135
136     res = SSM_S_OK;
137
138 CLEANUP:
139     return res;
140 }
141
142 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
143 {
144     return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
145 }
146
147 SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
148 {
149     return m_mapResourceHandler[pSensor->name]->stopObserve();
150 }
151
152 void CResourceFinder::onExecute(IN void *pArg)
153 {
154     SSMRESULT res = SSM_E_FAIL;
155     OCStackResult ret = OC_STACK_ERROR;
156     OC::QueryParamsMap queryParams;
157     OICResourceHandler *pResourceHandler = NULL;
158     intptr_t                 *pMessage =  reinterpret_cast<intptr_t *>(pArg);
159     std::shared_ptr< OC::OCResource > *pResource = NULL;
160     OC::OCPlatform::OCPresenceHandle presenceHandle = NULL;
161
162     std::string resourceHostAddress = "";
163     std::string resourceFullPath = "";
164
165     switch (pMessage[0])
166     {
167         case RESOURCE_DISCOVER_REQUESTPROFILE:
168             pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
169             pResourceHandler = new OICResourceHandler();
170             SSM_CLEANUP_ASSERT(pResourceHandler->initHandler(*pResource, this));
171
172             resourceFullPath = pResource->get()->host() + pResource->get()->uri();
173
174             resourceHostAddress = pResource->get()->host();
175             resourceHostAddress.erase(0, 7);        // erase 'coap://'
176
177             m_mapResourceHandler[resourceFullPath] = pResourceHandler;
178
179             m_mapResources[resourceHostAddress].push_back(resourceFullPath);
180
181             ret = pResource->get()->get(queryParams,
182                                         std::bind(&OICResourceHandler::onGetResourceProfile, pResourceHandler,
183                                                   std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
184
185             if (ret != OC_STACK_OK)
186                 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
187
188             break;
189
190         case RESOURCE_DISCOVER_INSTALL_RESOURCE:
191             if (m_mapResourcePresenceHandles.find(((ISSMResource *)pMessage[1])->ip) ==
192                 m_mapResourcePresenceHandles.end())
193             {
194                 ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
195                                                         "SoftSensorManager.Sensor",
196                                                         std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
197                                                                 std::placeholders::_2, std::placeholders::_3));
198
199                 if (ret != OC_STACK_OK)
200                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
201
202                 m_mapResourcePresenceHandles[((ISSMResource *)pMessage[1])->ip] = presenceHandle;
203             }
204
205             m_pResourceFinderEvent->onResourceFound((ISSMResource *) pMessage[1]);
206             break;
207
208         case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
209             m_pResourceFinderEvent->onResourceLost(&((OICResourceHandler *) pMessage[1])->m_SSMResource);
210
211             if (m_mapResourcePresenceHandles.find(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip) !=
212                 m_mapResourcePresenceHandles.end())
213             {
214                 ret = OC::OCPlatform::unsubscribePresence(
215                           m_mapResourcePresenceHandles[((OICResourceHandler *)pMessage[1])->m_SSMResource.ip]);
216
217                 if (ret != OC_STACK_OK)
218                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
219
220                 m_mapResourcePresenceHandles.erase(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip);
221             }
222
223             m_mapResourceHandler.erase(((OICResourceHandler *) pMessage[1])->m_SSMResource.name);
224             break;
225     }
226
227 CLEANUP:
228     ;
229 }
230
231 void CResourceFinder::onTerminate(IN void *pArg)
232 {
233     std::shared_ptr< OC::OCResource > *pResource = NULL;
234     intptr_t *pMessage = (intptr_t *)pArg;
235
236     switch (pMessage[0])
237     {
238         case RESOURCE_DISCOVER_REQUESTPROFILE:
239             pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
240             delete pResource;
241             break;
242
243         case RESOURCE_DISCOVER_INSTALL_RESOURCE:
244             break;
245
246         case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
247             break;
248     }
249
250     delete[] pMessage;
251 }