Add BrokerID for start and cancel Broker.
authorjyong2.kim <jyong2.kim@samsung.com>
Tue, 23 Jun 2015 10:00:15 +0000 (19:00 +0900)
committerUze Choi <uzchoi@samsung.com>
Wed, 24 Jun 2015 08:26:51 +0000 (08:26 +0000)
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>
service/basis/resourceBroker/include/BrokerTypes.h
service/basis/resourceBroker/include/DevicePresence.h
service/basis/resourceBroker/include/ResourceBroker.h
service/basis/resourceBroker/include/ResourcePresence.h
service/basis/resourceBroker/src/DeviceAssociation.cpp
service/basis/resourceBroker/src/DevicePresence.cpp
service/basis/resourceBroker/src/ResourceBroker.cpp
service/basis/resourceBroker/src/ResourcePresence.cpp

index a49469cc98b1b1cb6c892f5d16d5abce3b91fd3a..2f813e5d83f71de9f9adfd5278953beaf0020f5e 100755 (executable)
 
 #include <iostream>
 #include <functional>
+#include <list>
 
-#include "OCPlatform.h"
-#include "octypes.h"
 #include "logger.h"
-
 #include "PrimitiveResource.h"
 
 #define BROKER_TAG PCF("BROKER")
@@ -75,9 +73,12 @@ enum class BROKER_MODE
     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;
@@ -91,12 +92,21 @@ typedef std::shared_ptr<PrimitiveResource> PrimitiveResourcePtr;
 
 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_
index 495d01b9acd419d581842fb8c08b462fca81aa49..19ebf56c0eab810862b1067c9ea6ea3182d4fbc0 100755 (executable)
@@ -36,6 +36,9 @@ public:
 
     const std::string getAddress() const;
     void addPresenceResource(ResourcePresence * rPresence);
+    void removePresenceResource(ResourcePresence * rPresence);
+
+    bool isEmptyResourcePresence() const;
 
 private:
     std::string address;
@@ -44,8 +47,8 @@ private:
 
     std::list<ResourcePresence * > resourcePresenceList;
 
-    SubscribeCallback pSubscribeRequestCB;
-    TimeoutCallback pTimeoutCB;
+    SubscribeCB pSubscribeRequestCB;
+    TimeoutCB pTimeoutCB;
     PresenceSubscriber presenceSubscriber;
 
     void requestAllResourcePresence();
index 0ca22810226dd582b953ce40e9182001a2ca4a06..d2b9d497fe2c007afac1a4aee9c0c947557cbbcf 100755 (executable)
@@ -36,10 +36,11 @@ class ResourceBroker
 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:
@@ -48,7 +49,10 @@ 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);
 
index 7ef6a5ce5063a740b5617c280bc6cd1d445f6229..fcd65746e5939303c6f3db73fb2c9dbb03a3d881 100755 (executable)
@@ -33,9 +33,9 @@ public:
     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();
 
@@ -46,6 +46,8 @@ public:
 
     void changePresenceMode(BROKER_MODE newMode);
 
+    bool isEmptyRequester() const;
+
 private:
     PrimitiveResourcePtr primitiveResource;
 
@@ -54,10 +56,10 @@ private:
     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);
index 03757f3d36d1e85d9aeeca3c099baecb3b6114b8..ce0c199bcf376f8f95a2973d19396c0f47023a70 100644 (file)
@@ -79,5 +79,6 @@ void DeviceAssociation::removeDevice(DevicePresencePtr dPresence)
     if(foundDevice != nullptr)
     {
         s_deviceList.remove(foundDevice);
+        foundDevice.reset();
     }
 }
index dcec8ec45590727fe8a2eeb12f469a52f3d11091..ceffc58af18b68ccd9f333491b1f9639879f63b2 100755 (executable)
@@ -63,6 +63,11 @@ void DevicePresence::addPresenceResource(ResourcePresence * rPresence)
     resourcePresenceList.push_back(rPresence);
 }
 
+void DevicePresence::removePresenceResource(ResourcePresence * rPresence)
+{
+    resourcePresenceList.remove(rPresence);
+}
+
 void DevicePresence::requestAllResourcePresence()
 {
     for(auto it : resourcePresenceList)
@@ -71,6 +76,11 @@ void DevicePresence::requestAllResourcePresence()
     }
 }
 
+bool DevicePresence::isEmptyResourcePresence() const
+{
+    return resourcePresenceList.empty();
+}
+
 void DevicePresence::subscribeCB(OCStackResult ret,
         const unsigned int seq, const std::string& hostAddress)
 {
index 0a6c18efefaf504574091da0e2f599743cd40b23..d94effc0a98d544d4474a416983beb584306ca29 100755 (executable)
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
+#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);
     }
 }
 
@@ -42,8 +46,14 @@ ResourceBroker::~ResourceBroker()
     // 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()
@@ -60,19 +70,21 @@ 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)
     {
@@ -82,14 +94,58 @@ OCStackResult ResourceBroker::hostResource(PrimitiveResourcePtr pResource, Broke
         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)
@@ -97,11 +153,11 @@ BROKER_STATE ResourceBroker::getResourceState(PrimitiveResourcePtr pResource)
     BROKER_STATE retState = BROKER_STATE::NONE;
 
     ResourcePresencePtr foundResource = findResourcePresence(pResource);
-
     if(foundResource != nullptr)
     {
         retState = foundResource->getResourceState();
     }
+
     return retState;
 }
 
@@ -124,3 +180,22 @@ ResourcePresencePtr ResourceBroker::findResourcePresence(PrimitiveResourcePtr pR
 
     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;
+}
index 64e1561ce253e779c4a323b8d000dab81c758e91..8dd06dbc64b526438519c3b1e29b80efff4d7d59 100755 (executable)
@@ -64,18 +64,57 @@ void ResourcePresence::registerDevicePresence()
 
 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);