[SSM] Modify thread util and framework init method
[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         SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ITasker, (IBase**)&m_pTasker));
27         SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IResourceConnectivity, (IBase**)&m_pResourceConnectivity));
28
29         m_pResourceFinderEvent = NULL;
30         m_pPlatform = (OC::OCPlatform*)m_pResourceConnectivity->getPlatform();
31
32 CLEANUP:
33         return res;
34 }
35
36 void CResourceFinder::finalRelease()
37 {
38 }
39
40 SSMRESULT CResourceFinder::registerResourceFinderEvent(IN IResourceFinderEvent *pEvent)
41 {
42         m_pResourceFinderEvent = pEvent;
43         return SSM_S_OK;
44 }
45
46 void CResourceFinder::onResourceFound(std::shared_ptr<OC::OCResource> resource)
47 {
48         if (resource)
49         {
50                 int             *pMessage = new int[2];
51                 pMessage[0] = RESOURCE_DISCOVER_REQUESTPROFILE;
52                 pMessage[1] = (int)new std::shared_ptr<OC::OCResource>(resource);
53
54                 m_pTasker->addTask(this, pMessage);
55         }
56 }
57
58 SSMRESULT CResourceFinder::startResourceFinder()
59 {
60         //m_pPlatform->findResource("", "oc/core/service/SoftSensorManager/SoftSensor",
61         m_pPlatform->findResource("", "coap://224.0.1.187/oc/core?rt=SoftSensorManager.Sensor",
62                 std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
63
64         return SSM_S_OK;
65 }
66
67 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
68 {
69         return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
70 }
71
72 SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
73 {
74         return m_mapResourceHandler[pSensor->name]->stopObserve();
75 }
76
77 void CResourceFinder::onExecute(IN void* pArg)
78 {
79         SSMRESULT res = SSM_E_FAIL;
80         OC::QueryParamsMap      queryParams;
81         OICResourceHandler      *pResourceHandler = NULL;
82         int                                     *pMessage = (int*)pArg;
83         std::shared_ptr<OC::OCResource> *pResource = NULL;
84
85         switch (pMessage[0])
86         {
87         case RESOURCE_DISCOVER_REQUESTPROFILE:
88                 pResource = (std::shared_ptr<OC::OCResource>*)pMessage[1];
89                 pResourceHandler = new OICResourceHandler();
90                 SSM_CLEANUP_ASSERT(pResourceHandler->initHandler(*pResource, this));
91                 m_mapResourceHandler[pResource->get()->host() + pResource->get()->uri()] = pResourceHandler;
92                 pResource->get()->get(queryParams, std::bind(&OICResourceHandler::onGetResourceProfile,
93                         pResourceHandler, std::placeholders::_1, std::placeholders::_2));
94                 break;
95
96         case RESOURCE_DISCOVER_SETUP_RESOURCE:
97                 m_pResourceFinderEvent->onResourceFound((ISSMResource*)pMessage[1]);
98                 break;
99         }
100
101 CLEANUP:
102         ;
103 }
104
105 void CResourceFinder::onTerminate(IN void* pArg)
106 {
107         std::shared_ptr<OC::OCResource>         *pResource = NULL;
108         int                                     *pMessage = (int*)pArg;
109
110         switch (pMessage[0])
111         {
112         case RESOURCE_DISCOVER_REQUESTPROFILE:
113                 pResource = (std::shared_ptr<OC::OCResource>*)pMessage[1];
114                 delete pResource;
115                 break;
116
117         case RESOURCE_DISCOVER_SETUP_RESOURCE:
118                 break;
119         }
120
121         delete[] pMessage;
122 }