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 ListenResourcePropertiesContainer
43 friend class cereal::access;
44 friend class ListenResourceContainer;
46 template<class Archive>
47 void serialize(Archive& ar)
53 ar(cereal::make_nvp(OC::Key::OBSERVABLEKEY, obsTemp));
54 m_observable = obsTemp != 0;
56 catch(cereal::Exception&)
58 // we swallow this exception, since it means the key
59 // doesn't exist, allowing these to be optional
66 ar(cereal::make_nvp(OC::Key::SECUREKEY, secureTemp));
67 m_secure = secureTemp != 0;
70 ar(cereal::make_nvp(OC::Key::PORTKEY, m_port));
72 catch(cereal::Exception&)
77 ar(cereal::make_nvp(OC::Key::RESOURCETYPESKEY,m_resourceTypes));
79 catch(cereal::Exception&)
83 ar(cereal::make_nvp(OC::Key::INTERFACESKEY, m_interfaces));
85 catch(cereal::Exception&)
90 std::vector<std::string> m_resourceTypes;
91 std::vector<std::string> m_interfaces;
97 ListenResourceContainer() : m_loaded(false)
101 friend class cereal::access;
102 friend class ListenOCContainer;
104 template <class Archive>
105 void serialize(Archive& ar)
109 ar(cereal::make_nvp(OC::Key::URIKEY, m_uri));
112 catch(cereal::Exception&)
116 ar(cereal::make_nvp(OC::Key::PROPERTYKEY, m_props));
119 catch(cereal::Exception&)
126 ListenResourcePropertiesContainer m_props;
133 bool observable() const
135 return m_props.m_observable;
138 OCSecureType secureType() const
140 return m_props.m_secure?OCSecureType::IPv4Secure :OCSecureType::IPv4;
145 return m_props.m_port;
148 std::vector<std::string> resourceTypes() const
150 return m_props.m_resourceTypes;
153 std::vector<std::string> interfaces() const
155 return m_props.m_interfaces;
160 friend class cereal::access;
161 template <class Archive>
162 void serialize(Archive& ar)
164 std::vector<ListenResourceContainer> resources;
168 ListenOCContainer(std::weak_ptr<IClientWrapper> cw, const OCDevAddr& address,
169 std::stringstream& json):
170 m_clientWrapper(cw), m_address(address)
175 const std::vector<std::shared_ptr<OCResource>>& Resources() const
181 std::string ConvertOCAddrToString(OCSecureType sec, int secureport)
191 if(sec== OCSecureType::IPv4)
195 else if(sec == OCSecureType::IPv4Secure)
201 oclog() << "ConvertOCAddrToString(): invalid SecureType"<<std::flush;
202 throw ResourceInitException(false, false, false, false, false, true);
205 if(0== OCDevAddrToIPv4Addr(&m_address, &addr1, &addr2, &addr3, &addr4))
207 // nothing to do, successful case.
211 oclog() << "ConvertOCAddrToString(): Invalid Ip"
213 throw ResourceInitException(false, false, false, false, false, true);
216 os<<static_cast<int>(addr1)<<'.'
217 <<static_cast<int>(addr2)<<'.'
218 <<static_cast<int>(addr3)<<'.'
219 <<static_cast<int>(addr4);
221 if(sec == OCSecureType::IPv4Secure && secureport>0 && secureport<=65535)
223 port = static_cast<uint16_t>(secureport);
225 else if(sec == OCSecureType::IPv4 && 0==OCDevAddrToPort(&m_address, &port))
227 // nothing to do, this is a successful case
231 oclog() << "ConvertOCAddrToString() : Invalid Port"
233 throw ResourceInitException(false, false, false, false, true, false);
236 os <<":"<< static_cast<int>(port);
241 void LoadFromJson(std::stringstream& json)
243 cereal::JSONInputArchive archive(json);
245 std::vector<ListenResourceContainer> resources;
246 archive(cereal::make_nvp(OC::Key::OCKEY, resources));
250 for(const auto& res : resources)
256 m_resources.push_back(std::shared_ptr<OCResource>(
257 new OCResource(m_clientWrapper,
258 ConvertOCAddrToString(res.secureType(),res.port()),
259 res.m_uri, res.observable(), res.resourceTypes(),
264 catch(ResourceInitException& e)
266 oclog() << "listenCallback(): failed to create resource: " << e.what()
271 std::vector<std::shared_ptr<OC::OCResource>> m_resources;
272 std::weak_ptr<IClientWrapper> m_clientWrapper;