1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 /// This sample demonstrates : running one server in main thread, another
23 /// server in a separate thread, and running 2 clients in each thread.
30 #include <condition_variable>
33 #include "OCPlatform.h"
43 std::string m_resourceType;
44 OCResourceHandle m_resourceHandle;
45 OCRepresentation m_rep;
47 FooResource(std::string uri): m_isFoo(true), m_barCount (0),
48 m_uri(uri), m_resourceType("core.foo")
51 m_rep.setValue("isFoo", m_isFoo);
52 m_rep.setValue("barCount", m_barCount);
57 std::string resourceInterface = DEFAULT_INTERFACE;
59 uint8_t resourceProperty = OC_DISCOVERABLE;
61 EntityHandler eh(std::bind(&FooResource::entityHandler, this,
62 std::placeholders::_1));
63 OCStackResult result = OCPlatform::registerResource(m_resourceHandle, m_uri,
64 m_resourceType, resourceInterface, eh, resourceProperty);
65 if(OC_STACK_OK != result)
67 std::cout<<"Resource creation unsuccessful"<<std::endl;
74 OCRepresentation get()
76 m_rep.setValue("isFoo", m_isFoo);
77 m_rep.setValue("barCount", m_barCount);
82 void put(OCRepresentation& rep)
84 rep.getValue("isFoo", m_isFoo);
85 rep.getValue("barCount", m_barCount);
88 OCStackResult sendResponse(std::shared_ptr<OCResourceRequest> pRequest)
90 auto pResponse = std::make_shared<OC::OCResourceResponse>();
91 pResponse->setRequestHandle(pRequest->getRequestHandle());
92 pResponse->setResourceHandle(pRequest->getResourceHandle());
93 pResponse->setResourceRepresentation(get(), "");
94 pResponse->setErrorCode(200);
95 pResponse->setResponseResult(OC_EH_OK);
97 return OCPlatform::sendResponse(pResponse);
100 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
102 std::cout<<"\tConsumer Entity Handler:"<<std::endl;
103 OCEntityHandlerResult ehResult = OC_EH_ERROR;
107 // Note: Most of the handlers are not here, since this is for demoing client/server
108 // co-process existence. See simpleserver for a more complete example.
109 if(request->getRequestHandlerFlag() == RequestHandlerFlag::RequestFlag)
111 std::cout << "\t\trequestFlag : Request"<<std::endl;
113 if(request->getRequestType() == "GET")
115 std::cout<<"\t\t\trequestType : GET"<<std::endl;
116 if(OC_STACK_OK == sendResponse(request))
121 else if (request->getRequestType() == "PUT")
123 std::cout<<"\t\t\trequestType : PUT"<<std::endl;
125 OCRepresentation rep = request->getResourceRepresentation();
127 if(OC_STACK_OK == sendResponse(request))
134 std::cout<<"\t\t\trequestType : UNSUPPORTED: " <<
135 request->getRequestType()<<std::endl;
140 std::cout <<"\t\trequestFlag : UNSUPPORTED: ";
142 if(request->getRequestHandlerFlag()==RequestHandlerFlag::InitFlag)
144 std::cout<<"InitFlag"<<std::endl;
146 else if(request->getRequestHandlerFlag()== RequestHandlerFlag::ObserverFlag)
148 std::cout<<"ObserverFlag"<<std::endl;
154 std::cout << "Request Invalid!"<<std::endl;
161 void putResourceInfo(const HeaderOptions& headerOptions,
162 const OCRepresentation rep, const OCRepresentation rep2, const int eCode)
164 bool m_isFoo = false;
166 std::cout << "In PutResourceInfo" << std::endl;
168 std::cout <<"Clientside Put response to get was: "<<std::endl;
169 std::cout <<"ErrorCode: "<<eCode <<std::endl;
173 std::cout<<"Successful Put. Attributes sent were: "<<std::endl;
175 rep.getValue("isFoo", m_isFoo);
176 rep.getValue("barCount", m_barCount);
178 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
179 std::cout << "\tbarCount: "<< m_barCount << std::endl;
181 std::cout<<"Actual New values are: "<<std::endl;
183 rep.getValue("isFoo", m_isFoo);
184 rep.getValue("barCount", m_barCount);
186 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
187 std::cout << "\tbarCount: "<< m_barCount << std::endl;
191 void getResourceInfo(std::shared_ptr<OCResource> resource, const HeaderOptions& headerOptions,
192 const OCRepresentation rep,
195 bool m_isFoo = false;
197 std::cout << "In getResourceInfo" << std::endl;
199 std::cout<<"Clientside response to get was: "<<std::endl;
200 std::cout<<"Error Code: "<<eCode<<std::endl;
204 std::cout <<"Successful Get. Attributes are: "<<std::endl;
206 rep.getValue("isFoo", m_isFoo);
207 rep.getValue("barCount", m_barCount);
209 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
210 std::cout << "\tbarCount: "<< m_barCount << std::endl;
212 std::cout << "Doing a put on q/foo" <<std::endl;
213 OCRepresentation rep2(rep);
217 rep2.setValue("isFoo", m_isFoo);
218 rep2.setValue("barCount", m_barCount);
220 resource->put(rep2, QueryParamsMap(),
221 PutCallback(std::bind(putResourceInfo, std::placeholders::_1,
222 rep2, std::placeholders::_2, std::placeholders::_3)));
226 void printResourceInfo(std::shared_ptr<OCResource> resource)
228 std::cout << "Found Resource: "<<std::endl;
229 std::cout << "\tHost: "<< resource->host()<<std::endl;
230 std::cout << "\tURI: "<< resource->uri()<<std::endl;
232 // Get the resource types
233 std::cout << "\tList of resource types: " << std::endl;
234 for(auto &resourceTypes : resource->getResourceTypes())
236 std::cout << "\t\t" << resourceTypes << std::endl;
239 // Get the resource interfaces
240 std::cout << "\tList of resource interfaces: " << std::endl;
241 for(auto &resourceInterfaces : resource->getResourceInterfaces())
243 std::cout << "\t\t" << resourceInterfaces << std::endl;
247 void foundResource2(std::shared_ptr<OCResource> resource)
249 std::cout << "In foundResource2:" << std::endl;
251 if(resource && resource->uri() == "/q/foo2")
253 printResourceInfo(resource);
255 std::cout<<"Doing a get on q/foo."<<std::endl;
257 resource->get(QueryParamsMap(),
258 GetCallback(std::bind(getResourceInfo, resource,
259 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)));
263 std::cout << "foundResource2: Ignoring the resource which doesn't have uri /q/foo2\n";
267 void foundResource1(std::shared_ptr<OCResource> resource)
269 std::cout << "In foundResource1:" << std::endl;
270 if(resource && resource->uri() == "/q/foo1")
272 printResourceInfo(resource);
276 std::cout << "foundResource1: Ignoring the resource which doesn't have uri /q/foo1\n";
282 std::cout << "in client1\n";
284 std::cout<<"result1:" << OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.foo",
285 foundResource1)<< std::endl;
287 // A condition variable will free the mutex it is given, then do a non-
288 // intensive block until 'notify' is called on it. In this case, since we
289 // don't ever call cv.notify, this should be a non-processor intensive version
292 std::condition_variable cv;
293 std::unique_lock<std::mutex> lock(blocker);
299 std::cout << "in client2\n";
301 std::cout<<"result2:" << OCPlatform::findResource("",
302 "coap://224.0.1.187/oc/core?rt=core.foo",
303 foundResource2)<< std::endl;
305 // A condition variable will free the mutex it is given, then do a non-
306 // intensive block until 'notify' is called on it. In this case, since we
307 // don't ever call cv.notify, this should be a non-processor intensive version
310 std::condition_variable cv;
311 std::unique_lock<std::mutex> lock(blocker);
317 FooResource fooRes("/q/foo2");
319 if(!fooRes.createResource())
324 // A condition variable will free the mutex it is given, then do a non-
325 // intensive block until 'notify' is called on it. In this case, since we
326 // don't ever call cv.notify, this should be a non-processor intensive version
329 std::condition_variable cv;
330 std::unique_lock<std::mutex> lock(blocker);
337 OC::ServiceType::InProc,
339 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
340 0, // Uses randomly available port
341 OC::QualityOfService::LowQos
344 OCPlatform::Configure(cfg);
348 // main thread running as server
349 FooResource fooRes("/q/foo1");
350 if(!fooRes.createResource())
355 // Start a server in a seperate thread
356 std::thread t(server);
361 // Start each client in a seperate thread
362 std::thread t1(client1);
365 // Start each client in a seperate thread
366 std::thread t2(client2);
369 // A condition variable will free the mutex it is given, then do a non-
370 // intensive block until 'notify' is called on it. In this case, since we
371 // don't ever call cv.notify, this should be a non-processor intensive version
374 std::condition_variable cv;
375 std::unique_lock<std::mutex> lock(blocker);
378 catch(OCException& e)
380 std::cout<< "Exception in main: "<<e.what()<<std::endl;