Modifying version number for building on tizen 3.0
[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 = OC_STACK_OK;
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         if(eCode <= OC_STACK_RESOURCE_DELETED)
86     {
87
88         AttributeMap attributeMap = rep.getAttributeMap();
89
90         for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
91         {
92             if(attributeMap.find(it->first) == attributeMap.end())
93             {
94                 return;
95             }
96         }
97
98         if(rep.getUri().empty())
99         {
100                 cout << "uri is null\n";
101             return;
102         }
103
104         std::cout << std::endl;
105         std::cout << "========================================================" << std::endl;
106         std::cout << "Receive OBSERVE RESULT:" << std::endl;
107         std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
108         for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
109         {
110             std::cout << "\tAttribute name: " << it->first << " value: ";
111             for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
112             {
113                 std::cout << "\t" << *valueItr << " ";
114             }
115
116             std::cout << std::endl;
117         }
118
119         if(observe_count() > 30)
120         {
121             std::cout << "Cancelling Observe..." << std::endl;
122             OCStackResult result = g_curResource->cancelObserve();
123
124             std::cout << "Cancel result: " << result << std::endl;
125             sleep(10);
126             std::cout << "DONE" << std::endl;
127             std::exit(0);
128         }
129     }
130     else
131     {
132         std::cout << "onObserve Response error: " << eCode << std::endl;
133         std::exit(-1);
134     }
135 }
136
137 void foundResource(std::shared_ptr< OCResource > resource)
138 {
139     std::string resourceURI;
140     std::string hostAddress;
141     try
142     {
143         if(resource)
144         {
145 //            if(resource->uri().find("/a/NM/TempHumSensor/virtual") != std::string::npos)
146                         if(resource->uri().find("/a/NM/TempHumSensor") != std::string::npos)
147             {
148                 std::cout << std::endl;
149                 std::cout << "========================================================" << std::endl;
150                 std::cout << "DISCOVERED Resource(Consumer):" << std::endl;
151
152                 resourceURI = resource->uri();
153                 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
154
155                 hostAddress = resource->host();
156                 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
157
158                 startObserve(resource);
159             }
160         }
161         else
162         {
163             std::cout << "Resource is invalid" << std::endl;
164         }
165
166     }
167     catch(std::exception& e)
168     {
169     }
170 }
171
172 OCStackResult nmfindResource(const std::string& host , const std::string& resourceName)
173 {
174     return OCPlatform::findResource(host , resourceName , &foundResource);
175 }
176
177 void getRepresentation(std::shared_ptr< OCResource > resource)
178 {
179     if(resource)
180     {
181         std::cout << "Getting Light Representation..." << std::endl;
182     }
183 }
184
185 void onPut(const OCRepresentation& rep , const int eCode)
186 {
187     if(eCode == SUCCESS_RESPONSE)
188     {
189         std::cout << "PUT request was successful" << std::endl;
190
191         AttributeMap attributeMap = rep.getAttributeMap();
192
193         for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
194         {
195             std::cout << "\tAttribute name: " << it->first << " value: ";
196             for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
197             {
198                 std::cout << "\t" << *valueItr << " ";
199             }
200
201             std::cout << std::endl;
202         }
203
204         std::vector< OCRepresentation > children = rep.getChildren();
205
206         for(auto oit = children.begin() ; oit != children.end() ; ++oit)
207         {
208             attributeMap = oit->getAttributeMap();
209
210             for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
211             {
212                 std::cout << "\tAttribute name: " << it->first << " value: ";
213                 for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
214                 {
215                     std::cout << "\t" << *valueItr << " ";
216                 }
217
218                 std::cout << std::endl;
219             }
220         }
221
222         if(OBSERVE_TYPE_TO_USE == ObserveType::Observe)
223             std::cout << std::endl << "Observe is used." << std::endl << std::endl;
224         else if(OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
225             std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
226
227         QueryParamsMap test;
228
229         g_curResource->observe(ObserveType::Observe , test , &onObserve);
230
231     }
232     else
233     {
234         std::cout << "onPut Response error: " << eCode << std::endl;
235         std::exit(-1);
236     }
237 }
238
239 // callback handler on GET request
240 void onGet(const HeaderOptions &headerOption , const OCRepresentation& rep , const int eCode)
241 {
242     if(eCode == SUCCESS_RESPONSE)
243     {
244         std::cout << "GET request was successful" << std::endl;
245
246         AttributeMap attributeMap = rep.getAttributeMap();
247
248         std::cout << "Resource URI: " << rep.getUri() << std::endl;
249
250         for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
251         {
252             std::cout << "\tAttribute name: " << it->first << " value: ";
253             for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
254             {
255                 std::cout << "\t" << *valueItr << " ";
256             }
257
258             std::cout << std::endl;
259         }
260
261         std::vector< OCRepresentation > children = rep.getChildren();
262
263         for(auto oit = children.begin() ; oit != children.end() ; ++oit)
264         {
265             std::cout << "Child Resource URI: " << oit->getUri() << std::endl;
266
267             attributeMap = oit->getAttributeMap();
268
269             for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
270             {
271                 std::cout << "\tAttribute name: " << it->first << " value: ";
272                 for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
273                 {
274                     std::cout << "\t" << *valueItr << " ";
275                 }
276
277                 std::cout << std::endl;
278             }
279         }
280     }
281     else
282     {
283         std::cout << "onGET Response error: " << eCode << std::endl;
284         std::exit(-1);
285     }
286 }
287
288 void getLightRepresentation(std::shared_ptr< OCResource > resource)
289 {
290     if(resource)
291     {
292         std::cout << "Getting Light Representation..." << std::endl;
293
294         QueryParamsMap test;
295         resource->get(test , &onGet);
296     }
297 }
298
299 void PrintUsage()
300 {
301     std::cout << std::endl;
302     std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
303     std::cout << "   ObserveType : 1 - Observe" << std::endl;
304     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
305 }
306
307 int main(int argc , char* argv[])
308 {
309
310     if(argc == 1)
311     {
312         OBSERVE_TYPE_TO_USE = ObserveType::Observe;
313     }
314     else if(argc == 2)
315     {
316         int value = atoi(argv[1]);
317         if(value == 1)
318             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
319         else if(value == 2)
320             OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
321         else
322             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
323     }
324     else
325     {
326         PrintUsage();
327         return -1;
328     }
329
330     PlatformConfig cfg;
331
332     OCPlatform::Configure(cfg);
333
334     std::cout << "Created Platform..." << std::endl;
335     findResourceCandidate();
336
337     return 0;
338 }
339