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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "OCResource.h"
22 #include "OCUtilities.h"
24 #include <boost/lexical_cast.hpp>
29 using OC::result_guard;
30 using OC::checked_guard;
33 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper, const std::string& host,
34 const std::string& uri, const std::string& serverId,
35 OCConnectivityType connectivityType, bool observable,
36 const std::vector<std::string>& resourceTypes,
37 const std::vector<std::string>& interfaces)
38 : m_clientWrapper(clientWrapper), m_uri(uri), m_resourceId(serverId, m_uri),
40 m_connectivityType(connectivityType),
41 m_isObservable(observable),
42 m_isCollection(false), m_resourceTypes(resourceTypes), m_interfaces(interfaces),
43 m_observeHandle(nullptr)
45 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
46 != m_interfaces.end();
49 resourceTypes.empty() ||
51 m_clientWrapper.expired())
53 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
54 interfaces.empty(), m_clientWrapper.expired(), false, false);
58 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper, const std::string& host,
59 const std::string& uri, const std::string& serverId, bool observable,
60 const std::vector<std::string>& resourceTypes,
61 const std::vector<std::string>& interfaces)
62 : m_clientWrapper(clientWrapper), m_uri(uri), m_resourceId(serverId, m_uri), m_host(host),
63 m_isObservable(observable), m_isCollection(false), m_resourceTypes(resourceTypes),
64 m_interfaces(interfaces), m_observeHandle(nullptr)
66 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
67 != m_interfaces.end();
70 resourceTypes.empty() ||
72 m_clientWrapper.expired())
74 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
75 interfaces.empty(), m_clientWrapper.expired(), false, false);
80 OCResource::~OCResource()
84 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
85 GetCallback attributeHandler, QualityOfService QoS)
88 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
89 m_host, m_uri, m_connectivityType, queryParametersMap, m_headerOptions,
90 attributeHandler, QoS);
92 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
93 m_host, m_uri, queryParametersMap, m_headerOptions, attributeHandler, QoS);
97 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
98 GetCallback attributeHandler)
100 QualityOfService defaultQos = OC::QualityOfService::NaQos;
101 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
102 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
105 OCStackResult OCResource::get(const std::string& resourceType,
106 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
107 GetCallback attributeHandler)
109 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
110 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
112 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
115 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
116 QualityOfService QoS)
118 QueryParamsMap mapCpy(queryParametersMap);
120 if(!resourceType.empty())
122 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
125 if(!resourceInterface.empty())
127 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
130 return result_guard(get(mapCpy, attributeHandler, QoS));
133 OCStackResult OCResource::put(const OCRepresentation& rep,
134 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
135 QualityOfService QoS)
138 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
139 m_host, m_uri, m_connectivityType, rep, queryParametersMap,
140 m_headerOptions, attributeHandler, QoS);
142 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
143 m_host, m_uri, rep, queryParametersMap, m_headerOptions, attributeHandler, QoS);
147 OCStackResult OCResource::put(const OCRepresentation& rep,
148 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
150 QualityOfService defaultQos = OC::QualityOfService::NaQos;
151 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
152 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
155 OCStackResult OCResource::put(const std::string& resourceType,
156 const std::string& resourceInterface, const OCRepresentation& rep,
157 const QueryParamsMap& queryParametersMap,
158 PutCallback attributeHandler)
160 QualityOfService defaultQos = OC::QualityOfService::NaQos;
161 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
163 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
164 attributeHandler, defaultQos));
167 OCStackResult OCResource::put(const std::string& resourceType,
168 const std::string& resourceInterface, const OCRepresentation& rep,
169 const QueryParamsMap& queryParametersMap,
170 PutCallback attributeHandler,
171 QualityOfService QoS)
173 QueryParamsMap mapCpy(queryParametersMap);
175 if(!resourceType.empty())
177 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
180 if(!resourceInterface.empty())
182 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
185 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
188 OCStackResult OCResource::post(const OCRepresentation& rep,
189 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
190 QualityOfService QoS)
193 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
194 m_host, m_uri, m_connectivityType, rep, queryParametersMap,
195 m_headerOptions, attributeHandler, QoS);
197 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
198 m_host, m_uri, rep, queryParametersMap, m_headerOptions, attributeHandler, QoS);
202 OCStackResult OCResource::post(const OCRepresentation& rep,
203 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
205 QualityOfService defaultQos = OC::QualityOfService::NaQos;
206 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
207 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
210 OCStackResult OCResource::post(const std::string& resourceType,
211 const std::string& resourceInterface, const OCRepresentation& rep,
212 const QueryParamsMap& queryParametersMap,
213 PostCallback attributeHandler)
215 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
216 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
218 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
222 OCStackResult OCResource::post(const std::string& resourceType,
223 const std::string& resourceInterface, const OCRepresentation& rep,
224 const QueryParamsMap& queryParametersMap,
225 PostCallback attributeHandler,
226 QualityOfService QoS)
228 QueryParamsMap mapCpy(queryParametersMap);
230 if(!resourceType.empty())
232 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
235 if(!resourceInterface.empty())
237 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
240 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
243 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
246 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
247 m_host, m_uri, m_connectivityType, m_headerOptions, deleteHandler, QoS);
249 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
250 m_host, m_uri, m_headerOptions, deleteHandler, QoS);
254 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
256 QualityOfService defaultQos = OC::QualityOfService::NaQos;
257 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
259 return result_guard(deleteResource(deleteHandler, defaultQos));
262 OCStackResult OCResource::observe(ObserveType observeType,
263 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
264 QualityOfService QoS)
266 if(m_observeHandle != nullptr)
268 return result_guard(OC_STACK_INVALID_PARAM);
272 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
273 observeType, &m_observeHandle, m_host,
274 m_uri, m_connectivityType, queryParametersMap, m_headerOptions,
275 observeHandler, QoS);
277 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
278 observeType, &m_observeHandle, m_host,
279 m_uri, queryParametersMap, m_headerOptions, observeHandler, QoS);
283 OCStackResult OCResource::observe(ObserveType observeType,
284 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
286 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
287 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
289 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
292 OCStackResult OCResource::cancelObserve()
294 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
295 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
296 return result_guard(cancelObserve(defaultQoS));
299 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
301 if(m_observeHandle == nullptr)
303 return result_guard(OC_STACK_INVALID_PARAM);
306 OCStackResult result = checked_guard(m_clientWrapper.lock(),
307 &IClientWrapper::CancelObserveResource,
308 m_observeHandle, m_host, m_uri, m_headerOptions, QoS);
310 if(result == OC_STACK_OK)
312 m_observeHandle = nullptr;
318 std::string OCResource::host() const
323 std::string OCResource::uri() const
329 OCConnectivityType OCResource::connectivityType() const
331 return m_connectivityType;
335 bool OCResource::isObservable() const
337 return m_isObservable;
341 OCResourceIdentifier OCResource::uniqueIdentifier() const
346 std::string OCResource::sid() const
348 std::ostringstream os;
349 os << this->uniqueIdentifier().m_representation;
353 bool OCResource::operator==(const OCResource &other) const
355 return m_resourceId == other.m_resourceId;
358 bool OCResource::operator!=(const OCResource &other) const
360 return m_resourceId != other.m_resourceId;
363 bool OCResource::operator<(const OCResource &other) const
365 return m_resourceId < other.m_resourceId;
368 bool OCResource::operator>(const OCResource &other) const
370 return m_resourceId > other.m_resourceId;
373 bool OCResource::operator<=(const OCResource &other) const
375 return m_resourceId <= other.m_resourceId;
378 bool OCResource::operator>=(const OCResource &other) const
380 return m_resourceId >= other.m_resourceId;
383 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
384 const std::string& resourceUri)
385 :m_representation(0), m_resourceUri(resourceUri)
387 // test required so we can create Resources without a server. Will leave as default.
388 if(!wireServerIdentifier.empty())
390 m_representation = boost::lexical_cast<unsigned int>(wireServerIdentifier);
394 ostream& operator <<(ostream& os, const OCResourceIdentifier& ri)
397 os << ri.m_representation<<ri.m_resourceUri;
402 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
404 return m_representation == other.m_representation
405 && m_resourceUri == other.m_resourceUri;
408 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
410 return !(*this == other);
413 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
415 return m_resourceUri < other.m_resourceUri
416 || (m_resourceUri == other.m_resourceUri &&
417 m_representation < other.m_representation);
420 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
422 return *this != other && !(*this<other);
425 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
427 return !(*this > other);
430 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
432 return !(*this < other);