Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / groupsyncaction / group.cpp
old mode 100644 (file)
new mode 100755 (executable)
index d8b1082..fd90e82
@@ -38,44 +38,49 @@ void onFindResource(std::shared_ptr< OCResource > resource)
 {
     cout << "onFindResource" << endl;
 
-    if (resource)
+    try
     {
-        OCResourceHandle resourceHandle;
-        OCStackResult result = OCPlatform::registerResource(resourceHandle, resource);
-        if (OC_STACK_OK == result)
-        {
-            cout << "onFindResource : Resource creation was successful\n";
-        }
-        else
+        if (resource)
         {
-            cout << "onFindResource : Resource creation was unsuccessful\n";
-            return;
-        }
+            OCResourceHandle resourceHandle;
+            OCStackResult result = OCPlatform::registerResource(resourceHandle, resource);
+            if (OC_STACK_OK == result)
+            {
+                cout << "onFindResource : Resource creation was successful\n";
+            }
+            else
+            {
+                cout << "onFindResource : Resource creation was unsuccessful\n";
+                return;
+            }
 
-        result = gThingManager->joinGroup(collectionResourceType, resourceHandle);
-        if (OC_STACK_OK == result)
-        {
-            cout << "onFindResource : Joining group was successful\n";
+            result = gThingManager->joinGroup(collectionResourceType, resourceHandle);
+            if (OC_STACK_OK == result)
+            {
+                cout << "onFindResource : Joining group was successful\n";
+            }
+            else
+            {
+                cout << "onFindResource : Joining group was unsuccessful\n";
+
+                OCPlatform::unregisterResource(resourceHandle);
+                return;
+            }
+
+            gResourceHandleList.push_back(resourceHandle);
         }
         else
         {
-            cout << "onFindResource : Joining group was unsuccessful\n";
-
-            OCPlatform::unregisterResource(resourceHandle);
-            return;
+            cout << "onFindResource : There is no found resource." << endl;
         }
-
-        gResourceHandleList.push_back(resourceHandle);
-    }
-    else
+    }catch (std::exception& e)
     {
-        cout << "onFindResource : There is no found resource." << endl;
+        std::cout << "Exception: " << e.what() << std::endl;
     }
 }
 
 int main(int argc, char* argv[])
 {
-
     // Create PlatformConfig object
     PlatformConfig cfg
     { OC::ServiceType::InProc, OC::ModeType::Both/*OC::ModeType::Server*/, "0.0.0.0", 0,
@@ -113,8 +118,15 @@ int main(int argc, char* argv[])
             }
             else if (selectedMenu == 11)
             {
+                ostringstream query;
+                query << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.musicplayer";
+
+                cout << query.str() << endl;
                 result = OCPlatform::findResource("",
-                        "coap://224.0.1.187/oc/core?rt=core.musicplayer", onFindResource);
+                            query.str(),
+                            CT_DEFAULT,
+                            onFindResource);
+
                 if (OC_STACK_OK == result)
                 {
                     cout << "Finding music player was successful\n";
@@ -126,8 +138,13 @@ int main(int argc, char* argv[])
             }
             else if (selectedMenu == 12)
             {
-                result = OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.speaker",
-                        onFindResource);
+                ostringstream query;
+                query << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.speaker";
+                result = OCPlatform::findResource("",
+                            query.str(),
+                            CT_DEFAULT,
+                            onFindResource);
+
                 if (OC_STACK_OK == result)
                 {
                     cout << "Finding speaker was successful\n";
@@ -161,6 +178,9 @@ int main(int argc, char* argv[])
                     }
                 }
 
+                if(It == gResourceHandleList.end())
+                    continue;
+
                 gResourceHandleList.erase(It);
                 result = OCPlatform::unregisterResource(resourceHandle);
                 if (OC_STACK_OK == result)
@@ -196,6 +216,9 @@ int main(int argc, char* argv[])
                     }
                 }
 
+                if(It == gResourceHandleList.end())
+                    continue;
+
                 gResourceHandleList.erase(It);
                 result = OCPlatform::unregisterResource(resourceHandle);
                 if (OC_STACK_OK == result)
@@ -215,7 +238,7 @@ int main(int argc, char* argv[])
     }
     catch (OCException& e)
     {
-        //log(e.what());
+        std::cout << "Exception: " << e.what() << std::endl;
     }
 
     return 0;