Delete client callback for error response.
[platform/upstream/iotivity.git] / resource / examples / simpleclientHQ.cpp
index f26a919..6d631d3 100644 (file)
 
 // OCClient.cpp : Defines the entry point for the console application.
 //
+#include "iotivity_config.h"
+
 #include <set>
 #include <string>
 #include <cstdlib>
-#include <pthread.h>
 #include <mutex>
 #include <condition_variable>
 #include "OCPlatform.h"
 #include "OCApi.h"
 
+#if defined(HAVE_PTHREAD_H)
+#include <pthread.h>
+#endif
+#if defined(HAVE_WINDOWS_H)
+#include <windows.h>
+#endif
+
 using namespace OC;
 
 struct dereference_compare
@@ -43,11 +51,9 @@ typedef std::set<std::shared_ptr<OCResource>, dereference_compare> DiscoveredRes
 DiscoveredResourceSet discoveredResources;
 const int SUCCESS_RESPONSE = 0;
 std::shared_ptr<OCResource> curResource;
+std::mutex resourceLock;
 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
 
-//TODO-CA: Since CA CON message support is still in progress, this client uses
-// LowQos messages. This needs to change once Confirmable messages are supported in CA.
-
 class Light
 {
 public:
@@ -69,26 +75,26 @@ 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 == SUCCESS_RESPONSE)
     {
         std::cout << "OBSERVE RESULT:"<<std::endl;
-        if(sequenceNumber == 0)
+        if(sequenceNumber == (int) ObserveAction::ObserveRegister)
         {
-            std::cout << "\tObserve Registration Confirmed: "<< endl;
+            std::cout << "\tObserve Registration Confirmed: "<< std::endl;
         }
-        else if (sequenceNumber == 1)
+        else if (sequenceNumber == (int) ObserveAction::ObserveUnregister)
         {
-            std::cout << "\tObserve Cancel Confirmed: "<< endl;
+            std::cout << "\tObserve Cancel Confirmed: "<< std::endl;
             sleep(10);
             std::cout << "DONE"<<std::endl;
             std::exit(0);
         }
         else
         {
-            std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
+            std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
         }
 
         rep.getValue("state", mylight.m_state);
@@ -99,10 +105,10 @@ void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
         std::cout << "\tpower: " << mylight.m_power << std::endl;
         std::cout << "\tname: " << mylight.m_name << std::endl;
 
-        if(observe_count() > 30)
+        if(observe_count() == 11)
         {
             std::cout<<"Cancelling Observe..."<<std::endl;
-            OCStackResult result = curResource->cancelObserve(OC::QualityOfService::LowQos);
+            OCStackResult result = curResource->cancelObserve(OC::QualityOfService::HighQos);
 
             std::cout << "Cancel result: "<< result << " waiting for confirmation ..." <<std::endl;
         }
@@ -114,9 +120,9 @@ 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)
 {
-    if(eCode == SUCCESS_RESPONSE)
+    if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
     {
         std::cout << "POST request was successful" << std::endl;
 
@@ -137,12 +143,12 @@ 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;
         sleep(1);
         curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
-                OC::QualityOfService::LowQos);
+                OC::QualityOfService::HighQos);
 
     }
     else
@@ -152,9 +158,10 @@ 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)
 {
-    if(eCode == SUCCESS_RESPONSE)
+    if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
     {
         std::cout << "POST request was successful" << std::endl;
 
@@ -184,7 +191,7 @@ void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, con
         rep2.setValue("state", mylight.m_state);
         rep2.setValue("power", mylight.m_power);
         sleep(1);
-        curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::LowQos);
+        curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::HighQos);
     }
     else
     {
@@ -209,14 +216,14 @@ void postLightRepresentation(std::shared_ptr<OCResource> resource)
         rep.setValue("power", mylight.m_power);
 
         // Invoke resource's post API with rep, query map and the callback parameter
-        resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
+        resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::HighQos);
     }
 }
 
 // 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 == SUCCESS_RESPONSE)
