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::ObserverFlag)
146 std::cout<<"ObserverFlag"<<std::endl;
152 std::cout << "Request Invalid!"<<std::endl;
159 void putResourceInfo(const HeaderOptions& /*headerOptions*/,
160 const OCRepresentation rep, const OCRepresentation /*rep2*/, const int eCode)
162 bool m_isFoo = false;
164 std::cout << "In PutResourceInfo" << std::endl;
166 std::cout <<"Clientside Put response to get was: "<<std::endl;
167 std::cout <<"ErrorCode: "<<eCode <<std::endl;
171 std::cout<<"Successful Put. Attributes sent were: "<<std::endl;
173 rep.getValue("isFoo", m_isFoo);
174 rep.getValue("barCount", m_barCount);
176 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
177 std::cout << "\tbarCount: "<< m_barCount << std::endl;
179 std::cout<<"Actual New values are: "<<std::endl;
181 rep.getValue("isFoo", m_isFoo);
182 rep.getValue("barCount", m_barCount);
184 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
185 std::cout << "\tbarCount: "<< m_barCount << std::endl;
189 void getResourceInfo(std::shared_ptr<OCResource> resource, const HeaderOptions& /*headerOptions*/,
190 const OCRepresentation rep,
193 bool m_isFoo = false;
195 std::cout << "In getResourceInfo" << std::endl;
197 std::cout<<"Clientside response to get was: "<<std::endl;
198 std::cout<<"Error Code: "<<eCode<<std::endl;
202 std::cout <<"Successful Get. Attributes are: "<<std::endl;
204 rep.getValue("isFoo", m_isFoo);
205 rep.getValue("barCount", m_barCount);
207 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
208 std::cout << "\tbarCount: "<< m_barCount << std::endl;
210 std::cout << "Doing a put on q/foo" <<std::endl;
211 OCRepresentation rep2(rep);
215 rep2.setValue("isFoo", m_isFoo);
216 rep2.setValue("barCount", m_barCount);
218 resource->put(rep2, QueryParamsMap(),
219 PutCallback(std::bind(putResourceInfo, std::placeholders::_1,
220 rep2, std::placeholders::_2, std::placeholders::_3)));
224 void printResourceInfo(std::shared_ptr<OCResource> resource)
226 std::cout << "Found Resource: "<<std::endl;
227 std::cout << "\tHost: "<< resource->host()<<std::endl;
228 std::cout << "\tURI: "<< resource->uri()<<std::endl;
230 // Get the resource types
231 std::cout << "\tList of resource types: " << std::endl;
232 for(auto &resourceTypes : resource->getResourceTypes())
234 std::cout << "\t\t" << resourceTypes << std::endl;
237 // Get the resource interfaces
238 std::cout << "\tList of resource interfaces: " << std::endl;
239 for(auto &resourceInterfaces : resource->getResourceInterfaces())
241 std::cout << "\t\t" << resourceInterfaces << std::endl;
245 void foundResource2(std::shared_ptr<OCResource> resource)
247 std::cout << "In foundResource2:" << std::endl;
249 if(resource && resource->uri() == "/q/foo2")
251 printResourceInfo(resource);
253 std::cout<<"Doing a get on q/foo."<<std::endl;
255 resource->get(QueryParamsMap(),
256 GetCallback(std::bind(getResourceInfo, resource,
257 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)));
261 std::cout << "foundResource2: Ignoring the resource which doesn't have uri /q/foo2\n";
265 void foundResource1(std::shared_ptr<OCResource> resource)
267 std::cout << "In foundResource1:" << std::endl;
268 if(resource && resource->uri() == "/q/foo1")
270 printResourceInfo(resource);
274 std::cout << "foundResource1: Ignoring the resource which doesn't have uri /q/foo1\n";
280 std::cout << "in client1\n";
281 std::cout<<"result1:" << OCPlatform::findResource("", requestURI.str(),
282 CT_DEFAULT, foundResource1)<< std::endl;
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
289 std::condition_variable cv;
290 std::unique_lock<std::mutex> lock(blocker);
296 std::cout << "in client2\n";
297 std::cout<<"result2:" << OCPlatform::findResource("",
299 CT_DEFAULT, foundResource2)<< std::endl;
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
306 std::condition_variable cv;
307 std::unique_lock<std::mutex> lock(blocker);
313 FooResource fooRes("/q/foo2");
315 if(!fooRes.createResource())
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
325 std::condition_variable cv;
326 std::unique_lock<std::mutex> lock(blocker);
330 int main(int /*argc*/, char** /*argv[]*/)
333 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.foo";
336 OC::ServiceType::InProc,
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
343 OCPlatform::Configure(cfg);
347 // main thread running as server
348 FooResource fooRes("/q/foo1");
349 if(!fooRes.createResource())
354 // Start a server in a seperate thread
355 std::thread t(server);
360 // Start each client in a seperate thread
361 std::thread t1(client1);
364 // Start each client in a seperate thread
365 std::thread t2(client2);
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
373 std::condition_variable cv;
374 std::unique_lock<std::mutex> lock(blocker);
377 catch(OCException& e)
379 std::cout<< "Exception in main: "<<e.what()<<std::endl;