X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fexamples%2Fsimpleclient.cpp;h=5667b07c0ba63d6010b090ec7ec7f0a0e0ed3349;hb=7f00f942c39b7bc27c7eeecf213a239c3fe4173c;hp=85c0dc16eb12487ce96f2f66bd913427d22142d0;hpb=1e0a58c84d416bab6fdfbc2cf6175c2c7b150e09;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/examples/simpleclient.cpp b/resource/examples/simpleclient.cpp index 85c0dc1..5667b07 100644 --- a/resource/examples/simpleclient.cpp +++ b/resource/examples/simpleclient.cpp @@ -20,19 +20,34 @@ // 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" #include "OCApi.h" 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 TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_IP; +std::mutex curResourceLock; class Light { @@ -55,115 +70,155 @@ 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) { - if(eCode == OC_STACK_OK) + try { - std::cout << "OBSERVE RESULT:"< 30) - { - std::cout<<"Cancelling Observe..."<cancelObserve(); + if(observe_count() == 11) + { + std::cout<<"Cancelling Observe..."<cancelObserve(); - std::cout << "Cancel result: "<< result <("createduri") << std::endl; - } - else - { - rep.getValue("state", mylight.m_state); - rep.getValue("power", mylight.m_power); - rep.getValue("name", mylight.m_name); + std::cout << "POST request was successful" << std::endl; - std::cout << "\tstate: " << mylight.m_state << std::endl; - std::cout << "\tpower: " << mylight.m_power << std::endl; - std::cout << "\tname: " << mylight.m_name << std::endl; - } + if(rep.hasAttribute("createduri")) + { + std::cout << "\tUri of the created resource: " + << rep.getValue("createduri") << std::endl; + } + else + { + rep.getValue("state", mylight.m_state); + rep.getValue("power", mylight.m_power); + rep.getValue("name", mylight.m_name); + + std::cout << "\tstate: " << mylight.m_state << std::endl; + std::cout << "\tpower: " << mylight.m_power << std::endl; + std::cout << "\tname: " << mylight.m_name << std::endl; + } - 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; + if (OBSERVE_TYPE_TO_USE == ObserveType::Observe) + std::cout << std::endl << "Observe is used." << std::endl << std::endl; + else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll) + std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl; - curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve); + curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve); + } + else + { + std::cout << "onPost2 Response error: " << eCode << std::endl; + std::exit(-1); + } } - else + catch(std::exception& e) { - std::cout << "onPost2 Response error: " << eCode << std::endl; - std::exit(-1); + std::cout << "Exception: " << e.what() << " in onPost2" << std::endl; } + } -void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) +void onPost(const HeaderOptions& /*headerOptions*/, + const OCRepresentation& rep, const int eCode) { - if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED) + try { - std::cout << "POST request was successful" << std::endl; - - if(rep.hasAttribute("createduri")) + if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED + || eCode == OC_STACK_RESOURCE_CHANGED) { - std::cout << "\tUri of the created resource: " - << rep.getValue("createduri") << std::endl; - } - else - { - rep.getValue("state", mylight.m_state); - rep.getValue("power", mylight.m_power); - rep.getValue("name", mylight.m_name); + std::cout << "POST request was successful" << std::endl; - std::cout << "\tstate: " << mylight.m_state << std::endl; - std::cout << "\tpower: " << mylight.m_power << std::endl; - std::cout << "\tname: " << mylight.m_name << std::endl; - } + if(rep.hasAttribute("createduri")) + { + std::cout << "\tUri of the created resource: " + << rep.getValue("createduri") << std::endl; + } + else + { + rep.getValue("state", mylight.m_state); + rep.getValue("power", mylight.m_power); + rep.getValue("name", mylight.m_name); - OCRepresentation rep2; + std::cout << "\tstate: " << mylight.m_state << std::endl; + std::cout << "\tpower: " << mylight.m_power << std::endl; + std::cout << "\tname: " << mylight.m_name << std::endl; + } - std::cout << "Posting light representation..."<post(rep2, QueryParamsMap(), &onPost2); + curResource->post(rep2, QueryParamsMap(), &onPost2); + } + else + { + std::cout << "onPost Response error: " << eCode << std::endl; + std::exit(-1); + } } - else + catch(std::exception& e) { - std::cout << "onPost Response error: " << eCode << std::endl; - std::exit(-1); + std::cout << "Exception: " << e.what() << " in onPost" << std::endl; } } @@ -188,26 +243,33 @@ 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) { - if(eCode == OC_STACK_OK) + try { - std::cout << "PUT request was successful" << std::endl; + if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED) + { + std::cout << "PUT request was successful" << std::endl; - rep.getValue("state", mylight.m_state); - rep.getValue("power", mylight.m_power); - rep.getValue("name", mylight.m_name); + rep.getValue("state", mylight.m_state); + rep.getValue("power", mylight.m_power); + rep.getValue("name", mylight.m_name); - std::cout << "\tstate: " << mylight.m_state << std::endl; - std::cout << "\tpower: " << mylight.m_power << std::endl; - std::cout << "\tname: " << mylight.m_name << std::endl; + std::cout << "\tstate: " << mylight.m_state << std::endl; + std::cout << "\tpower: " << mylight.m_power << std::endl; + std::cout << "\tname: " << mylight.m_name << std::endl; - postLightRepresentation(curResource); + postLightRepresentation(curResource); + } + else + { + std::cout << "onPut Response error: " << eCode << std::endl; + std::exit(-1); + } } - else + catch(std::exception& e) { - std::cout << "onPut Response error: " << eCode << std::endl; - std::exit(-1); + std::cout << "Exception: " << e.what() << " in onPut" << std::endl; } } @@ -232,27 +294,34 @@ 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) { - if(eCode == OC_STACK_OK) + try { - std::cout << "GET request was successful" << std::endl; - std::cout << "Resource URI: " << rep.getUri() << std::endl; + if(eCode == OC_STACK_OK) + { + std::cout << "GET request was successful" << std::endl; + std::cout << "Resource URI: " << rep.getUri() << std::endl; - rep.getValue("state", mylight.m_state); - rep.getValue("power", mylight.m_power); - rep.getValue("name", mylight.m_name); + rep.getValue("state", mylight.m_state); + rep.getValue("power", mylight.m_power); + rep.getValue("name", mylight.m_name); - std::cout << "\tstate: " << mylight.m_state << std::endl; - std::cout << "\tpower: " << mylight.m_power << std::endl; - std::cout << "\tname: " << mylight.m_name << std::endl; + std::cout << "\tstate: " << mylight.m_state << std::endl; + std::cout << "\tpower: " << mylight.m_power << std::endl; + std::cout << "\tname: " << mylight.m_name << std::endl; - putLightRepresentation(curResource); + putLightRepresentation(curResource); + } + else + { + std::cout << "onGET Response error: " << eCode << std::endl; + std::exit(-1); + } } - else + catch(std::exception& e) { - std::cout << "onGET Response error: " << eCode << std::endl; - std::exit(-1); + std::cout << "Exception: " << e.what() << " in onGet" << std::endl; } } @@ -272,15 +341,31 @@ void getLightRepresentation(std::shared_ptr resource) // Callback to found resources void foundResource(std::shared_ptr resource) { - if(curResource) - { - std::cout << "Found another resource, ignoring"< lock(curResourceLock); + if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end()) + { + std::cout << "Found resource " << resource->uniqueIdentifier() << + " for the first time on server with ID: "<< resource->sid()<uniqueIdentifier()] = resource; + } + else + { + std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"< 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 @@ -323,46 +414,106 @@ void foundResource(std::shared_ptr resource) } catch(std::exception& e) { - //log(e.what()); + std::cerr << "Exception in foundResource: "<< e.what() << std::endl; } } -void PrintUsage() +void printUsage() { std::cout << std::endl; - std::cout << "Usage : simpleclient " << std::endl; + std::cout << "---------------------------------------------------------------------\n"; + std::cout << "Usage : simpleclient " << std::endl; std::cout << " ObserveType : 1 - Observe" << std::endl; std::cout << " ObserveType : 2 - ObserveAll" << std::endl; + std::cout << " TransportType : 1 - IP" << std::endl; + std::cout << " TransportType : 2 - TCP" << std::endl; + std::cout << "---------------------------------------------------------------------\n\n"; } -int main(int argc, char* argv[]) { - if (argc == 1) +void checkObserverValue(int value) +{ + if (value == 1) { OBSERVE_TYPE_TO_USE = ObserveType::Observe; + std::cout << "<===Setting ObserveType to Observe===>\n\n"; } - else if (argc == 2) + else if (value == 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; + OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll; + std::cout << "<===Setting ObserveType to ObserveAll===>\n\n"; } else { - PrintUsage(); + std::cout << "<===Invalid ObserveType selected." + <<" Setting ObserveType to Observe===>\n\n"; + } +} + +void checkTransportValue(int value) +{ + if (1 == value) + { + TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_IP; + std::cout << "<===Setting TransportType to IP===>\n\n"; + } + else if (2 == value) + { + TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_TCP; + std::cout << "<===Setting TransportType to TCP===>\n\n"; + } + else + { + std::cout << "<===Invalid TransportType selected." + <<" Setting TransportType to IP===>\n\n"; + } +} + +static FILE* client_open(const char* /*path*/, const char *mode) +{ + return fopen(SVR_DB_FILE_NAME, mode); +} + +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 IP===>\n\n"; + } + else if (argc == 2) + { + checkObserverValue(std::stoi(argv[1])); + } + else if (argc == 3) + { + checkObserverValue(std::stoi(argv[1])); + checkTransportValue(std::stoi(argv[2])); + } + else + { + std::cout << "<===Invalid number of command line arguments===>\n\n"; + return -1; + } + } + catch(std::exception& ) + { + std::cout << "<===Invalid input arguments===>\n\n"; return -1; } // 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); @@ -371,9 +522,19 @@ 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 - OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource); + requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light"; + + OCPlatform::findResource("", requestURI.str(), + CT_DEFAULT, &foundResource); std::cout<< "Finding Resource... " <