Update Resource Cache Initial Version
authorjyong2.kim <jyong2.kim@samsung.com>
Tue, 2 Jun 2015 01:26:17 +0000 (10:26 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Fri, 5 Jun 2015 02:13:58 +0000 (02:13 +0000)
fix identifier of CacheHandler class

Change-Id: Id27435b07c446f3e43ccc130b18a91225301da70
Signed-off-by: jyong2.kim <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1164
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/basis/resourceCache/include/CacheHandler.h [new file with mode: 0644]
service/basis/resourceCache/include/CacheTypes.h [new file with mode: 0644]
service/basis/resourceCache/include/ResourceCache.h [new file with mode: 0644]
service/basis/resourceCache/src/CacheHandler.cpp [new file with mode: 0644]
service/basis/resourceCache/src/ResourceCache.cpp [new file with mode: 0644]

diff --git a/service/basis/resourceCache/include/CacheHandler.h b/service/basis/resourceCache/include/CacheHandler.h
new file mode 100644 (file)
index 0000000..d09555a
--- /dev/null
@@ -0,0 +1,92 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef CACHEHANDLER_H_
+#define CACHEHANDLER_H_
+
+#include <list>
+#include <memory>
+#include <string>
+#include <boost/progress.hpp>
+
+#include "OCResource.h"
+#include "logger.h"
+
+#include "CacheTypes.h"
+
+#define CACHE_TAG  PCF("CACHE")
+
+class CacheHandler
+{
+public:
+    CacheHandler(
+            ServiceResource & pResource,
+            CacheCB func,
+            REPORT_FREQUENCY rf,
+            long repeatTime);
+    ~CacheHandler();
+
+    CacheID addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime);
+    CacheID deleteSubscriber(CacheID id);
+
+    std::shared_ptr<CacheData> getCachedData();
+    ServiceResource * getServiceResource();
+
+    // for request to base
+    OCStackResult requestToNetwork(std::string uri, std::string address, OCMethod method);
+    OCStackResult requestToNetwork(ServiceResource *pResource, OCMethod method);
+
+private:
+    // origin resource info
+    std::string uri;
+    std::string address;
+
+    // resource instance
+    ServiceResource *sResource;
+    std::shared_ptr<BaseResource> baseHandler;
+
+    // cached data info
+    std::shared_ptr<CacheData> data;
+    long updateTime;
+    CACHE_STATE state;
+
+    // subscriber info
+    SubscriberInfo *subscriberList;
+    SubscriberInfoPair findSubscriber(CacheID id);
+
+    // for requestCB from base
+    void onObserve(const HeaderOptions& ho, const ResponseStatement& _rep, int _result, int _seq);
+    void onGet(const OC::HeaderOptions& _ho,
+                const OC::OCRepresentation& _rep, const int _result);
+
+    ObserveCB pObserveCB;
+    GetCB pGetCB;
+
+    //
+    OCStackResult updateCacheData();
+
+    // for timer
+    void setUpdateTime();
+    void onTimer();
+    bool isAvailable();
+
+};
+
+#endif /* CACHEHANDLER_H_ */
diff --git a/service/basis/resourceCache/include/CacheTypes.h b/service/basis/resourceCache/include/CacheTypes.h
new file mode 100644 (file)
index 0000000..fdc9129
--- /dev/null
@@ -0,0 +1,78 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef CACHETYPES_H
+#define CACHETYPES_H
+
+#include <functional>
+#include <map>
+#include <memory>
+#include <string>
+
+#include "ocstack.h"
+#include "OCResource.h"
+
+#include "../include/PrimitiveResource.h"
+
+//class ServiceResource;
+
+typedef PrimitiveResource ServiceResource;
+
+enum class REPORT_FREQUENCY
+{
+    NONE = 0,
+    UPTODATE,
+    PERIODICTY
+};
+
+struct Report_Info
+{
+    REPORT_FREQUENCY rf;
+    int reportID;
+    long latestReportTime;
+    long repeatTime;
+};
+
+enum class CACHE_STATE
+{
+    READY = 0,
+    READY_YET,
+    LOST_SIGNAL,
+    DESTROYED,
+    UPDATING
+};
+
+typedef int CacheID;
+
+typedef std::map<std::string, std::string> CacheData;
+typedef std::function<OCStackResult(std::shared_ptr<ServiceResource>, CacheData)> CacheCB;
+typedef std::map<Report_Info, CacheCB> SubscriberInfo;
+typedef std::pair<Report_Info, CacheCB> SubscriberInfoPair;
+
+typedef OC::OCResource BaseResource;
+//typedef std::function<void(const OC::HeaderOptions&,
+//            const OC::OCRepresentation&, const int, const int)> ObserveCB;
+//typedef std::function<void(const OC::HeaderOptions&,
+//            const OC::OCRepresentation&, const int)> GetCB;
+
+typedef PrimitiveResource::GetCallback GetCB;
+typedef PrimitiveResource::ObserveCallback ObserveCB;
+
+#endif
diff --git a/service/basis/resourceCache/include/ResourceCache.h b/service/basis/resourceCache/include/ResourceCache.h
new file mode 100644 (file)
index 0000000..e232be1
--- /dev/null
@@ -0,0 +1,62 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef RESOURCECACHE_H_
+#define RESOURCECACHE_H_
+
+#include <list>
+#include <string>
+
+#include "../../../../resource/csdk/stack/include/octypes.h"
+#include "CacheHandler.h"
+#include "CacheTypes.h"
+
+#define CACHE_TAG PCF("CACHE")
+
+class ResourceCache
+{
+public:
+    ResourceCache * getInstance();
+
+    CacheID requestResourceCache(
+            ServiceResource & pResource,
+                CacheCB func = NULL, REPORT_FREQUENCY rf = REPORT_FREQUENCY::NONE, long time = 0l);
+
+//    OCStackResult cancelResourceCache(std::string address, std::string uri);
+    OCStackResult cancelResourceCache(ServiceResource & pResource, CacheID id);
+
+//    OCStackResult updateResourceCache(std::string address, std::string uri);
+    OCStackResult updateResourceCache(ServiceResource & pResource);
+
+//    OCStackResult getResourceCache(std::string address, std::string uri);
+    OCStackResult getResourceCache(ServiceResource & pResource);
+
+private:
+    ResourceCache();
+    ~ResourceCache();
+
+    static ResourceCache * s_instance;
+    static std::mutex s_mutexForCreation;
+    static std::list< CacheHandler * > * s_cacheDataList;
+
+    CacheHandler * findCacheHandler(ServiceResource & pResource);
+};
+
+#endif /* RESOURCECACHE_H_ */
diff --git a/service/basis/resourceCache/src/CacheHandler.cpp b/service/basis/resourceCache/src/CacheHandler.cpp
new file mode 100644 (file)
index 0000000..5d7d3a7
--- /dev/null
@@ -0,0 +1,187 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include "CacheHandler.h"
+
+#include <memory>
+#include <cstdlib>
+#include <functional>
+#include <map>
+#include <utility>
+#include <ctime>
+
+#include "OCApi.h"
+
+CacheHandler::CacheHandler(
+            ServiceResource & pResource,
+            CacheCB func,
+            REPORT_FREQUENCY rf,
+            long repeatTime
+            ):sResource(pResource)
+{
+    subscriberList = new SubscriberInfo();
+    data = new(CacheData());
+
+    state = CACHE_STATE::READY_YET;
+    updateTime = 0l;
+
+    pObserveCB = (ObserveCB)(std::bind(&CacheHandler::onObserve, this,
+            std::placeholders::_1, std::placeholders::_2,
+            std::placeholders::_3, std::placeholders::_4));
+    pGetCB = (GetCB)(std::bind(&CacheHandler::onGet, this,
+            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
+
+    if(pResource.isObservable())
+    {
+        pResource.requestObserve(pObserveCB);
+    }
+    else
+    {
+        // TODO set timer
+    }
+}
+
+CacheHandler::~CacheHandler()
+{
+    // TODO Auto-generated destructor stub
+    data.reset();
+
+    // TODO delete all node!!
+    subscriberList->clear();
+    delete subscriberList;
+}
+
+CacheID CacheHandler::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime)
+{
+    Report_Info newItem;
+    newItem.rf = rf;
+    newItem.latestReportTime = 0l;
+    newItem.repeatTime = repeatTime;
+
+    srand(time(NULL));
+    newItem.reportID = rand();
+
+    while(1)
+    {
+        if(findSubscriber(newItem.reportID) != nullptr || newItem.reportID == 0)
+        {
+            newItem.reportID = rand();
+        }
+        else
+        {
+            break;
+        }
+    }
+
+    subscriberList->insert(SubscriberInfoPair(newItem, func));
+
+    return newItem.reportID;
+}
+
+CacheID CacheHandler::deleteSubscriber(CacheID id)
+{
+    CacheID ret = 0;
+
+    SubscriberInfoPair pair = findSubscriber(id);
+    if(pair != nullptr)
+    {
+        ret = pair.first.reportID;
+        subscriberList->erase(pair.first);
+    }
+
+    return ret;
+}
+
+SubscriberInfoPair CacheHandler::findSubscriber(CacheID id)
+{
+    SubscriberInfoPair ret = nullptr;
+
+    for(auto i : subscriberList)
+    {
+        if(i->first.reportID == id)
+        {
+            ret = i;
+        }
+    }
+
+    return ret;
+}
+
+std::shared_ptr<CacheData> CacheHandler::getCachedData()
+{
+    if(state != CACHE_STATE::READY)
+    {
+        return NULL;
+    }
+    return data;
+}
+
+ServiceResource * CacheHandler::getServiceResource()
+{
+    ServiceResource ret = NULL;
+    if(sResource)
+    {
+        ret = sResource;
+    }
+
+    return ret;
+}
+
+void CacheHandler::onObserve(
+        const HeaderOptions& ho, const ResponseStatement& _rep, int _result, int _seq)
+{
+
+    if(_result != OC_STACK_OK)
+    {
+        // TODO handle error
+        return;
+    }
+//    if(_rep.empty() || _rep.emptyData())
+    if(_rep.getAttributes().isEmpty())
+    {
+        return;
+    }
+
+    ResourceAttributes att = _rep.getAttributes();
+
+    // set data
+    data->clear();
+    OC::OCRepresentation::iterator it = att.begin();
+    for(; att.end(); ++it)
+    {
+        std::string key = it->attrname();
+        // TODO change template or variant
+        std::string val = it->getValueToString();
+
+        data[key] = val;
+    }
+
+    // notify!!
+    std::map::iterator mapiter = subscriberList->begin();
+    for(; subscriberList->end(); ++mapiter)
+    {
+        if(mapiter->first().rf == REPORT_FREQUENCY::UPTODATE)
+        {
+            // TODO update report time
+//            mapiter->first().latestReportTime = now;
+            mapiter->second()(this->sResource, data);
+        }
+    }
+}
diff --git a/service/basis/resourceCache/src/ResourceCache.cpp b/service/basis/resourceCache/src/ResourceCache.cpp
new file mode 100644 (file)
index 0000000..01885e9
--- /dev/null
@@ -0,0 +1,145 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include "ResourceCache.h"
+
+ResourceCache * ResourceCache::s_instance = NULL;
+std::mutex ResourceCache::s_mutexForCreation;
+std::list< CacheHandler * > * ResourceCache::s_cacheDataList = NULL;
+
+ResourceCache::ResourceCache()
+{
+    // TODO Auto-generated constructor stub
+    if(!s_cacheDataList)
+    {
+        s_cacheDataList = new std::list< CacheHandler * >();
+    }
+}
+
+ResourceCache::~ResourceCache()
+{
+    // TODO Auto-generated destructor stub
+    if(s_cacheDataList)
+    {
+        s_cacheDataList->clear();
+        delete s_cacheDataList;
+    }
+}
+
+
+ResourceCache * ResourceCache::getInstance()
+{
+    if(!s_instance)
+    {
+        s_mutexForCreation.lock();
+        if(!s_instance)
+        {
+            s_instance = new ResourceCache();
+        }
+        s_mutexForCreation.unlock();
+    }
+    return s_instance;
+}
+
+CacheID ResourceCache::requestResourceCache(
+        ServiceResource & pResource, CacheCB func, REPORT_FREQUENCY rf, long reportTime)
+{
+    CacheID ret = 0;
+
+    if(rt == NULL || pResource == NULL)
+    {
+        return ret;
+    }
+
+    if(rt != REPORT_FREQUENCY::NONE)
+    {
+        if(func == NULL)
+        {
+            return ret;
+        }
+        if(!reportTime)
+        {
+            // default setting
+            reportTime = 30;
+        }
+    }
+
+    CacheHandler * newHandler = findCacheHandler(pResource);
+    if(newHandler == nullptr)
+    {
+        CacheHandler * newHandler = new CacheHandler(pResource, func, rf, reportTime);
+        s_cacheDataList->push_back(newHandler);
+    }
+    ret = newHandler->addSubscriber(func, rf, reportTime);
+
+    return ret;
+}
+
+OCStackResult ResourceCache::cancelResourceCache(ServiceResource & pResource, CacheID id)
+{
+    OCStackResult ret = OC_STACK_ERROR;
+
+    if(pResource == NULL || id == 0)
+    {
+        return ret;
+    }
+
+    // TODO cancel cache
+    CacheID retID = 0;
+    CacheHandler * deleteCacheHandler = findCacheHandler(pResource);
+    if(deleteCacheHandler == nullptr)
+    {
+        return ret;
+    }
+    else
+    {
+        retID = deleteCacheHandler->deleteSubscriber(id);
+    }
+
+    if(retID == id)
+    {
+        ret = OC_STACK_OK;
+    }
+
+    return ret;
+}
+
+CacheHandler * ResourceCache::findCacheHandler(ServiceResource & pResource)
+{
+    CacheHandler * retHandler = nullptr;
+    for (auto i : s_cacheDataList)
+    {
+        if(i->getServiceResource() == pResource)
+        {
+            retHandler = i;
+            break;
+        }
+    }
+    return retHandler;
+}
+
+OCStackResult ResourceCache::updateResourceCache(ServiceResource & pResource)
+{
+    OCStackResult ret = OC_STACK_ERROR;
+
+    // TODO update now (request get)
+
+    return ret;
+}