Implementation generate BrokerID.
Implementation cancelHostResource method.
Add resource and device delete logic.
Delete unused defined value.
Change-Id: I83ac5e2af16493ff9e81c757bc523ad934b281c0
Signed-off-by: jyong2.kim <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1400
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Hun-je Yeon <hunje.yeon@samsung.com>
Reviewed-by: kwon doil <doil.kwon@samsung.com>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
#include <iostream>
#include <functional>
+#include <list>
-#include "OCPlatform.h"
-#include "octypes.h"
#include "logger.h"
-
#include "PrimitiveResource.h"
#define BROKER_TAG PCF("BROKER")
NON_PRESENCE_MODE
};
-typedef std::function<OCStackResult(BROKER_STATE)> BrokerCB;
+typedef unsigned int BrokerID;
+
+typedef std::function<void(BROKER_STATE)> BrokerCB;
struct BrokerRequesterInfo
{
+ BrokerID brockerId;
BrokerCB brockerCB;
};
typedef std::shared_ptr<BrokerRequesterInfo> BrokerRequesterInfoPtr;
typedef std::shared_ptr<ResourcePresence> ResourcePresencePtr;
typedef std::shared_ptr<DevicePresence> DevicePresencePtr;
+typedef std::list< ResourcePresencePtr > PresenceList;
+
+struct BrokerCBResourcePair
+{
+ BrokerCBResourcePair(ResourcePresencePtr _pResource, BrokerCB _cb)
+ : pResource(_pResource), brokerCB(_cb){}
+ ResourcePresencePtr pResource;
+ BrokerCB brokerCB;
+};
+typedef std::map<BrokerID, BrokerCBResourcePair> BrokerIDMap;
-typedef OC::OCPlatform::OCPresenceHandle BasePresenceHandle;
typedef std::function<void(OCStackResult, const unsigned int,
- const std::string&)> SubscribeCallback;
+ const std::string&)> SubscribeCB;
-typedef std::function<void(const HeaderOptions&, const ResponseStatement&, int)> GetCallback;
-typedef std::function<void(int)> TimeoutCallback;
+typedef std::function<void(const HeaderOptions&, const ResponseStatement&, int)> RequestGetCB;
+typedef std::function<void(int)> TimeoutCB;
#endif // BROKERTYPES_H_
const std::string getAddress() const;
void addPresenceResource(ResourcePresence * rPresence);
+ void removePresenceResource(ResourcePresence * rPresence);
+
+ bool isEmptyResourcePresence() const;
private:
std::string address;
std::list<ResourcePresence * > resourcePresenceList;
- SubscribeCallback pSubscribeRequestCB;
- TimeoutCallback pTimeoutCB;
+ SubscribeCB pSubscribeRequestCB;
+ TimeoutCB pTimeoutCB;
PresenceSubscriber presenceSubscriber;
void requestAllResourcePresence();
public:
static ResourceBroker * getInstance();
- OCStackResult hostResource(PrimitiveResourcePtr pResource, BrokerCB cb);
+ BrokerID hostResource(PrimitiveResourcePtr pResource, BrokerCB cb);
- OCStackResult cancelHostResource(PrimitiveResourcePtr pResource);
+ BrokerID cancelHostResource(BrokerID brokerId);
+ BROKER_STATE getResourceState(BrokerID brokerId);
BROKER_STATE getResourceState(PrimitiveResourcePtr pResource);
private:
static ResourceBroker * s_instance;
static std::mutex s_mutexForCreation;
- static std::unique_ptr<std::list< ResourcePresencePtr >> s_presenceList;
+ static std::unique_ptr<PresenceList> s_presenceList;
+
+ static std::unique_ptr<BrokerIDMap> s_brokerIDMap;
+ BrokerID generateBrokerID();
ResourcePresencePtr findResourcePresence(PrimitiveResourcePtr pResource);
ResourcePresence(PrimitiveResourcePtr pResource);
~ResourcePresence();
- void addBrokerRequesterCB(BrokerCB _cb);
- void removeBrokerRequesterCB(BrokerCB _cb);
- void removeAllBrokerRequesterCB();
+ void addBrokerRequester(BrokerID _id, BrokerCB _cb);
+ void removeBrokerRequester(BrokerID _id);
+ void removeAllBrokerRequester();
void requestResourceState();
void changePresenceMode(BROKER_MODE newMode);
+ bool isEmptyRequester() const;
+
private:
PrimitiveResourcePtr primitiveResource;
BROKER_STATE state;
BROKER_MODE mode;
- GetCallback pGetCB;
+ RequestGetCB pGetCB;
void GetCB(const HeaderOptions &hos, const ResponseStatement& rep, int seq);
- TimeoutCallback pTimeoutCB;
+ TimeoutCB pTimeoutCB;
bool isWithinTime;
void TimeOutCB(int msg);
if(foundDevice != nullptr)
{
s_deviceList.remove(foundDevice);
+ foundDevice.reset();
}
}
resourcePresenceList.push_back(rPresence);
}
+void DevicePresence::removePresenceResource(ResourcePresence * rPresence)
+{
+ resourcePresenceList.remove(rPresence);
+}
+
void DevicePresence::requestAllResourcePresence()
{
for(auto it : resourcePresenceList)
}
}
+bool DevicePresence::isEmptyResourcePresence() const
+{
+ return resourcePresenceList.empty();
+}
+
void DevicePresence::subscribeCB(OCStackResult ret,
const unsigned int seq, const std::string& hostAddress)
{
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#include <time.h>
+
#include "BrokerTypes.h"
#include "ResourceBroker.h"
-#define OIC_COAP "coap://"
-#define DEFAULT_CONTEXT_VALUE 0x99
-
ResourceBroker * ResourceBroker::s_instance = NULL;
std::mutex ResourceBroker::s_mutexForCreation;
-std::unique_ptr<std::list< ResourcePresencePtr >> ResourceBroker::s_presenceList(nullptr);
+std::unique_ptr<PresenceList> ResourceBroker::s_presenceList(nullptr);
+std::unique_ptr<BrokerIDMap> ResourceBroker::s_brokerIDMap(nullptr);
ResourceBroker::ResourceBroker()
{
// TODO Auto-generated constructor stub
if(s_presenceList == nullptr)
{
- s_presenceList = std::unique_ptr<std::list< ResourcePresencePtr >>(new std::list<ResourcePresencePtr>);
+ s_presenceList = std::unique_ptr<PresenceList>(new PresenceList);
+ }
+ if(s_brokerIDMap == nullptr)
+ {
+ s_brokerIDMap = std::unique_ptr<BrokerIDMap>(new BrokerIDMap);
}
}
// TODO Auto-generated destructor stub
if(s_presenceList != nullptr)
{
+ s_presenceList->erase(s_presenceList->begin(), s_presenceList->end());
s_presenceList->clear();
}
+ if(s_brokerIDMap != nullptr)
+ {
+ s_brokerIDMap->erase(s_brokerIDMap->begin(), s_brokerIDMap->end());
+ s_brokerIDMap->clear();
+ }
}
ResourceBroker * ResourceBroker::getInstance()
return s_instance;
}
-OCStackResult ResourceBroker::hostResource(PrimitiveResourcePtr pResource, BrokerCB cb)
+BrokerID ResourceBroker::hostResource(PrimitiveResourcePtr pResource, BrokerCB cb)
{
- OCStackResult ret = OC_STACK_INVALID_PARAM;
+ BrokerID retID = 0;
+
if (pResource->getUri().empty() || pResource->getHost().empty())
{
- return ret;
+ return retID;
}
if (cb == NULL)
{
- ret = OC_STACK_INVALID_CALLBACK;
- return ret;
+ return retID;
}
+ retID = generateBrokerID();
+
ResourcePresencePtr presenceItem = findResourcePresence(pResource);
if(presenceItem == nullptr)
{
presenceItem = ResourcePresencePtr(new ResourcePresence(pResource));
s_presenceList->push_back(presenceItem);
}
- presenceItem->addBrokerRequesterCB(cb);
+ presenceItem->addBrokerRequester(retID, cb);
+
+ BrokerCBResourcePair pair(presenceItem, cb);
+ s_brokerIDMap->insert(std::pair<BrokerID, BrokerCBResourcePair>
+ (retID, BrokerCBResourcePair(presenceItem, cb)));
- return ret;
+ return retID;
}
-OCStackResult ResourceBroker::cancelHostResource(PrimitiveResourcePtr pResource)
+BrokerID ResourceBroker::cancelHostResource(BrokerID brokerId)
{
- return OC_STACK_OK;
+ BrokerIDMap::iterator it = s_brokerIDMap->find(brokerId);
+ if(brokerId == 0)
+ {
+ // input parameter is wrong.
+ // hostResource never return value 0;
+ return brokerId;
+ }
+ else if(it == s_brokerIDMap->end())
+ {
+ // not found requested brokerId in BrokerMap;
+ return brokerId;
+ }
+ else
+ {
+ ResourcePresencePtr presenceItem = it->second.pResource;
+ presenceItem->removeBrokerRequester(brokerId);
+ s_brokerIDMap->erase(brokerId);
+
+ if(presenceItem->isEmptyRequester())
+ {
+ auto iter = std::find(s_presenceList->begin(), s_presenceList->end(), presenceItem);
+ s_presenceList->erase(iter);
+ presenceItem.reset();
+ }
+ }
+
+ return brokerId;
+}
+
+BROKER_STATE ResourceBroker::getResourceState(BrokerID brokerId)
+{
+ BROKER_STATE retState = BROKER_STATE::NONE;
+
+ BrokerIDMap::iterator it = s_brokerIDMap->find(brokerId);
+ if(it != s_brokerIDMap->end())
+ {
+ ResourcePresencePtr foundResource = it->second.pResource;
+ retState = foundResource->getResourceState();
+ }
+
+ return retState;
}
BROKER_STATE ResourceBroker::getResourceState(PrimitiveResourcePtr pResource)
BROKER_STATE retState = BROKER_STATE::NONE;
ResourcePresencePtr foundResource = findResourcePresence(pResource);
-
if(foundResource != nullptr)
{
retState = foundResource->getResourceState();
}
+
return retState;
}
return retResource;
}
+
+BrokerID ResourceBroker::generateBrokerID()
+{
+ BrokerID retID = 0;
+ srand(time(NULL));
+
+ while(1)
+ {
+ if(retID == 0 || s_brokerIDMap->find(retID) != s_brokerIDMap->end())
+ {
+ retID = (unsigned int)rand();
+ }
+ else
+ {
+ break;
+ }
+ }
+ return retID;
+}
ResourcePresence::~ResourcePresence()
{
+ std::string deviceAddress = primitiveResource->getHost();
+
+ DevicePresencePtr foundDevice
+ = DeviceAssociation::getInstance()->findDevice(deviceAddress);
+
+ if(foundDevice != nullptr)
+ {
+ foundDevice->removePresenceResource(this);
+
+ if(foundDevice->isEmptyResourcePresence())
+ {
+ DeviceAssociation::getInstance()->removeDevice(foundDevice);
+ }
+ }
+
requesterList->clear();
state = BROKER_STATE::DESTROYED;
}
-void ResourcePresence::addBrokerRequesterCB(BrokerCB _cb)
+void ResourcePresence::addBrokerRequester(BrokerID _id, BrokerCB _cb)
{
BrokerRequesterInfoPtr newRequester = BrokerRequesterInfoPtr(new BrokerRequesterInfo());
+ newRequester->brockerId = _id;
newRequester->brockerCB = _cb;
requesterList->push_back(newRequester);
}
+void ResourcePresence::removeAllBrokerRequester()
+{
+ requesterList->erase(requesterList->begin(), requesterList->end());
+}
+
+void ResourcePresence::removeBrokerRequester(BrokerID _id)
+{
+ std::list<BrokerRequesterInfoPtr>::iterator iter = requesterList->begin();
+ for(; iter != requesterList->end(); ++iter)
+ {
+ if(iter->get()->brockerId == _id)
+ {
+ requesterList->erase(iter);
+ break;
+ }
+ }
+}
+
+bool ResourcePresence::isEmptyRequester() const
+{
+ return requesterList->empty();
+}
+
void ResourcePresence::requestResourceState()
{
primitiveResource->requestGet(pGetCB);