1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "BrokerTypes.h"
24 #include "ResourceBroker.h"
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);
35 ResourceBroker::~ResourceBroker()
37 if(s_presenceList != nullptr)
39 OIC_LOG_V(DEBUG, BROKER_TAG, "clear the ResourcePresenceList.");
40 s_presenceList->erase(s_presenceList->begin(), s_presenceList->end());
42 if(s_brokerIDMap != nullptr)
44 OIC_LOG_V(DEBUG, BROKER_TAG, "clear the brokerIDMap.");
45 s_brokerIDMap->erase(s_brokerIDMap->begin(), s_brokerIDMap->end());
49 ResourceBroker * ResourceBroker::getInstance()
53 s_mutexForCreation.lock();
56 s_instance = new ResourceBroker();
57 s_instance->initializeResourceBroker();
59 s_mutexForCreation.unlock();
64 BrokerID ResourceBroker::hostResource(PrimitiveResourcePtr pResource, BrokerCB cb)
66 OIC_LOG_V(DEBUG, BROKER_TAG, "hostResource().");
67 if(pResource == nullptr || cb == nullptr || cb == NULL)
69 throw InvalidParameterException("[hostResource] input parameter(PrimitiveResource or BrokerCB) is Invalid");
72 BrokerID retID = generateBrokerID();
74 ResourcePresencePtr presenceItem = findResourcePresence(pResource);
75 if(presenceItem == nullptr)
77 OIC_LOG_V(DEBUG, BROKER_TAG, "Not found any Handled Resource.");
78 OIC_LOG_V(DEBUG, BROKER_TAG, "Create New Resource Presence Handler.");
82 OIC_LOG_V(DEBUG, BROKER_TAG, "create the ResourcePresence.");
83 presenceItem.reset(new ResourcePresence());
84 presenceItem->initializeResourcePresence(pResource);
85 }catch(RCSPlatformException &e)
87 throw FailedSubscribePresenceException(e.getReasonCode());
89 if(s_presenceList != nullptr)
91 OIC_LOG_V(DEBUG, BROKER_TAG, "push the ResourcePresence in presenceList.");
92 s_presenceList->push_back(presenceItem);
95 OIC_LOG_V(DEBUG, BROKER_TAG, "add the BrokerRequester in ResourcePresence.");
96 presenceItem->addBrokerRequester(retID, cb);
98 BrokerCBResourcePair pair(presenceItem, cb);
99 s_brokerIDMap->insert(std::pair<BrokerID, BrokerCBResourcePair>
100 (retID, BrokerCBResourcePair(presenceItem, cb)));
105 void ResourceBroker::cancelHostResource(BrokerID brokerId)
107 OIC_LOG_V(DEBUG,BROKER_TAG,"cancelHostResource().");
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.");
116 BrokerIDMap::iterator it = s_brokerIDMap->find(brokerId);
117 if(it == s_brokerIDMap->end())
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.");
125 ResourcePresencePtr presenceItem = it->second.pResource;
126 presenceItem->removeBrokerRequester(brokerId);
127 s_brokerIDMap->erase(brokerId);
129 if(presenceItem->isEmptyRequester())
131 OIC_LOG_V(DEBUG,BROKER_TAG,"remove resourcePresence in presenceList because it is not including any requester info.");
132 s_presenceList->remove(presenceItem);
137 BROKER_STATE ResourceBroker::getResourceState(BrokerID brokerId)
139 OIC_LOG_V(DEBUG,BROKER_TAG,"getResourceState().");
142 OIC_LOG_V(DEBUG,BROKER_TAG,"brokerId is zero.");
143 throw InvalidParameterException("[getResourceState] input BrokerID is Invalid");
146 BROKER_STATE retState = BROKER_STATE::NONE;
148 BrokerIDMap::iterator it = s_brokerIDMap->find(brokerId);
149 if(it == s_brokerIDMap->end())
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");
157 ResourcePresencePtr foundResource = it->second.pResource;
158 retState = foundResource->getResourceState();
164 BROKER_STATE ResourceBroker::getResourceState(PrimitiveResourcePtr pResource)
166 OIC_LOG_V(DEBUG,BROKER_TAG,"getResourceState().");
167 if(pResource == nullptr)
169 throw InvalidParameterException("[getResourceState] input PrimitiveResource is Invalid");
172 BROKER_STATE retState = BROKER_STATE::NONE;
174 ResourcePresencePtr foundResource = findResourcePresence(pResource);
175 if(foundResource != nullptr)
177 retState = foundResource->getResourceState();
183 void ResourceBroker::initializeResourceBroker()
185 OIC_LOG_V(DEBUG,BROKER_TAG,"initializeResourceBroker().");
186 if(s_presenceList == nullptr)
188 OIC_LOG_V(DEBUG,BROKER_TAG,"create the presenceList.");
189 s_presenceList = std::unique_ptr<PresenceList>(new PresenceList);
191 if(s_brokerIDMap == nullptr)
193 OIC_LOG_V(DEBUG,BROKER_TAG,"create the brokerIDMap.");
194 s_brokerIDMap = std::unique_ptr<BrokerIDMap>(new BrokerIDMap);
198 ResourcePresencePtr ResourceBroker::findResourcePresence(PrimitiveResourcePtr pResource)
200 OIC_LOG_V(DEBUG,BROKER_TAG,"findResourcePresence().");
201 ResourcePresencePtr retResource(nullptr);
203 if(s_presenceList->empty() != true)
205 for(auto & it : * s_presenceList)
207 PrimitiveResourcePtr temp = it->getPrimitiveResource();
208 if(temp == pResource)
219 BrokerID ResourceBroker::generateBrokerID()
221 OIC_LOG_V(DEBUG,BROKER_TAG,"generateBrokerID().");
227 if(retID != 0 && s_brokerIDMap->find(retID) == s_brokerIDMap->end())
231 retID = (unsigned int)rand();
236 } // namespace Service