fix build error and syntax error.
arrange the pointer.
fix ResponseStatement's error.
Change-Id: Iea690231a9c139867c80eed6dd63b3033285836a
Signed-off-by: jyong2.kim <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1230
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
#ifndef __RESPONSESTATEMENT_H\r
#define __RESPONSESTATEMENT_H\r
\r
+#include <ResourceAttributes.h>\r
+\r
/**\r
* TODO : design for future flexibility\r
*/\r
std::vector<std::string> getResourceTypes() const;\r
std::vector<std::string> getResourceInterfaces() const;\r
\r
- ResourceAttributes getAttributes() const;\r
+ ResourceAttributes getAttributes() const{ return ResourceAttributes(); }\r
\r
~ResponseStatement() {}\r
};\r
--- /dev/null
+#******************************************************************
+#
+# 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.
+#
+#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+##
+# ResourceCache build script
+##
+
+Import('env')
+
+if env.get('RELEASE'):
+ env.AppendUnique(CCFLAGS = ['-Os'])
+ env.AppendUnique(CPPDEFINES = ['NDEBUG'])
+else:
+ env.AppendUnique(CCFLAGS = ['-g'])
+
+if env.get('LOGGING'):
+ env.AppendUnique(CPPDEFINES = ['TB_LOG'])
+
+lib_env = env.Clone()
+SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')
+resourcecache_env = lib_env.Clone()
+
+target_os = env.get('TARGET_OS')
+######################################################################
+# Build flags
+######################################################################
+resourcecache_env.AppendUnique(CPPPATH = ['include'])
+resourcecache_env.AppendUnique(CPPPATH = ['../common/primitiveResource/include'])
+resourcecache_env.PrependUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'libcoap'])
+
+if target_os not in ['windows', 'winrt']:
+ resourcecache_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++11'])
+
+if target_os == 'linux':
+ resourcecache_env.AppendUnique(LIBS = ['pthread'])
+
+######################################################################
+# Source files and Targets
+######################################################################
+CACHE_SRC_DIR = 'src/'
+resourcecache_src = [
+ CACHE_SRC_DIR + 'DataCache.cpp',
+ CACHE_SRC_DIR + 'ResourceCacheManager.cpp',
+ CACHE_SRC_DIR + '../../common/primitiveResource/src/PrimitiveResource.cpp',
+ CACHE_SRC_DIR + '../../common/primitiveResource/src/ResourceAttributes.cpp'
+ ]
+
+if target_os in ['tizen','android'] :
+ resourcecachesdk = resourcecache_env.SharedLibrary('ResourceCache', resourcecache_src)
+else :
+ resourcecachesdk = resourcecache_env.StaticLibrary('ResourceCache', resourcecache_src)
+
+resourcecache_env.InstallTarget(resourcecachesdk, 'libResouceCache')
+
+# Go to build sample apps
+#SConscript('SampleApp/SConscript')
+
#define CACHE_TAG PCF("CACHE")
+class DataCache;
+
enum class REPORT_FREQUENCY
{
NONE = 0,
READY_YET,
LOST_SIGNAL,
DESTROYED,
- UPDATING
+ UPDATING,
+ NONE
};
typedef int CacheID;
-typedef std::map<std::string, std::string> CacheData;
-typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>, CacheData)> CacheCB;
-typedef std::map<Report_Info, CacheCB> SubscriberInfo;
-typedef std::pair<Report_Info, CacheCB> SubscriberInfoPair;
+typedef std::map<std::string, std::string> CachedData;
+typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>, std::shared_ptr<CachedData>)> CacheCB;
+typedef std::map<int, std::pair<Report_Info, CacheCB>> SubscriberInfo;
+typedef std::pair<int, 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;
+typedef std::shared_ptr<DataCache> DataCachePtr;
+typedef std::shared_ptr<CachedData> CachedDataPtr;
+typedef std::shared_ptr<PrimitiveResource> PrimitiveResourcePtr;
+
#endif
#define DATACACHE_H_
#include <list>
-#include <memory>
#include <string>
#include <boost/progress.hpp>
{
public:
DataCache(
- PrimitiveResource & pResource,
+ PrimitiveResourcePtr pResource,
CacheCB func,
REPORT_FREQUENCY rf,
long repeatTime);
CacheID addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime);
CacheID deleteSubscriber(CacheID id);
- std::shared_ptr<CacheData> getCachedData();
- PrimitiveResource * getPrimitiveResource();
+ CachedDataPtr getCachedData();
+ CACHE_STATE getCacheState();
+ PrimitiveResourcePtr getPrimitiveResource();
+
+ SubscriberInfoPair findSubscriber(CacheID id);
private:
// origin resource info
std::string address;
// resource instance
- PrimitiveResource *sResource;
+ PrimitiveResourcePtr sResource;
std::shared_ptr<BaseResource> baseHandler;
- std::shared_ptr<ResourceAttributes> attributes;
-
// cached data info
- std::shared_ptr<CacheData> data;
+ CachedDataPtr data;
+ std::shared_ptr<ResourceAttributes> attributes;
long updateTime;
CACHE_STATE state;
// subscriber info
- SubscriberInfo *subscriberList;
- SubscriberInfoPair findSubscriber(CacheID id);
+ std::unique_ptr<SubscriberInfo> subscriberList;
// for requestCB from base
void onObserve(const HeaderOptions& _hos,
ObserveCB pObserveCB;
GetCB pGetCB;
- //
OCStackResult updateCacheData();
// for timer
class ResourceCacheManager
{
public:
- ResourceCacheManager * getInstance();
+ static ResourceCacheManager * getInstance();
CacheID requestResourceCache(
- PrimitiveResource & pResource,
+ PrimitiveResourcePtr pResource,
CacheCB func = NULL, REPORT_FREQUENCY rf = REPORT_FREQUENCY::NONE, long time = 0l);
-// OCStackResult cancelResourceCache(std::string address, std::string uri);
- OCStackResult cancelResourceCache(PrimitiveResource & pResource, CacheID id);
+ OCStackResult cancelResourceCache(PrimitiveResourcePtr pResource, CacheID id);
-// OCStackResult updateResourceCache(std::string address, std::string uri);
- OCStackResult updateResourceCache(PrimitiveResource & pResource);
+ OCStackResult updateResourceCache(PrimitiveResourcePtr pResource);
-// OCStackResult getResourceCache(std::string address, std::string uri);
- OCStackResult getResourceCache(PrimitiveResource & pResource);
+ CachedDataPtr getCachedData(PrimitiveResourcePtr pResource);
+ CachedDataPtr getCachedData(CacheID id);
+ CACHE_STATE getResourceCacheState(PrimitiveResourcePtr pResource);
+ CACHE_STATE getResourceCacheState(CacheID id);
+
+ ~ResourceCacheManager();
private:
ResourceCacheManager();
- ~ResourceCacheManager();
static ResourceCacheManager * s_instance;
static std::mutex s_mutexForCreation;
- static std::list< DataCache * > * s_cacheDataList;
+ static std::unique_ptr<std::list<DataCachePtr>> s_cacheDataList;
- DataCache * findCacheHandler(PrimitiveResource & pResource);
+ DataCachePtr findDataCache(PrimitiveResourcePtr pResource);
+ DataCachePtr findDataCache(CacheID id);
};
#endif /* RESOURCECACHEMANAGER_H_ */
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#include "CacheHandler.h"
-
#include <memory>
#include <cstdlib>
#include <functional>
#include "OCApi.h"
+#include "DataCache.h"
+
+#include "ResponseStatement.h"
+#include "ResourceAttributes.h"
+
DataCache::DataCache(
- PrimitiveResource & pResource,
+ PrimitiveResourcePtr pResource,
CacheCB func,
REPORT_FREQUENCY rf,
long repeatTime
):sResource(pResource)
{
- subscriberList = new SubscriberInfo();
- data = new CacheData();
+ subscriberList = std::unique_ptr<SubscriberInfo>(new SubscriberInfo());
+ data = std::make_shared<CachedData>();
state = CACHE_STATE::READY_YET;
updateTime = 0l;
pGetCB = (GetCB)(std::bind(&DataCache::onGet, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
- if(pResource.isObservable())
+ if(pResource->isObservable())
{
- pResource.requestObserve(pObserveCB);
+ pResource->requestObserve(pObserveCB);
}
else
{
// TODO delete all node!!
subscriberList->clear();
- delete subscriberList;
+ subscriberList.release();
}
CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime)
while(1)
{
- if(findSubscriber(newItem.reportID) != nullptr || newItem.reportID == 0)
+ if(findSubscriber(newItem.reportID).first == 0 || newItem.reportID == 0)
{
newItem.reportID = rand();
}
}
}
- subscriberList->insert(SubscriberInfoPair(newItem, func));
+ subscriberList->insert(std::make_pair(newItem.reportID, std::make_pair(newItem, func)));
return newItem.reportID;
}
CacheID ret = 0;
SubscriberInfoPair pair = findSubscriber(id);
- if(pair != nullptr)
+ if(pair.first != 0)
{
- ret = pair.first.reportID;
+ ret = pair.first;
subscriberList->erase(pair.first);
}
SubscriberInfoPair DataCache::findSubscriber(CacheID id)
{
- SubscriberInfoPair ret = nullptr;
+ SubscriberInfoPair ret;
- for(auto i : subscriberList)
+ for(auto & i : *subscriberList)
{
- if(i->first.reportID == id)
+ if(i.first == id)
{
- ret = i;
+ ret = std::make_pair(i.first, std::make_pair((Report_Info)i.second.first, (CacheCB)i.second.second));
}
}
return ret;
}
-std::shared_ptr<CacheData> DataCache::getCachedData()
+CachedDataPtr DataCache::getCachedData()
{
if(state != CACHE_STATE::READY)
{
- return NULL;
+ return nullptr;
}
return data;
}
-PrimitiveResource * DataCache::getPrimitiveResource()
+std::shared_ptr<PrimitiveResource> DataCache::getPrimitiveResource()
{
- PrimitiveResource ret = NULL;
- if(sResource)
- {
- ret = sResource;
- }
-
- return ret;
+ return sResource;
}
void DataCache::onObserve(
// TODO handle error
return;
}
- if(_rep.getAttributes().isEmpty())
+
+ if(_rep.getAttributes().empty())
{
return;
}
// set data
data->clear();
- ResourceAttributes::iterator it = att.begin();
- for(; att.end(); ++it)
- {
- std::string key = it->key();
- // TODO change template or variant
- std::string val = it->value();
- data[key] = val;
+
+ for(auto & i : att)
+ {
+ const std::string &key = i.key();
+// std::string value = i.value();
+ std::string val;
+ data->insert(CachedData::value_type(key, val));
}
// notify!!
- std::map::iterator mapiter = subscriberList->begin();
- for(; subscriberList->end(); ++mapiter)
+
+ for(auto & i : * subscriberList)
{
- if(mapiter->first().rf == REPORT_FREQUENCY::UPTODATE)
+ if(i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
{
- // TODO update report time
-// mapiter->first().latestReportTime = now;
- mapiter->second()(this->sResource, data);
+ i.second.second(this->sResource, data);
}
}
}
+
+void DataCache::onGet(const HeaderOptions& _hos,
+ const ResponseStatement& _rep, int _result)
+{
+
+}
+
+CACHE_STATE DataCache::getCacheState()
+{
+ return state;
+}
ResourceCacheManager * ResourceCacheManager::s_instance = NULL;
std::mutex ResourceCacheManager::s_mutexForCreation;
-std::list< DataCache * > * ResourceCacheManager::s_cacheDataList = NULL;
+std::unique_ptr<std::list<DataCachePtr>> ResourceCacheManager::s_cacheDataList(nullptr);
ResourceCacheManager::ResourceCacheManager()
{
// TODO Auto-generated constructor stub
if(!s_cacheDataList)
{
- s_cacheDataList = new std::list< DataCache * >();
+ s_cacheDataList = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
}
}
if(s_cacheDataList)
{
s_cacheDataList->clear();
- delete s_cacheDataList;
}
}
ResourceCacheManager * ResourceCacheManager::getInstance()
{
- if(!s_instance)
+ if(s_instance == nullptr)
{
s_mutexForCreation.lock();
- if(!s_instance)
+ if(s_instance == nullptr)
{
- s_instance = new ResourceCacheManager();
+ s_instance = new ResourceCacheManager();;
}
s_mutexForCreation.unlock();
}
}
CacheID ResourceCacheManager::requestResourceCache(
- PrimitiveResource & pResource, CacheCB func, REPORT_FREQUENCY rf, long reportTime)
+ PrimitiveResourcePtr pResource, CacheCB func,
+ REPORT_FREQUENCY rf, long reportTime)
{
CacheID ret = 0;
- if(rt == NULL || pResource == NULL)
- {
- return ret;
- }
-
- if(rt != REPORT_FREQUENCY::NONE)
+ if(rf != REPORT_FREQUENCY::NONE)
{
if(func == NULL)
{
}
}
- DataCache * newHandler = findCacheHandler(pResource);
+ DataCachePtr newHandler = findDataCache(pResource);
if(newHandler == nullptr)
{
- DataCache * newHandler = new DataCache(pResource, func, rf, reportTime);
+ DataCachePtr newHandler = std::make_shared<DataCache>(pResource, func, rf, reportTime);
s_cacheDataList->push_back(newHandler);
}
ret = newHandler->addSubscriber(func, rf, reportTime);
return ret;
}
-OCStackResult ResourceCacheManager::cancelResourceCache(PrimitiveResource & pResource, CacheID id)
+OCStackResult ResourceCacheManager::cancelResourceCache(PrimitiveResourcePtr pResource, CacheID id)
{
OCStackResult ret = OC_STACK_ERROR;
- if(pResource == NULL || id == 0)
+ if(id == 0)
{
return ret;
}
// TODO cancel cache
CacheID retID = 0;
- DataCache * deleteCacheHandler = findCacheHandler(pResource);
+ DataCachePtr deleteCacheHandler = findDataCache(pResource);
if(deleteCacheHandler == nullptr)
{
return ret;
return ret;
}
-DataCache * ResourceCacheManager::findCacheHandler(PrimitiveResource & pResource)
+DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource)
{
- DataCache * retHandler = nullptr;
- for (auto i : s_cacheDataList)
+ DataCachePtr retHandler = nullptr;
+ for (auto & i : * s_cacheDataList)
{
- if(i->getPrimitiveResource() == pResource)
+ if(i->getPrimitiveResource()->getUri() == pResource->getUri() &&
+ i->getPrimitiveResource()->getHost() == pResource->getHost())
{
retHandler = i;
break;
return retHandler;
}
-OCStackResult ResourceCacheManager::updateResourceCache(PrimitiveResource & pResource)
+DataCachePtr ResourceCacheManager::findDataCache(CacheID id)
+{
+ DataCachePtr retHandler = nullptr;
+ for (auto & i : * s_cacheDataList)
+ {
+ SubscriberInfoPair pair = i->findSubscriber(id);
+ if(pair.first != 0)
+ {
+ retHandler = i;
+ break;
+ }
+ }
+ return retHandler;
+}
+
+OCStackResult ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource)
{
OCStackResult ret = OC_STACK_ERROR;
return ret;
}
+
+CachedDataPtr ResourceCacheManager::getCachedData(PrimitiveResourcePtr pResource)
+{
+ DataCachePtr handler = findDataCache(pResource);
+ if(handler == nullptr)
+ {
+ return nullptr;
+ }
+ return handler->getCachedData();
+}
+
+CachedDataPtr ResourceCacheManager::getCachedData(CacheID id)
+{
+ DataCachePtr handler = findDataCache(id);
+ if(handler == nullptr)
+ {
+ return nullptr;
+ }
+ return handler->getCachedData();
+}
+
+CACHE_STATE ResourceCacheManager::getResourceCacheState(PrimitiveResourcePtr pResource)
+{
+ DataCachePtr handler = findDataCache(pResource);
+ if(handler == nullptr)
+ {
+ return CACHE_STATE::NONE;
+ }
+ return handler->getCacheState();
+}
+CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id)
+{
+ DataCachePtr handler = findDataCache(id);
+ if(handler == nullptr)
+ {
+ return CACHE_STATE::NONE;
+ }
+ return handler->getCacheState();
+}