Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / examples / threadingsample.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 /// This sample demonstrates : running one server in main thread, another
23 /// server in a separate thread, and running 2 clients in each thread.
24 ///
25
26
27 #include <memory>
28 #include <iostream>
29 #include <stdexcept>
30 #include <condition_variable>
31 #include <map>
32 #include <vector>
33
34 #include "OCPlatform.h"
35 #include "OCApi.h"
36 using namespace OC;
37
38 static std::ostringstream requestURI;
39
40 struct FooResource
41 {
42     bool m_isFoo;
43     int m_barCount;
44     std::string m_uri;
45     std::string m_resourceType;
46     OCResourceHandle m_resourceHandle;
47     OCRepresentation m_rep;
48
49     FooResource(std::string uri): m_isFoo(true), m_barCount (0),
50                                     m_uri(uri), m_resourceType("core.foo")
51     {
52         m_rep.setUri(m_uri);
53         m_rep.setValue("isFoo", m_isFoo);
54         m_rep.setValue("barCount", m_barCount);
55     }
56
57     bool createResource()
58     {
59         std::string resourceInterface = DEFAULT_INTERFACE;
60
61         uint8_t resourceProperty = OC_DISCOVERABLE;
62
63         EntityHandler eh(std::bind(&FooResource::entityHandler, this,
64                                     std::placeholders::_1));
65         OCStackResult result = OCPlatform::registerResource(m_resourceHandle, m_uri,
66                                     m_resourceType, resourceInterface, eh, resourceProperty);
67         if(OC_STACK_OK != result)
68         {
69             std::cout<<"Resource creation unsuccessful"<<std::endl;
70             return false;
71         }
72
73         return true;
74     }
75
76     OCRepresentation get()
77     {
78         m_rep.setValue("isFoo", m_isFoo);
79         m_rep.setValue("barCount", m_barCount);
80
81         return m_rep;
82     }
83
84     void put(OCRepresentation& rep)
85     {
86         rep.getValue("isFoo", m_isFoo);
87         rep.getValue("barCount", m_barCount);
88     }
89
90     OCStackResult sendResponse(std::shared_ptr<OCResourceRequest> pRequest)
91     {
92         auto pResponse = std::make_shared<OC::OCResourceResponse>();
93         pResponse->setRequestHandle(pRequest->getRequestHandle());
94         pResponse->setResourceHandle(pRequest->getResourceHandle());
95         pResponse->setResourceRepresentation(get(), "");
96         pResponse->setErrorCode(200);
97         pResponse->setResponseResult(OC_EH_OK);
98
99         return OCPlatform::sendResponse(pResponse);
100     }
101
102     OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
103     {
104         std::cout<<"\tConsumer Entity Handler:"<<std::endl;
105         OCEntityHandlerResult ehResult = OC_EH_ERROR;
106
107         if(request)
108         {
109             // Note: Most of the handlers are not here, since this is for demoing client/server
110             // co-process existence. See simpleserver for a more complete example.
111             if(request->getRequestHandlerFlag()  == RequestHandlerFlag::RequestFlag)
112             {
113                 std::cout << "\t\trequestFlag : Request"<<std::endl;
114
115                 if(request->getRequestType() == "GET")
116                 {
117                     std::cout<<"\t\t\trequestType : GET"<<std::endl;
118                     if(OC_STACK_OK == sendResponse(request))
119                     {
120                         ehResult = OC_EH_OK;
121                     }
122                 }
123                 else if (request->getRequestType() == "PUT")
124                 {
125                     std::cout<<"\t\t\trequestType : PUT"<<std::endl;
126
127                     OCRepresentation rep = request->getResourceRepresentation();
128                     put(rep);
129                     if(OC_STACK_OK == sendResponse(request))
130                     {
131                         ehResult = OC_EH_OK;
132                     }
133                 }
134                 else
135                 {
136                     std::cout<<"\t\t\trequestType : UNSUPPORTED: " <<
137                                 request->getRequestType()<<std::endl;
138                 }
139             }
140             else
141             {
142                 std::cout <<"\t\trequestFlag : UNSUPPORTED: ";
143
144                 if(request->getRequestHandlerFlag()== RequestHandlerFlag::ObserverFlag)
145                 {
146                     std::cout<<"ObserverFlag"<<std::endl;
147                 }
148             }
149         }
150         else
151         {
152             std::cout << "Request Invalid!"<<std::endl;
153         }
154
155         return ehResult;
156     }
157 };
158
159 void putResourceInfo(const HeaderOptions& headerOptions,
160         const OCRepresentation rep, const OCRepresentation rep2, const int eCode)
161 {
162    bool m_isFoo = false;
163    int m_barCount = 0;
164    std::cout << "In PutResourceInfo" << std::endl;
165
166    std::cout <<"Clientside Put response to get was: "<<std::endl;
167    std::cout <<"ErrorCode: "<<eCode <<std::endl;
168
169    if(eCode == 0)
170    {
171         std::cout<<"Successful Put.  Attributes sent were: "<<std::endl;
172
173         rep.getValue("isFoo", m_isFoo);
174         rep.getValue("barCount", m_barCount);
175
176         std::cout << "\tisFoo: "<< m_isFoo << std::endl;
177         std::cout << "\tbarCount: "<< m_barCount << std::endl;
178
179         std::cout<<"Actual New values are: "<<std::endl;
180
181         rep.getValue("isFoo", m_isFoo);
182         rep.getValue("barCount", m_barCount);
183
184         std::cout << "\tisFoo: "<< m_isFoo << std::endl;
185         std::cout << "\tbarCount: "<< m_barCount << std::endl;
186    }
187 }
188
189 void getResourceInfo(std::shared_ptr<OCResource> resource, const HeaderOptions& headerOptions,
190             const OCRepresentation rep,
191             const int eCode)
192 {
193     bool m_isFoo = false;
194     int m_barCount = 0;
195     std::cout << "In getResourceInfo" << std::endl;
196
197     std::cout<<"Clientside response to get was: "<<std::endl;
198     std::cout<<"Error Code: "<<eCode<<std::endl;
199
200     if(eCode == 0)
201     {
202         std::cout <<"Successful Get.  Attributes are: "<<std::endl;
203
204         rep.getValue("isFoo", m_isFoo);
205         rep.getValue("barCount", m_barCount);
206
207         std::cout << "\tisFoo: "<< m_isFoo << std::endl;
208         std::cout << "\tbarCount: "<< m_barCount << std::endl;
209
210         std::cout << "Doing a put on q/foo" <<std::endl;
211         OCRepresentation rep2(rep);
212         m_isFoo = false;
213         m_barCount = 211;
214
215         rep2.setValue("isFoo", m_isFoo);
216         rep2.setValue("barCount", m_barCount);
217
218         resource->put(rep2, QueryParamsMap(),
219             PutCallback(std::bind(putResourceInfo, std::placeholders::_1,
220                  rep2, std::placeholders::_2, std::placeholders::_3)));
221     }
222 }
223
224 void printResourceInfo(std::shared_ptr<OCResource> resource)
225 {
226         std::cout << "Found Resource: "<<std::endl;
227         std::cout << "\tHost: "<< resource->host()<<std::endl;
228         std::cout << "\tURI:  "<< resource->uri()<<std::endl;
229
230         // Get the resource types
231         std::cout << "\tList of resource types: " << std::endl;
232         for(auto &resourceTypes : resource->getResourceTypes())
233         {
234             std::cout << "\t\t" << resourceTypes << std::endl;
235         }
236
237         // Get the resource interfaces
238         std::cout << "\tList of resource interfaces: " << std::endl;
239         for(auto &resourceInterfaces : resource->getResourceInterfaces())
240         {
241             std::cout << "\t\t" << resourceInterfaces << std::endl;
242         }
243 }
244
245 void foundResource2(std::shared_ptr<OCResource> resource)
246 {
247     std::cout << "In foundResource2:" << std::endl;
248
249     if(resource && resource->uri() == "/q/foo2")
250     {
251         printResourceInfo(resource);
252
253         std::cout<<"Doing a get on q/foo."<<std::endl;
254
255         resource->get(QueryParamsMap(),
256             GetCallback(std::bind(getResourceInfo, resource,
257             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)));
258     }
259     else
260     {
261         std::cout << "foundResource2: Ignoring the resource which doesn't have uri /q/foo2\n";
262     }
263 }
264
265 void foundResource1(std::shared_ptr<OCResource> resource)
266 {
267     std::cout << "In foundResource1:" << std::endl;
268     if(resource && resource->uri() == "/q/foo1")
269     {
270         printResourceInfo(resource);
271     }
272     else
273     {
274         std::cout << "foundResource1: Ignoring the resource which doesn't have uri /q/foo1\n";
275     }
276 }
277
278 void client1()
279 {
280     std::cout << "in client1\n";
281     std::cout<<"result1:" << OCPlatform::findResource("", requestURI.str(),
282             CT_DEFAULT, foundResource1)<< std::endl;
283
284     // A condition variable will free the mutex it is given, then do a non-
285     // intensive block until 'notify' is called on it.  In this case, since we
286     // don't ever call cv.notify, this should be a non-processor intensive version
287     // of while(true);
288     std::mutex blocker;
289     std::condition_variable cv;
290     std::unique_lock<std::mutex> lock(blocker);
291     cv.wait(lock);
292 }
293
294 void client2()
295 {
296     std::cout << "in client2\n";
297     std::cout<<"result2:" << OCPlatform::findResource("",
298                 requestURI.str(),
299                 CT_DEFAULT, foundResource2)<< std::endl;
300
301     // A condition variable will free the mutex it is given, then do a non-
302     // intensive block until 'notify' is called on it.  In this case, since we
303     // don't ever call cv.notify, this should be a non-processor intensive version
304     // of while(true);
305     std::mutex blocker;
306     std::condition_variable cv;
307     std::unique_lock<std::mutex> lock(blocker);
308     cv.wait(lock);
309 }
310
311 void server()
312 {
313     FooResource fooRes("/q/foo2");
314
315     if(!fooRes.createResource())
316     {
317         return;
318     }
319
320     // A condition variable will free the mutex it is given, then do a non-
321     // intensive block until 'notify' is called on it.  In this case, since we
322     // don't ever call cv.notify, this should be a non-processor intensive version
323     // of while(true);
324     std::mutex blocker;
325     std::condition_variable cv;
326     std::unique_lock<std::mutex> lock(blocker);
327     cv.wait(lock);
328 }
329
330 int main(int argc, char* argv[])
331 {
332
333     requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.foo";
334
335     PlatformConfig cfg {
336         OC::ServiceType::InProc,
337         OC::ModeType::Both,
338         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
339         0,         // Uses randomly available port
340         OC::QualityOfService::LowQos
341     };
342
343     OCPlatform::Configure(cfg);
344
345     try
346     {
347         // main thread running as server
348         FooResource fooRes("/q/foo1");
349         if(!fooRes.createResource())
350         {
351             return -1;
352         }
353
354         // Start a server in a seperate thread
355         std::thread t(server);
356         t.detach();
357
358         sleep(10);
359
360         // Start each client in a seperate thread
361         std::thread t1(client1);
362         t1.detach();
363
364         // Start each client in a seperate thread
365         std::thread t2(client2);
366         t2.detach();
367
368         // A condition variable will free the mutex it is given, then do a non-
369         // intensive block until 'notify' is called on it.  In this case, since we
370         // don't ever call cv.notify, this should be a non-processor intensive version
371         // of while(true);
372         std::mutex blocker;
373         std::condition_variable cv;
374         std::unique_lock<std::mutex> lock(blocker);
375         cv.wait(lock);
376     }
377     catch(OCException& e)
378     {
379         std::cout<< "Exception in main: "<<e.what()<<std::endl;
380     }
381
382     return 0;
383 }
384