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)
53 ar(cereal::make_nvp(OC::Key::BMKEY, m_bm));
54 // In case of observable
55 if(m_bm & OC_OBSERVABLE)
60 catch(cereal::Exception&)
62 ar.setNextName(nullptr);
68 ar(cereal::make_nvp(OC::Key::SECUREKEY, secureTemp));
69 m_secure = secureTemp != 0;
72 ar(cereal::make_nvp(OC::Key::PORTKEY, m_port));
74 catch(cereal::Exception&)
76 ar.setNextName(nullptr);
87 class ListenResourcePropertiesContainer
89 friend class cereal::access;
90 friend class ListenResourceContainer;
92 template<class Archive>
93 void serialize(Archive& ar)
97 ar(cereal::make_nvp(OC::Key::POLICYKEY, m_policy));
100 catch(cereal::Exception&)
102 // we swallow this exception, since it means the key
103 // doesn't exist, allowing these to be optional
104 oclog() << "Invalid POLICYKEY"<<std::flush;
105 ar.setNextName(nullptr);
110 ar(cereal::make_nvp(OC::Key::RESOURCETYPESKEY,m_resourceTypes));
112 catch(cereal::Exception&)
114 ar.setNextName(nullptr);
118 ar(cereal::make_nvp(OC::Key::INTERFACESKEY, m_interfaces));
120 catch(cereal::Exception&)
122 ar.setNextName(nullptr);
126 std::vector<std::string> m_resourceTypes;
127 std::vector<std::string> m_interfaces;
128 ListenResourcePolicyContainer m_policy;
132 ListenResourceContainer() : m_loaded(false)
136 friend class cereal::access;
137 friend class ListenOCContainer;
139 template <class Archive>
140 void serialize(Archive& ar)
144 ar(cereal::make_nvp(OC::Key::URIKEY, m_uri));
147 catch(cereal::Exception&)
149 ar.setNextName(nullptr);
153 ar(cereal::make_nvp(OC::Key::SERVERIDKEY, m_serverId));
156 catch(cereal::Exception&)
158 ar.setNextName(nullptr);
162 ar(cereal::make_nvp(OC::Key::PROPERTYKEY, m_props));
165 catch(cereal::Exception&)
167 ar.setNextName(nullptr);
173 std::string m_serverId;
175 ListenResourcePropertiesContainer m_props;
182 bool observable() const
184 return m_props.m_policy.m_observable;
187 OCSecureType secureType() const
189 return m_props.m_policy.m_secure ? OCSecureType::Secure : OCSecureType::NotSecure;
194 return m_props.m_policy.m_port;
197 std::vector<std::string> resourceTypes() const
199 return m_props.m_resourceTypes;
202 std::vector<std::string> interfaces() const
204 return m_props.m_interfaces;
209 friend class cereal::access;
210 template <class Archive>
211 void serialize(Archive& ar)
213 std::vector<ListenResourceContainer> resources;
217 ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
218 const OCDevAddr& devAddr, std::stringstream& json)
219 : m_clientWrapper(cw), m_devAddr(devAddr)
224 const std::vector<std::shared_ptr<OCResource>>& Resources() const
230 void LoadFromJson(std::stringstream& json)
232 cereal::JSONInputArchive archive(json);
234 std::vector<ListenResourceContainer> resources;
235 archive(cereal::make_nvp(OC::Key::OCKEY, resources));
239 for(const auto& res : resources)
245 m_resources.push_back(std::shared_ptr<OCResource>(
246 new OCResource(m_clientWrapper, m_devAddr,
247 res.m_uri, res.m_serverId, res.observable(),
248 res.resourceTypes(), res.interfaces())));
252 catch(ResourceInitException& e)
254 oclog() << "listenCallback(): failed to create resource: " << e.what()
259 std::vector<std::shared_ptr<OC::OCResource>> m_resources;
260 std::weak_ptr<IClientWrapper> m_clientWrapper;
261 const OCDevAddr& m_devAddr;