Commit 7b9ca77b authored by KIM JungYong's avatar KIM JungYong Committed by Uze Choi

Modify RE cache callback function.

https://jira.iotivity.org/browse/IOT-2093

RE cache callback was not report error and error code

In this patch,
1. RE cache callback was changed to call on error including error code.
  std::function< void(const RCSResourceAttributes& attrs, int eCode) >
  CacheUpdatedCallback;
2. Related unit test and sample was updated.
3. Related other service was updated.

Change-Id: Iafb16d3025c491c45e70bbe347ce72b61a82aae2
Signed-off-by: default avatarKIM JungYong <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/19109Reviewed-by: default avatarUze Choi <uzchoi@samsung.com>
Tested-by: default avatarUze Choi <uzchoi@samsung.com>
parent fd60586e
......@@ -30,7 +30,7 @@ RemoteResourceUnit::RemoteResourceUnit()
pStateChangedCB = std::bind(&RemoteResourceUnit::stateChangedCB, this,
std::placeholders::_1);
pCacheUpdateCB = std::bind(&RemoteResourceUnit::cacheUpdateCB, this,
std::placeholders::_1);
std::placeholders::_1, std::placeholders::_2);
}
RemoteResourceUnit::~RemoteResourceUnit()
......@@ -115,7 +115,7 @@ void RemoteResourceUnit::stateChangedCB(ResourceState changedState) const
}
}
void RemoteResourceUnit::cacheUpdateCB(const RCSResourceAttributes & updatedAtt) const
void RemoteResourceUnit::cacheUpdateCB(const RCSResourceAttributes & updatedAtt, int) const
{
std::lock_guard<std::mutex> lock(m_mutex);
(void)updatedAtt;
......
......@@ -54,7 +54,7 @@ namespace OIC
RCSRemoteResourceObject::CacheUpdatedCallback pCacheUpdateCB;
void stateChangedCB(ResourceState changedState) const;
void cacheUpdateCB(const RCSResourceAttributes & updatedAtt) const;
void cacheUpdateCB(const RCSResourceAttributes & updatedAtt, int) const;
public:
static RemoteResourceUnit::Ptr createRemoteResourceInfo(
......
......@@ -735,7 +735,7 @@ TEST_F(DiscoverResourceUnitTest, onUpdateCalled)
namespace
{
void onCacheCB(const RCSResourceAttributes &)
void onCacheCB(const RCSResourceAttributes &, int)
{
}
}
......@@ -814,7 +814,7 @@ TEST_F(RemoteResourceUnitTest, onCacheCBCalled)
{
bool isCalled = false;
mocks.ExpectCallFunc(onCacheCB).Do(
[this, &isCalled](const RCSResourceAttributes &)
[this, &isCalled](const RCSResourceAttributes &, int)
{
isCalled = true;
});
......
......@@ -148,7 +148,7 @@ public final class RcsRemoteResourceObject extends RcsObject {
* the updated attributes
*
*/
public void onCacheUpdated(RcsResourceAttributes attributes);
public void onCacheUpdated(RcsResourceAttributes attributes, int eCode);
}
......
......@@ -135,7 +135,8 @@ namespace
}
}
void onCacheUpdated(const RCSResourceAttributes& attrs, const JavaGlobalRef& listener)
void onCacheUpdated(const RCSResourceAttributes& attrs, int eCode,
const JavaGlobalRef& listener)
{
LOGD("onCacheUpdated");
......@@ -186,7 +187,7 @@ void initRCSRemoteResourceObject(JNIEnvWrapper* env)
auto clsOnCacheUpdatedListener = env->FindClass(CLS_NAME_ON_CACHE_UPDATED_LISTENER);
g_method_onCacheUpdated = env->GetMethodID(clsOnCacheUpdatedListener, "onCacheUpdated",
"(" AS_SIG(CLS_NAME_RESOURCEATTRIBUTES) ")V");
"(" AS_SIG(CLS_NAME_RESOURCEATTRIBUTES) "I)V");
auto clsOnRemoteAttributesReceivedListener =
env->FindClass(CLS_NAME_ON_REMOTE_ATTRIBUTES_RECEIVED_LISTENER);
......@@ -341,7 +342,8 @@ Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeStartCaching
if (listener)
{
res->startCaching(std::bind(onCacheUpdated,
std::placeholders::_1, JavaGlobalRef{ env, listener }));
std::placeholders::_1, std::placeholders::_2,
JavaGlobalRef{ env, listener }));
}
else
{
......
......@@ -112,7 +112,7 @@ public class ResourceClientActivity extends Activity
private OnCacheUpdatedListener mOnCacheUpdatedListener = new OnCacheUpdatedListener() {
@Override
public void onCacheUpdated(RcsResourceAttributes attrs) {
public void onCacheUpdated(RcsResourceAttributes attrs, int eCode) {
Log.i(LOG_TAG, "onCacheUpdated");
mHandler.obtainMessage(MSG_ID_ATTRIBUTE_RECEIVED, attrs)
......
......@@ -270,9 +270,9 @@ void onResourceStateChanged(ResourceState resourceState)
}
}
void onCacheUpdated(const RCSResourceAttributes& attributes)
void onCacheUpdated(const RCSResourceAttributes& attributes, int eCode)
{
std::cout << "onCacheUpdated callback" << std::endl;
std::cout << "onCacheUpdated callback : " << eCode << std::endl;
printAttributes(attributes);
}
......
......@@ -127,7 +127,7 @@ void onResourceStateChanged(const ResourceState &resourceState)
&logMessage);
}
void onCacheUpdated(const RCSResourceAttributes &attributes)
void onCacheUpdated(const RCSResourceAttributes &attributes, int)
{
dlog_print(DLOG_INFO, LOG_TAG, "#### onCacheUpdated callback");
......@@ -277,7 +277,7 @@ static void setAttributeToRemoteServer(int setValue)
&logMessage);
}
static void startCaching(std::function <void (const RCSResourceAttributes &)>cb)
static void startCaching(std::function <void (const RCSResourceAttributes &, int)>cb)
{
string logMessage = "";
......
......@@ -187,7 +187,8 @@ namespace OIC
*
* @param attrs the updated attributes
*/
typedef std::function< void(const RCSResourceAttributes& attrs) > CacheUpdatedCallback;
typedef std::function< void(const RCSResourceAttributes& attrs, int eCode) >
CacheUpdatedCallback;
/**
* Callback definition to be invoked when the response of getRemoteAttributes is
......
......@@ -82,7 +82,7 @@ namespace OIC
typedef int CacheID;
typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>,
const RCSResourceAttributes &)> CacheCB;
const RCSResourceAttributes &, int eCode)> CacheCB;
typedef std::map<int, std::pair<Report_Info, CacheCB>> SubscriberInfo;
typedef std::pair<int, std::pair<Report_Info, CacheCB>> SubscriberInfoPair;
......
......@@ -98,7 +98,7 @@ namespace OIC
CacheID generateCacheID();
SubscriberInfoPair findSubscriber(CacheID id);
void notifyObservers(const RCSResourceAttributes Att);
void notifyObservers(const RCSResourceAttributes Att, int eCode);
};
} // namespace Service
} // namespace OIC
......
......@@ -39,7 +39,7 @@ namespace OIC
{
public:
typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>,
const RCSResourceAttributes &)> DataCacheCB;
const RCSResourceAttributes &, int)> DataCacheCB;
typedef std::shared_ptr<ObserveCache> Ptr;
public:
......
......@@ -208,14 +208,7 @@ namespace OIC
const ResponseStatement &_rep, int _result, unsigned int _seq)
{
if (_result != OC_STACK_OK || _rep.getAttributes().empty() || lastSequenceNum > _seq)
{
return;
}
else
{
lastSequenceNum = _seq;
}
if (state != CACHE_STATE::READY)
{
......@@ -231,7 +224,7 @@ namespace OIC
networkTimer.cancel(networkTimeOutHandle);
networkTimeOutHandle = networkTimer.post(CACHE_DEFAULT_EXPIRED_MILLITIME, pTimerCB);
notifyObservers(_rep.getAttributes());
notifyObservers(_rep.getAttributes(), _result);
}
void DataCache::onGet(const HeaderOptions & /*_hos*/,
......@@ -257,10 +250,10 @@ namespace OIC
pollingHandle = pollingTimer.post(CACHE_DEFAULT_REPORT_MILLITIME, pPollingCB);
}
notifyObservers(_rep.getAttributes());
notifyObservers(_rep.getAttributes(), _result);
}
void DataCache::notifyObservers(const RCSResourceAttributes Att)
void DataCache::notifyObservers(const RCSResourceAttributes Att, int eCode)
{
{
std::lock_guard<std::mutex> lock(att_mutex);
......@@ -276,7 +269,7 @@ namespace OIC
{
if (i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
{
i.second.second(this->sResource, Att);
i.second.second(this->sResource, Att, eCode);
}
}
}
......
......@@ -102,22 +102,18 @@ namespace OIC
void ObserveCache::onObserve(const HeaderOptions &,
const ResponseStatement & rep, int _result, unsigned int)
{
if (!convertOCResultToSuccess((OCStackResult)_result))
{
return;
}
m_state = CACHE_STATE::READY;
if (m_attributes == rep.getAttributes())
if (m_attributes == rep.getAttributes() &&
convertOCResultToSuccess((OCStackResult)_result))
{
return ;
}
m_attributes = rep.getAttributes();
if (m_reportCB)
{
m_reportCB(m_wpResource.lock(), m_attributes);
m_attributes = rep.getAttributes();
m_reportCB(m_wpResource.lock(), m_attributes, _result);
}
}
......
......@@ -74,7 +74,8 @@ class DataCacheTest : public TestWithMock
mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(false);
cacheHandler.reset(new DataCache());
cb = ([](std::shared_ptr<PrimitiveResource >, const RCSResourceAttributes &)->OCStackResult
cb = ([](std::shared_ptr<PrimitiveResource >,
const RCSResourceAttributes &, int) -> OCStackResult
{
return OC_STACK_OK;
});
......
......@@ -51,7 +51,8 @@ class ResourceCacheManagerTest : public TestWithMock
pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), deleter);
});
mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(false);
cb = ([](std::shared_ptr<PrimitiveResource >, const RCSResourceAttributes &)->OCStackResult
cb = ([](std::shared_ptr<PrimitiveResource >,
const RCSResourceAttributes &, int) -> OCStackResult
{
return OC_STACK_OK;
});
......
......@@ -92,12 +92,12 @@ namespace
}
OCStackResult cachingCallback(std::shared_ptr< PrimitiveResource >,
const RCSResourceAttributes& data,
const RCSResourceAttributes& data, int eCode,
RCSRemoteResourceObject::CacheUpdatedCallback onCacheUpdated)
{
SCOPE_LOG_F(DEBUG, TAG);
onCacheUpdated(data);
onCacheUpdated(data, eCode);
return OC_STACK_OK;
}
......@@ -299,7 +299,8 @@ namespace OIC
{
m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
m_primitiveResource,
std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
std::bind(cachingCallback, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3,
std::move(cb)), CACHE_METHOD::OBSERVE_ONLY,
REPORT_FREQUENCY::UPTODATE, 0);
}
......@@ -308,7 +309,8 @@ namespace OIC
{
m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
m_primitiveResource,
std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
std::bind(cachingCallback, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3,
std::move(cb)), CACHE_METHOD::ITERATED_GET,
REPORT_FREQUENCY::UPTODATE, 0);
}
......
......@@ -47,7 +47,7 @@ void getRemoteAttributesCallback(const RCSResourceAttributes&, int) {}
void setRemoteAttributesCallback(const RCSResourceAttributes&, int) {}
void setRemoteRepresentationCallback(const HeaderOpts&, const RCSRepresentation&, int) {}
void resourceStateChanged(ResourceState) { }
void cacheUpdatedCallback(const RCSResourceAttributes&) {}
void cacheUpdatedCallback(const RCSResourceAttributes&, int) {}
class RemoteResourceObjectTest: public TestWithMock
{
......@@ -294,7 +294,7 @@ TEST_F(RemoteResourceObjectTest, CacheStateIsUnreadyAfterStartCaching)
TEST_F(RemoteResourceObjectTest, CacheStateIsReadyAfterCacheUpdated)
{
mocks.ExpectCallFunc(cacheUpdatedCallback).
Do([this](const RCSResourceAttributes&){ Proceed(); });
Do([this](const RCSResourceAttributes&, int){ Proceed(); });
object->startCaching(cacheUpdatedCallback);
Wait();
......@@ -305,7 +305,7 @@ TEST_F(RemoteResourceObjectTest, CacheStateIsReadyAfterCacheUpdated)
TEST_F(RemoteResourceObjectTest, IsCachedAvailableReturnsTrueWhenCacheIsReady)
{
mocks.ExpectCallFunc(cacheUpdatedCallback).
Do([this](const RCSResourceAttributes&){ Proceed(); });
Do([this](const RCSResourceAttributes&, int){ Proceed(); });
object->startCaching(cacheUpdatedCallback);
Wait();
......@@ -316,12 +316,12 @@ TEST_F(RemoteResourceObjectTest, IsCachedAvailableReturnsTrueWhenCacheIsReady)
TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWheneverCacheUpdated)
{
mocks.OnCallFunc(cacheUpdatedCallback).
Do([this](const RCSResourceAttributes&){ Proceed(); });
Do([this](const RCSResourceAttributes&, int){ Proceed(); });
object->startCaching(cacheUpdatedCallback);
Wait();
mocks.ExpectCallFunc(cacheUpdatedCallback).
Do([this](const RCSResourceAttributes&){ Proceed(); });
Do([this](const RCSResourceAttributes&, int){ Proceed(); });
server->setAttribute(ATTR_KEY, ATTR_VALUE + 1);
......@@ -333,15 +333,15 @@ TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWithUpdate
constexpr int newValue = ATTR_VALUE + 1;
mocks.OnCallFunc(cacheUpdatedCallback).
Do([this](const RCSResourceAttributes&){ Proceed(); });
Do([this](const RCSResourceAttributes&, int){ Proceed(); });
object->startCaching(cacheUpdatedCallback);
Wait();
mocks.ExpectCallFunc(cacheUpdatedCallback).
Match([this](const RCSResourceAttributes& attrs){
Match([this](const RCSResourceAttributes& attrs, int){
return attrs.at(ATTR_KEY) == newValue;
}).
Do([this](const RCSResourceAttributes&){ Proceed(); });
Do([this](const RCSResourceAttributes&, int){ Proceed(); });
server->setAttribute(ATTR_KEY, newValue);
......@@ -356,7 +356,7 @@ TEST_F(RemoteResourceObjectTest, GetCachedAttributesThrowsIfCachingIsNotStarted)
TEST_F(RemoteResourceObjectTest, CachedAttributesHasSameAttributesWithServer)
{
mocks.OnCallFunc(cacheUpdatedCallback).
Do([this](const RCSResourceAttributes&){ Proceed(); });
Do([this](const RCSResourceAttributes&, int){ Proceed(); });
object->startCaching(cacheUpdatedCallback);
Wait();
......@@ -373,7 +373,7 @@ TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfCachingIsNotStarted)
TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfKeyIsInvalid)
{
mocks.OnCallFunc(cacheUpdatedCallback).
Do([this](const RCSResourceAttributes&){ Proceed(); });
Do([this](const RCSResourceAttributes&, int){ Proceed(); });
object->startCaching(cacheUpdatedCallback);
Wait();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment