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 <cereal/cereal.hpp>
22 #include <cereal/types/memory.hpp>
23 #include <cereal/types/vector.hpp>
24 #include <cereal/archives/json.hpp>
26 #include <StringConstants.h>
30 class ListenOCContainer
33 enum class OCSecureType
39 class DiscoveredResources
43 friend class cereal::access;
47 friend class cereal::access;
48 friend class Resource;
50 template<class Archive>
51 void serialize(Archive& ar)
56 ar(cereal::make_nvp(OC::Key::BMKEY, m_bm));
58 if(m_bm & OC_OBSERVABLE)
63 catch(cereal::Exception&)
65 ar.setNextName(nullptr);
72 ar(cereal::make_nvp(OC::Key::SECUREKEY, secureTemp));
73 m_secure = secureTemp != 0;
76 ar(cereal::make_nvp(OC::Key::PORTKEY, m_port));
78 catch(cereal::Exception&)
80 ar.setNextName(nullptr);
90 template <class Archive>
91 void serialize(Archive& ar)
95 ar(cereal::make_nvp(OC::Key::URIKEY, m_uri));
98 catch(cereal::Exception&)
100 ar.setNextName(nullptr);
105 ar(cereal::make_nvp(OC::Key::RESOURCETYPESKEY,m_resourceTypes));
108 catch(cereal::Exception&)
110 ar.setNextName(nullptr);
115 ar(cereal::make_nvp(OC::Key::INTERFACESKEY, m_interfaces));
118 catch(cereal::Exception&)
120 ar.setNextName(nullptr);
125 ar(cereal::make_nvp(OC::Key::POLICYKEY, m_policy));
128 catch(cereal::Exception&)
130 ar.setNextName(nullptr);
133 // Although not expected, a server id as part of a resource's own
134 // representation is legal. It may be used if needed.
137 ar(cereal::make_nvp(OC::Key::DEVICEIDKEY, m_serverId));
140 catch(cereal::Exception&)
142 ar.setNextName(nullptr);
146 Resource(): m_loaded(false)
149 bool observable() const
151 return m_policy.m_observable;
154 OCSecureType secureType() const
156 return m_policy.m_secure ? OCSecureType::Secure : OCSecureType::NotSecure;
161 return m_policy.m_port;
164 std::vector<std::string> resourceTypes() const
166 return m_resourceTypes;
169 std::vector<std::string> interfaces() const
179 std::string m_serverId;
180 std::vector<std::string> m_resourceTypes;
181 std::vector<std::string> m_interfaces;
182 ResourcePolicy m_policy;
187 DiscoveredResources()
191 friend class cereal::access;
192 friend class ListenOCContainer;
194 template <class Archive>
195 void serialize(Archive& ar)
199 ar(cereal::make_nvp(OC::Key::DEVICEIDKEY, m_serverIdOfThisDevice));
201 catch(cereal::Exception&) { ar.setNextName(nullptr); }
205 ar(cereal::make_nvp(OC::Key::LINKS, resources));
207 catch(cereal::Exception&) { ar.setNextName(nullptr); }
210 std::string m_serverIdOfThisDevice;
211 std::vector<Resource> resources;
215 friend class cereal::access;
216 template <class Archive>
217 void serialize(Archive& ar)
219 std::vector<DiscoveredResources> resources;
223 ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
224 const OCDevAddr& devAddr, std::stringstream& json)
225 : m_clientWrapper(cw), m_devAddr(devAddr)
231 const std::vector<std::shared_ptr<OCResource>>& Resources() const
237 void LoadFromJson(std::stringstream& json)
239 cereal::JSONInputArchive archive(json);
241 std::vector<DiscoveredResources> resources;
242 archive(cereal::make_nvp(OC::Key::OCKEY, resources));
246 for(const auto& resourcesAtDevice : resources)
248 std::string serverIDForThisResourceRep = resourcesAtDevice.m_serverIdOfThisDevice;
250 for (const auto& resource : resourcesAtDevice.resources)
254 if(resource.loaded())
256 m_resources.push_back(std::shared_ptr<OCResource>
263 serverIDForThisResourceRep,
264 resource.observable(),
265 resource.resourceTypes(),
266 resource.interfaces()
271 catch(ResourceInitException& e)
273 oclog() << "listenCallback(): failed to create resource: " << e.what()
279 std::vector<std::shared_ptr<OC::OCResource>> m_resources;
280 std::weak_ptr<IClientWrapper> m_clientWrapper;
281 const OCDevAddr& m_devAddr;