From: Sashi Penta Date: Thu, 21 Aug 2014 00:01:05 +0000 (-0700) Subject: CPP Default entity handler functionality and misc fixes. X-Git-Tag: 1.2.0+RC1~2308 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e2cc251bc27cdf0595af8f97d14e2c8a9983f73c;p=platform%2Fupstream%2Fiotivity.git CPP Default entity handler functionality and misc fixes. Patch 1: Fixed simpleclient to work with Observe, along with JSON representation changes. Patch 2: Fixed some stuff in simpleclientserver, and other things. Patch 3: C++ change to discover multiple resources on a server Patch 4: C++ Default handler functionality. Fixes in C Stack for Default entity hanlder for C++ stack to work. Few other misc. fixes. Patch 5: Changed entityHandler name to EntityHandler. Added Try/Catch around read_json Fix for simpleclientserver Change-Id: I3e2006b73de22567ccb7d770d5843e5a5eebce47 --- diff --git a/OCLib/InProcClientWrapper.cpp b/OCLib/InProcClientWrapper.cpp index c82bfa8..be63b91 100644 --- a/OCLib/InProcClientWrapper.cpp +++ b/OCLib/InProcClientWrapper.cpp @@ -151,8 +151,22 @@ namespace OC std::stringstream requestStream; requestStream << clientResponse->resJSONPayload; + + // TODO this should got logger + // std::cout << "Listen: " << clientResponse->resJSONPayload << std::endl; + boost::property_tree::ptree root; - boost::property_tree::read_json(requestStream, root); + + try + { + boost::property_tree::read_json(requestStream, root); + } + catch(boost::property_tree::json_parser::json_parser_error &e) + { + std::cout << "read_json failed: "<< e.what() <second(pRequest, pResponse); + if(entityHandlerEntry->second) + { + entityHandlerEntry->second(pRequest, pResponse); + } + else + { + // TODO Logging + std::cout << "C stack should not call again for parent resource\n"; + } } else { std::cout << "No entity handler found." << endl; @@ -132,7 +140,7 @@ OCEntityHandlerResult entityHandler(OCEntityHandlerFlag flag, OCEntityHandlerReq if (payLoad.size() < entityHandlerRequest->resJSONPayloadLen) { strncpy((char*)entityHandlerRequest->resJSONPayload, payLoad.c_str(), entityHandlerRequest->resJSONPayloadLen); - cout << (char*)entityHandlerRequest->resJSONPayload << endl; + cout << "Payload: " << (char*)entityHandlerRequest->resJSONPayload << endl; } else { @@ -223,13 +231,26 @@ namespace OC { std::lock_guard lock(*cLock); - result = OCCreateResource(&resourceHandle, // OCResourceHandle *handle + if(NULL != eHandler) + { + result = OCCreateResource(&resourceHandle, // OCResourceHandle *handle resourceTypeName.c_str(), // const char * resourceTypeName resourceInterface.c_str(), //const char * resourceInterfaceName //TODO fix this resourceURI.c_str(), // const char * uri - entityHandler, // OCEntityHandler entityHandler + EntityHandler, // OCEntityHandler entityHandler resourceProperties // uint8_t resourceProperties ); + } + else + { + result = OCCreateResource(&resourceHandle, // OCResourceHandle *handle + resourceTypeName.c_str(), // const char * resourceTypeName + resourceInterface.c_str(), //const char * resourceInterfaceName //TODO fix this + resourceURI.c_str(), // const char * uri + NULL, // OCEntityHandler entityHandler + resourceProperties // uint8_t resourceProperties + ); + } if(result != OC_STACK_OK) { diff --git a/OCLib/OCPlatform.cpp b/OCLib/OCPlatform.cpp index f94fca2..1bb5997 100644 --- a/OCLib/OCPlatform.cpp +++ b/OCLib/OCPlatform.cpp @@ -182,7 +182,7 @@ namespace OC return result; } - OCStackResult bindResource(const OCResourceHandle collectionHandle, const OCResourceHandle resourceHandle) + OCStackResult OCPlatform::bindResource(const OCResourceHandle collectionHandle, const OCResourceHandle resourceHandle) { OCStackResult result = OC_STACK_OK; @@ -197,7 +197,7 @@ namespace OC return result; } - OCStackResult bindResources(const OCResourceHandle collectionHandle, const std::vector& resourceHandles) + OCStackResult OCPlatform::bindResources(const OCResourceHandle collectionHandle, const std::vector& resourceHandles) { OCStackResult result = OC_STACK_OK; diff --git a/OCLib/OCUtilities.cpp b/OCLib/OCUtilities.cpp index e11c97f..de8f730 100644 --- a/OCLib/OCUtilities.cpp +++ b/OCLib/OCUtilities.cpp @@ -74,7 +74,7 @@ OC::Utilities::QueryParamsKeyVal OC::Utilities::getQueryParams(const std::string break; } - coap_uri_t coapuri = {0}; + coap_uri_t coapuri = {{0}}; unsigned char* uristr = reinterpret_cast(const_cast(_uri.c_str())); if(coap_split_uri(uristr, _uri.length(), &coapuri) < 0) diff --git a/csdk/stack/src/occollection.c b/csdk/stack/src/occollection.c index df7db4e..9b039bf 100644 --- a/csdk/stack/src/occollection.c +++ b/csdk/stack/src/occollection.c @@ -223,13 +223,19 @@ BuildCollectionBatchJSONResponse(OCEntityHandlerFlag flag, stackRet = BuildRootResourceJSON(resource, ehRequest); if (stackRet == OC_STACK_OK) { + OCResourceHandle origResourceHandle = ehRequest->resource; + for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++) { OCResource* temp = resource->rsrcResources[i]; if (temp) { //TODO ("Proper Error handling"); + + ehRequest->resource = (OCResourceHandle) temp; + ehRet = temp->entityHandler(OC_REQUEST_FLAG, ehRequest); + if(ehRet == OC_EH_OK) { unsigned char* buffer = ehRequest->resJSONPayload; @@ -239,7 +245,7 @@ BuildCollectionBatchJSONResponse(OCEntityHandlerFlag flag, ehRequest->resJSONPayload = buffer; if ( resource->rsrcResources[i+1] && ehRequest->resJSONPayloadLen > sizeof(OC_JSON_SEPARATOR) ) { - *buffer = OC_JSON_SEPARATOR; + * buffer = OC_JSON_SEPARATOR; buffer++; ehRequest->resJSONPayload = buffer; ehRequest->resJSONPayloadLen = ehRequest->resJSONPayloadLen - 1; @@ -256,6 +262,8 @@ BuildCollectionBatchJSONResponse(OCEntityHandlerFlag flag, break; } } + + ehRequest->resource = origResourceHandle; } return stackRet; } diff --git a/csdk/stack/src/ocstack.c b/csdk/stack/src/ocstack.c index baf9b14..3c1ef97 100644 --- a/csdk/stack/src/ocstack.c +++ b/csdk/stack/src/ocstack.c @@ -73,6 +73,7 @@ OCStackResult HandleStackRequests(OCRequest * request) { result = BuildJSONResponse(resHandling, resource, request); } + return result; } diff --git a/examples/roomclient.cpp b/examples/roomclient.cpp index 7fb0eb2..b171f61 100644 --- a/examples/roomclient.cpp +++ b/examples/roomclient.cpp @@ -186,7 +186,6 @@ void getRoomRepresentation(std::shared_ptr resource) // Callback to found resources void foundResource(std::shared_ptr resource) { - if(curResource) { std::cout << "Found another resource, ignoring"<observe(OBSERVE_TYPE_TO_USE, test, &onObserve); + } else { @@ -209,7 +220,6 @@ void getLightRepresentation(std::shared_ptr resource) // Invoke resource's get API with the callback parameter QueryParamsMap test; - test["if"] = BATCH_INTERFACE; resource->get(test, &onGet); } } diff --git a/examples/simpleclientserver.cpp b/examples/simpleclientserver.cpp index ab6343f..dd5900f 100644 --- a/examples/simpleclientserver.cpp +++ b/examples/simpleclientserver.cpp @@ -25,6 +25,7 @@ /// #include #include +#include #include #include #include @@ -35,8 +36,13 @@ using namespace OC; class ClientWorker { private: - void putResourceInfo(const AttributeMap requestedPut, const AttributeMap attrMap, const int eCode) + void putResourceInfo(const OCRepresentation rep, const OCRepresentation rep2, const int eCode) { + std::cout << "In PutResourceInfo" << std::endl; + + AttributeMap requestedPut = rep.getAttributeMap(); + AttributeMap attrMap = rep2.getAttributeMap(); + std::cout <<"Clientside Put response to get was: "<put(attrMap2, QueryParamsMap(), std::function(std::bind(&ClientWorker::putResourceInfo, this, attrMap2, std::placeholders::_1, std::placeholders::_2))); + rep2.setAttributeMap(attrMap2); + + m_resource->put(rep2, QueryParamsMap(), std::function(std::bind(&ClientWorker::putResourceInfo, this, rep2, std::placeholders::_1, std::placeholders::_2))); } } void foundResource(std::shared_ptr resource) { + std::cout << "In foundResource" << std::endl; if(resource && resource->uri() == "/q/foo") { { @@ -121,7 +134,7 @@ private: std::cout<<"Doing a get on q/foo."<get(test, std::function(std::bind(&ClientWorker::getResourceInfo, this, std::placeholders::_1, std::placeholders::_2))); + resource->get(test, std::function(std::bind(&ClientWorker::getResourceInfo, this, std::placeholders::_1, std::placeholders::_2))); } } @@ -174,8 +187,10 @@ struct FooResource return true; } - void getRepresentation(AttributeMap& attributeMap) + void getRepresentation(OCRepresentation& rep) { + AttributeMap attributeMap; + AttributeValues isFooVal; isFooVal.push_back(m_isFoo ? "true" : "false"); @@ -184,14 +199,26 @@ struct FooResource attributeMap["isFoo"] = isFooVal; attributeMap["barCount"] = barCt; + + rep.setAttributeMap(attributeMap); } - void setRepresentation(const AttributeMap& attributeMap) + void setRepresentation(OCRepresentation& rep) { - auto fooVector = attributeMap.at("isFoo"); - m_isFoo = fooVector[0] == "true"; - auto barVector = attributeMap.at("barCount"); - m_barCount = std::stoi(barVector[0]); + AttributeMap attributeMap(rep.getAttributeMap()); + + try + { + auto fooVector = attributeMap.at("isFoo"); + m_isFoo = fooVector[0] == "true"; + auto barVector = attributeMap.at("barCount"); + m_barCount = std::stoi(barVector[0]); + rep.setAttributeMap(attributeMap); + } + catch(std::out_of_range& e) + { + std::cerr << "setRepresentation(): unable to look up values in attributeMap: " << e.what() << '\n'; + } } void entityHandler(std::shared_ptr request, std::shared_ptr response) @@ -210,27 +237,28 @@ struct FooResource { std::cout<<"\t\t\trequestType : GET"<setErrorCode(200); - response->setResourceRepresentation(attrs); + response->setResourceRepresentation(rep, ""); } } else if (request->getRequestType() == "PUT") { std::cout<<"\t\t\trequestType : PUT"<getAttributeRepresentation()); + OCRepresentation rep = request->getResourceRepresentation(); + setRepresentation(rep); if(response) { response->setErrorCode(200); - AttributeMap attrs; - getRepresentation(attrs); - response->setResourceRepresentation(attrs); + OCRepresentation rep; + getRepresentation(rep); + response->setResourceRepresentation(rep, ""); } } else diff --git a/include/OCResourceResponse.h b/include/OCResourceResponse.h index 5c4c33f..bdcc288 100644 --- a/include/OCResourceResponse.h +++ b/include/OCResourceResponse.h @@ -172,7 +172,7 @@ namespace OC payload << "\""<first<<"\":" << itr->second.front(); } - payload << "}},"; + payload << "}}"; // Children stuff std::vector children = rep.getChildren(); @@ -232,16 +232,13 @@ namespace OC payload << "\"" ; payload << rep.getUri(); payload << "\"" ; - payload << "},"; + payload << "}"; std::vector children = rep.getChildren(); for(auto oitr = children.begin(); oitr != children.end(); ++oitr) { - if(oitr != children.begin()) - { - payload << ','; - } + payload << ','; payload << "{"; diff --git a/makefile b/makefile index 4c4c489..5e6e95a 100644 --- a/makefile +++ b/makefile @@ -19,7 +19,7 @@ CXX_INC += -I./csdk/logger/include CXX_INC += -I./csdk/libcoap # Force metatargets to build: -.PHONY: prep_dirs c_sdk simpleserver simpleclient roomserver roomclient +.PHONY: prep_dirs c_sdk simpleserver simpleclient simpleclientserver roomserver roomclient all: .PHONY