RD query sample application
authorHabib Virji <habib.virji@samsung.com>
Tue, 25 Aug 2015 21:27:48 +0000 (22:27 +0100)
committerJon A. Cruz <jonc@osg.samsung.com>
Wed, 26 Aug 2015 06:10:03 +0000 (06:10 +0000)
Sample application to query resource published on the resource directory.

Change-Id: I783a2f39d1a3d9e9a70c142a0372f823fe927ae5
Signed-off-by: Habib Virji <habib.virji@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2268
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
service/resource-directory/samples/SConscript
service/resource-directory/samples/rd_queryClient.cpp [new file with mode: 0644]

index 14dea18..b9a2a16 100644 (file)
@@ -27,6 +27,7 @@ if env.get('SECURED') == '1':
 ######################################################################
 rd_server = rd_sample_app_env.Program('rd_server', 'rd_main.c')
 rd_publishingClient = rd_sample_app_env.Program('rd_publishingClient', 'rd_publishingClient.cpp')
+rd_queryClient = rd_sample_app_env.Program('rd_queryClient', 'rd_queryClient.cpp')
 
 Alias("resource_directory", [rd_server, rd_publishingClient])
 
diff --git a/service/resource-directory/samples/rd_queryClient.cpp b/service/resource-directory/samples/rd_queryClient.cpp
new file mode 100644 (file)
index 0000000..4a24c9f
--- /dev/null
@@ -0,0 +1,79 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#include <vector>
+
+#include "OCApi.h"
+#include "OCPlatform.h"
+
+bool g_foundResource = true;
+
+void foundResource(std::shared_ptr< OC::OCResource > resource)
+{
+    try
+    {
+        std::cout << "Found resource response." << std::endl;
+        if (resource)
+        {
+            if (resource->uri() == "/a/light")
+            {
+                std::cout << "Found Resource at @ URI: " << resource->uri() << "\tHost Address: " <<
+                          resource->host() << std::endl;
+            }
+        }
+        else
+        {
+            std::cout << "Resource is invalid " << resource->uri() << std::endl;
+        }
+        g_foundResource = false;
+        exit(0);
+    }
+    catch (std::exception &ex)
+    {
+        std::cout << "Exception: " << ex.what() << " in foundResource" << std::endl;
+        exit(1);
+    }
+}
+
+int main()
+{
+    OC::PlatformConfig cfg;
+    OC::OCPlatform::Configure(cfg);
+    bool sendRequest = true;
+
+    std::cout << "Created Platform..." << std::endl;
+
+    while (g_foundResource)
+    {
+        try
+        {
+            if (sendRequest)
+            {
+                sendRequest = false;
+                std::cout << "Finding Resource light" << std::endl;
+                OC::OCPlatform::findResource("",  "/oic/res?rt=core.light", CT_DEFAULT, &foundResource);
+            }
+        }
+        catch (OC::OCException &ex)
+        {
+            sendRequest = true;
+            std::cout << "Exception finding resources : " << ex.reason() << std::endl;
+        }
+    }
+}