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>
34 #include "OCPlatform.h"
38 static std::ostringstream requestURI;
45 std::string m_resourceType;
46 OCResourceHandle m_resourceHandle;
47 OCRepresentation m_rep;
49 FooResource(std::string uri): m_isFoo(true), m_barCount (0),
50 m_uri(uri), m_resourceType("core.foo")
53 m_rep.setValue("isFoo", m_isFoo);
54 m_rep.setValue("barCount", m_barCount);
59 std::string resourceInterface = DEFAULT_INTERFACE;
61 uint8_t resourceProperty = OC_DISCOVERABLE;
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)
69 std::cout<<"Resource creation unsuccessful"<<std::endl;
76 OCRepresentation get()
78 m_rep.setValue("isFoo", m_isFoo);
79 m_rep.setValue("barCount", m_barCount);
84 void put(OCRepresentation& rep)
86 rep.getValue("isFoo", m_isFoo);
87 rep.getValue("barCount", m_barCount);
90 OCStackResult sendResponse(std::shared_ptr<OCResourceRequest> pRequest)
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);
99 return OCPlatform::sendResponse(pResponse);
102 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
104 std::cout<<"\tConsumer Entity Handler:"<<std::endl;
105 OCEntityHandlerResult ehResult = OC_EH_ERROR;
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)
113 std::cout << "\t\trequestFlag : Request"<<std::endl;
115 if(request->getRequestType() == "GET")
117 std::cout<<"\t\t\trequestType : GET"<<std::endl;
118 if(OC_STACK_OK == sendResponse(request))
123 else if (request->getRequestType() == "PUT")
125 std::cout<<"\t\t\trequestType : PUT"<<std::endl;
127 OCRepresentation rep = request->getResourceRepresentation();
129 if(OC_STACK_OK == sendResponse(request))
136 std::cout<<"\t\t\trequestType : UNSUPPORTED: " <<
137 request->getRequestType()<<std::endl;
142 std::cout <<"\t\trequestFlag : UNSUPPORTED: ";
144 if(request->getRequestHandlerFlag()==RequestHandlerFlag::InitFlag)
146 std::cout<<"InitFlag"<<std::endl;
148 else if(request->getRequestHandlerFlag()== RequestHandlerFlag::ObserverFlag)
150 std::cout<<"ObserverFlag"<<std::endl;
156 std::cout << "Request Invalid!"<<std::endl;
163 void putResourceInfo(const HeaderOptions& headerOptions,
164 const OCRepresentation rep, const OCRepresentation rep2, const int eCode)
166 bool m_isFoo = false;
168 std::cout << "In PutResourceInfo" << std::endl;
170 std::cout <<"Clientside Put response to get was: "<<std::endl;
171 std::cout <<"ErrorCode: "<<eCode <<std::endl;
175 std::cout<<"Successful Put. Attributes sent were: "<<std::endl;
177 rep.getValue("isFoo", m_isFoo);
178 rep.getValue("barCount", m_barCount);
180 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
181 std::cout << "\tbarCount: "<< m_barCount << std::endl;
183 std::cout<<"Actual New values are: "<<std::endl;
185 rep.getValue("isFoo", m_isFoo);
186 rep.getValue("barCount", m_barCount);
188 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
189 std::cout << "\tbarCount: "<< m_barCount << std::endl;
193 void getResourceInfo(std::shared_ptr<OCResource> resource, const HeaderOptions& headerOptions,
194 const OCRepresentation rep,
197 bool m_isFoo = false;
199 std::cout << "In getResourceInfo" << std::endl;
201 std::cout<<"Clientside response to get was: "<<std::endl;
202 std::cout<<"Error Code: "<<eCode<<std::endl;
206 std::cout <<"Successful Get. Attributes are: "<<std::endl;
208 rep.getValue("isFoo", m_isFoo);
209 rep.getValue("barCount", m_barCount);
211 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
212 std::cout << "\tbarCount: "<< m_barCount << std::endl;
214 std::cout << "Doing a put on q/foo" <<std::endl;
215 OCRepresentation rep2(rep);
219 rep2.setValue("isFoo", m_isFoo);
220 rep2.setValue("barCount", m_barCount);
222 resource->put(rep2, QueryParamsMap(),
223 PutCallback(std::bind(putResourceInfo, std::placeholders::_1,
224 rep2, std::placeholders::_2, std::placeholders::_3)));
228 void printResourceInfo(std::shared_ptr<OCResource> resource)
230 std::cout << "Found Resource: "<<std::endl;
231 std::cout << "\tHost: "<< resource->host()<<std::endl;
232 std::cout << "\tURI: "<< resource->uri()<<std::endl;
234 // Get the resource types
235 std::cout << "\tList of resource types: " << std::endl;
236 for(auto &resourceTypes : resource->getResourceTypes())
238 std::cout << "\t\t" << resourceTypes << std::endl;
241 // Get the resource interfaces
242 std::cout << "\tList of resource interfaces: " << std::endl;
243 for(auto &resourceInterfaces : resource->getResourceInterfaces())
245 std::cout << "\t\t" << resourceInterfaces << std::endl;
249 void foundResource2(std::shared_ptr<OCResource> resource)
251 std::cout << "In foundResource2:" << std::endl;
253 if(resource && resource->uri() == "/q/foo2")
255 printResourceInfo(resource);
257 std::cout<<"Doing a get on q/foo."<<std::endl;
259 resource->get(QueryParamsMap(),
260 GetCallback(std::bind(getResourceInfo, resource,
261 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)));
265 std::cout << "foundResource2: Ignoring the resource which doesn't have uri /q/foo2\n";
269 void foundResource1(std::shared_ptr<OCResource> resource)
271 std::cout << "In foundResource1:" << std::endl;
272 if(resource && resource->uri() == "/q/foo1")
274 printResourceInfo(resource);
278 std::cout << "foundResource1: Ignoring the resource which doesn't have uri /q/foo1\n";
284 std::cout << "in client1\n";
285 std::cout<<"result1:" << OCPlatform::findResource("", requestURI.str(),
286 OC_ALL, foundResource1)<< std::endl;
288 // A condition variable will free the mutex it is given, then do a non-
289 // intensive block until 'notify' is called on it. In this case, since we
290 // don't ever call cv.notify, this should be a non-processor intensive version
293 std::condition_variable cv;
294 std::unique_lock<std::mutex> lock(blocker);
300 std::cout << "in client2\n";
301 std::cout<<"result2:" << OCPlatform::findResource("",
303 OC_ALL, 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);
334 int main(int argc, char* argv[])
337 requestURI << OC_MULTICAST_DISCOVERY_URI << "?rt=core.foo";
340 OC::ServiceType::InProc,
342 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
343 0, // Uses randomly available port
344 OC::QualityOfService::LowQos
347 OCPlatform::Configure(cfg);
351 // main thread running as server
352 FooResource fooRes("/q/foo1");
353 if(!fooRes.createResource())
358 // Start a server in a seperate thread
359 std::thread t(server);
364 // Start each client in a seperate thread
365 std::thread t1(client1);
368 // Start each client in a seperate thread
369 std::thread t2(client2);
372 // A condition variable will free the mutex it is given, then do a non-
373 // intensive block until 'notify' is called on it. In this case, since we
374 // don't ever call cv.notify, this should be a non-processor intensive version
377 std::condition_variable cv;
378 std::unique_lock<std::mutex> lock(blocker);
381 catch(OCException& e)
383 std::cout<< "Exception in main: "<<e.what()<<std::endl;