Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-directory / samples / rd_queryClient.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #include "OCApi.h"
21 #include "OCPlatform.h"
22
23 bool g_foundResource = true;
24
25 void foundResource(std::shared_ptr< OC::OCResource > resource)
26 {
27     try
28     {
29         std::cout << "Found resource response." << std::endl;
30         if (resource)
31         {
32             if (resource->uri() == "/a/light")
33             {
34                 std::cout << "Found Resource at @ URI: " << resource->uri() << "\tHost Address: " <<
35                           resource->host() << std::endl;
36             }
37         }
38         else
39         {
40             std::cout << "Resource is invalid " << resource->uri() << std::endl;
41         }
42         g_foundResource = false;
43         exit(0);
44     }
45     catch (std::exception &ex)
46     {
47         std::cout << "Exception: " << ex.what() << " in foundResource" << std::endl;
48         exit(1);
49     }
50 }
51
52 int main()
53 {
54     OC::PlatformConfig cfg;
55     OC::OCPlatform::Configure(cfg);
56     bool sendRequest = true;
57
58     std::cout << "Created Platform..." << std::endl;
59
60     while (g_foundResource)
61     {
62         try
63         {
64             if (sendRequest)
65             {
66                 sendRequest = false;
67                 std::cout << "Finding Resource light" << std::endl;
68                 OC::OCPlatform::findResource("",  "/oic/res?rt=core.light", CT_DEFAULT, &foundResource);
69             }
70         }
71         catch (OC::OCException &ex)
72         {
73             sendRequest = true;
74             std::cout << "Exception finding resources : " << ex.reason() << std::endl;
75         }
76     }
77 }