- std::string ConvertOCAddrToString(OCSecureType sec, int secureport)
- {
- uint16_t port;
- std::ostringstream os;
-
- if(sec== OCSecureType::IPv4)
- {
- os<<"coap://";
- }
- else if(sec == OCSecureType::IPv4Secure)
- {
- os<<"coaps://";
- }
- else
- {
- oclog() << "ConvertOCAddrToString(): invalid SecureType"<<std::flush;
- throw ResourceInitException(false, false, false, false, false, true);
- }
-
- uint8_t a;
- uint8_t b;
- uint8_t c;
- uint8_t d;
- if(OCDevAddrToIPv4Addr(&m_address, &a, &b, &c, &d) != 0)
- {
- oclog() << "ConvertOCAddrToString(): Invalid Ip"
- << std::flush;
- throw ResourceInitException(false, false, false, false, false, true);
- }
-
- os<<static_cast<int>(a)<<"."<<static_cast<int>(b)
- <<"."<<static_cast<int>(c)<<"."<<static_cast<int>(d);
-
- if(sec == OCSecureType::IPv4Secure && secureport>0 && secureport<=65535)
- {
- port = static_cast<uint16_t>(secureport);
- }
- else if(sec == OCSecureType::IPv4 && 0==OCDevAddrToPort(&m_address, &port))
- {
- // nothing to do, this is a successful case
- }
- else
- {
- oclog() << "ConvertOCAddrToString() : Invalid Port"
- <<std::flush;
- throw ResourceInitException(false, false, false, false, true, false);
- }
-
- os <<":"<< static_cast<int>(port);
-
- return os.str();
- }
-
- void LoadFromJson(std::stringstream& json)
- {
- cereal::JSONInputArchive archive(json);
-
- std::vector<ListenResourceContainer> resources;
- archive(cereal::make_nvp(OC::Key::OCKEY, resources));
-
- m_resources.clear();
-
- for(const auto& res : resources)
- {
- try
- {
- if(res.loaded())
- {
- m_resources.push_back(std::shared_ptr<OCResource>(
- new OCResource(m_clientWrapper,
- ConvertOCAddrToString(res.secureType(),res.port()),
- res.m_uri, res.m_serverId, m_connectivityType, res.observable(),
- res.resourceTypes(), res.interfaces())));
- }
-
- }
- catch(ResourceInitException& e)
- {
- oclog() << "listenCallback(): failed to create resource: " << e.what()
- << std::flush;
- }
- }
- }