6f4cd868a429c9f6caf5f71d8561469d1c37061d
[platform/upstream/iotivity.git] / service / notification-manager / SampleApp / linux / sampleConsumer / SampleConsumer.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 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 // OCClient.cpp : Defines the entry point for the console application.
22 //
23
24 #include <string>
25 #include <cstdlib>
26 #include <pthread.h>
27 #include "OCPlatform.h"
28 #include "OCApi.h"
29
30 using namespace OC;
31
32 const int SUCCESS_RESPONSE = 0;
33 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
34
35 std::shared_ptr< OCResource > g_curResource;
36
37 OCStackResult nmfindResource(const std::string& host , const std::string& resourceName);
38 void onObserve(const HeaderOptions &headerOption , const OCRepresentation& rep , const int& eCode, const int& sequenceNumber);
39
40 void findResourceCandidate()
41 {
42     try
43     {
44         nmfindResource("" , "coap://224.0.1.187/oc/core?rt=NotificationManager.Hosting");
45         std::cout << "Finding Resource... " << std::endl;
46         while(true)
47         {
48                 char signal;
49                         cin >> signal;
50
51                         switch(signal)
52                         {
53                         case 'q':
54                         case 'Q':
55                                 exit(-1);
56                         default:
57                                 break;
58                         }
59         }
60
61     }
62     catch(OCException& e)
63     {
64     }
65 }
66
67 void startObserve(std::shared_ptr< OCResource > resource)
68 {
69     g_curResource = resource;
70
71     QueryParamsMap test;
72     resource->observe(ObserveType::Observe , test , &onObserve);
73 }
74
75 int observe_count()
76 {
77     static int oc = 0;
78     return ++oc;
79 }
80
81 void onObserve(const HeaderOptions &headerOption , const OCRepresentation& rep , const int& eCode, const int& sequenceNumber)
82 {
83         std::cout << "onObserve" << std::endl;
84     if(eCode == SUCCESS_RESPONSE)
85     {
86
87         AttributeMap attributeMap = rep.getAttributeMap();
88
89         for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
90         {
91             if(attributeMap.find(it->first) == attributeMap.end())
92             {
93                 return;
94             }
95         }
96
97         if(rep.getUri().empty())
98         {
99             return;
100         }
101
102         std::cout << std::endl;
103         std::cout << "========================================================" << std::endl;
104         std::cout << "Receive OBSERVE RESULT:" << std::endl;
105         std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
106         for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
107         {
108             std::cout << "\tAttribute name: " << it->first << " value: ";
109             for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
110             {
111                 std::cout << "\t" << *valueItr << " ";
112             }
113
114             std::cout << std::endl;
115         }
116
117         if(observe_count() > 30)
118         {
119             std::cout << "Cancelling Observe..." << std::endl;
120             OCStackResult result = g_curResource->cancelObserve();
121
122             std::cout << "Cancel result: " << result << std::endl;
123             sleep(10);
124             std::cout << "DONE" << std::endl;
125             std::exit(0);
126         }
127     }
128     else
129     {
130         std::cout << "onObserve Response error: " << eCode << std::endl;
131         std::exit(-1);
132     }
133 }
134
135 void foundResource(std::shared_ptr< OCResource > resource)
136 {
137     std::string resourceURI;
138     std::string hostAddress;
139     try
140     {
141         if(resource)
142         {
143             if(resource->uri().find("/a/NM/TempHumSensor/virtual") != std::string::npos)
144             {
145                 std::cout << std::endl;
146                 std::cout << "========================================================" << std::endl;
147                 std::cout << "DISCOVERED Resource(Consumer):" << std::endl;
148
149                 resourceURI = resource->uri();
150                 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
151
152                 hostAddress = resource->host();
153                 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
154
155                 startObserve(resource);
156             }
157         }
158         else
159         {
160             std::cout << "Resource is invalid" << std::endl;
161         }
162
163     }
164     catch(std::exception& e)
165     {
166     }
167 }
168
169 OCStackResult nmfindResource(const std::string& host , const std::string& resourceName)
170 {
171     return OCPlatform::findResource(host , resourceName , &foundResource);
172 }
173
174 void getRepresentation(std::shared_ptr< OCResource > resource)
175 {
176     if(resource)
177     {
178         std::cout << "Getting Light Representation..." << std::endl;
179     }
180 }
181
182 void onPut(const OCRepresentation& rep , const int eCode)
183 {
184     if(eCode == SUCCESS_RESPONSE)
185     {
186         std::cout << "PUT request was successful" << std::endl;
187
188         AttributeMap attributeMap = rep.getAttributeMap();
189
190         for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
191         {
192             std::cout << "\tAttribute name: " << it->first << " value: ";
193             for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
194             {
195                 std::cout << "\t" << *valueItr << " ";
196             }
197
198             std::cout << std::endl;
199         }
200
201         std::vector< OCRepresentation > children = rep.getChildren();
202
203         for(auto oit = children.begin() ; oit != children.end() ; ++oit)
204         {
205             attributeMap = oit->getAttributeMap();
206
207             for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
208             {
209                 std::cout << "\tAttribute name: " << it->first << " value: ";
210                 for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
211                 {
212                     std::cout << "\t" << *valueItr << " ";
213                 }
214
215                 std::cout << std::endl;
216             }
217         }
218
219         if(OBSERVE_TYPE_TO_USE == ObserveType::Observe)
220             std::cout << std::endl << "Observe is used." << std::endl << std::endl;
221         else if(OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
222             std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
223
224         QueryParamsMap test;
225
226         g_curResource->observe(ObserveType::Observe , test , &onObserve);
227
228     }
229     else
230     {
231         std::cout << "onPut Response error: " << eCode << std::endl;
232         std::exit(-1);
233     }
234 }
235
236 // callback handler on GET request
237 void onGet(const HeaderOptions &headerOption , const OCRepresentation& rep , const int eCode)
238 {
239     if(eCode == SUCCESS_RESPONSE)
240     {
241         std::cout << "GET request was successful" << std::endl;
242
243         AttributeMap attributeMap = rep.getAttributeMap();
244
245         std::cout << "Resource URI: " << rep.getUri() << std::endl;
246
247         for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
248         {
249             std::cout << "\tAttribute name: " << it->first << " value: ";
250             for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
251             {
252                 std::cout << "\t" << *valueItr << " ";
253             }
254
255             std::cout << std::endl;
256         }
257
258         std::vector< OCRepresentation > children = rep.getChildren();
259
260         for(auto oit = children.begin() ; oit != children.end() ; ++oit)
261         {
262             std::cout << "Child Resource URI: " << oit->getUri() << std::endl;
263
264             attributeMap = oit->getAttributeMap();
265
266             for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
267             {
268                 std::cout << "\tAttribute name: " << it->first << " value: ";
269                 for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
270                 {
271                     std::cout << "\t" << *valueItr << " ";
272                 }
273
274                 std::cout << std::endl;
275             }
276         }
277     }
278     else
279     {
280         std::cout << "onGET Response error: " << eCode << std::endl;
281         std::exit(-1);
282     }
283 }
284
285 void getLightRepresentation(std::shared_ptr< OCResource > resource)
286 {
287     if(resource)
288     {
289         std::cout << "Getting Light Representation..." << std::endl;
290
291         QueryParamsMap test;
292         resource->get(test , &onGet);
293     }
294 }
295
296 void PrintUsage()
297 {
298     std::cout << std::endl;
299     std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
300     std::cout << "   ObserveType : 1 - Observe" << std::endl;
301     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
302 }
303
304 int main(int argc , char* argv[])
305 {
306
307     if(argc == 1)
308     {
309         OBSERVE_TYPE_TO_USE = ObserveType::Observe;
310     }
311     else if(argc == 2)
312     {
313         int value = atoi(argv[1]);
314         if(value == 1)
315             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
316         else if(value == 2)
317             OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
318         else
319             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
320     }
321     else
322     {
323         PrintUsage();
324         return -1;
325     }
326
327     PlatformConfig cfg;
328
329     OCPlatform::Configure(cfg);
330
331     std::cout << "Created Platform..." << std::endl;
332     findResourceCandidate();
333
334     return 0;
335 }
336