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 provides steps to define an interface for a resource
23 /// (properties and methods) and host this resource on the server.
24 /// Additionally, it'll have a client example to discover it as well.
29 #include <condition_variable>
32 #include "OCPlatform.h"
36 static void printUsage()
38 std::cout<< " Usage simpleclientserver <0|1>" << std::endl;
39 std::cout<< " ConnectivityType: Default IP" << std::endl;
40 std::cout << " ConnectivityType : 0 - IPv4" << std::endl;
41 std::cout << " ConnectivityType : 1 - IPv6 (Currently Not Supported)" << std::endl;
47 void putResourceInfo(const HeaderOptions& headerOptions,
48 const OCRepresentation rep, const OCRepresentation rep2, const int eCode)
50 std::cout << "In PutResourceInfo" << std::endl;
52 std::cout <<"Clientside Put response to get was: "<<std::endl;
53 std::cout <<"ErrorCode: "<<eCode <<std::endl;
57 std::cout<<"Successful Put. Attributes sent were: "<<std::endl;
59 rep.getValue("isFoo", m_isFoo);
60 rep.getValue("barCount", m_barCount);
62 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
63 std::cout << "\tbarCount: "<< m_barCount << std::endl;
65 std::cout<<"Actual New values are: "<<std::endl;
67 rep.getValue("isFoo", m_isFoo);
68 rep.getValue("barCount", m_barCount);
70 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
71 std::cout << "\tbarCount: "<< m_barCount << std::endl;
77 void getResourceInfo(const HeaderOptions& headerOptions, const OCRepresentation rep,
80 std::cout << "In getResourceInfo" << std::endl;
82 std::cout<<"Clientside response to get was: "<<std::endl;
83 std::cout<<"Error Code: "<<eCode<<std::endl;
87 std::cout <<"Successful Get. Attributes are: "<<std::endl;
89 rep.getValue("isFoo", m_isFoo);
90 rep.getValue("barCount", m_barCount);
92 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
93 std::cout << "\tbarCount: "<< m_barCount << std::endl;
95 std::cout << "Doing a put on q/foo" <<std::endl;
96 OCRepresentation rep2(rep);
100 rep2.setValue("isFoo", m_isFoo);
101 rep2.setValue("barCount", m_barCount);
103 m_resource->put(rep2, QueryParamsMap(),
104 PutCallback(std::bind(&ClientWorker::putResourceInfo, this, std::placeholders::_1,
105 rep2, std::placeholders::_2, std::placeholders::_3)));
109 void foundResource(std::shared_ptr<OCResource> resource)
111 std::cout << "In foundResource" << std::endl;
112 if(resource && resource->uri() == "/q/foo")
115 std::lock_guard<std::mutex> lock(m_resourceLock);
121 m_resource = resource;
124 std::cout << "Found Resource: "<<std::endl;
125 std::cout << "\tHost: "<< resource->host()<<std::endl;
126 std::cout << "\tURI: "<< resource->uri()<<std::endl;
128 // Get the resource types
129 std::cout << "\tList of resource types: " << std::endl;
130 for(auto &resourceTypes : resource->getResourceTypes())
132 std::cout << "\t\t" << resourceTypes << std::endl;
135 // Get the resource interfaces
136 std::cout << "\tList of resource interfaces: " << std::endl;
137 for(auto &resourceInterfaces : resource->getResourceInterfaces())
139 std::cout << "\t\t" << resourceInterfaces << std::endl;
142 std::cout<<"Doing a get on q/foo."<<std::endl;
144 resource->get(QueryParamsMap(),
145 GetCallback(std::bind(&ClientWorker::getResourceInfo, this,
146 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)));
151 ClientWorker(OCConnectivityType ct):m_connectivityType{ct}
156 std::ostringstream requestURI;
157 requestURI << OC_MULTICAST_DISCOVERY_URI << "?rt=core.foo";
159 std::cout<<"Starting Client find:"<<std::endl;
160 FindCallback f (std::bind(&ClientWorker::foundResource, this, std::placeholders::_1));
161 std::cout<<"result:" <<
162 OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, f)
165 std::cout<<"Finding Resource..."<<std::endl;
168 std::unique_lock<std::mutex> lk(m_mutex);
174 std::mutex m_resourceLock;
175 std::condition_variable m_cv;
176 std::shared_ptr<OCResource> m_resource;
177 OCConnectivityType m_connectivityType;
186 OCResourceHandle m_resourceHandle;
187 OCRepresentation m_rep;
189 FooResource(): m_isFoo(true), m_barCount (0)
191 m_rep.setUri("/q/foo");
192 m_rep.setValue("isFoo", m_isFoo);
193 m_rep.setValue("barCount", m_barCount);
196 bool createResource()
198 std::string resourceURI = "/q/foo";
199 std::string resourceTypeName = "core.foo";
200 std::string resourceInterface = DEFAULT_INTERFACE;
202 uint8_t resourceProperty = OC_DISCOVERABLE;
204 EntityHandler eh(std::bind(&FooResource::entityHandler,
205 this, std::placeholders::_1));
206 OCStackResult result = OCPlatform::registerResource(m_resourceHandle,
207 resourceURI, resourceTypeName,
209 eh, resourceProperty);
210 if(OC_STACK_OK != result)
212 std::cout<<"Resource creation unsuccessful"<<std::endl;
219 OCRepresentation get()
221 m_rep.setValue("isFoo", m_isFoo);
222 m_rep.setValue("barCount", m_barCount);
227 void put(OCRepresentation& rep)
229 rep.getValue("isFoo", m_isFoo);
230 rep.getValue("barCount", m_barCount);
233 OCStackResult sendResponse(std::shared_ptr<OCResourceRequest> pRequest)
235 auto pResponse = std::make_shared<OC::OCResourceResponse>();
236 pResponse->setRequestHandle(pRequest->getRequestHandle());
237 pResponse->setResourceHandle(pRequest->getResourceHandle());
238 pResponse->setResourceRepresentation(get(), "");
239 pResponse->setErrorCode(200);
240 pResponse->setResponseResult(OC_EH_OK);
242 return OCPlatform::sendResponse(pResponse);
245 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
247 std::cout<<"\tConsumer Entity Handler:"<<std::endl;
248 OCEntityHandlerResult ehResult = OC_EH_ERROR;
252 // Note: Most of the handlers are not here, since this is for
253 // demoing client/server co-process existence.
254 // See simpleserver for a more complete example.
255 if(request->getRequestHandlerFlag() == RequestHandlerFlag::RequestFlag)
257 std::cout << "\t\trequestFlag : Request"<<std::endl;
259 if(request->getRequestType() == "GET")
261 std::cout<<"\t\t\trequestType : GET"<<std::endl;
262 if(OC_STACK_OK == sendResponse(request))
267 else if (request->getRequestType() == "PUT")
269 std::cout<<"\t\t\trequestType : PUT"<<std::endl;
271 OCRepresentation rep = request->getResourceRepresentation();
273 if(OC_STACK_OK == sendResponse(request))
280 std::cout<<"\t\t\trequestType : UNSUPPORTED: "<<
281 request->getRequestType()<<std::endl;
286 std::cout <<"\t\trequestFlag : UNSUPPORTED: ";
288 if(request->getRequestHandlerFlag()== RequestHandlerFlag::ObserverFlag)
290 std::cout<<"ObserverFlag"<<std::endl;
296 std::cout << "Request Invalid!"<<std::endl;
304 int main(int argc, char* argv[])
306 OCConnectivityType connectivityType = CT_ADAPTER_IP;
312 std::size_t inputValLen;
313 int optionSelected = std::stoi(argv[1], &inputValLen);
315 if(inputValLen == strlen(argv[1]))
317 if(optionSelected == 0)
319 std::cout << "Using IPv4."<< std::endl;
320 connectivityType = CT_IP_USE_V4;
322 else if(optionSelected == 1)
324 std::cout << "IPv6 is currently not supported."<< std::endl;
327 //TODO: printUsage to be removed when IPv6 is available.
328 //connectivityType = CT_IP_USE_V6;
332 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
337 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
340 catch(std::exception& )
342 std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
351 OC::ServiceType::InProc,
353 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
354 0, // Uses randomly available port
355 OC::QualityOfService::LowQos
358 OCPlatform::Configure(cfg);
364 if(!fooRes.createResource())
369 ClientWorker cw(connectivityType);
372 catch(OCException& e)
374 std::cout<< "Exception in main: "<<e.what()<<std::endl;