Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceBroker / src / ResourceBroker.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 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 <time.h>
22
23 #include "BrokerTypes.h"
24 #include "ResourceBroker.h"
25
26 namespace OIC
27 {
28     namespace Service
29     {
30         ResourceBroker * ResourceBroker::s_instance = NULL;
31         std::mutex ResourceBroker::s_mutexForCreation;
32         std::unique_ptr<PresenceList>  ResourceBroker::s_presenceList(nullptr);
33         std::unique_ptr<BrokerIDMap> ResourceBroker::s_brokerIDMap(nullptr);
34
35         ResourceBroker::~ResourceBroker()
36         {
37             if(s_presenceList != nullptr)
38             {
39                 OIC_LOG_V(DEBUG, BROKER_TAG, "clear the ResourcePresenceList.");
40                 s_presenceList->erase(s_presenceList->begin(), s_presenceList->end());
41             }
42             if(s_brokerIDMap != nullptr)
43             {
44                 OIC_LOG_V(DEBUG, BROKER_TAG, "clear the brokerIDMap.");
45                 s_brokerIDMap->erase(s_brokerIDMap->begin(), s_brokerIDMap->end());
46             }
47         }
48
49         ResourceBroker * ResourceBroker::getInstance()
50         {
51             if (!s_instance)
52             {
53                 s_mutexForCreation.lock();
54                 if (!s_instance)
55                 {
56                     s_instance = new ResourceBroker();
57                     s_instance->initializeResourceBroker();
58                 }
59                 s_mutexForCreation.unlock();
60             }
61             return s_instance;
62         }
63
64         BrokerID ResourceBroker::hostResource(PrimitiveResourcePtr pResource, BrokerCB cb)
65         {
66             OIC_LOG_V(DEBUG, BROKER_TAG, "hostResource().");
67             if(pResource == nullptr || cb == nullptr || cb == NULL)
68             {
69                 throw InvalidParameterException("[hostResource] input parameter(PrimitiveResource or BrokerCB) is Invalid");
70             }
71
72             BrokerID retID = generateBrokerID();
73
74             ResourcePresencePtr presenceItem = findResourcePresence(pResource);
75             if(presenceItem == nullptr)
76             {
77                 OIC_LOG_V(DEBUG, BROKER_TAG, "Not found any Handled Resource.");
78                 OIC_LOG_V(DEBUG, BROKER_TAG, "Create New Resource Presence Handler.");
79
80                 try
81                 {
82                     OIC_LOG_V(DEBUG, BROKER_TAG, "create the ResourcePresence.");
83                     presenceItem.reset(new ResourcePresence());
84                     presenceItem->initializeResourcePresence(pResource);
85                 }catch(RCSPlatformException &e)
86                 {
87                     throw FailedSubscribePresenceException(e.getReasonCode());
88                 }
89                 if(s_presenceList != nullptr)
90                 {
91                     OIC_LOG_V(DEBUG, BROKER_TAG, "push the ResourcePresence in presenceList.");
92                     s_presenceList->push_back(presenceItem);
93                 }
94             }
95             OIC_LOG_V(DEBUG, BROKER_TAG, "add the BrokerRequester in ResourcePresence.");
96             presenceItem->addBrokerRequester(retID, cb);
97
98             BrokerCBResourcePair pair(presenceItem, cb);
99             s_brokerIDMap->insert(std::pair<BrokerID, BrokerCBResourcePair>
100                 (retID, BrokerCBResourcePair(presenceItem, cb)));
101
102             return retID;
103         }
104
105         void ResourceBroker::cancelHostResource(BrokerID brokerId)
106         {
107             OIC_LOG_V(DEBUG,BROKER_TAG,"cancelHostResource().");
108             if(brokerId == 0)
109             {
110                 // input parameter is wrong.
111                 // hostResource never return value 0;
112                 OIC_LOG_V(DEBUG,BROKER_TAG,"brokerId is zero.");
113                 throw InvalidParameterException("[cancelHostResource] brokerId is invalid.");
114             }
115
116             BrokerIDMap::iterator it = s_brokerIDMap->find(brokerId);
117             if(it == s_brokerIDMap->end())
118             {
119                 // not found requested brokerId in BrokerMap;
120                 OIC_LOG_V(DEBUG,BROKER_TAG,"brokerId is not found in brokerIDMap.");
121                 throw InvalidParameterException("[cancelHostResource] brokerId is not found in brokerIDMap.");
122             }
123             else
124             {
125                 ResourcePresencePtr presenceItem = it->second.pResource;
126                 presenceItem->removeBrokerRequester(brokerId);
127                 s_brokerIDMap->erase(brokerId);
128
129                 if(presenceItem->isEmptyRequester())
130                 {
131                     OIC_LOG_V(DEBUG,BROKER_TAG,"remove resourcePresence in presenceList because it is not including any requester info.");
132                     s_presenceList->remove(presenceItem);
133                 }
134             }
135         }
136
137         BROKER_STATE ResourceBroker::getResourceState(BrokerID brokerId)
138         {
139             OIC_LOG_V(DEBUG,BROKER_TAG,"getResourceState().");
140             if(brokerId == 0)
141             {
142                 OIC_LOG_V(DEBUG,BROKER_TAG,"brokerId is zero.");
143                 throw InvalidParameterException("[getResourceState] input BrokerID is Invalid");
144             }
145
146             BROKER_STATE retState = BROKER_STATE::NONE;
147
148             BrokerIDMap::iterator it = s_brokerIDMap->find(brokerId);
149             if(it == s_brokerIDMap->end())
150             {
151                 // not found requested brokerId in BrokerMap;
152                 OIC_LOG_V(DEBUG,BROKER_TAG,"brokerId is not found in brokerIDMap.");
153                 throw InvalidParameterException("[getResourceState] input BrokerID is unknown ID");
154             }
155             else
156             {
157                 ResourcePresencePtr foundResource = it->second.pResource;
158                 retState = foundResource->getResourceState();
159             }
160
161             return retState;
162         }
163
164         BROKER_STATE ResourceBroker::getResourceState(PrimitiveResourcePtr pResource)
165         {
166             OIC_LOG_V(DEBUG,BROKER_TAG,"getResourceState().");
167             if(pResource == nullptr)
168             {
169                 throw InvalidParameterException("[getResourceState] input PrimitiveResource is Invalid");
170             }
171
172             BROKER_STATE retState = BROKER_STATE::NONE;
173
174             ResourcePresencePtr foundResource = findResourcePresence(pResource);
175             if(foundResource != nullptr)
176             {
177                 retState = foundResource->getResourceState();
178             }
179
180             return retState;
181         }
182
183         void ResourceBroker::initializeResourceBroker()
184         {
185             OIC_LOG_V(DEBUG,BROKER_TAG,"initializeResourceBroker().");
186             if(s_presenceList == nullptr)
187             {
188                 OIC_LOG_V(DEBUG,BROKER_TAG,"create the presenceList.");
189                 s_presenceList = std::unique_ptr<PresenceList>(new PresenceList);
190             }
191             if(s_brokerIDMap == nullptr)
192             {
193                 OIC_LOG_V(DEBUG,BROKER_TAG,"create the brokerIDMap.");
194                 s_brokerIDMap = std::unique_ptr<BrokerIDMap>(new BrokerIDMap);
195             }
196         }
197
198         ResourcePresencePtr ResourceBroker::findResourcePresence(PrimitiveResourcePtr pResource)
199         {
200             OIC_LOG_V(DEBUG,BROKER_TAG,"findResourcePresence().");
201             ResourcePresencePtr retResource(nullptr);
202
203             if(s_presenceList->empty() != true)
204             {
205                 for(auto & it : * s_presenceList)
206                 {
207                     PrimitiveResourcePtr temp = it->getPrimitiveResource();
208                     if(temp == pResource)
209                     {
210                         retResource = it;
211                         break;
212                     }
213                 }
214             }
215
216             return retResource;
217         }
218
219         BrokerID ResourceBroker::generateBrokerID()
220         {
221             OIC_LOG_V(DEBUG,BROKER_TAG,"generateBrokerID().");
222             BrokerID retID = 0;
223             srand(time(NULL));
224
225             while(1)
226             {
227                 if(retID != 0 && s_brokerIDMap->find(retID) == s_brokerIDMap->end())
228                 {
229                     break;
230                 }
231                 retID = (unsigned int)rand();
232             }
233
234             return retID;
235         }
236     } // namespace Service
237 } // namespace OIC