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"
22 #if (defined(__APPLE__) && defined(TARGET_OS_IPHONE))
25 #include <bits/atomic_base.h>
26 #include <bits/shared_ptr_base.h>
35 #include "PrimitiveResource.h"
36 #include "DeviceAssociation.h"
37 #include "DevicePresence.h"
41 using namespace OIC::Service;
43 void getCallback(const HeaderOptions &hos, const ResponseStatement& rep,
44 int eCode, std::weak_ptr<ResourcePresence> this_ptr)
46 OIC_LOG_V(DEBUG,BROKER_TAG,"getCallback().\n");
47 std::shared_ptr<ResourcePresence> Ptr = this_ptr.lock();
50 Ptr->getCB(hos, rep, eCode);
53 void timeOutCallback(unsigned int msg, std::weak_ptr<ResourcePresence> this_ptr)
55 OIC_LOG_V(DEBUG,BROKER_TAG,"timeOutCallback().\n");
56 std::shared_ptr<ResourcePresence> Ptr = this_ptr.lock();
68 ResourcePresence::ResourcePresence()
69 : requesterList(nullptr), primitiveResource(nullptr),
70 state(BROKER_STATE::REQUESTED), mode(BROKER_MODE::NON_PRESENCE_MODE),
71 isWithinTime(true), receivedTime(0L), timeoutHandle(0)
75 void ResourcePresence::initializeResourcePresence(PrimitiveResourcePtr pResource)
77 OIC_LOG_V(DEBUG,BROKER_TAG,"initializeResourcePresence().\n");
78 pGetCB = std::bind(getCallback, std::placeholders::_1, std::placeholders::_2,
79 std::placeholders::_3, std::weak_ptr<ResourcePresence>(shared_from_this()));
80 pTimeoutCB = std::bind(timeOutCallback, std::placeholders::_1,
81 std::weak_ptr<ResourcePresence>(shared_from_this()));
82 pPollingCB = std::bind(&ResourcePresence::pollingCB, this, std::placeholders::_1);
84 primitiveResource = pResource;
86 = std::unique_ptr<std::list<BrokerRequesterInfoPtr>>
87 (new std::list<BrokerRequesterInfoPtr>);
89 timeoutHandle = expiryTimer.post(BROKER_SAFE_MILLISECOND, pTimeoutCB);
90 OIC_LOG_V(DEBUG,BROKER_TAG,"initializeResourcePresence::requestGet.\n");
91 primitiveResource->requestGet(pGetCB);
93 registerDevicePresence();
97 ResourcePresence::~ResourcePresence()
99 std::string deviceAddress = primitiveResource->getHost();
101 DevicePresencePtr foundDevice
102 = DeviceAssociation::getInstance()->findDevice(deviceAddress);
104 if(foundDevice != nullptr)
106 foundDevice->removePresenceResource(this);
108 if(foundDevice->isEmptyResourcePresence())
110 DeviceAssociation::getInstance()->removeDevice(foundDevice);
114 requesterList->clear();
116 state = BROKER_STATE::DESTROYED;
119 void ResourcePresence::addBrokerRequester(BrokerID _id, BrokerCB _cb)
121 OIC_LOG_V(DEBUG,BROKER_TAG,"addBrokerRequester().\n");
122 requesterList->push_back(
123 std::make_shared<BrokerRequesterInfo>(BrokerRequesterInfo(_id, _cb)));
126 void ResourcePresence::removeAllBrokerRequester()
128 OIC_LOG_V(DEBUG,BROKER_TAG,"removeAllBrokerRequester().\n");
129 if(requesterList != nullptr)
131 requesterList->erase(requesterList->begin(), requesterList->end());
135 void ResourcePresence::removeBrokerRequester(BrokerID _id)
137 OIC_LOG_V(DEBUG,BROKER_TAG,"removeBrokerRequester().\n");
138 std::list<BrokerRequesterInfoPtr>::iterator iter = requesterList->begin();
139 for(; iter != requesterList->end(); ++iter)
141 if(iter->get()->brokerId == _id)
143 OIC_LOG_V(DEBUG,BROKER_TAG,"find broker-id in requesterList.\n");
144 requesterList->erase(iter);
150 bool ResourcePresence::isEmptyRequester() const
152 OIC_LOG_V(DEBUG,BROKER_TAG,"isEmptyRequester().\n");
153 return (requesterList!=nullptr)?requesterList->empty():true;
156 int ResourcePresence::requesterListSize() const {
157 OIC_LOG_V(DEBUG,BROKER_TAG,"requesterListSize().\n");
158 return (requesterList!=nullptr)?requesterList->size():0;
161 void ResourcePresence::requestResourceState() const
163 OIC_LOG_V(DEBUG,BROKER_TAG,"requestResourceState().\n");
164 primitiveResource->requestGet(pGetCB);
165 OIC_LOG_V(DEBUG, BROKER_TAG, "Request Get\n");
168 void ResourcePresence::registerDevicePresence()
170 OIC_LOG_V(DEBUG,BROKER_TAG,"registerDevicePresence().\n");
171 std::string deviceAddress = primitiveResource->getHost();
173 DevicePresencePtr foundDevice
174 = DeviceAssociation::getInstance()->findDevice(deviceAddress);
176 if(foundDevice == nullptr)
180 foundDevice.reset(new DevicePresence());
181 foundDevice->initializeDevicePresence(primitiveResource);
186 DeviceAssociation::getInstance()->addDevice(foundDevice);
188 foundDevice->addPresenceResource(this);
191 void ResourcePresence::executeAllBrokerCB(BROKER_STATE changedState)
193 OIC_LOG_V(DEBUG, BROKER_TAG, "executeAllBrokerCB().\n");
194 if(state != changedState)
196 setResourcestate(changedState);
197 if(requesterList->empty() != true)
199 std::list<BrokerRequesterInfoPtr> list = * requesterList;
200 for(BrokerRequesterInfoPtr item : list)
202 item->brokerCB(state);
208 void ResourcePresence::setResourcestate(BROKER_STATE _state)
210 OIC_LOG_V(DEBUG, BROKER_TAG, "setResourcestate().\n");
211 this->state = _state;
214 void ResourcePresence::timeOutCB(unsigned int /*msg*/)
216 OIC_LOG_V(DEBUG, BROKER_TAG, "timeOutCB()");
217 OIC_LOG_V(DEBUG, BROKER_TAG, "waiting for terminate getCB\n");
218 std::unique_lock<std::mutex> lock(cbMutex);
224 if((receivedTime == 0) || ((receivedTime + BROKER_SAFE_SECOND) > currentTime ))
226 this->isWithinTime = true;
229 this->isWithinTime = false;
230 OIC_LOG_V(DEBUG, BROKER_TAG,
231 "Timeout execution. will be discard after receiving cb message.\n");
233 executeAllBrokerCB(BROKER_STATE::LOST_SIGNAL);
237 void ResourcePresence::pollingCB(unsigned int /*msg*/)
239 OIC_LOG_V(DEBUG, BROKER_TAG, "pollingCB().\n");
240 if(this->requesterList->size() != 0)
242 this->requestResourceState();
243 timeoutHandle = expiryTimer.post(BROKER_SAFE_MILLISECOND,pTimeoutCB);
247 void ResourcePresence::getCB(const HeaderOptions & /*hos*/,
248 const ResponseStatement & /*rep*/, int eCode)
250 OIC_LOG_V(DEBUG, BROKER_TAG, "getCB().\n");
251 OIC_LOG_V(DEBUG, BROKER_TAG, "waiting for terminate TimeoutCB.\n");
252 std::unique_lock<std::mutex> lock(cbMutex);
256 receivedTime = currentTime;
258 verifiedGetResponse(eCode);
262 expiryTimer.cancel(timeoutHandle);
266 if(mode == BROKER_MODE::NON_PRESENCE_MODE)
268 expiryTimer.post(BROKER_SAFE_MILLISECOND,pPollingCB);
273 void ResourcePresence::verifiedGetResponse(int eCode)
275 OIC_LOG_V(DEBUG, BROKER_TAG, "verifiedGetResponse().\n");
276 BROKER_STATE verifiedState = BROKER_STATE::NONE;
280 case OC_STACK_CONTINUE:
281 verifiedState = BROKER_STATE::ALIVE;
284 case OC_STACK_RESOURCE_DELETED:
285 verifiedState = BROKER_STATE::DESTROYED;
288 case OC_STACK_INVALID_REQUEST_HANDLE:
289 case OC_STACK_TIMEOUT:
290 case OC_STACK_COMM_ERROR:
291 case OC_STACK_PRESENCE_STOPPED:
292 case OC_STACK_PRESENCE_TIMEOUT:
294 verifiedState = BROKER_STATE::LOST_SIGNAL;
298 executeAllBrokerCB(verifiedState);
299 OIC_LOG_V(DEBUG, BROKER_TAG, "resource state : %d",(int)state);
302 const PrimitiveResourcePtr ResourcePresence::getPrimitiveResource() const
304 OIC_LOG_V(DEBUG, BROKER_TAG, "getPrimitiveResource()\n");
305 return primitiveResource;
308 BROKER_STATE ResourcePresence::getResourceState() const
310 OIC_LOG_V(DEBUG, BROKER_TAG, "getResourceState()\n");
314 void ResourcePresence::changePresenceMode(BROKER_MODE newMode)
316 OIC_LOG_V(DEBUG, BROKER_TAG, "changePresenceMode()\n");
319 expiryTimer.cancel(timeoutHandle);
320 if(newMode == BROKER_MODE::NON_PRESENCE_MODE)
322 timeoutHandle = expiryTimer.post(BROKER_SAFE_MILLISECOND,pTimeoutCB);
323 requestResourceState();
328 } // namespace Service