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;
32 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper, const std::string& host,
33 const std::string& uri, const std::string& serverId,
34 OCConnectivityType connectivityType, bool observable,
35 const std::vector<std::string>& resourceTypes,
36 const std::vector<std::string>& interfaces)
37 : m_clientWrapper(clientWrapper), m_uri(uri), m_resourceId(serverId, m_uri),
39 m_connectivityType(connectivityType),
40 m_isObservable(observable),
41 m_isCollection(false), m_resourceTypes(resourceTypes), m_interfaces(interfaces),
42 m_observeHandle(nullptr)
44 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
45 != m_interfaces.end();
48 resourceTypes.empty() ||
50 m_clientWrapper.expired())
52 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
53 interfaces.empty(), m_clientWrapper.expired(), false, false);
57 OCResource::~OCResource()
61 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
62 GetCallback attributeHandler, QualityOfService QoS)
64 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
65 m_host, m_uri, m_connectivityType, queryParametersMap, m_headerOptions,
66 attributeHandler, QoS);
69 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
70 GetCallback attributeHandler)
72 QualityOfService defaultQos = OC::QualityOfService::NaQos;
73 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
74 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
77 OCStackResult OCResource::get(const std::string& resourceType,
78 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
79 GetCallback attributeHandler)
81 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
82 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
84 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
87 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
90 QueryParamsMap mapCpy(queryParametersMap);
92 if(!resourceType.empty())
94 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
97 if(!resourceInterface.empty())
99 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
102 return result_guard(get(mapCpy, attributeHandler, QoS));
105 OCStackResult OCResource::put(const OCRepresentation& rep,
106 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
107 QualityOfService QoS)
109 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
110 m_host, m_uri, m_connectivityType, rep, queryParametersMap,
111 m_headerOptions, attributeHandler, QoS);
114 OCStackResult OCResource::put(const OCRepresentation& rep,
115 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
117 QualityOfService defaultQos = OC::QualityOfService::NaQos;
118 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
119 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
122 OCStackResult OCResource::put(const std::string& resourceType,
123 const std::string& resourceInterface, const OCRepresentation& rep,
124 const QueryParamsMap& queryParametersMap,
125 PutCallback attributeHandler)
127 QualityOfService defaultQos = OC::QualityOfService::NaQos;
128 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
130 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
131 attributeHandler, defaultQos));
134 OCStackResult OCResource::put(const std::string& resourceType,
135 const std::string& resourceInterface, const OCRepresentation& rep,
136 const QueryParamsMap& queryParametersMap,
137 PutCallback attributeHandler,
138 QualityOfService QoS)
140 QueryParamsMap mapCpy(queryParametersMap);
142 if(!resourceType.empty())
144 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
147 if(!resourceInterface.empty())
149 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
152 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
155 OCStackResult OCResource::post(const OCRepresentation& rep,
156 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
157 QualityOfService QoS)
159 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
160 m_host, m_uri, m_connectivityType, rep, queryParametersMap,
161 m_headerOptions, attributeHandler, QoS);
164 OCStackResult OCResource::post(const OCRepresentation& rep,
165 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
167 QualityOfService defaultQos = OC::QualityOfService::NaQos;
168 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
169 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
172 OCStackResult OCResource::post(const std::string& resourceType,
173 const std::string& resourceInterface, const OCRepresentation& rep,
174 const QueryParamsMap& queryParametersMap,
175 PostCallback attributeHandler)
177 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
178 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
180 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
184 OCStackResult OCResource::post(const std::string& resourceType,
185 const std::string& resourceInterface, const OCRepresentation& rep,
186 const QueryParamsMap& queryParametersMap,
187 PostCallback attributeHandler,
188 QualityOfService QoS)
190 QueryParamsMap mapCpy(queryParametersMap);
192 if(!resourceType.empty())
194 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
197 if(!resourceInterface.empty())
199 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
202 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
205 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
207 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
208 m_host, m_uri, m_connectivityType, m_headerOptions, deleteHandler, QoS);
211 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
213 QualityOfService defaultQos = OC::QualityOfService::NaQos;
214 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
216 return result_guard(deleteResource(deleteHandler, defaultQos));
219 OCStackResult OCResource::observe(ObserveType observeType,
220 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
221 QualityOfService QoS)
223 if(m_observeHandle != nullptr)
225 return result_guard(OC_STACK_INVALID_PARAM);
228 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
229 observeType, &m_observeHandle, m_host,
230 m_uri, m_connectivityType, queryParametersMap, m_headerOptions,
231 observeHandler, QoS);
234 OCStackResult OCResource::observe(ObserveType observeType,
235 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
237 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
238 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
240 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
243 OCStackResult OCResource::cancelObserve()
245 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
246 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
247 return result_guard(cancelObserve(defaultQoS));
250 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
252 if(m_observeHandle == nullptr)
254 return result_guard(OC_STACK_INVALID_PARAM);
257 OCStackResult result = checked_guard(m_clientWrapper.lock(),
258 &IClientWrapper::CancelObserveResource,
259 m_observeHandle, m_host, m_uri, m_headerOptions, QoS);
261 if(result == OC_STACK_OK)
263 m_observeHandle = nullptr;
269 std::string OCResource::host() const
274 std::string OCResource::uri() const
279 OCConnectivityType OCResource::connectivityType() const
281 return m_connectivityType;
284 bool OCResource::isObservable() const
286 return m_isObservable;
290 OCResourceIdentifier OCResource::uniqueIdentifier() const
295 std::string OCResource::sid() const
297 std::ostringstream os;
298 os << this->uniqueIdentifier().m_representation;
302 bool OCResource::operator==(const OCResource &other) const
304 return m_resourceId == other.m_resourceId;
307 bool OCResource::operator!=(const OCResource &other) const
309 return m_resourceId != other.m_resourceId;
312 bool OCResource::operator<(const OCResource &other) const
314 return m_resourceId < other.m_resourceId;
317 bool OCResource::operator>(const OCResource &other) const
319 return m_resourceId > other.m_resourceId;
322 bool OCResource::operator<=(const OCResource &other) const
324 return m_resourceId <= other.m_resourceId;
327 bool OCResource::operator>=(const OCResource &other) const
329 return m_resourceId >= other.m_resourceId;
332 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
333 const std::string& resourceUri)
334 :m_representation(0), m_resourceUri(resourceUri)
336 // test required so we can create Resources without a server. Will leave as default.
337 if(!wireServerIdentifier.empty())
339 m_representation = boost::lexical_cast<unsigned int>(wireServerIdentifier);
343 ostream& operator <<(ostream& os, const OCResourceIdentifier& ri)
346 os << ri.m_representation<<ri.m_resourceUri;
351 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
353 return m_representation == other.m_representation
354 && m_resourceUri == other.m_resourceUri;
357 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
359 return !(*this == other);
362 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
364 return m_resourceUri < other.m_resourceUri
365 || (m_resourceUri == other.m_resourceUri &&
366 m_representation < other.m_representation);
369 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
371 return *this != other && !(*this<other);
374 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
376 return !(*this > other);
379 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
381 return !(*this < other);