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
60 ar.setNextName(nullptr);
67 ar(cereal::make_nvp(OC::Key::SECUREKEY, secureTemp));
68 m_secure = secureTemp != 0;
71 ar(cereal::make_nvp(OC::Key::PORTKEY, m_port));
73 catch(cereal::Exception&)
75 ar.setNextName(nullptr);
80 ar(cereal::make_nvp(OC::Key::RESOURCETYPESKEY,m_resourceTypes));
82 catch(cereal::Exception&)
84 ar.setNextName(nullptr);
88 ar(cereal::make_nvp(OC::Key::INTERFACESKEY, m_interfaces));
90 catch(cereal::Exception&)
92 ar.setNextName(nullptr);
97 std::vector<std::string> m_resourceTypes;
98 std::vector<std::string> m_interfaces;
104 ListenResourceContainer() : m_loaded(false)
108 friend class cereal::access;
109 friend class ListenOCContainer;
111 template <class Archive>
112 void serialize(Archive& ar)
116 ar(cereal::make_nvp(OC::Key::URIKEY, m_uri));
119 catch(cereal::Exception&)
121 ar.setNextName(nullptr);
125 ar(cereal::make_nvp(OC::Key::SERVERIDKEY, m_serverId));
128 catch(cereal::Exception&)
130 ar.setNextName(nullptr);
134 ar(cereal::make_nvp(OC::Key::PROPERTYKEY, m_props));
137 catch(cereal::Exception&)
139 ar.setNextName(nullptr);
145 std::string m_serverId;
147 ListenResourcePropertiesContainer m_props;
154 bool observable() const
156 return m_props.m_observable;
159 OCSecureType secureType() const
161 return m_props.m_secure?OCSecureType::IPv4Secure :OCSecureType::IPv4;
166 return m_props.m_port;
169 std::vector<std::string> resourceTypes() const
171 return m_props.m_resourceTypes;
174 std::vector<std::string> interfaces() const
176 return m_props.m_interfaces;
181 friend class cereal::access;
182 template <class Archive>
183 void serialize(Archive& ar)
185 std::vector<ListenResourceContainer> resources;
189 ListenOCContainer(std::weak_ptr<IClientWrapper> cw, const OCDevAddr& address,
190 OCConnectivityType connectivityType, std::stringstream& json):
191 m_clientWrapper(cw), m_address(address), m_connectivityType(connectivityType)
196 const std::vector<std::shared_ptr<OCResource>>& Resources() const
202 std::string ConvertOCAddrToString(OCSecureType sec, int secureport)
207 if(sec== OCSecureType::IPv4)
211 else if(sec == OCSecureType::IPv4Secure)
217 oclog() << "ConvertOCAddrToString(): invalid SecureType"<<std::flush;
218 throw ResourceInitException(false, false, false, false, false, true);
225 if(OCDevAddrToIPv4Addr(&m_address, &a, &b, &c, &d) != 0)
227 oclog() << "ConvertOCAddrToString(): Invalid Ip"
229 throw ResourceInitException(false, false, false, false, false, true);
232 os<<static_cast<int>(a)<<"."<<static_cast<int>(b)
233 <<"."<<static_cast<int>(c)<<"."<<static_cast<int>(d);
235 if(sec == OCSecureType::IPv4Secure && secureport>0 && secureport<=65535)
237 port = static_cast<uint16_t>(secureport);
239 else if(sec == OCSecureType::IPv4 && 0==OCDevAddrToPort(&m_address, &port))
241 // nothing to do, this is a successful case
245 oclog() << "ConvertOCAddrToString() : Invalid Port"
247 throw ResourceInitException(false, false, false, false, true, false);
250 os <<":"<< static_cast<int>(port);
255 void LoadFromJson(std::stringstream& json)
257 cereal::JSONInputArchive archive(json);
259 std::vector<ListenResourceContainer> resources;
260 archive(cereal::make_nvp(OC::Key::OCKEY, resources));
264 for(const auto& res : resources)
270 m_resources.push_back(std::shared_ptr<OCResource>(
271 new OCResource(m_clientWrapper,
272 ConvertOCAddrToString(res.secureType(),res.port()),
273 res.m_uri, res.m_serverId, m_connectivityType, res.observable(),
274 res.resourceTypes(), res.interfaces())));
278 catch(ResourceInitException& e)
280 oclog() << "listenCallback(): failed to create resource: " << e.what()
285 std::vector<std::shared_ptr<OC::OCResource>> m_resources;
286 std::weak_ptr<IClientWrapper> m_clientWrapper;
288 OCConnectivityType m_connectivityType;