From 54b5b92ba8eba5b7b4d63486ed73d6c1397df59d Mon Sep 17 00:00:00 2001 From: Thuyen Tran Date: Mon, 11 Aug 2014 19:04:13 -0700 Subject: [PATCH] Add ObserveAll case in SimpleClient C++ Change-Id: I8769f8bec28ddfa607819926384f2aee9143618f --- OCLib/InProcClientWrapper.cpp | 24 ++++++++++++++++---- OCLib/OCResource.cpp | 4 ++-- csdk/occoap/src/occoap.c | 2 +- examples/simpleclient.cpp | 49 ++++++++++++++++++++++++++++++++++------ include/IClientWrapper.h | 2 +- include/InProcClientWrapper.h | 2 +- include/OCApi.h | 10 ++++++-- include/OCResource.h | 5 ++-- include/OutOfProcClientWrapper.h | 2 +- 9 files changed, 79 insertions(+), 21 deletions(-) diff --git a/OCLib/InProcClientWrapper.cpp b/OCLib/InProcClientWrapper.cpp index 98df52e..6c24be6 100644 --- a/OCLib/InProcClientWrapper.cpp +++ b/OCLib/InProcClientWrapper.cpp @@ -347,22 +347,24 @@ namespace OC struct ObserveContext { - std::function callback; + std::function callback; }; OCStackApplicationResult observeResourceCallback(void* ctx, OCDoHandle handle, OCClientResponse* clientResponse) { ObserveContext* context = static_cast(ctx); AttributeMap attrs; + uint32_t sequenceNumber = clientResponse->sequenceNumber; if(clientResponse->result == OC_STACK_OK) { attrs = parseGetSetCallback(clientResponse); } - std::thread exec(context->callback, attrs, clientResponse->result); + std::thread exec(context->callback, attrs, clientResponse->result, sequenceNumber); exec.detach(); return OC_STACK_KEEP_TRANSACTION; } - OCStackResult InProcClientWrapper::ObserveResource(OCDoHandle* handle, const std::string& host, const std::string& uri, std::function& callback) + + OCStackResult InProcClientWrapper::ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, const std::string& uri, std::function& callback) { OCStackResult result; OCCallbackData* cbdata = new OCCallbackData(); @@ -371,13 +373,27 @@ namespace OC cbdata->context = static_cast(ctx); cbdata->cb = &observeResourceCallback; + OCMethod method; + if (observeType == ObserveType::Observe) + { + method = OC_REST_OBSERVE; + } + else if (observeType == ObserveType::ObserveAll) + { + method = OC_REST_OBSERVE_ALL; + } + else + { + method = OC_REST_OBSERVE_ALL; + } + ostringstream os; os << host<< uri; { std::lock_guard lock(m_csdkLock); //result = OCDoResource(handle, OC_REST_OBSERVE, uri.c_str(), host.c_str(), nullptr, OC_CONFIRMABLE, cbdata); - result = OCDoResource(handle, OC_REST_OBSERVE, os.str().c_str(), nullptr, nullptr, OC_NON_CONFIRMABLE, cbdata); + result = OCDoResource(handle, method, os.str().c_str(), nullptr, nullptr, OC_NON_CONFIRMABLE, cbdata); } return result; } diff --git a/OCLib/OCResource.cpp b/OCLib/OCResource.cpp index e822404..4f12ef1 100644 --- a/OCLib/OCResource.cpp +++ b/OCLib/OCResource.cpp @@ -47,7 +47,7 @@ namespace OC { return m_clientWrapper->SetResourceAttributes(m_host, m_uri, attributeMap, queryParametersMap, attributeHandler); } - OCStackResult OCResource::observe(std::function observeHandler) + OCStackResult OCResource::observe(ObserveType observeType, std::function observeHandler) { if(m_observeHandle != nullptr) { @@ -55,7 +55,7 @@ namespace OC { } else { - return m_clientWrapper->ObserveResource(&m_observeHandle, m_host, m_uri, observeHandler); + return m_clientWrapper->ObserveResource(observeType, &m_observeHandle, m_host, m_uri, observeHandler); } } diff --git a/csdk/occoap/src/occoap.c b/csdk/occoap/src/occoap.c index 82742fe..ac04a33 100644 --- a/csdk/occoap/src/occoap.c +++ b/csdk/occoap/src/occoap.c @@ -207,7 +207,7 @@ static void HandleCoAPResponses(struct coap_context_t *ctx, { if(clientResponse->sequenceNumber != 0) { - if(clientResponse->sequenceNumber <= cbNode->sequenceNumber) + if(cbNode->method == OC_REST_OBSERVE && (clientResponse->sequenceNumber <= cbNode->sequenceNumber)) { OC_LOG_V(DEBUG, TAG, "Observe notification came out of order. \ Ignoring Incoming:%d Against Current:%d.", diff --git a/examples/simpleclient.cpp b/examples/simpleclient.cpp index 05bae3a..ece8eff 100644 --- a/examples/simpleclient.cpp +++ b/examples/simpleclient.cpp @@ -24,11 +24,13 @@ #include #include #include "OCPlatform.h" +#include "OCApi.h" using namespace OC; const int SUCCESS_RESPONSE = 0; std::shared_ptr curResource; +static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe; int observe_count() { @@ -36,11 +38,12 @@ int observe_count() return ++oc; } -void onObserve(const AttributeMap attributeMap, const int eCode) +void onObserve(const AttributeMap& attributeMap, const int& eCode, const int& sequenceNumber) { if(eCode == SUCCESS_RESPONSE) { std::cout << "OBSERVE RESULT:"<first << " value: "; @@ -51,8 +54,8 @@ void onObserve(const AttributeMap attributeMap, const int eCode) std::cout << std::endl; } - - if(observe_count() > 3) + + if(observe_count() > 30) { std::cout<<"Cancelling Observe..."<cancelObserve(); @@ -87,7 +90,14 @@ void onPut(const AttributeMap attributeMap, const int eCode) std::cout << std::endl; } - curResource->observe(&onObserve); + + if (OBSERVE_TYPE_TO_USE == ObserveType::Observe) + std::cout << endl << "Observe is used." << endl << endl; + else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll) + std::cout << endl << "ObserveAll is used." << endl << endl; + + curResource->observe(OBSERVE_TYPE_TO_USE, &onObserve); + } else { @@ -203,11 +213,36 @@ void foundResource(std::shared_ptr resource) } } - -int main() +void PrintUsage() { - // Create PlatformConfig object + std::cout << endl; + std::cout << "Usage : simpleclient " << endl; + std::cout << " ObserveType : 1 - Observe" << endl; + std::cout << " ObserveType : 2 - ObserveAll" << endl; +} +int main(int argc, char* argv[]) { + if (argc == 1) + { + OBSERVE_TYPE_TO_USE = ObserveType::Observe; + } + else if (argc == 2) + { + int value = atoi(argv[1]); + if (value == 1) + OBSERVE_TYPE_TO_USE = ObserveType::Observe; + else if (value == 2) + OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll; + else + OBSERVE_TYPE_TO_USE = ObserveType::Observe; + } + else + { + PrintUsage(); + return -1; + } + + // Create PlatformConfig object PlatformConfig cfg; cfg.ipAddress = "134.134.161.33"; cfg.port = 5683; diff --git a/include/IClientWrapper.h b/include/IClientWrapper.h index 682ff7d..6458cfd 100644 --- a/include/IClientWrapper.h +++ b/include/IClientWrapper.h @@ -35,7 +35,7 @@ namespace OC std::function)>& callback) = 0; virtual OCStackResult GetResourceAttributes(const std::string& host, const std::string& uri, std::function& callback)=0; virtual OCStackResult SetResourceAttributes(const std::string& host, const std::string& uri, const AttributeMap& attributes, const QueryParamsMap& queryParams, std::function& callback)=0; - virtual OCStackResult ObserveResource(OCDoHandle* handle, const std::string& host, const std::string& uri, std::function& callback)=0; + virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, const std::string& uri, std::function& callback)=0; virtual OCStackResult CancelObserveResource(OCDoHandle handle, const std::string& host, const std::string& uri)=0; virtual ~IClientWrapper(){} diff --git a/include/InProcClientWrapper.h b/include/InProcClientWrapper.h index 0ea19b7..952e2ec 100644 --- a/include/InProcClientWrapper.h +++ b/include/InProcClientWrapper.h @@ -46,7 +46,7 @@ namespace OC virtual OCStackResult ListenForResource(const std::string& serviceUrl, const std::string& resourceType, std::function)>& callback); virtual OCStackResult GetResourceAttributes(const std::string& host, const std::string& uri, std::function& callback); virtual OCStackResult SetResourceAttributes(const std::string& host, const std::string& uri, const AttributeMap& attributes, const QueryParamsMap& queryParams, std::function& callback); - virtual OCStackResult ObserveResource(OCDoHandle* handle, const std::string& host, const std::string& uri, std::function& callback); + virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, const std::string& uri, std::function& callback); virtual OCStackResult CancelObserveResource(OCDoHandle handle, const std::string& host, const std::string& uri); // Note: this should never be called by anyone but the handler for the listen command. It is public becuase that needs to be a non-instance callback diff --git a/include/OCApi.h b/include/OCApi.h index b6fd91c..68853a5 100644 --- a/include/OCApi.h +++ b/include/OCApi.h @@ -78,6 +78,12 @@ namespace OC { ObserverFlag }; + enum class ObserveType + { + Observe, + ObserveAll + }; + // TODO: To find the complete JSon data structure and modify map value type // Typedef for attribute values and attribute map. typedef std::vector AttributeValues; @@ -91,10 +97,10 @@ namespace OC { // Default interface const std::string DEFAULT_INTERFACE = "oc.mi.def"; - // Used in discovering (GET) links to other resources of a collection. + // Used in discovering (GET) links to other resources of a collection. const std::string LINK_INTERFACE = "oc.mi.ll"; - // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection. + // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection. const std::string BATCH_INTERFACE = "oc.mi.b"; } // namespace OC diff --git a/include/OCResource.h b/include/OCResource.h index 7e0699f..d2b3796 100644 --- a/include/OCResource.h +++ b/include/OCResource.h @@ -116,7 +116,7 @@ namespace OC * @param AttributeMap Map which can either have all the attribute names and values * (which will represent entire state of the resource) or a * set of attribute names and values which needs to be modified - * @param QueryParamsMap Map which can have the query parameter name and value + * @param QueryParamsMap Map which can have the query parameter name and value * @param attributeHandler * The callback function will be invoked with a map of attribute name and values. * The callback function will also have the result from this Put operation @@ -131,6 +131,7 @@ namespace OC /** * Function to set observation on the resource + * @param observeType allows the client to specify how it wants to observe. * @param observeHandler handles callback * The callback function will be invoked with a map of attribute name and values. * The callback function will also have the result from this observe operation @@ -138,7 +139,7 @@ namespace OC * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. * NOTE: OCStackResult is defined in ocstack.h. */ - OCStackResult observe(std::function observeHandler); + OCStackResult observe(ObserveType observeType, std::function observeHandler); /** * Function to cancel the observation on the resource diff --git a/include/OutOfProcClientWrapper.h b/include/OutOfProcClientWrapper.h index a56d616..106080f 100644 --- a/include/OutOfProcClientWrapper.h +++ b/include/OutOfProcClientWrapper.h @@ -33,7 +33,7 @@ namespace OC virtual OCStackResult GetResourceAttributes(const std::string& host, const std::string& uri, std::function& callback){return OC_STACK_NOTIMPL;} virtual OCStackResult SetResourceAttributes(const std::string& host, const std::string& uri, const AttributeMap& attributes, const QueryParamsMap& queryParams, std::function& callback){return OC_STACK_NOTIMPL;} - virtual OCStackResult ObserveResource(OCDoHandle* handle, const std::string& host, const std::string& uri, std::function& callback){return OC_STACK_NOTIMPL;} + virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, const std::string& uri, std::function& callback){return OC_STACK_NOTIMPL;} virtual OCStackResult CancelObserveResource(OCDoHandle handle, const std::string& host, const std::string& uri){return OC_STACK_NOTIMPL;} virtual std::shared_ptr parseOCResource(IClientWrapper::Ptr clientWrapper, const std::string& host, const boost::property_tree::ptree resourceNode) {return nullptr;} -- 2.7.4