X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fexamples%2Fsimpleclient.cpp;h=5667b07c0ba63d6010b090ec7ec7f0a0e0ed3349;hb=refs%2Ftags%2Ftizen_4.0.m2_release;hp=a76b2237f22f9755cc91d0fbac269222a0b817b8;hpb=158a05a27a10bf4c980ed536d7694a9c373a9573;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/examples/simpleclient.cpp b/resource/examples/simpleclient.cpp index a76b223..5667b07 100644 --- a/resource/examples/simpleclient.cpp +++ b/resource/examples/simpleclient.cpp @@ -20,10 +20,19 @@ // OCClient.cpp : Defines the entry point for the console application. // +#include "iotivity_config.h" +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_PTHREAD_H +#include +#endif +#ifdef HAVE_WINDOWS_H +#include +#endif #include #include #include -#include #include #include #include "OCPlatform.h" @@ -31,12 +40,13 @@ using namespace OC; +static const char* SVR_DB_FILE_NAME = "./oic_svr_db_client.dat"; typedef std::map> DiscoveredResourceMap; DiscoveredResourceMap discoveredResources; std::shared_ptr curResource; static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe; -static OCConnectivityType connectivityType = OC_WIFI; +static OCConnectivityType TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_IP; std::mutex curResourceLock; class Light @@ -60,16 +70,20 @@ int observe_count() return ++oc; } -void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep, +void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep, const int& eCode, const int& sequenceNumber) { try { - if(eCode == OC_STACK_OK) + if(eCode == OC_STACK_OK && sequenceNumber <= MAX_SEQUENCE_NUMBER) { - std::cout << "OBSERVE RESULT:"< 30) + if(observe_count() == 11) { std::cout<<"Cancelling Observe..."<cancelObserve(); @@ -91,8 +105,17 @@ void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep, } else { - std::cout << "onObserve Response error: " << eCode << std::endl; - std::exit(-1); + if(eCode == OC_STACK_OK) + { + std::cout << "No observe option header is returned in the response." << std::endl; + std::cout << "For a registration request, it means the registration failed" + << std::endl; + } + else + { + std::cout << "onObserve Response error: " << eCode << std::endl; + std::exit(-1); + } } } catch(std::exception& e) @@ -102,11 +125,13 @@ void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep, } -void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) +void onPost2(const HeaderOptions& /*headerOptions*/, + const OCRepresentation& rep, const int eCode) { try { - if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED) + if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED + || eCode == OC_STACK_RESOURCE_CHANGED) { std::cout << "POST request was successful" << std::endl; @@ -127,9 +152,9 @@ void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, co } if (OBSERVE_TYPE_TO_USE == ObserveType::Observe) - std::cout << endl << "Observe is used." << endl << endl; + std::cout << std::endl << "Observe is used." << std::endl << std::endl; else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll) - std::cout << endl << "ObserveAll is used." << endl << endl; + std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl; curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve); @@ -147,11 +172,13 @@ void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, co } -void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) +void onPost(const HeaderOptions& /*headerOptions*/, + const OCRepresentation& rep, const int eCode) { try { - if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED) + if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED + || eCode == OC_STACK_RESOURCE_CHANGED) { std::cout << "POST request was successful" << std::endl; @@ -216,11 +243,11 @@ void postLightRepresentation(std::shared_ptr resource) } // callback handler on PUT request -void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) +void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode) { try { - if(eCode == OC_STACK_OK) + if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED) { std::cout << "PUT request was successful" << std::endl; @@ -267,7 +294,7 @@ void putLightRepresentation(std::shared_ptr resource) } // Callback handler on GET request -void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) +void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode) { try { @@ -314,7 +341,7 @@ void getLightRepresentation(std::shared_ptr resource) // Callback to found resources void foundResource(std::shared_ptr resource) { - cout << "In foundResource\n"; + std::cout << "In foundResource\n"; std::string resourceURI; std::string hostAddress; try @@ -367,9 +394,15 @@ void foundResource(std::shared_ptr resource) if(resourceURI == "/a/light") { - curResource = resource; - // Call a local function which will internally invoke get API on the resource pointer - getLightRepresentation(resource); + if (resource->connectivityType() & TRANSPORT_TYPE_TO_USE) + { + curResource = resource; + // Get the resource host address + std::cout << "\tAddress of selected resource: " << resource->host() << std::endl; + + // Call a local function which will internally invoke get API on the resource pointer + getLightRepresentation(resource); + } } } else @@ -381,7 +414,7 @@ void foundResource(std::shared_ptr resource) } catch(std::exception& e) { - //log(e.what()); + std::cerr << "Exception in foundResource: "<< e.what() << std::endl; } } @@ -389,12 +422,11 @@ void printUsage() { std::cout << std::endl; std::cout << "---------------------------------------------------------------------\n"; - std::cout << "Usage : simpleclient " << std::endl; + std::cout << "Usage : simpleclient " << std::endl; std::cout << " ObserveType : 1 - Observe" << std::endl; std::cout << " ObserveType : 2 - ObserveAll" << std::endl; - std::cout << " connectivityType: Default WIFI" << std::endl; - std::cout << " ConnectivityType : 0 - ETHERNET"<< std::endl; - std::cout << " ConnectivityType : 1 - WIFI"<< std::endl; + std::cout << " TransportType : 1 - IP" << std::endl; + std::cout << " TransportType : 2 - TCP" << std::endl; std::cout << "---------------------------------------------------------------------\n\n"; } @@ -417,47 +449,49 @@ void checkObserverValue(int value) } } -void checkConnectivityValue(int value) +void checkTransportValue(int value) { - if(value == 0) + if (1 == value) { - connectivityType = OC_ETHERNET; - std::cout << "<===Setting connectivityType to Ethernet===>\n\n"; + TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_IP; + std::cout << "<===Setting TransportType to IP===>\n\n"; } - else if(value == 1) + else if (2 == value) { - connectivityType = OC_WIFI; - std::cout << "<===Setting connectivityType to WIFI===>\n\n"; + TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_TCP; + std::cout << "<===Setting TransportType to TCP===>\n\n"; } else { - std::cout << "<===Invalid ConnectivitType selected." - <<"Setting ConnectivityType to WIFI===>\n\n"; + std::cout << "<===Invalid TransportType selected." + <<" Setting TransportType to IP===>\n\n"; } } -int main(int argc, char* argv[]) { +static FILE* client_open(const char* /*path*/, const char *mode) +{ + return fopen(SVR_DB_FILE_NAME, mode); +} - ostringstream requestURI; +int main(int argc, char* argv[]) { + std::ostringstream requestURI; + OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink, NULL, NULL}; try { printUsage(); if (argc == 1) { - std::cout << "<===Setting ObserveType to Observe and ConnectivityType to WIFI===>\n\n"; + std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n"; } else if (argc == 2) { - - checkObserverValue(stoi(argv[1])); - std::cout << "<===No ConnectivtyType selected. " - << "Setting ConnectivityType to WIFI===>\n\n"; + checkObserverValue(std::stoi(argv[1])); } - else if(argc == 3) + else if (argc == 3) { - checkObserverValue(stoi(argv[1])); - checkConnectivityValue(stoi(argv[2])); + checkObserverValue(std::stoi(argv[1])); + checkTransportValue(std::stoi(argv[2])); } else { @@ -465,7 +499,7 @@ int main(int argc, char* argv[]) { return -1; } } - catch(exception& e) + catch(std::exception& ) { std::cout << "<===Invalid input arguments===>\n\n"; return -1; @@ -474,10 +508,12 @@ int main(int argc, char* argv[]) { // Create PlatformConfig object PlatformConfig cfg { OC::ServiceType::InProc, - OC::ModeType::Client, - "0.0.0.0", - 0, - OC::QualityOfService::LowQos + OC::ModeType::Both, + OCConnectivityType::CT_ADAPTER_IP, + OCConnectivityType::CT_ADAPTER_IP, + (OCTransportAdapter)(OCTransportAdapter::OC_ADAPTER_IP|OCTransportAdapter::OC_ADAPTER_TCP), + OC::QualityOfService::HighQos, + &ps }; OCPlatform::Configure(cfg); @@ -486,17 +522,17 @@ int main(int argc, char* argv[]) { // makes it so that all boolean values are printed as 'true/false' in this stream std::cout.setf(std::ios::boolalpha); // Find all resources - requestURI << OC_WELL_KNOWN_QUERY << "?rt=core.light"; + requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light"; OCPlatform::findResource("", requestURI.str(), - connectivityType, &foundResource); + CT_DEFAULT, &foundResource); std::cout<< "Finding Resource... " <