X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Finclude%2FOCSerialization.h;h=e440ab0c91a98e06ac6ffc4de17c3e63bf2e1e38;hb=7f00f942c39b7bc27c7eeecf213a239c3fe4173c;hp=b0bf50a5d922c665ebc8147fcd0b45d9c2232e88;hpb=7b77bb661f38fea1ecb96cfe6eb4b3bbab839b80;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/include/OCSerialization.h b/resource/include/OCSerialization.h index b0bf50a..e440ab0 100644 --- a/resource/include/OCSerialization.h +++ b/resource/include/OCSerialization.h @@ -18,288 +18,153 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -#include -#include -#include -#include - #include +#include "ocpayload.h" +#include "ocrandom.h" +#include "oic_string.h" namespace OC { class ListenOCContainer { private: - enum class OCSecureType - { - IPv4Secure, - IPv4 - }; + static std::vector StringLLToVector(OCStringLL* ll) + { + std::vector strs; + while(ll) + { + strs.push_back(ll->value); + ll = ll->next; + } + return strs; + } - class ListenResourceContainer - { - class ListenResourcePropertiesContainer + public: + ListenOCContainer(std::weak_ptr cw, + OCDevAddr& devAddr, OCDiscoveryPayload* payload) { - friend class cereal::access; - friend class ListenResourceContainer; + OCDevAddr currentDevAddr = devAddr; - template - void serialize(Archive& ar) + while (payload) { - try - { - m_observable=false; - int obsTemp; - ar(cereal::make_nvp(OC::Key::OBSERVABLEKEY, obsTemp)); - m_observable = obsTemp != 0; - } - catch(cereal::Exception&) + std::string deviceName; + if (payload->name) { - // we swallow this exception, since it means the key - // doesn't exist, allowing these to be optional - ar.setNextName(nullptr); + deviceName = payload->name; } - try + OCResourcePayload* res = payload->resources; + while (res) { - m_secure = false; - int secureTemp; - ar(cereal::make_nvp(OC::Key::SECUREKEY, secureTemp)); - m_secure = secureTemp != 0; - m_port = -1; - ar(cereal::make_nvp(OC::Key::PORTKEY, m_port)); - } - catch(cereal::Exception&) - { - ar.setNextName(nullptr); - } + currentDevAddr.flags = res->secure ? + (OCTransportFlags)(OC_FLAG_SECURE | devAddr.flags) : + devAddr.flags; - try - { - ar(cereal::make_nvp(OC::Key::RESOURCETYPESKEY,m_resourceTypes)); - } - catch(cereal::Exception&) - { - ar.setNextName(nullptr); - } - try - { - ar(cereal::make_nvp(OC::Key::INTERFACESKEY, m_interfaces)); - } - catch(cereal::Exception&) - { - ar.setNextName(nullptr); - } - } - - bool m_observable; - std::vector m_resourceTypes; - std::vector m_interfaces; - bool m_secure; - int m_port; - }; + currentDevAddr.port = (res->port != 0) ? res->port : devAddr.port; - public: - ListenResourceContainer() : m_loaded(false) - {} - - private: - friend class cereal::access; - friend class ListenOCContainer; - - template - void serialize(Archive& ar) - { - try - { - ar(cereal::make_nvp(OC::Key::URIKEY, m_uri)); - m_loaded=true; - } - catch(cereal::Exception&) - { - ar.setNextName(nullptr); - } - try - { - ar(cereal::make_nvp(OC::Key::SERVERIDKEY, m_serverId)); - m_loaded=true; - } - catch(cereal::Exception&) - { - ar.setNextName(nullptr); - } - try - { - ar(cereal::make_nvp(OC::Key::PROPERTYKEY, m_props)); - m_loaded=true; - } - catch(cereal::Exception&) - { - ar.setNextName(nullptr); + if (payload->baseURI) + { + OCDevAddr rdPubAddr = currentDevAddr; + + std::string baseURI = std::string(payload->baseURI); + size_t len = baseURI.length(); + int addressLen = baseURI.find_first_of(":"); + std::string ipaddress = baseURI.substr(0, addressLen); + int port = atoi(baseURI.substr(addressLen + 1, len).c_str()); + OICStrcpy(rdPubAddr.addr, addressLen + 1, ipaddress.c_str()); + rdPubAddr.port = port; + m_resources.push_back(std::shared_ptr( + new OC::OCResource(cw, rdPubAddr, + std::string(res->uri), + std::string(payload->sid), + res->bitmap, + StringLLToVector(res->types), + StringLLToVector(res->interfaces), + deviceName + ))); + } + else + { + m_resources.push_back(std::shared_ptr( + new OC::OCResource(cw, currentDevAddr, + std::string(res->uri), + std::string(payload->sid), + res->bitmap, + StringLLToVector(res->types), + StringLLToVector(res->interfaces), + deviceName + ))); + +#ifdef TCP_ADAPTER + if (res->tcpPort != 0) + { + OCDevAddr tcpDevAddr = currentDevAddr; + tcpDevAddr.port = res->tcpPort; + tcpDevAddr.adapter = OC_ADAPTER_TCP; + m_resources.push_back(std::shared_ptr( + new OC::OCResource(cw, tcpDevAddr, + std::string(res->uri), + std::string(payload->sid), + res->bitmap, + StringLLToVector(res->types), + StringLLToVector(res->interfaces), + deviceName + ))); + } +#endif + } + res = res->next; + } + payload = payload->next; } } - - std::string m_uri; - std::string m_serverId; - bool m_loaded; - ListenResourcePropertiesContainer m_props; - - bool loaded() const +#ifdef WITH_MQ + ListenOCContainer(std::weak_ptr cw, + OCDevAddr& devAddr, OCRepPayload* payload) { - return m_loaded; - } - - bool observable() const - { - return m_props.m_observable; - } - - OCSecureType secureType() const - { - return m_props.m_secure?OCSecureType::IPv4Secure :OCSecureType::IPv4; - } - - int port() const - { - return m_props.m_port; - } - - std::vector resourceTypes() const - { - return m_props.m_resourceTypes; - } + if (payload) + { + char**topicList = nullptr; + size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0}; + OCRepPayloadGetStringArray(payload, "topiclist", &topicList, dimensions); - std::vector interfaces() const - { - return m_props.m_interfaces; + for(size_t idx = 0; idx < dimensions[0]; idx++) + { + m_resources.push_back(std::shared_ptr( + new OC::OCResource(cw, devAddr, + std::string(topicList[idx]), + "", + OC_OBSERVABLE, + {OC_RSRVD_RESOURCE_TYPE_MQ_TOPIC}, + {DEFAULT_INTERFACE}, + deviceName + ))); + } + } } - }; - private: - friend class cereal::access; - template - void serialize(Archive& ar) + ListenOCContainer(std::weak_ptr cw, + OCDevAddr& devAddr, const std::string& topicUri) { - std::vector resources; - ar(resources); + m_resources.push_back(std::shared_ptr( + new OC::OCResource(cw, devAddr, + topicUri, + "", + OC_OBSERVABLE, + {OC_RSRVD_RESOURCE_TYPE_MQ_TOPIC}, + {DEFAULT_INTERFACE}, + deviceName + ))); } - public: -#ifdef CA_INT - ListenOCContainer(std::weak_ptr cw, const OCDevAddr& address, - uint8_t connectivityType, std::stringstream& json): - m_clientWrapper(cw), m_address(address), m_connectivityType(connectivityType) -#else - ListenOCContainer(std::weak_ptr cw, const OCDevAddr& address, - std::stringstream& json): - m_clientWrapper(cw), m_address(address) #endif - { - LoadFromJson(json); - } const std::vector>& Resources() const { return m_resources; } - private: - std::string ConvertOCAddrToString(OCSecureType sec, int secureport) - { - char stringAddress[DEV_ADDR_SIZE_MAX]; - uint16_t port; - - ostringstream os; - - if(sec== OCSecureType::IPv4) - { - os<<"coap://"; - } - else if(sec == OCSecureType::IPv4Secure) - { - os<<"coaps://"; - } - else - { - oclog() << "ConvertOCAddrToString(): invalid SecureType"<0 && secureport<=65535) - { - port = static_cast(secureport); - } - else if(sec == OCSecureType::IPv4 && 0==OCDevAddrToPort(&m_address, &port)) - { - // nothing to do, this is a successful case - } - else - { - oclog() << "ConvertOCAddrToString() : Invalid Port" - <(port); - - return os.str(); - } - - void LoadFromJson(std::stringstream& json) - { - cereal::JSONInputArchive archive(json); - - std::vector resources; - archive(cereal::make_nvp(OC::Key::OCKEY, resources)); - - m_resources.clear(); - - for(const auto& res : resources) - { - try - { - if(res.loaded()) - { -#ifdef CA_INT - m_resources.push_back(std::shared_ptr( - new OCResource(m_clientWrapper, - ConvertOCAddrToString(res.secureType(),res.port()), - res.m_uri, res.m_serverId, m_connectivityType, res.observable(), - res.resourceTypes(), res.interfaces()))); -#else - m_resources.push_back(std::shared_ptr( - new OCResource(m_clientWrapper, - ConvertOCAddrToString(res.secureType(),res.port()), - res.m_uri, res.m_serverId, res.observable(), - res.resourceTypes(), res.interfaces()))); -#endif - } - - } - catch(ResourceInitException& e) - { - oclog() << "listenCallback(): failed to create resource: " << e.what() - << std::flush; - } - } - } std::vector> m_resources; - std::weak_ptr m_clientWrapper; - OCDevAddr m_address; - uint8_t m_connectivityType; }; }