Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / notification-manager / SampleApp / linux / sampleConsumer / SampleConsumer.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
21 /**
22  * @file SampleConsumer.cpp
23  * @brief Defines the entry point for the sample consumer application about Resource Hosting.
24  */
25
26 #include <string>
27 #include <cstdlib>
28 #include <pthread.h>
29 #include "OCPlatform.h"
30 #include "OCApi.h"
31 #include <mutex>
32
33 using namespace OC;
34
35 const int SUCCESS_RESPONSE = OC_STACK_OK;
36
37 #define OC_WELL_KNOWN_COORDINATING_QUERY "/oic/res?rt=Resource.Hosting"
38
39 #define OBSERVE 1
40 #define GET     2
41 #define PUT     3
42 #define DELETE  4
43
44 std::shared_ptr< OCResource > g_curResource;
45 std::shared_ptr< OCResource > g_curObserveResource;
46 std::mutex curResourceLock;
47
48 OCStackResult nmfindResource(const std::string &host , const std::string &resourceName);
49 void onObserve(const HeaderOptions &headerOption , const OCRepresentation &rep , const int &eCode,
50                const int &sequenceNumber);
51
52 void onPut(const HeaderOptions &headerOption, const OCRepresentation &rep, const int eCode);
53 void onGet(const HeaderOptions &headerOption , const OCRepresentation &rep , const int eCode);
54 void onDelete(const HeaderOptions &headerOption , const int eCode);
55
56
57 void findResourceCandidate()
58 {
59     try
60     {
61         nmfindResource("" , OC_WELL_KNOWN_COORDINATING_QUERY);
62         std::cout << "Finding Resource... " << std::endl;
63
64     }
65     catch (OCException &e)
66     {
67         std::cout << "Exception for find resource : " << e.reason() << std::endl;
68     }
69 }
70
71 void startObserve(std::shared_ptr< OCResource > resource)
72 {
73     if (resource == NULL)
74     {
75         std::cout << "startObserve() error : resource == null" << std::endl;
76         return;
77     }
78
79     if(g_curObserveResource == NULL)
80     {
81         g_curObserveResource = resource;
82         std::cout << "request for new observation" << std::endl;
83     }
84     else if(g_curObserveResource == g_curResource)
85     {
86         std::cout << "already registered same observation" << std::endl;
87         return;
88     }
89     else
90     {
91         std::cout << "change observed resource" << std::endl;
92         g_curObserveResource->cancelObserve();
93         g_curObserveResource = resource;
94     }
95
96     QueryParamsMap test;
97     if (OC_STACK_OK != resource->observe(ObserveType::Observe , test , &onObserve))
98         std::cout << "To Fail resource observe() process" << std::endl;
99 }
100
101 void startGet(std::shared_ptr< OCResource > resource)
102 {
103
104     if (resource == NULL)
105     {
106         std::cout << "startObserve() error : resource == null" << std::endl;
107         return;
108     }
109
110     QueryParamsMap test;
111     std::cout << "URI :" << resource->uri() << std::endl;
112     if (OC_STACK_OK != resource->get(test, &onGet))
113         std::cout << "To Fail resource get() process" << std::endl;
114 }
115
116 void startPut(std::shared_ptr< OCResource > resource)
117 {
118     if (resource == NULL)
119     {
120         std::cout << "startObserve() error : resource == null" << std::endl;
121         return;
122     }
123
124     g_curResource = resource;
125     OCRepresentation rep;
126     rep.setValue("temperature", 25);
127     rep.setValue("humidity", 10);
128
129     QueryParamsMap test;
130     if (OC_STACK_OK != resource->put(rep, test, &onPut))
131         std::cout << "To Fail resource put() process" << std::endl;
132 }
133
134 void startDelete(std::shared_ptr< OCResource > resource)
135 {
136     if (resource == NULL)
137     {
138         std::cout << "startObserve() error : resource == null" << std::endl;
139         return;
140     }
141
142     g_curResource = resource;
143     if (OC_STACK_OK != resource->deleteResource(&onDelete))
144         std::cout << "To Fail resource delete() process" << std::endl;
145 }
146
147 int observe_count()
148 {
149     static int oc = 0;
150     return ++oc;
151 }
152
153 void onObserve(const HeaderOptions &/*headerOption*/, const OCRepresentation &rep , const int &eCode,
154                const int &sequenceNumber)
155 {
156     std::cout << "onObserve" << std::endl;
157
158     if (eCode <= OC_STACK_OK)
159     {
160         std::cout << std::endl;
161         std::cout << "========================================================" << std::endl;
162         std::cout << "Receive OBSERVE RESULT:" << std::endl;
163         std::cout << "\tUri: " << rep.getUri() << std::endl;
164         std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
165         std::cout << "\tTemperature : " << rep.getValue<int>("temperature") << std::endl;
166         std::cout << "\tHumidity : " << rep.getValue<int>("humidity") << std::endl;
167
168         if (observe_count() > 30)
169         {
170             std::cout << "Cancelling Observe..." << std::endl;
171             OCStackResult result = g_curResource->cancelObserve();
172
173             std::cout << "Cancel result: " << result << std::endl;
174             sleep(10);
175             std::cout << "DONE" << std::endl;
176             std::exit(0);
177         }
178     }
179     else
180     {
181         std::cout << "onObserve Response error: " << eCode << std::endl;
182         std::exit(-1);
183     }
184 }
185
186 void foundResource(std::shared_ptr< OCResource > resource)
187 {
188     std::string resourceURI;
189     std::string hostAddress;
190
191     std::cout << "foundResource" << std::endl;
192
193     try
194     {
195         std::cout << "mutex lock passed" << std::endl;
196         if (resource)
197         {
198             std::cout << resource->uri() << std::endl;
199             if (resource->uri() == "/a/TempHumSensor")
200             {
201                 std::cout << std::endl;
202                 std::cout << "========================================================" << std::endl;
203                 std::cout << "DISCOVERED Resource(Consumer):" << std::endl;
204
205                 resourceURI = resource->uri();
206                 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
207
208                 hostAddress = resource->host();
209                 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
210
211                 g_curResource = resource;
212             }
213         }
214         else
215         {
216             std::cout << "Resource is invalid" << std::endl;
217         }
218
219     }
220     catch (std::exception &e)
221     {
222         std::cout << "Exception: " << e.what() << " in foundResource" << std::endl;
223     }
224 }
225
226 OCStackResult nmfindResource(const std::string &host , const std::string &resourceName)
227 {
228     return OCPlatform::findResource(host , resourceName , CT_DEFAULT, &foundResource);
229 }
230
231 void getRepresentation(std::shared_ptr< OCResource > resource)
232 {
233     if (resource)
234     {
235         std::cout << "Getting Light Representation..." << std::endl;
236     }
237 }
238
239 void onPut(const HeaderOptions &/*headerOption*/, const OCRepresentation &rep, const int eCode)
240 {
241     try
242     {
243         if (eCode == OC_STACK_OK)
244         {
245             std::cout << "PUT request was successful" << std::endl;
246             int humidity;
247             int temperature;
248             rep.getValue("temperature", temperature);
249             rep.getValue("humidity", humidity);
250
251
252             std::cout << "\t temperature: " << temperature << std::endl;
253             std::cout << "\t humidity: " << humidity << std::endl;
254         }
255         else
256         {
257             std::cout << "onPut Response error: " << eCode << std::endl;
258             std::exit(-1);
259         }
260     }
261     catch (std::exception &e)
262     {
263         std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
264     }
265 }
266
267 //callback hadnler on DELETE request
268 void onDelete(const HeaderOptions &/*headerOption*/, const int eCode)
269 {
270     try
271     {
272         if (eCode == OC_STACK_RESOURCE_DELETED)
273         {
274             std::cout << "DELETE request was successful" << std::endl;
275         }
276         else
277         {
278             std::cout << "onDelete Response error: " << eCode << std::endl;
279             std::exit(-1);
280         }
281     }
282     catch (std::exception &e)
283     {
284         std::cout << "Exception: " << e.what() << " in onDelete" << std::endl;
285     }
286 }
287
288 // callback handler on GET request
289 void onGet(const HeaderOptions &/*headerOption*/, const OCRepresentation &rep , const int eCode)
290 {
291     std::cout << "GET request was successful1" << std::endl;
292     if (eCode == SUCCESS_RESPONSE)
293     {
294         std::cout << "GET request was successful" << std::endl;
295         std::cout << "Resource URI: " << rep.getUri().c_str() << std::endl;
296         std::cout << "\tTemperature : " << rep.getValue<int>("temperature") << std::endl;
297         std::cout << "\tHumidity : " << rep.getValue<int>("humidity") << std::endl;
298     }
299     else
300     {
301         std::cout << "onGET Response error: " << eCode << std::endl;
302         std::exit(-1);
303     }
304 }
305
306 void getLightRepresentation(std::shared_ptr< OCResource > resource)
307 {
308     if (resource)
309     {
310         std::cout << "Getting Light Representation..." << std::endl;
311
312         QueryParamsMap test;
313         resource->get(test , &onGet);
314     }
315 }
316
317 void PrintUsage()
318 {
319     std::cout << std::endl;
320     std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
321     std::cout << "   ObserveType : 1 - Observe" << std::endl;
322     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
323 }
324
325 void PRINT()
326 {
327     std::cout << std::endl;
328     std::cout << "********************************************" << std::endl;
329     std::cout << "*  method Type : 1 - Observe               *" << std::endl;
330     std::cout << "*  method Type : 2 - Get                   *" << std::endl;
331     std::cout << "*  method Type : 3 - Put                   *" << std::endl;
332     std::cout << "*  method Type : 4 - Delete                *" << std::endl;
333     std::cout << "********************************************" << std::endl;
334     std::cout << std::endl;
335 }
336
337 int main()
338 {
339
340     int in;
341     PlatformConfig cfg;
342
343     OCPlatform::Configure(cfg);
344
345     std::cout << "Created Platform..." << std::endl;
346
347     g_curResource = NULL;
348     g_curObserveResource = NULL;
349
350     findResourceCandidate();
351
352     while (1)
353     {
354         sleep(2);
355         if(g_curResource == NULL)
356         {
357             continue;
358         }
359         PRINT();
360
361         in = 0;
362         std::cin >> in;
363
364         if(std::cin.fail())
365         {
366             std::cin.clear();
367             std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
368             std::cout << "Invalid input type, please try again" << std::endl;
369             continue;
370         }
371
372         try {
373             switch ((int)in)
374             {
375                 case OBSERVE:
376                     startObserve(g_curResource);
377                     break;
378                 case GET:
379                     startGet(g_curResource);
380                     break;
381                 case PUT:
382                     startPut(g_curResource);
383                     break;
384                 case DELETE:
385                     startDelete(g_curResource);
386                     break;
387                 default:
388                     std::cout << "Invalid input, please try again" << std::endl;
389                     break;
390             }
391         }catch(OCException & e) {
392             std::cout<< "Caught OCException [Code: "<<e.code()<<" Reason: "<<e.reason()<<std::endl;
393         }
394     }
395
396     return 0;
397 }