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 - IP" << std::endl;
46 void putResourceInfo(const HeaderOptions& /*headerOptions*/,
47 const OCRepresentation rep, const OCRepresentation /*rep2*/, const int eCode)
49 std::cout << "In PutResourceInfo" << std::endl;
51 std::cout <<"Clientside Put response to get was: "<<std::endl;
52 std::cout <<"ErrorCode: "<<eCode <<std::endl;
56 std::cout<<"Successful Put. Attributes sent were: "<<std::endl;
58 rep.getValue("isFoo", m_isFoo);
59 rep.getValue("barCount", m_barCount);
61 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
62 std::cout << "\tbarCount: "<< m_barCount << std::endl;
64 std::cout<<"Actual New values are: "<<std::endl;
66 rep.getValue("isFoo", m_isFoo);
67 rep.getValue("barCount", m_barCount);
69 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
70 std::cout << "\tbarCount: "<< m_barCount << std::endl;
76 void getResourceInfo(const HeaderOptions& /*headerOptions*/, const OCRepresentation rep,
79 std::cout << "In getResourceInfo" << std::endl;
81 std::cout<<"Clientside response to get was: "<<std::endl;
82 std::cout<<"Error Code: "<<eCode<<std::endl;
86 std::cout <<"Successful Get. Attributes are: "<<std::endl;
88 rep.getValue("isFoo", m_isFoo);
89 rep.getValue("barCount", m_barCount);
91 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
92 std::cout << "\tbarCount: "<< m_barCount << std::endl;
94 std::cout << "Doing a put on q/foo" <<std::endl;
95 OCRepresentation rep2(rep);
99 rep2.setValue("isFoo", m_isFoo);
100 rep2.setValue("barCount", m_barCount);
102 m_resource->put(rep2, QueryParamsMap(),
103 PutCallback(std::bind(&ClientWorker::putResourceInfo, this, std::placeholders::_1,
104 rep2, std::placeholders::_2, std::placeholders::_3)));
108 void foundResource(std::shared_ptr<OCResource> resource)
110 std::cout << "In foundResource" << std::endl;
111 if(resource && resource->uri() == "/q/foo")
114 std::lock_guard<std::mutex> lock(m_resourceLock);
120 m_resource = resource;
123 std::cout << "Found Resource: "<<std::endl;
124 std::cout << "\tHost: "<< resource->host()<<std::endl;
125 std::cout << "\tURI: "<< resource->uri()<<std::endl;
127 // Get the resource types
128 std::cout << "\tList of resource types: " << std::endl;
129 for(auto &resourceTypes : resource->getResourceTypes())
131 std::cout << "\t\t" << resourceTypes << std::endl;
134 // Get the resource interfaces
135 std::cout << "\tList of resource interfaces: " << std::endl;
136 for(auto &resourceInterfaces : resource->getResourceInterfaces())
138 std::cout << "\t\t" << resourceInterfaces << std::endl;
141 std::cout<<"Doing a get on q/foo."<<std::endl;
143 resource->get(QueryParamsMap(),
144 GetCallback(std::bind(&ClientWorker::getResourceInfo, this,
145 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)));
150 ClientWorker(OCConnectivityType ct):m_connectivityType{ct}
155 std::ostringstream requestURI;
156 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.foo";
158 std::cout<<"Starting Client find:"<<std::endl;
159 FindCallback f (std::bind(&ClientWorker::foundResource, this, std::placeholders::_1));
160 std::cout<<"result:" <<
161 OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, f)
164 std::cout<<"Finding Resource..."<<std::endl;
167 std::unique_lock<std::mutex> lk(m_mutex);
173 std::mutex m_resourceLock;
174 std::condition_variable m_cv;
175 std::shared_ptr<OCResource> m_resource;
176 OCConnectivityType m_connectivityType;
185 OCResourceHandle m_resourceHandle;
186 OCRepresentation m_rep;
188 FooResource(): m_isFoo(true), m_barCount (0)
190 m_rep.setUri("/q/foo");
191 m_rep.setValue("isFoo", m_isFoo);
192 m_rep.setValue("barCount", m_barCount);
195 bool createResource()
197 std::string resourceURI = "/q/foo";
198 std::string resourceTypeName = "core.foo";
199 std::string resourceInterface = DEFAULT_INTERFACE;
201 uint8_t resourceProperty = OC_DISCOVERABLE;
203 EntityHandler eh(std::bind(&FooResource::entityHandler,
204 this, std::placeholders::_1));
205 OCStackResult result = OCPlatform::registerResource(m_resourceHandle,
206 resourceURI, resourceTypeName,
208 eh, resourceProperty);
209 if(OC_STACK_OK != result)
211 std::cout<<"Resource creation unsuccessful"<<std::endl;
218 OCRepresentation get()
220 m_rep.setValue("isFoo", m_isFoo);
221 m_rep.setValue("barCount", m_barCount);
226 void put(OCRepresentation& rep)
228 rep.getValue("isFoo", m_isFoo);
229 rep.getValue("barCount", m_barCount);
232 OCStackResult sendResponse(std::shared_ptr<OCResourceRequest> pRequest)
234 auto pResponse = std::make_shared<OC::OCResourceResponse>();
235 pResponse->setRequestHandle(pRequest->getRequestHandle());
236 pResponse->setResourceHandle(pRequest->getResourceHandle());
237 pResponse->setResourceRepresentation(get(), "");
238 pResponse->setErrorCode(200);
239 pResponse->setResponseResult(OC_EH_OK);
241 return OCPlatform::sendResponse(pResponse);
244 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
246 std::cout<<"\tConsumer Entity Handler:"<<std::endl;
247 OCEntityHandlerResult ehResult = OC_EH_ERROR;
251 // Note: Most of the handlers are not here, since this is for
252 // demoing client/server co-process existence.
253 // See simpleserver for a more complete example.
254 if(request->getRequestHandlerFlag() == RequestHandlerFlag::RequestFlag)
256 std::cout << "\t\trequestFlag : Request"<<std::endl;
258 if(request->getRequestType() == "GET")
260 std::cout<<"\t\t\trequestType : GET"<<std::endl;
261 if(OC_STACK_OK == sendResponse(request))
266 else if (request->getRequestType() == "PUT")
268 std::cout<<"\t\t\trequestType : PUT"<<std::endl;
270 OCRepresentation rep = request->getResourceRepresentation();
272 if(OC_STACK_OK == sendResponse(request))
279 std::cout<<"\t\t\trequestType : UNSUPPORTED: "<<
280 request->getRequestType()<<std::endl;
285 std::cout <<"\t\trequestFlag : UNSUPPORTED: ";
287 if(request->getRequestHandlerFlag()== RequestHandlerFlag::ObserverFlag)
289 std::cout<<"ObserverFlag"<<std::endl;
295 std::cout << "Request Invalid!"<<std::endl;
303 int main(int argc, char* argv[])
305 OCConnectivityType connectivityType = CT_ADAPTER_IP;
311 std::size_t inputValLen;
312 int optionSelected = std::stoi(argv[1], &inputValLen);
314 if(inputValLen == strlen(argv[1]))
316 if(optionSelected == 0)
318 std::cout << "Using IP."<< std::endl;
319 connectivityType = CT_ADAPTER_IP;
323 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
328 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
331 catch(std::exception& )
333 std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
342 OC::ServiceType::InProc,
344 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
345 0, // Uses randomly available port
346 OC::QualityOfService::LowQos
349 OCPlatform::Configure(cfg);
355 if(!fooRes.createResource())
360 ClientWorker cw(connectivityType);
363 catch(OCException& e)
365 std::cout<< "Exception in main: "<<e.what()<<std::endl;