+    if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
     {
         std::cout << "PUT request was successful" << std::endl;
 
@@ -253,12 +260,12 @@ void putLightRepresentation(std::shared_ptr<OCResource> resource)
         rep.setValue("power", mylight.m_power);
 
         // Invoke resource's put API with rep, query map and the callback parameter
-        resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::LowQos);
+        resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::HighQos);
     }
 }
 
 // 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 == SUCCESS_RESPONSE)
     {
@@ -291,7 +298,7 @@ void getLightRepresentation(std::shared_ptr<OCResource> resource)
         // Invoke resource's get API with the callback parameter
 
         QueryParamsMap test;
-        resource->get(test, &onGet,OC::QualityOfService::LowQos);
+        resource->get(test, &onGet,OC::QualityOfService::HighQos);
     }
 }
 
@@ -302,26 +309,28 @@ void foundResource(std::shared_ptr<OCResource> resource)
     std::string hostAddress;
     try
     {
-        if(discoveredResources.find(resource) == discoveredResources.end())
-        {
-            std::cout << "Found resource " << resource->uniqueIdentifier() <<
-                " for the first time on server with ID: "<< resource->sid()<<std::endl;
-            discoveredResources.insert(resource);
-        }
-        else
-        {
-            std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
-        }
-
-        if(curResource)
-        {
-            std::cout << "Found another resource, ignoring"<<std::endl;
-            return;
-        }
-
         // Do some operations with resource object.
         if(resource)
         {
+            std::lock_guard<std::mutex> lk(resourceLock);
+
+            if(discoveredResources.find(resource) == discoveredResources.end())
+            {
+                std::cout << "Found resource " << resource->uniqueIdentifier() <<
+                    " for the first time on server with ID: "<< resource->sid()<<std::endl;
+                discoveredResources.insert(resource);
+            }
+            else
+            {
+                std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
+            }
+
+            if(curResource)
+            {
+                std::cout << "Found another resource, ignoring"<<std::endl;
+                return;
+            }
+
             std::cout<<"DISCOVERED Resource:"<<std::endl;
             // Get the resource URI
             resourceURI = resource->uri();
@@ -349,7 +358,8 @@ void foundResource(std::shared_ptr<OCResource> resource)
             {
                 curResource = resource;
                 sleep(1);
-                // Call a local function which will internally invoke get API on the resource pointer
+                // Call a local function which will internally invoke get
+                // API on the resource pointer
                 getLightRepresentation(resource);
             }
         }
@@ -362,47 +372,34 @@ void foundResource(std::shared_ptr<OCResource> resource)
     }
     catch(std::exception& e)
     {
-        //log(e.what());
+        std::cerr << "Exception in foundResource: "<< e.what() <<std::endl;
     }
 }
 
 void PrintUsage()
 {
     std::cout << std::endl;
-#ifdef CA_INT
     std::cout << "Usage : simpleclientHQ <ObserveType> <ConnectivityType>" << std::endl;
-#else
-    std::cout << "Usage : simpleclientHQ <ObserveType>" << std::endl;
-#endif
     std::cout << "   ObserveType : 1 - Observe" << std::endl;
     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
-#ifdef CA_INT
-    std::cout<<"    ConnectivityType: Default WIFI" << std::endl;
-    std::cout << "   ConnectivityType : 0 - ETHERNET"<< std::endl;
-    std::cout << "   ConnectivityType : 1 - WIFI"<< std::endl;
-#endif
+    std::cout << "   ConnectivityType: Default IP" << std::endl;
+    std::cout << "   ConnectivityType : 0 - IP"<< std::endl;
 }
 
 int main(int argc, char* argv[]) {
 
-    ostringstream requestURI;
+    std::ostringstream requestURI;
 
-#ifdef CA_INT
-    OCConnectivityType connectivityType = OC_WIFI;
-#endif
+    OCConnectivityType connectivityType = CT_ADAPTER_IP;
     try
     {
         if (argc == 1)
         {
             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
         }
-#ifdef CA_INT
-        else if (argc >= 2)
-#else
-        else if (argc == 2)
-#endif
+        else if (argc ==2 || argc==3)
         {
-            int value = stoi(argv[1]);
+            int value = std::stoi(argv[1]);
             if (value == 1)
                 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
             else if (value == 2)
@@ -410,35 +407,30 @@ int main(int argc, char* argv[]) {
             else
                 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
 
-#ifdef CA_INT
             if(argc == 3)
             {
                 std::size_t inputValLen;
-                int optionSelected = stoi(argv[2], &inputValLen);
+                int optionSelected = std::stoi(argv[2], &inputValLen);
 
                 if(inputValLen == strlen(argv[2]))
                 {
                     if(optionSelected == 0)
                     {
-                        connectivityType = OC_ETHERNET;
-                    }
-                    else if(optionSelected == 1)
-                    {
-                        connectivityType = OC_WIFI;
+                        std::cout << "Using IP."<< std::endl;
+                        connectivityType = CT_ADAPTER_IP;
                     }
                     else
                     {
-                        std::cout << "Invalid connectivity type selected. Using default WIFI"
+                        std::cout << "Invalid connectivity type selected. Using default IP"
                             << std::endl;
                     }
                 }
                 else
                 {
-                    std::cout << "Invalid connectivity type selected. Using default WIFI"
-                    << std::endl;
+                    std::cout << "Invalid connectivity type selected. Using default IP"
+                            << std::endl;
                 }
             }
-#endif
         }
         else
         {
@@ -446,9 +438,11 @@ int main(int argc, char* argv[]) {
             return -1;
         }
     }
-    catch(exception& e)
+    catch(std::exception&)
     {
-        std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
+        std::cout << "Invalid input argument." << std::endl;
+        PrintUsage();
+        return -1;
     }
 
 
@@ -458,7 +452,7 @@ int main(int argc, char* argv[]) {
         OC::ModeType::Client,
         "0.0.0.0",
         0,
-        OC::QualityOfService::LowQos
+        OC::QualityOfService::HighQos
     };
 
     OCPlatform::Configure(cfg);
@@ -466,32 +460,18 @@ int main(int argc, char* argv[]) {
     try
     {
         // Find all resources
-        requestURI << OC_WELL_KNOWN_QUERY << "?rt=core.light";
+        requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
 
-#ifdef CA_INT
         OCPlatform::findResource("", requestURI.str(),
                 connectivityType, &foundResource, OC::QualityOfService::LowQos);
-#else
-        OCPlatform::findResource("", requestURI.str(), &foundResource,
-                OC::QualityOfService::LowQos);
-#endif
         std::cout<< "Finding Resource... " <<std::endl;
 
         // Find resource is done twice so that we discover the original resources a second time.
         // These resources will have the same uniqueidentifier (yet be different objects), so that
         // we can verify/show the duplicate-checking code in foundResource(above);
-#ifdef CA_INT
         OCPlatform::findResource("", requestURI.str(),
                 connectivityType, &foundResource, OC::QualityOfService::LowQos);
-#else
-        OCPlatform::findResource("", requestURI.str(), &foundResource,
-                OC::QualityOfService::LowQos);
-#endif
         std::cout<< "Finding Resource for second time... " <<std::endl;
-        while(true)
-        {
-            // some operations
-        }
 
         // A condition variable will free the mutex it is given, then do a non-
         // intensive block until 'notify' is called on it.  In this case, since we
@@ -502,11 +482,13 @@ int main(int argc, char* argv[]) {
         std::unique_lock<std::mutex> lock(blocker);
         cv.wait(lock);
 
-    }catch(OCException& e)
+    }
+    catch(OCException& e)
     {
-        //log(e.what());
+        oclog() << "Exception in main: "<<e.what();
     }
 
     return 0;
 }
 
+