From 3009bf85537c39f5eb69786a802b2568a8331cd8 Mon Sep 17 00:00:00 2001 From: Erich Keane Date: Tue, 9 Sep 2014 15:36:33 -0700 Subject: [PATCH] Cleaned up a number of line lengths. Cleaned up iterator-for-loops vs for:each syntax for clarity where easily possible. Switched to by ref where we were previously doing unnecessary pointer parameters Went entirely thorugh InProcClientWrapper.cpp and fixed line lengths. Change-Id: I69f706e64f67d9a822639f239575eb316675c1d7 --- include/InProcClientWrapper.h | 28 ++++--- include/OCApi.h | 7 +- include/OutOfProcClientWrapper.h | 29 ++++--- include/ResourceInitException.h | 18 ++-- src/InProcClientWrapper.cpp | 172 ++++++++++++++++++++++----------------- src/OCUtilities.cpp | 32 ++++---- 6 files changed, 159 insertions(+), 127 deletions(-) diff --git a/include/InProcClientWrapper.h b/include/InProcClientWrapper.h index 060ea6b..60fbe3a 100644 --- a/include/InProcClientWrapper.h +++ b/include/InProcClientWrapper.h @@ -1,6 +1,6 @@ //****************************************************************** // -// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved. +// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved. // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // @@ -43,27 +43,31 @@ namespace OC InProcClientWrapper(std::weak_ptr csdkLock, PlatformConfig cfg); virtual ~InProcClientWrapper(); - virtual OCStackResult ListenForResource(const std::string& serviceUrl, const std::string& resourceType, - FindCallback& callback); + virtual OCStackResult ListenForResource(const std::string& serviceUrl, + const std::string& resourceType, FindCallback& callback); - virtual OCStackResult GetResourceAttributes(const std::string& host, const std::string& uri, const QueryParamsMap& queryParams, - GetCallback& callback); + virtual OCStackResult GetResourceAttributes(const std::string& host, const std::string& uri, + const QueryParamsMap& queryParams, GetCallback& callback); - virtual OCStackResult SetResourceAttributes(const std::string& host, const std::string& uri, - const OCRepresentation& attributes, const QueryParamsMap& queryParams, PutCallback& callback); + virtual OCStackResult SetResourceAttributes(const std::string& host, const std::string& uri, + const OCRepresentation& attributes, const QueryParamsMap& queryParams, + PutCallback& callback); - virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle, - const std::string& host, const std::string& uri, const QueryParamsMap& queryParams, + virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle, + const std::string& host, const std::string& uri, const QueryParamsMap& queryParams, ObserveCallback& callback); - virtual OCStackResult CancelObserveResource(OCDoHandle handle, const std::string& host, const std::string& uri); + virtual OCStackResult CancelObserveResource(OCDoHandle handle, const std::string& host, + const std::string& uri); virtual OCStackResult SubscribePresence(OCDoHandle* handle, const std::string& host, SubscribeCallback& presenceHandler); virtual OCStackResult UnsubscribePresence(OCDoHandle handle); - // 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 - virtual std::shared_ptr parseOCResource(IClientWrapper::Ptr clientWrapper, const std::string& host, const boost::property_tree::ptree resourceNode); + // 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 + virtual std::shared_ptr parseOCResource(IClientWrapper::Ptr clientWrapper, + const std::string& host, const boost::property_tree::ptree resourceNode); private: void listeningFunc(); std::string assembleSetResourceUri(std::string uri, const QueryParamsMap& queryParams); diff --git a/include/OCApi.h b/include/OCApi.h index 89471e2..8d03c51 100644 --- a/include/OCApi.h +++ b/include/OCApi.h @@ -167,12 +167,11 @@ namespace OC m_children = children; } - OCResource* getResource() const + std::weak_ptr getResource() const { // TODO Needs to be implemented - OCResource* res = NULL; - - return res; + std::weak_ptr wp; + return wp; } AttributeMap getAttributeMap() const diff --git a/include/OutOfProcClientWrapper.h b/include/OutOfProcClientWrapper.h index 2ff41fe..2a2abde 100644 --- a/include/OutOfProcClientWrapper.h +++ b/include/OutOfProcClientWrapper.h @@ -29,24 +29,29 @@ namespace OC { public: OutOfProcClientWrapper(std::weak_ptr csdkLock, PlatformConfig cfg) { } - - virtual OCStackResult ListenForResource(const std::string& serviceUrl, const std::string& resourceType, - FindCallback& callback) {return OC_STACK_NOTIMPL;} - virtual OCStackResult GetResourceAttributes(const std::string& host, const std::string& uri, + virtual OCStackResult ListenForResource(const std::string& serviceUrl, + const std::string& resourceType, FindCallback& callback) {return OC_STACK_NOTIMPL;} + + virtual OCStackResult GetResourceAttributes(const std::string& host, const std::string& uri, const QueryParamsMap& queryParams, GetCallback& callback){return OC_STACK_NOTIMPL;} - virtual OCStackResult SetResourceAttributes(const std::string& host, const std::string& uri, - const OCRepresentation& attributes, const QueryParamsMap& queryParams, PutCallback& callback){return OC_STACK_NOTIMPL;} + virtual OCStackResult SetResourceAttributes(const std::string& host, const std::string& uri, + const OCRepresentation& attributes, const QueryParamsMap& queryParams, + PutCallback& callback){return OC_STACK_NOTIMPL;} - virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, - const std::string& uri, const QueryParamsMap& queryParams, + virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle, + const std::string& host, const std::string& uri, const QueryParamsMap& queryParams, ObserveCallback& 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;} + 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; + } virtual OCStackResult SubscribePresence(OCDoHandle* handle, const std::string& host, SubscribeCallback& presenceHandler){return OC_STACK_NOTIMPL;} diff --git a/include/ResourceInitException.h b/include/ResourceInitException.h index de867bb..8cca8c2 100644 --- a/include/ResourceInitException.h +++ b/include/ResourceInitException.h @@ -33,7 +33,7 @@ namespace OC : m_missingUri(missingUri), m_missingType(missingType), m_missingInterface(missingInterface), m_missingClientWrapper(missingClientWrapper) { } - + bool isClientWrapperMissing() const { return m_missingClientWrapper; @@ -43,36 +43,36 @@ namespace OC { return m_missingUri; } - + bool isTypeMissing() const { return m_missingType; } - + bool isInterfaceMissing() const { return m_missingInterface; } - - virtual const char* what() noexcept + + virtual const char* what() const noexcept { std::string ret; - + if(isUriMissing()) { ret += "Missing URI;"; } - + if(isTypeMissing()) { ret += "Missing Resource Type;"; } - + if(isInterfaceMissing()) { ret += "Missing Interface;"; } - + if(isClientWrapperMissing()) { ret += "Missing ClientWrapper;"; diff --git a/src/InProcClientWrapper.cpp b/src/InProcClientWrapper.cpp index 6c375a4..29bb7d1 100644 --- a/src/InProcClientWrapper.cpp +++ b/src/InProcClientWrapper.cpp @@ -86,15 +86,15 @@ namespace OC } - - std::string convertOCAddrToString(OCDevAddr* addr) + + std::string convertOCAddrToString(OCDevAddr& addr) { // TODO: we currently assume this is a IPV4 address, need to figure out the actual value uint8_t a, b, c, d; uint16_t port; - if(OCDevAddrToIPv4Addr(addr, &a, &b, &c, &d) ==0 && OCDevAddrToPort(addr, &port)==0) + if(OCDevAddrToIPv4Addr(&addr, &a, &b, &c, &d) ==0 && OCDevAddrToPort(&addr, &port)==0) { ostringstream os; os << "coap://"<<(int)a<<'.'<<(int)b<<'.'<<(int)c<<'.'<<(int)d<<':'<<(int)port; @@ -112,7 +112,7 @@ namespace OC IClientWrapper::Ptr clientWrapper; }; - + const std::string URIKEY = "href"; const std::string OBSERVABLEKEY = "obs"; const std::string RESOURCETYPESKEY= "rt"; @@ -120,36 +120,43 @@ namespace OC const std::string PROPERTYKEY = "prop"; const std::string REPKEY = "rep"; - std::shared_ptr InProcClientWrapper::parseOCResource(IClientWrapper::Ptr clientWrapper, const std::string& host, const boost::property_tree::ptree resourceNode) + std::shared_ptr InProcClientWrapper::parseOCResource( + IClientWrapper::Ptr clientWrapper, const std::string& host, + const boost::property_tree::ptree resourceNode) { std::string uri = resourceNode.get(URIKEY, ""); bool obs = resourceNode.get(OBSERVABLEKEY,0) == 1; std::vector rTs; std::vector ifaces; - boost::property_tree::ptree properties = resourceNode.get_child(PROPERTYKEY, boost::property_tree::ptree()); + boost::property_tree::ptree properties = + resourceNode.get_child(PROPERTYKEY, boost::property_tree::ptree()); - boost::property_tree::ptree rT = properties.get_child(RESOURCETYPESKEY, boost::property_tree::ptree()); + boost::property_tree::ptree rT = + properties.get_child(RESOURCETYPESKEY, boost::property_tree::ptree()); for(auto itr : rT) { rTs.push_back(itr.second.data()); } - boost::property_tree::ptree iF = properties.get_child(INTERFACESKEY, boost::property_tree::ptree()); + boost::property_tree::ptree iF = + properties.get_child(INTERFACESKEY, boost::property_tree::ptree()); for(auto itr : iF) { ifaces.push_back(itr.second.data()); } - return std::shared_ptr(new OCResource(clientWrapper, host, uri, obs, rTs, ifaces)); + return std::shared_ptr( + new OCResource(clientWrapper, host, uri, obs, rTs, ifaces)); } - - OCStackApplicationResult listenCallback(void* ctx, OCDoHandle handle, OCClientResponse* clientResponse) + + OCStackApplicationResult listenCallback(void* ctx, OCDoHandle handle, + OCClientResponse* clientResponse) { if(clientResponse->result == OC_STACK_OK) { ListenContext* context = static_cast(ctx); - + std::stringstream requestStream; requestStream << clientResponse->resJSONPayload; @@ -168,21 +175,24 @@ namespace OC // TODO: Do we want to handle this somehow? Perhaps we need to log this? return OC_STACK_KEEP_TRANSACTION; } - - boost::property_tree::ptree payload = root.get_child("oc", boost::property_tree::ptree()); - + + boost::property_tree::ptree payload = + root.get_child("oc", boost::property_tree::ptree()); + for(auto payloadItr : payload) { try { - std::string host = convertOCAddrToString(clientResponse->addr); - std::shared_ptr resource = context->clientWrapper->parseOCResource(context->clientWrapper, host, payloadItr.second); - - // Note: the call to detach allows the underlying thread to continue until completion - // and allows us to destroy the exec object. - // This is apparently NOT a memory leak, as the thread will apparently take care of itself. - // Additionally, the only parameter here is - // a shared ptr, so OCResource will be disposed of properly upon completion of the callback handler. + std::string host = convertOCAddrToString(*clientResponse->addr); + std::shared_ptr resource = + context->clientWrapper->parseOCResource(context->clientWrapper, host, + payloadItr.second); + + // Note: the call to detach allows the underlying thread to continue until + // completion and allows us to destroy the exec object. This is apparently NOT + // a memory leak, as the thread will apparently take care of itself. + // Additionally, the only parameter here is a shared ptr, so OCResource will be + // disposed of properly upon completion of the callback handler. std::thread exec(context->callback,resource); exec.detach(); } @@ -200,9 +210,10 @@ namespace OC std::cout<<"listen Callback got failed result: " << clientResponse->result< lock(*cLock); OCDoHandle handle; - result = OCDoResource(&handle, OC_REST_GET, - resourceType.c_str(), - nullptr, nullptr, - static_cast(m_cfg.QoS), + result = OCDoResource(&handle, OC_REST_GET, + resourceType.c_str(), + nullptr, nullptr, + static_cast(m_cfg.QoS), cbdata); } else @@ -232,7 +243,7 @@ namespace OC } return result; } - + struct GetContext { GetCallback callback; @@ -242,8 +253,8 @@ namespace OC { PutCallback callback; }; - - + + OCRepresentation parseGetSetCallback(OCClientResponse* clientResponse) { std::stringstream requestStream; @@ -287,15 +298,18 @@ namespace OC { std::vector rTs; std::vector ifaces; - boost::property_tree::ptree properties = resourceNode.get_child(PROPERTYKEY, boost::property_tree::ptree()); + boost::property_tree::ptree properties = + resourceNode.get_child(PROPERTYKEY, boost::property_tree::ptree()); - boost::property_tree::ptree rT = properties.get_child(RESOURCETYPESKEY, boost::property_tree::ptree()); + boost::property_tree::ptree rT = + properties.get_child(RESOURCETYPESKEY, boost::property_tree::ptree()); for(auto itr : rT) { rTs.push_back(itr.second.data()); } - boost::property_tree::ptree iF = properties.get_child(INTERFACESKEY, boost::property_tree::ptree()); + boost::property_tree::ptree iF = + properties.get_child(INTERFACESKEY, boost::property_tree::ptree()); for(auto itr : iF) { ifaces.push_back(itr.second.data()); @@ -314,7 +328,8 @@ namespace OC if( resourceNode.count(REPKEY) != 0 ) { - boost::property_tree::ptree rep = resourceNode.get_child(REPKEY, boost::property_tree::ptree()); + boost::property_tree::ptree rep = + resourceNode.get_child(REPKEY, boost::property_tree::ptree()); AttributeMap attrs; for( auto item : rep) { @@ -350,25 +365,26 @@ namespace OC return root_resource; } - OCStackApplicationResult getResourceCallback(void* ctx, OCDoHandle handle, OCClientResponse* clientResponse) + OCStackApplicationResult getResourceCallback(void* ctx, OCDoHandle handle, + OCClientResponse* clientResponse) { GetContext* context = static_cast(ctx); std::cout << "GET JSON: " << (char*) clientResponse->resJSONPayload << endl; - + OCRepresentation rep; if(clientResponse->result == OC_STACK_OK) { rep = parseGetSetCallback(clientResponse); - } + } std::thread exec(context->callback, rep, clientResponse->result); exec.detach(); return OC_STACK_DELETE_TRANSACTION; } - OCStackResult InProcClientWrapper::GetResourceAttributes(const std::string& host, const std::string& uri, - const QueryParamsMap& queryParams, GetCallback& callback) + OCStackResult InProcClientWrapper::GetResourceAttributes(const std::string& host, + const std::string& uri, const QueryParamsMap& queryParams, GetCallback& callback) { OCStackResult result; OCCallbackData* cbdata = new OCCallbackData(); @@ -388,9 +404,9 @@ namespace OC { std::lock_guard lock(*cLock); OCDoHandle handle; - result = OCDoResource(&handle, OC_REST_GET, os.str().c_str(), - nullptr, nullptr, - static_cast(m_cfg.QoS), + result = OCDoResource(&handle, OC_REST_GET, os.str().c_str(), + nullptr, nullptr, + static_cast(m_cfg.QoS), cbdata); } else @@ -400,8 +416,9 @@ namespace OC return result; } - - OCStackApplicationResult setResourceCallback(void* ctx, OCDoHandle handle, OCClientResponse* clientResponse) + + OCStackApplicationResult setResourceCallback(void* ctx, OCDoHandle handle, + OCClientResponse* clientResponse) { SetContext* context = static_cast(ctx); OCRepresentation attrs; @@ -414,10 +431,11 @@ namespace OC exec.detach(); return OC_STACK_DELETE_TRANSACTION; } - - std::string InProcClientWrapper::assembleSetResourceUri(std::string uri, const QueryParamsMap& queryParams) + + std::string InProcClientWrapper::assembleSetResourceUri(std::string uri, + const QueryParamsMap& queryParams) { - if(uri.back() == '/') + if(uri.back() == '/') { uri.resize(uri.size()-1); } @@ -442,7 +460,7 @@ namespace OC std::string ret = uri + queryString; return ret; } - + std::string InProcClientWrapper::assembleSetResourcePayload(const OCRepresentation& rep) { AttributeMap attributes = rep.getAttributeMap(); @@ -455,19 +473,20 @@ namespace OC { if(itr != attributes.begin()) { - payload << ','; + payload << ','; } - + payload << "\""<first<<"\":\""<< itr->second.front()<<"\""; } payload << "}}"; return payload.str(); } - OCStackResult InProcClientWrapper::SetResourceAttributes(const std::string& host, const std::string& uri, - const OCRepresentation& attributes, const QueryParamsMap& queryParams, PutCallback& callback) + OCStackResult InProcClientWrapper::SetResourceAttributes(const std::string& host, + const std::string& uri, const OCRepresentation& attributes, + const QueryParamsMap& queryParams, PutCallback& callback) { - OCStackResult result; + OCStackResult result; OCCallbackData* cbdata = new OCCallbackData(); SetContext* ctx = new SetContext(); ctx->callback = callback; @@ -485,10 +504,10 @@ namespace OC { std::lock_guard lock(*cLock); OCDoHandle handle; - result = OCDoResource(&handle, OC_REST_PUT, - os.str().c_str(), nullptr, - assembleSetResourcePayload(attributes).c_str(), - static_cast(m_cfg.QoS), + result = OCDoResource(&handle, OC_REST_PUT, + os.str().c_str(), nullptr, + assembleSetResourcePayload(attributes).c_str(), + static_cast(m_cfg.QoS), cbdata); } else @@ -504,7 +523,8 @@ namespace OC ObserveCallback callback; }; - OCStackApplicationResult observeResourceCallback(void* ctx, OCDoHandle handle, OCClientResponse* clientResponse) + OCStackApplicationResult observeResourceCallback(void* ctx, OCDoHandle handle, + OCClientResponse* clientResponse) { ObserveContext* context = static_cast(ctx); OCRepresentation attrs; @@ -518,8 +538,9 @@ namespace OC return OC_STACK_KEEP_TRANSACTION; } - OCStackResult InProcClientWrapper::ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, - const std::string& uri, const QueryParamsMap& queryParams, ObserveCallback& callback) + OCStackResult InProcClientWrapper::ObserveResource(ObserveType observeType, OCDoHandle* handle, + const std::string& host, const std::string& uri, const QueryParamsMap& queryParams, + ObserveCallback& callback) { OCStackResult result; OCCallbackData* cbdata = new OCCallbackData(); @@ -547,15 +568,15 @@ namespace OC os << host << assembleSetResourceUri(uri, queryParams).c_str(); // std::cout << "OBSERVE URI: " << os.str() << std::endl; // TODO: end of above - + auto cLock = m_csdkLock.lock(); if(cLock) { std::lock_guard lock(*cLock); - result = OCDoResource(handle, method, - os.str().c_str(), nullptr, - nullptr, - static_cast(m_cfg.QoS), + result = OCDoResource(handle, method, + os.str().c_str(), nullptr, + nullptr, + static_cast(m_cfg.QoS), cbdata); } else @@ -566,7 +587,8 @@ namespace OC return result; } - OCStackResult InProcClientWrapper::CancelObserveResource(OCDoHandle handle, const std::string& host, const std::string& uri) + OCStackResult InProcClientWrapper::CancelObserveResource(OCDoHandle handle, + const std::string& host, const std::string& uri) { OCStackResult result; auto cLock = m_csdkLock.lock(); @@ -574,7 +596,7 @@ namespace OC if(cLock) { std::lock_guard lock(*cLock); - result = OCCancel(handle); + result = OCCancel(handle); } else { @@ -589,7 +611,8 @@ namespace OC SubscribeCallback callback; }; - OCStackApplicationResult subscribePresenceCallback(void* ctx, OCDoHandle handle, OCClientResponse* clientResponse) + OCStackApplicationResult subscribePresenceCallback(void* ctx, OCDoHandle handle, + OCClientResponse* clientResponse) { SubscribePresenceContext* context = static_cast(ctx); std::thread exec(context->callback, clientResponse->result, clientResponse->sequenceNumber); @@ -598,8 +621,8 @@ namespace OC return OC_STACK_KEEP_TRANSACTION; } - OCStackResult InProcClientWrapper::SubscribePresence(OCDoHandle* handle, const std::string& host, - SubscribeCallback& presenceHandler) + OCStackResult InProcClientWrapper::SubscribePresence(OCDoHandle* handle, + const std::string& host, SubscribeCallback& presenceHandler) { OCStackResult result; OCCallbackData* cbdata = new OCCallbackData(); @@ -618,7 +641,8 @@ namespace OC if(cLock) { - result = OCDoResource(handle, OC_REST_PRESENCE, os.str().c_str(), nullptr, nullptr, OC_NON_CONFIRMABLE, cbdata); + result = OCDoResource(handle, OC_REST_PRESENCE, os.str().c_str(), nullptr, nullptr, + OC_NON_CONFIRMABLE, cbdata); } else { @@ -631,7 +655,7 @@ namespace OC { OCStackResult result; auto cLock = m_csdkLock.lock(); - + if(cLock) { std::lock_guard lock(*cLock); diff --git a/src/OCUtilities.cpp b/src/OCUtilities.cpp index 33f9f72..95da802 100644 --- a/src/OCUtilities.cpp +++ b/src/OCUtilities.cpp @@ -29,30 +29,30 @@ extern "C" { } namespace OC{ // Helper function to escape special character. - std::string escapeString(const std::string& value) + std::string escapeString(const std::string& value) { std::ostringstream stringStream; - for (auto iter = value.cbegin(); iter != value.cend(); iter++) + for (const char& c : value) { - switch (*iter) + switch (c) { - case '\\': stringStream << "\\\\"; + case '\\': stringStream << "\\\\"; break; - case '"': stringStream << "\\\""; + case '"': stringStream << "\\\""; break; - case '/': stringStream << "\\/"; + case '/': stringStream << "\\/"; break; - case '\b': stringStream << "\\b"; + case '\b': stringStream << "\\b"; break; - case '\f': stringStream << "\\f"; + case '\f': stringStream << "\\f"; break; - case '\n': stringStream << "\\n"; + case '\n': stringStream << "\\n"; break; - case '\r': stringStream << "\\r"; + case '\r': stringStream << "\\r"; break; - case '\t': stringStream << "\\t"; + case '\t': stringStream << "\\t"; break; - default: stringStream << *iter; + default: stringStream << c; break; } } @@ -74,10 +74,10 @@ static OC::Utilities::QueryParamsKeyVal tempPatch(const std::string& _uri) std::vector queryparams; boost::split(queryparams, _uri, boost::is_any_of("&")); - for(std::vector::iterator it = queryparams.begin(); it != queryparams.end(); ++it) + for(std::string& it: queryparams) { std::vector keyval; - boost::split(keyval, *it, boost::is_any_of("=")); + boost::split(keyval, it, boost::is_any_of("=")); if(2 == keyval.size()) { qp[keyval.at(0)] = keyval.at(1); @@ -142,10 +142,10 @@ OC::Utilities::QueryParamsKeyVal OC::Utilities::getQueryParams(const std::string } //[TODO] use foreach - for(std::vector::iterator it = queryparams.begin(); it != queryparams.end(); ++it) + for(std::string& it : queryparams) { std::vector keyval; - boost::split(keyval, *it, boost::is_any_of("=")); + boost::split(keyval, it, boost::is_any_of("=")); if(2 == keyval.size()) { qp[keyval.at(0)] = keyval.at(1); -- 2.7.4