From 6b56d7a2cefb9ad595b0fdbab33486d56cb415aa Mon Sep 17 00:00:00 2001 From: "doil.kwon" Date: Mon, 29 Jun 2015 14:07:58 +0900 Subject: [PATCH] Added Supporting the logic about Non-Presence mode. - timeoutCB : will be recieved callback message within SAFE_TIME(5sec) - pollingCB : re-request 'get' to server and re-execute primitiveTimer that timeoutCB is registered. Re-Aligned about functions and variables list in ".h" file. Signed-off-by: doil.kwon Change-Id: I4a60d455314d7488567ca218fe7cc6114b370796 Reviewed-on: https://gerrit.iotivity.org/gerrit/1437 Tested-by: jenkins-iotivity Reviewed-by: JungYong KIM Reviewed-by: Uze Choi --- .../modules/resourceBroker/SConscript | 9 +- .../modules/resourceBroker/include/BrokerTypes.h | 8 +- .../resourceBroker/include/DeviceAssociation.h | 0 .../resourceBroker/include/DevicePresence.h | 13 +- .../resourceBroker/include/ResourceBroker.h | 9 +- .../resourceBroker/include/ResourcePresence.h | 33 +++-- .../resourceBroker/src/DeviceAssociation.cpp | 1 - .../modules/resourceBroker/src/DevicePresence.cpp | 10 +- .../modules/resourceBroker/src/ResourceBroker.cpp | 2 - .../resourceBroker/src/ResourcePresence.cpp | 153 +++++++++++++++------ 10 files changed, 151 insertions(+), 87 deletions(-) mode change 100644 => 100755 service/resource-manipulation/modules/resourceBroker/include/DeviceAssociation.h mode change 100644 => 100755 service/resource-manipulation/modules/resourceBroker/src/DeviceAssociation.cpp diff --git a/service/resource-manipulation/modules/resourceBroker/SConscript b/service/resource-manipulation/modules/resourceBroker/SConscript index 40b26eb..0158975 100755 --- a/service/resource-manipulation/modules/resourceBroker/SConscript +++ b/service/resource-manipulation/modules/resourceBroker/SConscript @@ -43,8 +43,9 @@ target_os = env.get('TARGET_OS') ###################################################################### resourcebroker_env.AppendUnique(CPPPATH = ['include']) resourcebroker_env.AppendUnique(CPPPATH = ['../common/primitiveResource/include']) +resourcebroker_env.AppendUnique(CPPPATH = ['../common/primitiveTimer/include']) resourcebroker_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')]) -resourcebroker_env.PrependUnique(LIBS = ['service_common','oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'libcoap']) +resourcebroker_env.PrependUnique(LIBS = ['service_common', 'oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'libcoap']) if target_os not in ['windows', 'winrt']: @@ -61,7 +62,9 @@ resourcebroker_src = [ BROKER_SRC_DIR + 'DeviceAssociation.cpp', BROKER_SRC_DIR + 'DevicePresence.cpp', BROKER_SRC_DIR + 'ResourcePresence.cpp', - BROKER_SRC_DIR + 'ResourceBroker.cpp' + BROKER_SRC_DIR + 'ResourceBroker.cpp', + BROKER_SRC_DIR + '../../common/primitiveTimer/src/PrimitiveTimer.cpp', + BROKER_SRC_DIR + '../../common/primitiveTimer/src/PrimitiveTimer_Impl.cpp' ] @@ -73,6 +76,6 @@ else : resourcebroker_env.InstallTarget(resourcebrokersdk, 'libResouceBroker') # Go to build sample apps -#SConscript('SampleApp/SConscript') +SConscript('SampleApp/SConscript') diff --git a/service/resource-manipulation/modules/resourceBroker/include/BrokerTypes.h b/service/resource-manipulation/modules/resourceBroker/include/BrokerTypes.h index 2f813e5..5c27a91 100755 --- a/service/resource-manipulation/modules/resourceBroker/include/BrokerTypes.h +++ b/service/resource-manipulation/modules/resourceBroker/include/BrokerTypes.h @@ -29,7 +29,7 @@ #include "PrimitiveResource.h" #define BROKER_TAG PCF("BROKER") - +#define SAFE_TIME (5l) #define BROKER_TRANSPORT OCConnectivityType::CT_ADAPTER_IP using namespace OIC::Service; @@ -96,8 +96,8 @@ typedef std::list< ResourcePresencePtr > PresenceList; struct BrokerCBResourcePair { - BrokerCBResourcePair(ResourcePresencePtr _pResource, BrokerCB _cb) - : pResource(_pResource), brokerCB(_cb){} + BrokerCBResourcePair(ResourcePresencePtr pResource, BrokerCB cb) + : pResource(pResource), brokerCB(cb){} ResourcePresencePtr pResource; BrokerCB brokerCB; }; @@ -107,6 +107,6 @@ typedef std::function SubscribeCB; typedef std::function RequestGetCB; -typedef std::function TimeoutCB; +typedef std::function TimeoutCB; #endif // BROKERTYPES_H_ diff --git a/service/resource-manipulation/modules/resourceBroker/include/DeviceAssociation.h b/service/resource-manipulation/modules/resourceBroker/include/DeviceAssociation.h old mode 100644 new mode 100755 diff --git a/service/resource-manipulation/modules/resourceBroker/include/DevicePresence.h b/service/resource-manipulation/modules/resourceBroker/include/DevicePresence.h index 19ebf56..e1c39c9 100755 --- a/service/resource-manipulation/modules/resourceBroker/include/DevicePresence.h +++ b/service/resource-manipulation/modules/resourceBroker/include/DevicePresence.h @@ -34,27 +34,28 @@ public: DevicePresence(PrimitiveResourcePtr pResource); ~DevicePresence(); - const std::string getAddress() const; void addPresenceResource(ResourcePresence * rPresence); void removePresenceResource(ResourcePresence * rPresence); bool isEmptyResourcePresence() const; + const std::string getAddress() const; private: + void requestAllResourcePresence(); + void subscribeCB(OCStackResult ret,const unsigned int seq, const std::string& Hostaddress); + void * timeOutCB(unsigned int msg); + + std::list resourcePresenceList; + std::string address; DEVICE_STATE state; bool isWithinTime; - std::list resourcePresenceList; - SubscribeCB pSubscribeRequestCB; TimeoutCB pTimeoutCB; PresenceSubscriber presenceSubscriber; - void requestAllResourcePresence(); - void subscribeCB(OCStackResult ret,const unsigned int seq, const std::string& Hostaddress); - void timeOutCB(int msg); }; diff --git a/service/resource-manipulation/modules/resourceBroker/include/ResourceBroker.h b/service/resource-manipulation/modules/resourceBroker/include/ResourceBroker.h index d2b9d49..c38d59f 100755 --- a/service/resource-manipulation/modules/resourceBroker/include/ResourceBroker.h +++ b/service/resource-manipulation/modules/resourceBroker/include/ResourceBroker.h @@ -37,7 +37,6 @@ public: static ResourceBroker * getInstance(); BrokerID hostResource(PrimitiveResourcePtr pResource, BrokerCB cb); - BrokerID cancelHostResource(BrokerID brokerId); BROKER_STATE getResourceState(BrokerID brokerId); @@ -47,15 +46,13 @@ private: ResourceBroker(); ~ResourceBroker(); + BrokerID generateBrokerID(); + ResourcePresencePtr findResourcePresence(PrimitiveResourcePtr pResource); + static ResourceBroker * s_instance; static std::mutex s_mutexForCreation; static std::unique_ptr s_presenceList; - static std::unique_ptr s_brokerIDMap; - BrokerID generateBrokerID(); - - ResourcePresencePtr findResourcePresence(PrimitiveResourcePtr pResource); - }; #endif /* RESOURCEBROKER_H_ */ diff --git a/service/resource-manipulation/modules/resourceBroker/include/ResourcePresence.h b/service/resource-manipulation/modules/resourceBroker/include/ResourcePresence.h index fcd6574..4029f74 100755 --- a/service/resource-manipulation/modules/resourceBroker/include/ResourcePresence.h +++ b/service/resource-manipulation/modules/resourceBroker/include/ResourcePresence.h @@ -24,8 +24,10 @@ #include #include #include +#include #include "BrokerTypes.h" +#include "../../common/primitiveTimer/include/PrimitiveTimer.h" class ResourcePresence { @@ -36,35 +38,38 @@ public: void addBrokerRequester(BrokerID _id, BrokerCB _cb); void removeBrokerRequester(BrokerID _id); void removeAllBrokerRequester(); - void requestResourceState(); - - PrimitiveResourcePtr getPrimitiveResource(); - BROKER_STATE getResourceState(); void setResourcestate(BROKER_STATE _state); - void executeAllBrokerCB(); - + void executeAllBrokerCB(BROKER_STATE changedState); void changePresenceMode(BROKER_MODE newMode); bool isEmptyRequester() const; + PrimitiveResourcePtr getPrimitiveResource(); + BROKER_STATE getResourceState(); + private: - PrimitiveResourcePtr primitiveResource; + void getCB(const HeaderOptions &hos, const ResponseStatement& rep, int eCode); + void verifiedGetResponse(int eCode); + void registerDevicePresence(); + void * timeOutCB(unsigned int msg); + void * pollingCB(unsigned int msg); std::unique_ptr> requesterList; + PrimitiveResourcePtr primitiveResource; + PrimitiveTimer primitiveTimer; BROKER_STATE state; BROKER_MODE mode; - RequestGetCB pGetCB; - void GetCB(const HeaderOptions &hos, const ResponseStatement& rep, int seq); - - TimeoutCB pTimeoutCB; - bool isWithinTime; - void TimeOutCB(int msg); + std::atomic_bool isTimeoutCB; + std::atomic_long receivedTime; + unsigned int timeoutHandle; - void registerDevicePresence(); + RequestGetCB pGetCB; + TimeoutCB pTimeoutCB; + TimeoutCB pPollingCB; }; diff --git a/service/resource-manipulation/modules/resourceBroker/src/DeviceAssociation.cpp b/service/resource-manipulation/modules/resourceBroker/src/DeviceAssociation.cpp old mode 100644 new mode 100755 index ce0c199..8fbe793 --- a/service/resource-manipulation/modules/resourceBroker/src/DeviceAssociation.cpp +++ b/service/resource-manipulation/modules/resourceBroker/src/DeviceAssociation.cpp @@ -28,7 +28,6 @@ std::list< DevicePresencePtr > DeviceAssociation::s_deviceList; DeviceAssociation::DeviceAssociation() { // TODO Auto-generated constructor stub - } DeviceAssociation::~DeviceAssociation() diff --git a/service/resource-manipulation/modules/resourceBroker/src/DevicePresence.cpp b/service/resource-manipulation/modules/resourceBroker/src/DevicePresence.cpp index ceffc58..3903ff8 100755 --- a/service/resource-manipulation/modules/resourceBroker/src/DevicePresence.cpp +++ b/service/resource-manipulation/modules/resourceBroker/src/DevicePresence.cpp @@ -42,10 +42,8 @@ DevicePresence::DevicePresence(PrimitiveResourcePtr pResource) state = DEVICE_STATE::REQUESTED; pTimeoutCB = std::bind(&DevicePresence::timeOutCB, this, std::placeholders::_1); - isWithinTime = true; //TODO generate Timer(if(!isTimer)) - //register pTimeroutCB } DevicePresence::~DevicePresence() @@ -84,11 +82,10 @@ bool DevicePresence::isEmptyResourcePresence() const void DevicePresence::subscribeCB(OCStackResult ret, const unsigned int seq, const std::string& hostAddress) { - std::cout << "Call Back Deivce's Presence\n"; const char* log = hostAddress.c_str(); OC_LOG_V(DEBUG, BROKER_TAG, "Received presence CB from: %s",log); OC_LOG_V(DEBUG, BROKER_TAG, "In subscribeCB: %d",ret); - //TODO : cancel timer if(isTimer) + if(isWithinTime) { switch(ret) @@ -142,14 +139,15 @@ void DevicePresence::subscribeCB(OCStackResult ret, { OC_LOG_V(DEBUG, BROKER_TAG, "This response is Timeout but device steel alive"); this->state = DEVICE_STATE::ALIVE; - //notify cb message to user. isWithinTime = true; } } -void DevicePresence::timeOutCB(int msg) +void * DevicePresence::timeOutCB(unsigned int msg) { this->isWithinTime = false; OC_LOG_V(DEBUG, BROKER_TAG, "Timeout execution. will be discard after receiving cb message"); state = DEVICE_STATE::LOST_SIGNAL; + + return NULL; } diff --git a/service/resource-manipulation/modules/resourceBroker/src/ResourceBroker.cpp b/service/resource-manipulation/modules/resourceBroker/src/ResourceBroker.cpp index d94effc..22885f5 100755 --- a/service/resource-manipulation/modules/resourceBroker/src/ResourceBroker.cpp +++ b/service/resource-manipulation/modules/resourceBroker/src/ResourceBroker.cpp @@ -30,7 +30,6 @@ std::unique_ptr ResourceBroker::s_brokerIDMap(nullptr); ResourceBroker::ResourceBroker() { - // TODO Auto-generated constructor stub if(s_presenceList == nullptr) { s_presenceList = std::unique_ptr(new PresenceList); @@ -43,7 +42,6 @@ ResourceBroker::ResourceBroker() ResourceBroker::~ResourceBroker() { - // TODO Auto-generated destructor stub if(s_presenceList != nullptr) { s_presenceList->erase(s_presenceList->begin(), s_presenceList->end()); diff --git a/service/resource-manipulation/modules/resourceBroker/src/ResourcePresence.cpp b/service/resource-manipulation/modules/resourceBroker/src/ResourcePresence.cpp index 8dd06db..48a53cf 100755 --- a/service/resource-manipulation/modules/resourceBroker/src/ResourcePresence.cpp +++ b/service/resource-manipulation/modules/resourceBroker/src/ResourcePresence.cpp @@ -18,24 +18,36 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -#include "ResourcePresence.h" +#include "../include/ResourcePresence.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "PrimitiveResource.h" #include "DeviceAssociation.h" #include "DevicePresence.h" ResourcePresence::ResourcePresence(PrimitiveResourcePtr pResource) { primitiveResource = pResource; - - pGetCB = std::bind(&ResourcePresence::GetCB, this, + isTimeoutCB = false; + receivedTime = 0L; + pGetCB = std::bind(&ResourcePresence::getCB, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); - pTimeoutCB = std::bind(&ResourcePresence::TimeOutCB, this, - std::placeholders::_1); + pTimeoutCB = std::bind(&ResourcePresence::timeOutCB, this,std::placeholders::_1); + pPollingCB = std::bind(&ResourcePresence::pollingCB, this,std::placeholders::_1); requesterList = std::unique_ptr>(new std::list); - //TODO generate Timer(if(!isTimer)) - //register pTimeroutCB + timeoutHandle = primitiveTimer.requestTimer(SAFE_TIME, pTimeoutCB); state = BROKER_STATE::REQUESTED; isWithinTime = true; @@ -48,7 +60,6 @@ ResourcePresence::ResourcePresence(PrimitiveResourcePtr pResource) void ResourcePresence::registerDevicePresence() { - // find device...... std::string deviceAddress = primitiveResource->getHost(); DevicePresencePtr foundDevice @@ -117,80 +128,132 @@ bool ResourcePresence::isEmptyRequester() const void ResourcePresence::requestResourceState() { + OC_LOG_V(DEBUG, BROKER_TAG, "Request Get\n"); primitiveResource->requestGet(pGetCB); - } -void ResourcePresence::executeAllBrokerCB() + +void ResourcePresence::executeAllBrokerCB(BROKER_STATE changedState) { OC_LOG_V(DEBUG, BROKER_TAG, "executeAllBrokerCB()"); - for(BrokerRequesterInfoPtr & item : * requesterList) + if(state != changedState) { - item->brockerCB(this->state); + state = changedState; + for(BrokerRequesterInfoPtr & item : * requesterList) + { + item->brockerCB(state); + } } } + void ResourcePresence::setResourcestate(BROKER_STATE _state) { this->state = _state; } -void ResourcePresence::TimeOutCB(int msg) + +void * ResourcePresence::timeOutCB(unsigned int msg) { + isTimeoutCB = true; + + time_t currentTime; + time(¤tTime); + currentTime+=0L; + + if((receivedTime == 0L) || ((receivedTime+SAFE_TIME) > currentTime)) + { + return NULL; + } this->isWithinTime = false; OC_LOG_V(DEBUG, BROKER_TAG, "Timeout execution. will be discard after receiving cb message"); - this->state = BROKER_STATE::LOST_SIGNAL; + executeAllBrokerCB(BROKER_STATE::LOST_SIGNAL); + pollingCB(0); + + isTimeoutCB = false; + + return NULL; +} + +void * ResourcePresence::pollingCB(unsigned int msg) +{ + this->requestResourceState(); + timeoutHandle = primitiveTimer.requestTimer(SAFE_TIME,pTimeoutCB); + + return NULL; } -void ResourcePresence::GetCB(const HeaderOptions &hos, const ResponseStatement& rep, int seq) + +void ResourcePresence::getCB(const HeaderOptions &hos, const ResponseStatement& rep, int eCode) { + OC_LOG_V(DEBUG, BROKER_TAG, "response getCB\n"); + while(isTimeoutCB) + { + OC_LOG_V(DEBUG, BROKER_TAG, "wait\n"); + sleep(2); + } + time_t currentTime; + time(¤tTime); + receivedTime = currentTime; try { - //TODO : cancel timer if(isTimer) + state = BROKER_STATE::ALIVE; + verifiedGetResponse(eCode); + if(isWithinTime) { + primitiveTimer.cancelTimer(timeoutHandle); + isWithinTime = true; + } + + } + catch(std::exception& e) + { + + } + + if(mode == BROKER_MODE::NON_PRESENCE_MODE) + { + // TODO set timer & request get + primitiveTimer.requestTimer(SAFE_TIME,pPollingCB); + } + +} - OC_LOG_V(DEBUG, BROKER_TAG, "broker state :: %d",(int)state); +void ResourcePresence::verifiedGetResponse(int eCode) +{ + switch(eCode) + { + case OC_STACK_OK: + case OC_STACK_CONTINUE: state = BROKER_STATE::ALIVE; - OC_LOG_V(DEBUG, BROKER_TAG, "broker state changed :: %d",(int)state); - OC_LOG_V(DEBUG, BROKER_TAG, "GET request was successful"); + OC_LOG_V(DEBUG, BROKER_TAG, "resource state : %d",(int)state); + break; + case OC_STACK_INVALID_REQUEST_HANDLE: + case OC_STACK_RESOURCE_DELETED: + case OC_STACK_TIMEOUT: + case OC_STACK_COMM_ERROR: + case OC_STACK_PRESENCE_STOPPED: + case OC_STACK_PRESENCE_TIMEOUT: if(!requesterList->empty()) { - executeAllBrokerCB(); + executeAllBrokerCB(BROKER_STATE::LOST_SIGNAL); } else { - OC_LOG_V(DEBUG, BROKER_TAG, "None exist resource for request"); - state = BROKER_STATE::DESTROYED; + setResourcestate(BROKER_STATE::LOST_SIGNAL); } - } - else - { - OC_LOG_V(DEBUG, BROKER_TAG, "its message is not available because of Timeout msg"); - OC_LOG_V(DEBUG, BROKER_TAG, "broker state :: %d",(int)state); - state = BROKER_STATE::ALIVE; - OC_LOG_V(DEBUG, BROKER_TAG, "broker state changed :: %d",(int)state); - OC_LOG_V(DEBUG, BROKER_TAG, "GET request was successful"); - //notify cb message to user. + break; + + default: if(!requesterList->empty()) { - executeAllBrokerCB(); + executeAllBrokerCB(BROKER_STATE::LOST_SIGNAL); } else { - OC_LOG_V(DEBUG, BROKER_TAG, "None exist resource for request"); - state = BROKER_STATE::DESTROYED; + setResourcestate(BROKER_STATE::LOST_SIGNAL); } - isWithinTime = true; - } - } - catch(std::exception& e) - { - - } - - if(mode == BROKER_MODE::NON_PRESENCE_MODE) - { - // TODO set timer & request get + break; } } -- 2.7.4