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 ListenResourceContainer
41 class ListenResourcePolicyContainer
43 friend class cereal::access;
44 friend class ListenResourceContainer;
45 friend class ListenResourcePropertiesContainer;
47 template<class Archive>
48 void serialize(Archive& ar)
56 ar(cereal::make_nvp(OC::Key::BMKEY, m_bm));
57 // In case of observable
58 if(m_bm & OC_OBSERVABLE)
63 catch(cereal::Exception&)
65 ar.setNextName(nullptr);
70 ar(cereal::make_nvp(OC::Key::SECUREKEY, secureTemp));
71 m_secure = secureTemp != 0;
73 ar(cereal::make_nvp(OC::Key::PORTKEY, m_port));
75 catch(cereal::Exception&)
77 ar.setNextName(nullptr);
88 class ListenResourcePropertiesContainer
90 friend class cereal::access;
91 friend class ListenResourceContainer;
93 template<class Archive>
94 void serialize(Archive& ar)
98 ar(cereal::make_nvp(OC::Key::POLICYKEY, m_policy));
101 catch(cereal::Exception&)
103 // we swallow this exception, since it means the key
104 // doesn't exist, allowing these to be optional
105 oclog() << "Invalid POLICYKEY"<<std::flush;
106 ar.setNextName(nullptr);
111 ar(cereal::make_nvp(OC::Key::RESOURCETYPESKEY,m_resourceTypes));
113 catch(cereal::Exception&)
115 ar.setNextName(nullptr);
119 ar(cereal::make_nvp(OC::Key::INTERFACESKEY, m_interfaces));
121 catch(cereal::Exception&)
123 ar.setNextName(nullptr);
127 std::vector<std::string> m_resourceTypes;
128 std::vector<std::string> m_interfaces;
129 ListenResourcePolicyContainer m_policy;
133 ListenResourceContainer() : m_loaded(false)
137 friend class cereal::access;
138 friend class ListenOCContainer;
140 template <class Archive>
141 void serialize(Archive& ar)
145 ar(cereal::make_nvp(OC::Key::URIKEY, m_uri));
148 catch(cereal::Exception&)
150 ar.setNextName(nullptr);
154 ar(cereal::make_nvp(OC::Key::SERVERIDKEY, m_serverId));
157 catch(cereal::Exception&)
159 ar.setNextName(nullptr);
163 ar(cereal::make_nvp(OC::Key::PROPERTYKEY, m_props));
166 catch(cereal::Exception&)
168 ar.setNextName(nullptr);
174 std::string m_serverId;
176 ListenResourcePropertiesContainer m_props;
183 bool observable() const
185 return m_props.m_policy.m_observable;
188 OCSecureType secureType() const
190 return m_props.m_policy.m_secure ? OCSecureType::Secure : OCSecureType::NotSecure;
195 return m_props.m_policy.m_port;
198 std::vector<std::string> resourceTypes() const
200 return m_props.m_resourceTypes;
203 std::vector<std::string> interfaces() const
205 return m_props.m_interfaces;
210 friend class cereal::access;
211 template <class Archive>
212 void serialize(Archive& ar)
214 std::vector<ListenResourceContainer> resources;
218 ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
219 const OCDevAddr& devAddr, std::stringstream& json)
220 : m_clientWrapper(cw), m_devAddr(devAddr)
225 const std::vector<std::shared_ptr<OCResource>>& Resources() const
231 void LoadFromJson(std::stringstream& json)
233 cereal::JSONInputArchive archive(json);
235 std::vector<ListenResourceContainer> resources;
236 archive(cereal::make_nvp(OC::Key::OCKEY, resources));
240 for(const auto& res : resources)
246 if (res.secureType() == OCSecureType::Secure)
249 (OCTransportFlags)(OC_FLAG_SECURE | m_devAddr.flags);
251 if (res.port() != -1)
253 m_devAddr.port = res.port();
255 m_resources.push_back(std::shared_ptr<OCResource>(
256 new OCResource(m_clientWrapper, m_devAddr,
257 res.m_uri, res.m_serverId, res.observable(),
258 res.resourceTypes(), res.interfaces())));
262 catch(ResourceInitException& e)
264 oclog() << "listenCallback(): failed to create resource: " << e.what()
269 std::vector<std::shared_ptr<OC::OCResource>> m_resources;
270 std::weak_ptr<IClientWrapper> m_clientWrapper;