X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fexamples%2Fpresenceclient.cpp;h=72b672a9043a6706f07f63efab5c4c338b4cc977;hb=7f00f942c39b7bc27c7eeecf213a239c3fe4173c;hp=857d0f4b1c204a629f4b0d6c21ccb481942a059a;hpb=3375ed12998d1261d46d4ececbdb500194ce2573;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/examples/presenceclient.cpp b/resource/examples/presenceclient.cpp index 857d0f4..72b672a 100644 --- a/resource/examples/presenceclient.cpp +++ b/resource/examples/presenceclient.cpp @@ -25,16 +25,18 @@ #include #include #include - +#include #include "OCPlatform.h" #include "OCApi.h" using namespace OC; std::shared_ptr curResource; - +std::mutex resourceLock; static int TEST_CASE = 0; +static OCConnectivityType connectivityType = CT_ADAPTER_IP; + /** * List of methods that can be inititated from the client */ @@ -50,17 +52,19 @@ typedef enum { void printUsage() { - std::cout << "Usage : presenceclient -t <1|2>" << std::endl; + std::cout << "Usage : presenceclient -t <1|2|3|4|5|6> -c <0|1>" << std::endl; std::cout << "-t 1 : Discover Resources and Initiate Unicast Presence" << std::endl; std::cout << "-t 2 : Discover Resources and Initiate Unicast Presence with Filter" << std::endl; std::cout << "-t 3 : Discover Resources and Initiate Unicast Presence with Two Filters" - << std::endl; + << std::endl; std::cout << "-t 4 : Discover Resources and Initiate Multicast Presence" << std::endl; std::cout << "-t 5 : Discover Resources and Initiate Multicast Presence with Filter" << std::endl; std::cout << "-t 6 : Discover Resources and Initiate Multicast Presence with two Filters" - << std::endl; + << std::endl; + std::cout<<"ConnectivityType: Default IP" << std::endl; + std::cout << "-c 0 : Send message with IP" << std::endl; } // Callback to presence @@ -80,9 +84,6 @@ void presenceHandler(OCStackResult result, const unsigned int nonce, const std:: case OC_STACK_PRESENCE_TIMEOUT: std::cout << "Presence Timeout\n"; break; - case OC_STACK_VIRTUAL_DO_NOT_HANDLE: - std::cout << "Virtual do not handle\n"; - break; default: std::cout << "Error\n"; break; @@ -92,9 +93,11 @@ void presenceHandler(OCStackResult result, const unsigned int nonce, const std:: // Callback to found resources void foundResource(std::shared_ptr resource) { + std::lock_guard lock(resourceLock); if(curResource) { std::cout << "Found another resource, ignoring"< resource) if(TEST_CASE == TEST_UNICAST_PRESENCE_NORMAL) { result = OCPlatform::subscribePresence(presenceHandle, hostAddress, - &presenceHandler); + connectivityType, &presenceHandler); if(result == OC_STACK_OK) { std::cout<< "Subscribed to unicast address: " << hostAddress << std::endl; @@ -151,7 +154,7 @@ void foundResource(std::shared_ptr resource) TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS) { result = OCPlatform::subscribePresence(presenceHandle, hostAddress, - "core.light", &presenceHandler); + "core.light", connectivityType, &presenceHandler); if(result == OC_STACK_OK) { std::cout<< "Subscribed to unicast address: " << hostAddress; @@ -165,7 +168,7 @@ void foundResource(std::shared_ptr resource) if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS) { result = OCPlatform::subscribePresence(presenceHandle, hostAddress, "core.fan", - &presenceHandler); + connectivityType, &presenceHandler); if(result == OC_STACK_OK) { std::cout<< "Subscribed to unicast address: " << hostAddress; @@ -187,25 +190,63 @@ void foundResource(std::shared_ptr resource) } catch(std::exception& e) { + std::cerr << "Exception in foundResource: "<< e.what() << std::endl; //log(e.what()); } } int main(int argc, char* argv[]) { + + std::ostringstream requestURI; + int opt; - while ((opt = getopt(argc, argv, "t:")) != -1) + int optionSelected; + + try { - switch(opt) + while ((opt = getopt(argc, argv, "t:c:")) != -1) { - case 't': - TEST_CASE = atoi(optarg); - break; - default: - printUsage(); - return -1; + switch(opt) + { + case 't': + TEST_CASE = std::stoi(optarg); + break; + case 'c': + std::size_t inputValLen; + optionSelected = std::stoi(optarg, &inputValLen); + + if(inputValLen == strlen(optarg)) + { + if(optionSelected == 0) + { + std::cout << "Using IP."<< std::endl; + connectivityType = CT_ADAPTER_IP; + } + else + { + std::cout << "Invalid connectivity type selected. Using default IP" + << std::endl; + } + } + else + { + std::cout << "Invalid connectivity type selected. Using default IP" + << std::endl; + } + break; + default: + printUsage(); + return -1; + } } } + catch(std::exception& ) + { + std::cout << "Invalid input argument. Using IP as connectivity type" + << std::endl; + } + if(TEST_CASE >= MAX_TESTS || TEST_CASE <= 0) { printUsage(); @@ -233,7 +274,10 @@ int main(int argc, char* argv[]) { if(TEST_CASE == TEST_MULTICAST_PRESENCE_NORMAL) { result = OCPlatform::subscribePresence(presenceHandle, - OC_MULTICAST_IP, presenceHandler); + "", + connectivityType, + presenceHandler); + if(result == OC_STACK_OK) { std::cout << "Subscribed to multicast presence." << std::endl; @@ -245,8 +289,10 @@ int main(int argc, char* argv[]) { } else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTER) { - result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.light", - &presenceHandler); + result = OCPlatform::subscribePresence(presenceHandle, + "", "core.light", + connectivityType, + &presenceHandler); if(result == OC_STACK_OK) { std::cout << "Subscribed to multicast presence with resource type"; @@ -259,8 +305,10 @@ int main(int argc, char* argv[]) { } else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTERS) { - result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.light", - &presenceHandler); + result = OCPlatform::subscribePresence(presenceHandle, + "", "core.light", + connectivityType, + &presenceHandler); if(result == OC_STACK_OK) { std::cout << "Subscribed to multicast presence with resource type"; @@ -271,8 +319,10 @@ int main(int argc, char* argv[]) { } std::cout << "\"core.light\"." << std::endl; - result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.fan", - &presenceHandler); + result = OCPlatform::subscribePresence(presenceHandle, + "", "core.fan", + connectivityType, + &presenceHandler); if(result == OC_STACK_OK) { std::cout<< "Subscribed to multicast presence with resource type"; @@ -286,7 +336,10 @@ int main(int argc, char* argv[]) { else { // Find all resources - result = OCPlatform::findResource("", "coap://224.0.1.187/oc/core", &foundResource); + requestURI << OC_RSRVD_WELL_KNOWN_URI; + + result = OCPlatform::findResource("", requestURI.str(), + CT_DEFAULT, &foundResource); if(result == OC_STACK_OK) { std::cout << "Finding Resource... " << std::endl; @@ -306,11 +359,13 @@ int main(int argc, char* argv[]) { std::unique_lock lock(blocker); cv.wait(lock); - }catch(OCException& e) + } + catch(OCException& e) { - //log(e.what()); + oclog() << "Exception in main: "<< e.what(); } return 0; } +