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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "ResourcePresence.h"
23 #include <bits/atomic_base.h>
24 #include <bits/shared_ptr_base.h>
32 #include "PrimitiveResource.h"
33 #include "DeviceAssociation.h"
34 #include "DevicePresence.h"
38 using namespace OIC::Service;
40 void getCallback(const HeaderOptions &hos, const ResponseStatement& rep,
41 int eCode, std::weak_ptr<ResourcePresence> this_ptr)
43 OIC_LOG_V(DEBUG,BROKER_TAG,"getCallback().\n");
44 std::shared_ptr<ResourcePresence> Ptr = this_ptr.lock();
47 Ptr->getCB(hos, rep, eCode);
50 void timeOutCallback(unsigned int msg, std::weak_ptr<ResourcePresence> this_ptr)
52 OIC_LOG_V(DEBUG,BROKER_TAG,"timeOutCallback().\n");
53 std::shared_ptr<ResourcePresence> Ptr = this_ptr.lock();
65 ResourcePresence::ResourcePresence()
66 : requesterList(nullptr), primitiveResource(nullptr),
67 state(BROKER_STATE::REQUESTED), mode(BROKER_MODE::NON_PRESENCE_MODE),
68 isWithinTime(true), receivedTime(0L), timeoutHandle(0)
72 void ResourcePresence::initializeResourcePresence(PrimitiveResourcePtr pResource)
74 OIC_LOG_V(DEBUG,BROKER_TAG,"initializeResourcePresence().\n");
75 pGetCB = std::bind(getCallback, std::placeholders::_1, std::placeholders::_2,
76 std::placeholders::_3, std::weak_ptr<ResourcePresence>(shared_from_this()));
77 pTimeoutCB = std::bind(timeOutCallback, std::placeholders::_1,
78 std::weak_ptr<ResourcePresence>(shared_from_this()));
79 pPollingCB = std::bind(&ResourcePresence::pollingCB, this, std::placeholders::_1);
81 primitiveResource = pResource;
83 = std::unique_ptr<std::list<BrokerRequesterInfoPtr>>
84 (new std::list<BrokerRequesterInfoPtr>);
86 timeoutHandle = expiryTimer.post(BROKER_SAFE_MILLISECOND, pTimeoutCB);
87 OIC_LOG_V(DEBUG,BROKER_TAG,"initializeResourcePresence::requestGet.\n");
88 primitiveResource->requestGet(pGetCB);
90 registerDevicePresence();
94 ResourcePresence::~ResourcePresence()
96 std::string deviceAddress = primitiveResource->getHost();
98 DevicePresencePtr foundDevice
99 = DeviceAssociation::getInstance()->findDevice(deviceAddress);
101 if(foundDevice != nullptr)
103 foundDevice->removePresenceResource(this);
105 if(foundDevice->isEmptyResourcePresence())
107 DeviceAssociation::getInstance()->removeDevice(foundDevice);
111 requesterList->clear();
113 state = BROKER_STATE::DESTROYED;
116 void ResourcePresence::addBrokerRequester(BrokerID _id, BrokerCB _cb)
118 OIC_LOG_V(DEBUG,BROKER_TAG,"addBrokerRequester().\n");
119 requesterList->push_back(
120 std::make_shared<BrokerRequesterInfo>(BrokerRequesterInfo(_id, _cb)));
123 void ResourcePresence::removeAllBrokerRequester()
125 OIC_LOG_V(DEBUG,BROKER_TAG,"removeAllBrokerRequester().\n");
126 if(requesterList != nullptr)
128 requesterList->erase(requesterList->begin(), requesterList->end());
132 void ResourcePresence::removeBrokerRequester(BrokerID _id)
134 OIC_LOG_V(DEBUG,BROKER_TAG,"removeBrokerRequester().\n");
135 std::list<BrokerRequesterInfoPtr>::iterator iter = requesterList->begin();
136 for(; iter != requesterList->end(); ++iter)
138 if(iter->get()->brokerId == _id)
140 OIC_LOG_V(DEBUG,BROKER_TAG,"find broker-id in requesterList.\n");
141 requesterList->erase(iter);
147 bool ResourcePresence::isEmptyRequester() const
149 OIC_LOG_V(DEBUG,BROKER_TAG,"isEmptyRequester().\n");
150 return (requesterList!=nullptr)?requesterList->empty():true;
153 int ResourcePresence::requesterListSize() const {
154 OIC_LOG_V(DEBUG,BROKER_TAG,"requesterListSize().\n");
155 return (requesterList!=nullptr)?requesterList->size():0;
158 void ResourcePresence::requestResourceState() const
160 OIC_LOG_V(DEBUG,BROKER_TAG,"requestResourceState().\n");
161 primitiveResource->requestGet(pGetCB);
162 OIC_LOG_V(DEBUG, BROKER_TAG, "Request Get\n");
165 void ResourcePresence::registerDevicePresence()
167 OIC_LOG_V(DEBUG,BROKER_TAG,"registerDevicePresence().\n");
168 std::string deviceAddress = primitiveResource->getHost();
170 DevicePresencePtr foundDevice
171 = DeviceAssociation::getInstance()->findDevice(deviceAddress);
173 if(foundDevice == nullptr)
177 foundDevice.reset(new DevicePresence());
178 foundDevice->initializeDevicePresence(primitiveResource);
183 DeviceAssociation::getInstance()->addDevice(foundDevice);
185 foundDevice->addPresenceResource(this);
188 void ResourcePresence::executeAllBrokerCB(BROKER_STATE changedState)
190 OIC_LOG_V(DEBUG, BROKER_TAG, "executeAllBrokerCB().\n");
191 if(state != changedState)
193 setResourcestate(changedState);
194 if(requesterList->empty() != true)
196 std::list<BrokerRequesterInfoPtr> list = * requesterList;
197 for(BrokerRequesterInfoPtr item : list)
199 item->brokerCB(state);
205 void ResourcePresence::setResourcestate(BROKER_STATE _state)
207 OIC_LOG_V(DEBUG, BROKER_TAG, "setResourcestate().\n");
208 this->state = _state;
211 void ResourcePresence::timeOutCB(unsigned int /*msg*/)
213 OIC_LOG_V(DEBUG, BROKER_TAG, "timeOutCB()");
214 OIC_LOG_V(DEBUG, BROKER_TAG, "waiting for terminate getCB\n");
215 std::unique_lock<std::mutex> lock(cbMutex);
221 if((receivedTime == 0) || ((receivedTime + BROKER_SAFE_SECOND) > currentTime ))
223 this->isWithinTime = true;
226 this->isWithinTime = false;
227 OIC_LOG_V(DEBUG, BROKER_TAG,
228 "Timeout execution. will be discard after receiving cb message.\n");
230 executeAllBrokerCB(BROKER_STATE::LOST_SIGNAL);
234 void ResourcePresence::pollingCB(unsigned int /*msg*/)
236 OIC_LOG_V(DEBUG, BROKER_TAG, "pollingCB().\n");
237 if(this->requesterList->size() != 0)
239 this->requestResourceState();
240 timeoutHandle = expiryTimer.post(BROKER_SAFE_MILLISECOND,pTimeoutCB);
244 void ResourcePresence::getCB(const HeaderOptions & /*hos*/,
245 const ResponseStatement & /*rep*/, int eCode)
247 OIC_LOG_V(DEBUG, BROKER_TAG, "getCB().\n");
248 OIC_LOG_V(DEBUG, BROKER_TAG, "waiting for terminate TimeoutCB.\n");
249 std::unique_lock<std::mutex> lock(cbMutex);
253 receivedTime = currentTime;
255 verifiedGetResponse(eCode);
259 expiryTimer.cancel(timeoutHandle);
263 if(mode == BROKER_MODE::NON_PRESENCE_MODE)
265 expiryTimer.post(BROKER_SAFE_MILLISECOND,pPollingCB);
270 void ResourcePresence::verifiedGetResponse(int eCode)
272 OIC_LOG_V(DEBUG, BROKER_TAG, "verifiedGetResponse().\n");
273 BROKER_STATE verifiedState = BROKER_STATE::NONE;
277 case OC_STACK_CONTINUE:
278 verifiedState = BROKER_STATE::ALIVE;
281 case OC_STACK_RESOURCE_DELETED:
282 verifiedState = BROKER_STATE::DESTROYED;
285 case OC_STACK_INVALID_REQUEST_HANDLE:
286 case OC_STACK_TIMEOUT:
287 case OC_STACK_COMM_ERROR:
288 case OC_STACK_PRESENCE_STOPPED:
289 case OC_STACK_PRESENCE_TIMEOUT:
291 verifiedState = BROKER_STATE::LOST_SIGNAL;
295 executeAllBrokerCB(verifiedState);
296 OIC_LOG_V(DEBUG, BROKER_TAG, "resource state : %d",(int)state);
299 const PrimitiveResourcePtr ResourcePresence::getPrimitiveResource() const
301 OIC_LOG_V(DEBUG, BROKER_TAG, "getPrimitiveResource()\n");
302 return primitiveResource;
305 BROKER_STATE ResourcePresence::getResourceState() const
307 OIC_LOG_V(DEBUG, BROKER_TAG, "getResourceState()\n");
311 void ResourcePresence::changePresenceMode(BROKER_MODE newMode)
313 OIC_LOG_V(DEBUG, BROKER_TAG, "changePresenceMode()\n");
316 expiryTimer.cancel(timeoutHandle);
317 if(newMode == BROKER_MODE::NON_PRESENCE_MODE)
319 timeoutHandle = expiryTimer.post(BROKER_SAFE_MILLISECOND,pTimeoutCB);
320 requestResourceState();
325 } // namespace Service